public void SaveTo(string filename) { var entities = new List <dynamic>(); File.Entities.Clear(); NewIds.Clear(); // we need to define all the ids before serializing foreach (var defPair in Factory.Definitions) { IDeclarator parent = defPair.Value.Parent; File.Entities.Add(new SerializationModel.Entity { Id = (uint)File.Entities.Count, Name = defPair.Value.Name, Type = Factory.GetEntityType(defPair.Key), Visibility = parent == null ? EntityFactory.VISIBILITY.PUBLIC : (EntityFactory.VISIBILITY)parent.GetVisibilityOf(defPair.Value.Name) }); NewIds[defPair.Value] = File.Entities.Last().Id; } // then we just have to get each data individually foreach (IDefinition definition in NewIds.Keys) { if (entities.Count == 9) { Console.WriteLine("On debug ici"); } dynamic entity = GetSerializableEntityFrom(definition); if (entity == null) { throw new InvalidOperationException($"Cannot determine the type of entity to serialize {entity.Type}"); } entities.Add(entity); } // finally we persist the data into a file using (var saveFile = new StreamWriter(filename)) { BinarySerializer.Serializer.Serialize(File, saveFile.BaseStream); foreach (var entity in entities) { BinarySerializer.Serializer.Serialize(entity, saveFile.BaseStream); } } }
public Controller LoadFrom(string filename) { var entities = new List <dynamic>(); Controller.Reset(); File.Entities.Clear(); NewIds.Clear(); FileIdToCoreId = new Dictionary <uint, uint> { { 0, (uint)EntityFactory.BASE_ID.GLOBAL_CTX }, { 1, (uint)EntityFactory.BASE_ID.BOOLEAN_TYPE }, { 2, (uint)EntityFactory.BASE_ID.INTEGER_TYPE }, { 3, (uint)EntityFactory.BASE_ID.FLOATING_TYPE }, { 4, (uint)EntityFactory.BASE_ID.CHARACTER_TYPE }, { 5, (uint)EntityFactory.BASE_ID.STRING_TYPE }, { 6, (uint)EntityFactory.BASE_ID.DICT_TYPE }, { 7, (uint)EntityFactory.BASE_ID.ANY_TYPE }, { 8, (uint)EntityFactory.BASE_ID.MATRIX_TYPE }, { 9, (uint)EntityFactory.BASE_ID.RESSOURCE_TYPE } }; using (var loadFile = new StreamReader(filename)) { //récupérer les entites du fichier File = BinarySerializer.Serializer.Deserialize <CoreFile>(loadFile.BaseStream); if (File.MagicNumber != EntityFactory.MagicNumber) { throw new FileLoadException("Trying to load an invalid or corrupted dnai file"); } //vérification de la version if (File.Version > Controller.Version) { throw new FileLoadException($"Trying to load a file compiled from DNAI.Core {File.Version.Value}: update your DNAI.Core"); } //récupération des données des entités foreach (var entity in File.Entities) { var serialized = GetSerializedEntityFrom(entity, loadFile.BaseStream); if (serialized == null) { throw new FileLoadException("Trying to load an invalid or corrupted dnai file"); } entities.Add(serialized); } } //déclaration de toutes les entités for (int i = 0; i < entities.Count; i++) { //si l'entité est un contexte if (entities[i] is SerializationModel.Context context) { SerializationModel.Entity parent = File.Entities[i]; foreach (uint childID in context.Children) { SerializationModel.Entity child = File.Entities[(int)childID]; if (child.Type != EntityFactory.ENTITY.DATA_TYPE) { FileIdToCoreId[child.Id] = Controller.Declare(child.Type, FileIdToCoreId[parent.Id], child.Name, child.Visibility); } } } } var funcs = new Dictionary <SerializationModel.Entity, SerializationModel.Function>(); //réplication des toutes les entités (sauf les instructions) for (int i = 0; i < entities.Count; i++) { SerializationModel.Entity entity = File.Entities[i]; dynamic data = entities[i]; ReplicateEntityFrom(entity, data); if (data is SerializationModel.Function func) { funcs[entity] = func; } } //réplication des instructions foreach (var func in funcs) { uint funcId = FileIdToCoreId[func.Key.Id]; ReplicateInstructionsFrom(funcId, func.Value); } return(Controller); }