modernuo/Projects/Server/Serialization/GenericPersistence.cs
Kamron Batman a55e271a69
fix: Cleans up file system paths and archiving (#815)
* Makes EnsureDirectory properly work for relative and absolute paths
* Adds a `PathUtility.GetFullPath` which returns full paths for relative paths to `Core.BaseDirectory`. If the path is absolute, it will return as-is.
* Moves EnsureDirectory to `PathUtility`. So `ScriptsHandler.EnsureDirectory` and `AssemblyHandler.EnsureDirectory` are now `PathUtility.EnsureDirectory`
* Fixes crash guard so that it copies accounts properly.
* Changes world save and auto archive to use a random folder name inside of the temp folder.
2021-10-05 08:54:44 -07:00

84 lines
3 KiB
C#

/*************************************************************************
* 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 Register(
string name,
Action<IGenericWriter> serializer,
Action<IGenericReader> deserializer,
int priority = Persistence.DefaultPriority
)
{
BufferWriter saveBuffer = null;
void Serialize()
{
saveBuffer ??= new BufferWriter(true);
saveBuffer.Seek(0, SeekOrigin.Begin);
serializer(saveBuffer);
}
void WriterSnapshot(string savePath)
{
var path = Path.Combine(savePath, name);
PathUtility.EnsureDirectory(path);
string binPath = Path.Combine(path, $"{name}.bin");
using var bin = new BinaryFileWriter(binPath, true);
saveBuffer!.Resize((int)saveBuffer.Position);
bin.Write(saveBuffer.Buffer);
}
void Deserialize(string savePath)
{
var path = Path.Combine(savePath, name);
PathUtility.EnsureDirectory(path);
string binPath = Path.Combine(path, $"{name}.bin");
if (!File.Exists(binPath))
{
return;
}
try
{
using FileStream fs = new FileStream(binPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var br = new BinaryFileReader(fs);
deserializer(br);
}
catch (Exception e)
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine($"***** Bad deserialize of {name} *****");
Console.WriteLine(e.ToString());
Utility.PopColor();
}
}
Persistence.Register(name, Serialize, WriterSnapshot, Deserialize, priority);
}
}
}