private static List <Tuple <ConstructorInfo, string> > ReadTypes <I>(BinaryReader tdbReader) { var constructorTypes = new[] { typeof(I) }; var count = tdbReader.ReadInt32(); var types = new List <Tuple <ConstructorInfo, string> >(count); for (var i = 0; i < count; ++i) { var typeName = tdbReader.ReadString(); var t = AssemblyHandler.FindTypeByFullName(typeName, false); if (t?.IsAbstract != false) { Persistence.WriteConsoleLine("failed"); var issue = t?.IsAbstract == true ? "marked abstract" : "not found"; Persistence.WriteConsoleLine($"Error: Type '{typeName}' was {issue}. Delete all of those types? (y/n)"); if (Console.ReadKey(true).Key == ConsoleKey.Y) { types.Add(null); Persistence.WriteConsole("Loading..."); continue; } Persistence.WriteConsoleLine("Types will not be deleted. An exception will be thrown."); throw new Exception($"Bad type '{typeName}'"); } var ctor = t.GetConstructor(constructorTypes); if (ctor != null) { types.Add(new Tuple <ConstructorInfo, string>(ctor, typeName)); } else { throw new Exception($"Type '{t}' does not have a serialization constructor"); } } return(types); }
public static void LoadData <I, T>( string path, IIndexInfo <I> indexInfo, List <EntityIndex <T> > entities ) where T : class, ISerializable { var indexType = indexInfo.TypeName; string dataPath = Path.Combine(path, indexType, $"{indexType}.bin"); if (!File.Exists(dataPath)) { return; } using FileStream bin = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read); BufferReader br = null; foreach (var entry in entities) { T t = entry.Entity; // Skip this entry if (t == null) { bin.Seek(entry.Length, SeekOrigin.Current); continue; } var buffer = GC.AllocateUninitializedArray <byte>(entry.Length); if (br == null) { br = new BufferReader(buffer); } else { br.SwapBuffers(buffer, out _); } bin.Read(buffer.AsSpan()); string error; try { t.Deserialize(br); error = br.Position != entry.Length ? $"Serialized object was {entry.Length} bytes, but {br.Position} bytes deserialized" : null; } catch (Exception e) { error = e.ToString(); } if (error == null) { t.InitializeSaveBuffer(buffer); } else { Utility.PushColor(ConsoleColor.Red); Persistence.WriteConsoleLine($"***** Bad deserialize of {t.GetType()} *****"); Persistence.WriteConsoleLine(error); Utility.PopColor(); Persistence.WriteConsoleLine("Delete the object and continue? (y/n)"); if (Console.ReadKey(true).Key != ConsoleKey.Y) { throw new Exception("Deserialization failed."); } t.Delete(); } } }
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 br = new BufferReader(GC.AllocateUninitializedArray <byte>((int)bin.Length)); deserializer(br); } catch (Exception e) { Utility.PushColor(ConsoleColor.Red); Persistence.WriteConsoleLine($"***** Bad deserialize of {name} *****"); Persistence.WriteConsoleLine(e.ToString()); Utility.PopColor(); } } Persistence.Register(Serialize, WriterSnapshot, Deserialize, priority); }