/************************************************************************* * 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 . * *************************************************************************/ using System; using System.IO; namespace Server; public static class GenericPersistence { public static void Register( string name, Action serializer, Action 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) { string binPath = Path.Combine(savePath, name, $"{name}.bin"); var buffer = saveBuffer!.Buffer.AsSpan(0, (int)saveBuffer.Position); AdhocPersistence.WriteSnapshot(binPath, buffer); } void Deserialize(string savePath) => AdhocPersistence.Deserialize(Path.Combine(savePath, name, $"{name}.bin"), deserializer); Persistence.Register(name, Serialize, WriterSnapshot, Deserialize, priority); } }