fix: Cleans up entity persistence. Generalizes Mobiles/Items/Guilds (#1528)

### Summary
- [X] Fixed a bug where entity persistence was serialized out of order, causing world corruption.
- [X] Fixed LastSerialized not being utilized properly and dangling references still becoming an issue.
- [X] Added a new `GenericEntityPersistence<T>` type to encapsulate `ISerializable` serialization.
- [X] Removing the custom logic and moved Items, Mobiles, Guilds, and Accounts  to GenericEntityPersistence.
- [X] Changed serialization to use the singleton pattern to reduce calling methods from stored variables.
This commit is contained in:
Kamron Batman 2023-10-01 22:10:47 -07:00 • committed by GitHub
parent e0225c6e59
commit f2de2fbb77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 721 additions and 970 deletions

View file

@ -6,8 +6,10 @@ using Server.Mobiles;
namespace Server.Engines.CannedEvil;
public static class ChampionTitleSystem
public class ChampionTitleSystem : GenericPersistence
{
private static ChampionTitleSystem _championTitlePersistence;
// All of the players with murders
private static readonly Dictionary<PlayerMobile, ChampionTitleContext> _championTitleContexts = new();
@ -15,7 +17,7 @@ public static class ChampionTitleSystem
public static void Configure()
{
GenericPersistence.Register("ChampionTitles", Serialize, Deserialize);
_championTitlePersistence = new ChampionTitleSystem();
}
public static void Initialize()
@ -33,7 +35,11 @@ public static class ChampionTitleSystem
}
}
private static void Deserialize(IGenericReader reader)
public ChampionTitleSystem() : base("ChampionTitles", 10)
{
}
public override void Deserialize(IGenericReader reader)
{
var version = reader.ReadEncodedInt();
@ -47,7 +53,7 @@ public static class ChampionTitleSystem
}
}
private static void Serialize(IGenericWriter writer)
public override void Serialize(IGenericWriter writer)
{
writer.WriteEncodedInt(0); // version
@ -59,10 +65,10 @@ public static class ChampionTitleSystem
}
}
public static bool GetChampionTitleContext(this PlayerMobile player, out ChampionTitleContext context) =>
public static bool GetChampionTitleContext(PlayerMobile player, out ChampionTitleContext context) =>
_championTitleContexts.TryGetValue(player, out context);
public static ChampionTitleContext GetOrCreateChampionTitleContext(this PlayerMobile player)
public static ChampionTitleContext GetOrCreateChampionTitleContext(PlayerMobile player)
{
ref var context = ref CollectionsMarshal.GetValueRefOrAddDefault(_championTitleContexts, player, out var exists);
if (!exists)
@ -76,7 +82,7 @@ public static class ChampionTitleSystem
// Called when killing a harrower. Will give a minimum of 1 point.
public static void AwardHarrowerTitle(PlayerMobile pm)
{
var context = pm.GetOrCreateChampionTitleContext();
var context = GetOrCreateChampionTitleContext(pm);
var count = 1;
for (var i = 0; i < ChampionSpawnInfo.Table.Length; i++)
@ -91,9 +97,9 @@ public static class ChampionTitleSystem
context.Harrower = Math.Max(count, context.Harrower); // Harrower titles never decay.
}
public static int GetChampionTitleLabel(this PlayerMobile player)
public static int GetChampionTitleLabel(PlayerMobile player)
{
if (!player.GetChampionTitleContext(out var context))
if (!GetChampionTitleContext(player, out var context))
{
return 0;
}