fix(core): Fixes serialization registry (#574)
This commit is contained in:
parent
837ca91843
commit
0d7ec99cbe
4 changed files with 31 additions and 7 deletions
|
|
@ -78,7 +78,7 @@ namespace Server
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Persistence.Register(Serialize, WriterSnapshot, Deserialize, priority);
|
Persistence.Register(name, Serialize, WriterSnapshot, Deserialize, priority);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,10 @@ namespace Server
|
||||||
{
|
{
|
||||||
public const int DefaultPriority = 100;
|
public const int DefaultPriority = 100;
|
||||||
|
|
||||||
public static readonly SortedSet<RegistryEntry> _registry = new(new RegistryEntryComparer());
|
private static readonly SortedSet<RegistryEntry> _registry = new(new RegistryEntryComparer());
|
||||||
|
|
||||||
public static void Register(
|
public static void Register(
|
||||||
|
string name,
|
||||||
Action serializer,
|
Action serializer,
|
||||||
Action<string> snapshotWriter,
|
Action<string> snapshotWriter,
|
||||||
Action<string> deserializer,
|
Action<string> deserializer,
|
||||||
|
|
@ -37,6 +38,7 @@ namespace Server
|
||||||
_registry.Add(
|
_registry.Add(
|
||||||
new RegistryEntry
|
new RegistryEntry
|
||||||
{
|
{
|
||||||
|
Name = name,
|
||||||
Priority = priority,
|
Priority = priority,
|
||||||
Serialize = serializer,
|
Serialize = serializer,
|
||||||
WriteSnapshot = snapshotWriter,
|
WriteSnapshot = snapshotWriter,
|
||||||
|
|
@ -67,8 +69,9 @@ namespace Server
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RegistryEntry
|
public record RegistryEntry
|
||||||
{
|
{
|
||||||
|
public string Name { get; init; }
|
||||||
public int Priority { get; init; }
|
public int Priority { get; init; }
|
||||||
public Action Serialize { get; init; } // Serializing to memory buffers
|
public Action Serialize { get; init; } // Serializing to memory buffers
|
||||||
public Action<string> WriteSnapshot { get; init; }
|
public Action<string> WriteSnapshot { get; init; }
|
||||||
|
|
@ -77,8 +80,29 @@ namespace Server
|
||||||
|
|
||||||
internal class RegistryEntryComparer : IComparer<RegistryEntry>
|
internal class RegistryEntryComparer : IComparer<RegistryEntry>
|
||||||
{
|
{
|
||||||
public int Compare(RegistryEntry x, RegistryEntry y) =>
|
public int Compare(RegistryEntry x, RegistryEntry y)
|
||||||
x?.Priority.CompareTo(y?.Priority) ?? 1;
|
{
|
||||||
|
if (x == y)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x == null)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First sort by priority
|
||||||
|
var cmp = x.Priority.CompareTo(y.Priority);
|
||||||
|
|
||||||
|
// Then alphabetically. We won't allow the same entry (by name) twice in the SortedSet
|
||||||
|
return cmp != 0 ? cmp : x.Name?.CompareOrdinal(y.Name) ?? -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ namespace Server
|
||||||
_savePath = Path.Combine(Core.BaseDirectory, savePath);
|
_savePath = Path.Combine(Core.BaseDirectory, savePath);
|
||||||
|
|
||||||
// Mobiles & Items
|
// Mobiles & Items
|
||||||
Persistence.Register(SaveEntities, WriteEntities, LoadEntities, 1);
|
Persistence.Register("Mobiles & Items", SaveEntities, WriteEntities, LoadEntities, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace Server.Accounting
|
||||||
public static int Count => _accountsByName.Count;
|
public static int Count => _accountsByName.Count;
|
||||||
|
|
||||||
public static void Configure() =>
|
public static void Configure() =>
|
||||||
Persistence.Register(Serialize, WriteSnapshot, Deserialize);
|
Persistence.Register("Accounts", Serialize, WriteSnapshot, Deserialize);
|
||||||
|
|
||||||
internal static void Serialize() =>
|
internal static void Serialize() =>
|
||||||
EntityPersistence.SaveEntities(_accountsById.Values, account => account.Serialize());
|
EntityPersistence.SaveEntities(_accountsById.Values, account => account.Serialize());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue