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); try { deserializer(new BufferReader(buffer)); } catch (Exception e) { Console.WriteLine("[Persistence]: Failed to deserialize"); Console.WriteLine(e); } }
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 WriteEntities <I, T>( IIndexInfo <I> indexInfo, Dictionary <I, T> entities, List <Type> types, string savePath, out Dictionary <string, int> counts ) where T : class, ISerializable { counts = new Dictionary <string, int>(); var typeName = indexInfo.TypeName; var path = Path.Combine(savePath, typeName); AssemblyHandler.EnsureDirectory(path); string idxPath = Path.Combine(path, $"{typeName}.idx"); string tdbPath = Path.Combine(path, $"{typeName}.tdb"); string binPath = Path.Combine(path, $"{typeName}.bin"); using var idx = new BinaryFileWriter(idxPath, false); using var tdb = new BinaryFileWriter(tdbPath, false); using var bin = new BinaryFileWriter(binPath, true); idx.Write(entities.Count); foreach (var e in entities.Values) { long start = bin.Position; idx.Write(e.TypeRef); idx.Write(e.Serial); idx.Write(start); e.SerializeTo(bin); idx.Write((int)(bin.Position - start)); var type = e.GetType().FullName; if (type != null) { counts[type] = (counts.TryGetValue(type, out var count) ? count : 0) + 1; } } tdb.Write(types.Count); for (int i = 0; i < types.Count; ++i) { tdb.Write(types[i].FullName); } }
public static void Serialize(string path, Action <IGenericWriter> serializer) { AssemblyHandler.EnsureDirectory(Path.GetDirectoryName(path)); using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.None); var writer = new BinaryFileWriter(fs, true); try { serializer(writer); } catch (Exception e) { Console.WriteLine("[Persistence]: Failed to serialize"); Console.WriteLine(e); } }
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); AssemblyHandler.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); AssemblyHandler.EnsureDirectory(path); string binPath = Path.Combine(path, $"{name}.bin"); if (!File.Exists(binPath)) { return; } try { using FileStream bin = new FileStream(binPath, FileMode.Open, FileAccess.Read, FileShare.Read); var buffer = GC.AllocateUninitializedArray <byte>((int)bin.Length); bin.Read(buffer); deserializer(new BufferReader(buffer)); } 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); }