Ejemplo n.º 1
0
        private dynamic GetSerializedEntityFrom(SerializationModel.Entity entity, Stream file)
        {
            switch (entity.Type)
            {
            case EntityFactory.ENTITY.CONTEXT:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.Context>(file));

            case EntityFactory.ENTITY.VARIABLE:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.Variable>(file));

            case EntityFactory.ENTITY.FUNCTION:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.Function>(file));

            case EntityFactory.ENTITY.DATA_TYPE:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.DataType>(file));

            case EntityFactory.ENTITY.ENUM_TYPE:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.EnumType>(file));

            case EntityFactory.ENTITY.OBJECT_TYPE:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.ObjectType>(file));

            case EntityFactory.ENTITY.LIST_TYPE:
                return(BinarySerializer.Serializer.Deserialize <SerializationModel.ListType>(file));

            default:
                return(null);
            }
        }
Ejemplo n.º 2
0
        private void ReplicateEntityFrom(SerializationModel.Entity entity, dynamic data)
        {
            uint entityId = FileIdToCoreId[entity.Id];

            switch (entity.Type)
            {
            case EntityFactory.ENTITY.VARIABLE:
                ReplicateVariableFrom(entityId, data);
                break;

            case EntityFactory.ENTITY.FUNCTION:
                ReplicateFunctionFrom(entityId, data);
                break;

            case EntityFactory.ENTITY.ENUM_TYPE:
                ReplicateEnumFrom(entityId, data);
                break;

            case EntityFactory.ENTITY.OBJECT_TYPE:
                ReplicateObjectTypeFrom(entityId, data);
                break;

            case EntityFactory.ENTITY.LIST_TYPE:
                ReplicateListTypeFrom(entityId, data);
                break;
            }
        }
Ejemplo n.º 3
0
        private dynamic GetSerializableEntityFrom(IDefinition definition)
        {
            uint definitionId = GetIdOf(definition);
            int  index        = (int)definitionId;

            SerializationModel.Entity entity = File.Entities[index];

            switch (entity.Type)
            {
            case EntityFactory.ENTITY.CONTEXT:
                return(GetContextFrom((Context)definition));

            case EntityFactory.ENTITY.DATA_TYPE:
                return(GetDataTypeFrom((DataType)definition));

            case EntityFactory.ENTITY.ENUM_TYPE:
                return(GetEnumFrom((EnumType)definition));

            case EntityFactory.ENTITY.FUNCTION:
                return(GetFunctionFrom((Function)definition));

            case EntityFactory.ENTITY.LIST_TYPE:
                return(GetListTypeFrom((ListType)definition));

            case EntityFactory.ENTITY.OBJECT_TYPE:
                return(GetObjectTypeFrom((ObjectType)definition));

            case EntityFactory.ENTITY.VARIABLE:
                return(GetVariableFrom((Variable)definition));

            default:
                return(null);
            }
        }
Ejemplo n.º 4
0
        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);
        }