fix(serialization): Updates serialization flow & fixes serializing accounts (#521)

This commit is contained in:
Kamron Batman 2021-02-24 22:14:15 -08:00 committed by GitHub
parent 78e3450671
commit 8d54d273a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 747 additions and 381 deletions

View file

@ -0,0 +1,54 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: GenericPersistence.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
namespace Server
{
public static class GenericPersistence
{
public static void Serialize(Action<IGenericWriter> serializer) => serializer(new BufferWriter(true));
public static void WriteSnapshot(string path, Action<IGenericWriter> serializer)
{
AssemblyHandler.EnsureDirectory(Path.GetDirectoryName(path));
using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.None);
serializer(new BinaryFileWriter(fs, true));
}
public static void Deserialize(string path, Action<IGenericReader> deserializer, bool ensure = true)
{
AssemblyHandler.EnsureDirectory(Path.GetDirectoryName(path));
if (!File.Exists(path))
{
if (ensure)
{
new FileInfo(path).Create().Close();
}
return;
}
using FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
// TODO: Support files larger than 2GB
var buffer = GC.AllocateUninitializedArray<byte>((int)fs.Length);
deserializer(new BufferReader(buffer));
}
}
}

View file

@ -14,57 +14,103 @@
*************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Server
{
public static class Persistence
{
public static void Serialize(string path, Action<IGenericWriter> serializer)
public static readonly SortedSet<RegistryEntry> _registry = new(new RegistryEntryComparer());
public static void Register(
Action serializer,
Action<string> snapshotWriter,
Action<string> deserializer,
int priority = 100
)
{
AssemblyHandler.EnsureDirectory(Path.GetDirectoryName(path));
_registry.Add(
new RegistryEntry
{
Priority = priority,
Serialize = serializer,
WriteSnapshot = snapshotWriter,
Deserialize = deserializer
}
);
}
using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.None);
var writer = new BinaryFileWriter(fs, true);
try
public static void Load(string path)
{
// This should probably not be parallel since Mobiles must be loaded before Items
foreach (var entry in _registry)
{
serializer(writer);
}
catch (Exception e)
{
Console.WriteLine("[Persistence]: Failed to serialize");
Console.WriteLine(e);
entry.Deserialize(path);
}
}
public static void Deserialize(string path, Action<IGenericReader> deserializer, bool ensure = true)
public static void Serialize()
{
AssemblyHandler.EnsureDirectory(Path.GetDirectoryName(path));
Parallel.ForEach(_registry, entry => entry.Serialize());
}
if (!File.Exists(path))
public static void WriteSnapshot(string path)
{
foreach (var entry in _registry)
{
if (ensure)
{
new FileInfo(path).Create().Close();
}
return;
entry.WriteSnapshot(path);
}
}
using FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
// TODO: Support files larger than 2GB
var buffer = GC.AllocateUninitializedArray<byte>((int)fs.Length);
public class RegistryEntry
{
public int Priority { get; init; }
public Action Serialize { get; init; } // Serializing to memory buffers
public Action<string> WriteSnapshot { get; init; }
public Action<string> Deserialize { get; init; }
}
internal class RegistryEntryComparer : IComparer<RegistryEntry>
{
public int Compare(RegistryEntry x, RegistryEntry y) =>
x?.Priority.CompareTo(y?.Priority) ?? 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteConsole(string message)
{
var now = DateTime.UtcNow;
Console.Write("[{0} {1}] Persistence: {2}", now.ToShortDateString(), now.ToLongTimeString(), message);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteConsoleLine(string message)
{
var now = DateTime.UtcNow;
Console.WriteLine("[{0} {1}] Persistence: {2}", now.ToShortDateString(), now.ToLongTimeString(), message);
}
public static void TraceException(Exception ex)
{
try
{
deserializer(new BufferReader(buffer));
using var op = new StreamWriter("save-errors.log", true);
op.WriteLine("# {0}", DateTime.UtcNow);
op.WriteLine(ex);
op.WriteLine();
op.WriteLine();
}
catch (Exception e)
catch
{
Console.WriteLine("[Persistence]: Failed to deserialize");
Console.WriteLine(e);
// ignored
}
Console.WriteLine(ex);
}
}
}