2021-01-18 13:16:18 -08:00
|
|
|
/*************************************************************************
|
|
|
|
|
* ModernUO *
|
2024-06-23 10:51:20 -07:00
|
|
|
* Copyright 2019-2024 - ModernUO Development Team *
|
2021-01-18 13:16:18 -08:00
|
|
|
* Email: hi@modernuo.com *
|
|
|
|
|
* File: Persistence.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;
|
2021-02-24 22:14:15 -08:00
|
|
|
using System.Collections.Generic;
|
2021-01-18 13:16:18 -08:00
|
|
|
using System.IO;
|
2024-06-23 10:51:20 -07:00
|
|
|
using System.IO.MemoryMappedFiles;
|
2021-01-18 13:16:18 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
namespace Server;
|
|
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
public abstract class Persistence
|
2021-01-18 13:16:18 -08:00
|
|
|
{
|
2023-10-01 22:10:47 -07:00
|
|
|
private static readonly SortedSet<Persistence> _registry = new(new PersistenceComparer());
|
2022-10-10 21:47:08 -07:00
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
public int Priority { get; }
|
2022-10-10 21:47:08 -07:00
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
public Persistence(int priority = 100)
|
2021-01-18 13:16:18 -08:00
|
|
|
{
|
2023-10-01 22:10:47 -07:00
|
|
|
Priority = priority;
|
|
|
|
|
_registry.Add(this);
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2021-03-09 23:49:19 -08:00
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
public bool Register() => _registry.Add(this);
|
|
|
|
|
|
|
|
|
|
public void Unregister() => _registry.Remove(this);
|
2021-01-18 13:16:18 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static void Load(string path)
|
|
|
|
|
{
|
2022-10-11 22:17:22 -07:00
|
|
|
var typesDb = LoadTypes(path);
|
|
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
foreach (var entry in _registry)
|
|
|
|
|
{
|
|
|
|
|
(entry as IGenericEntityPersistence)?.DeserializeIndexes(path, typesDb);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// This should probably not be parallel since Mobiles must be loaded before Items
|
|
|
|
|
foreach (var entry in _registry)
|
2021-02-24 22:14:15 -08:00
|
|
|
{
|
2022-10-11 22:17:22 -07:00
|
|
|
entry.Deserialize(path, typesDb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
private static unsafe Dictionary<ulong, string> LoadTypes(string path)
|
2022-10-11 22:17:22 -07:00
|
|
|
{
|
|
|
|
|
var db = new Dictionary<ulong, string>();
|
|
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
string typesPath = Path.Combine(path, "SerializedTypes.db");
|
|
|
|
|
if (!File.Exists(typesPath))
|
2022-10-11 22:17:22 -07:00
|
|
|
{
|
|
|
|
|
return db;
|
2021-02-24 22:14:15 -08:00
|
|
|
}
|
2022-10-11 22:17:22 -07:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
using var mmf = MemoryMappedFile.CreateFromFile(typesPath, FileMode.Open);
|
|
|
|
|
using var accessor = mmf.CreateViewStream();
|
2022-10-11 22:17:22 -07:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
byte* ptr = null;
|
|
|
|
|
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
|
|
|
|
|
var dataReader = new UnmanagedDataReader(ptr, accessor.Length);
|
|
|
|
|
|
|
|
|
|
var version = dataReader.ReadInt();
|
|
|
|
|
var count = dataReader.ReadInt();
|
2022-10-11 22:17:22 -07:00
|
|
|
|
|
|
|
|
for (var i = 0; i < count; ++i)
|
|
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
var hash = dataReader.ReadULong();
|
|
|
|
|
var typeName = dataReader.ReadStringRaw();
|
2022-10-11 22:17:22 -07:00
|
|
|
db[hash] = typeName;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
accessor.SafeMemoryMappedViewHandle.ReleasePointer();
|
2022-10-11 22:17:22 -07:00
|
|
|
return db;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2021-01-18 13:16:18 -08:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
// Note: This is strictly on a background thread
|
2024-09-14 09:57:43 -07:00
|
|
|
internal static void WriteSnapshotAll(string path, HashSet<Type> typeSet)
|
2023-10-01 22:10:47 -07:00
|
|
|
{
|
2023-10-03 00:42:49 -07:00
|
|
|
foreach (var p in _registry)
|
|
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
p.WriteSnapshot(path, typeSet);
|
2023-10-03 00:42:49 -07:00
|
|
|
}
|
2023-10-01 22:10:47 -07:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
WriteSerializedTypesSnapshot(path, typeSet);
|
|
|
|
|
}
|
2024-06-23 10:51:20 -07:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
public static void WriteSerializedTypesSnapshot(string path, HashSet<Type> types)
|
2023-10-01 22:10:47 -07:00
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
string typesPath = Path.Combine(path, "SerializedTypes.db");
|
|
|
|
|
using var fs = new FileStream(typesPath, FileMode.Create);
|
|
|
|
|
using var writer = new MemoryMapFileWriter(fs, 1024 * 1024 * 4);
|
|
|
|
|
|
|
|
|
|
writer.Write(0); // version
|
|
|
|
|
writer.Write(types.Count);
|
2024-06-23 10:51:20 -07:00
|
|
|
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
var fullName = type.FullName;
|
|
|
|
|
writer.Write(HashUtility.ComputeHash64(fullName));
|
|
|
|
|
writer.WriteStringRaw(fullName);
|
2023-10-01 22:10:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
internal static void SerializeAll()
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2023-10-01 22:10:47 -07:00
|
|
|
foreach (var p in _registry)
|
|
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
p.Serialize();
|
2023-10-01 22:10:47 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2022-05-10 11:57:37 -07:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
internal static void PostWorldSaveAll()
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
foreach (var p in _registry)
|
2021-02-24 22:14:15 -08:00
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
p.PostWorldSave();
|
2021-02-24 22:14:15 -08:00
|
|
|
}
|
2024-06-23 10:51:20 -07:00
|
|
|
}
|
2022-10-11 22:17:22 -07:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
internal static void PostDeserializeAll()
|
|
|
|
|
{
|
|
|
|
|
foreach (var p in _registry)
|
2022-10-11 22:17:22 -07:00
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
p.PostDeserialize();
|
2022-10-11 22:17:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
// Note: This should only be run on a background thread
|
2024-09-14 09:57:43 -07:00
|
|
|
public abstract void WriteSnapshot(string savePath, HashSet<Type> typeSet);
|
2024-06-23 10:51:20 -07:00
|
|
|
|
|
|
|
|
public abstract void Serialize();
|
2023-10-01 22:10:47 -07:00
|
|
|
|
|
|
|
|
public abstract void Deserialize(string savePath, Dictionary<ulong, string> typesDb);
|
|
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
public virtual void PostWorldSave()
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2023-10-01 22:10:47 -07:00
|
|
|
}
|
2022-10-27 23:27:22 -07:00
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
public virtual void PostDeserialize()
|
|
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2021-02-24 22:14:15 -08:00
|
|
|
|
2023-10-01 22:10:47 -07:00
|
|
|
internal class PersistenceComparer : IComparer<Persistence>
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2023-10-01 22:10:47 -07:00
|
|
|
public int Compare(Persistence x, Persistence y)
|
2021-02-24 22:14:15 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
if (x == y)
|
2021-01-18 13:16:18 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
return 0;
|
2021-01-18 13:16:18 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (x == null)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2021-01-18 13:16:18 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (y == null)
|
2021-04-15 19:55:57 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
return -1;
|
2021-04-15 19:55:57 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
// 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
|
2023-10-01 22:10:47 -07:00
|
|
|
return cmp != 0 ? cmp : x.GetHashCode().CompareTo(y.GetHashCode());
|
2021-02-24 22:14:15 -08:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2021-01-18 13:16:18 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static void TraceException(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
try
|
2021-02-24 22:14:15 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
using var op = new StreamWriter("save-errors.log", true);
|
|
|
|
|
op.WriteLine("# {0}", Core.Now);
|
2021-02-24 22:14:15 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
op.WriteLine(ex);
|
2021-02-24 22:14:15 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
op.WriteLine();
|
|
|
|
|
op.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
2021-01-18 13:16:18 -08:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
Console.WriteLine(ex);
|
2021-01-18 13:16:18 -08:00
|
|
|
}
|
|
|
|
|
}
|