2021-02-24 22:14:15 -08:00
|
|
|
/*************************************************************************
|
|
|
|
|
* ModernUO *
|
2024-06-23 10:51:20 -07:00
|
|
|
* Copyright 2019-2024 - ModernUO Development Team *
|
2021-02-24 22:14:15 -08:00
|
|
|
* 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;
|
2022-10-11 22:17:22 -07:00
|
|
|
using System.Collections.Generic;
|
2021-02-24 22:14:15 -08:00
|
|
|
using System.IO;
|
2024-09-14 09:57:43 -07:00
|
|
|
using System.IO.MemoryMappedFiles;
|
2021-02-24 22:14:15 -08:00
|
|
|
|
2022-08-16 08:59:00 -07:00
|
|
|
namespace Server;
|
|
|
|
|
|
2023-10-03 00:42:49 -07:00
|
|
|
public abstract class GenericPersistence : Persistence, IGenericSerializable
|
2021-02-24 22:14:15 -08:00
|
|
|
{
|
2023-10-01 22:10:47 -07:00
|
|
|
public string Name { get; }
|
2024-09-14 09:57:43 -07:00
|
|
|
public string SaveFilePath { get; protected set; } // "<Folder>/<System>.bin"
|
2021-03-09 23:41:54 -08:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
public byte SerializedThread { get; set; }
|
|
|
|
|
public int SerializedPosition { get; set; }
|
|
|
|
|
public int SerializedLength { get; set; }
|
2021-03-09 23:41:54 -08:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
public GenericPersistence(string name, int priority) : base(priority)
|
2023-10-01 22:10:47 -07:00
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
Name = name;
|
|
|
|
|
SaveFilePath = Path.Combine(Name, $"{Name}.bin");
|
2024-06-23 10:51:20 -07:00
|
|
|
}
|
2023-10-03 00:42:49 -07:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
public override void Serialize()
|
|
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
World.PushToCache(this);
|
2024-06-23 10:51:20 -07:00
|
|
|
}
|
2023-10-03 00:42:49 -07:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
public override void WriteSnapshot(string savePath, HashSet<Type> typeSet)
|
2023-10-03 00:42:49 -07:00
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
if (SerializedLength == 0)
|
2024-06-23 10:51:20 -07:00
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2024-06-23 10:51:20 -07:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
var file = Path.Combine(savePath, SaveFilePath);
|
|
|
|
|
var dir = Path.GetDirectoryName(file);
|
|
|
|
|
PathUtility.EnsureDirectory(dir);
|
|
|
|
|
|
|
|
|
|
var threads = World._threadWorkers;
|
|
|
|
|
|
|
|
|
|
using var binFs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
|
|
|
|
|
|
|
|
var thread = SerializedThread;
|
|
|
|
|
var heapStart = SerializedPosition;
|
|
|
|
|
var heapLength = SerializedLength;
|
|
|
|
|
|
|
|
|
|
binFs.Write(threads[thread].GetHeap(heapStart, heapLength));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override unsafe void Deserialize(string savePath, Dictionary<ulong, string> typesDb)
|
|
|
|
|
{
|
|
|
|
|
// Assume savePath has the Core.BaseDirectory already prepended
|
|
|
|
|
var dataPath = Path.GetFullPath(SaveFilePath, savePath);
|
|
|
|
|
var file = new FileInfo(dataPath);
|
|
|
|
|
|
|
|
|
|
if (!file.Exists || file.Length <= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2024-06-23 10:51:20 -07:00
|
|
|
}
|
2021-03-09 23:41:54 -08:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
var fileLength = file.Length;
|
|
|
|
|
|
|
|
|
|
string error;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var mmf = MemoryMappedFile.CreateFromFile(dataPath, FileMode.Open);
|
|
|
|
|
using var accessor = mmf.CreateViewStream();
|
|
|
|
|
|
|
|
|
|
byte* ptr = null;
|
|
|
|
|
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
|
|
|
|
|
var dataReader = new UnmanagedDataReader(ptr, accessor.Length, typesDb);
|
|
|
|
|
Deserialize(dataReader);
|
|
|
|
|
|
|
|
|
|
error = dataReader.Position != fileLength
|
|
|
|
|
? $"Serialized {fileLength} bytes, but {dataReader.Position} bytes deserialized"
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
accessor.SafeMemoryMappedViewHandle.ReleasePointer();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2024-06-23 10:51:20 -07:00
|
|
|
{
|
2024-09-14 09:57:43 -07:00
|
|
|
error = e.ToString();
|
2024-06-23 10:51:20 -07:00
|
|
|
}
|
2021-02-24 22:14:15 -08:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
if (error != null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"***** Bad deserialize of {file.FullName} *****");
|
|
|
|
|
Console.WriteLine(error);
|
2021-02-24 22:14:15 -08:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
Console.Write("Skip this file and continue? (y/n): ");
|
|
|
|
|
var y = Console.ReadLine();
|
2023-10-01 22:10:47 -07:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
if (!y.InsensitiveEquals("y"))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Deserialization failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-01 22:10:47 -07:00
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
public abstract void Serialize(IGenericWriter writer);
|
2023-10-01 22:10:47 -07:00
|
|
|
public abstract void Deserialize(IGenericReader reader);
|
2021-02-24 22:14:15 -08:00
|
|
|
}
|