Ejemplo n.º 1
0
 /// <summary>
 /// Called when the entity is being loaded from disk.
 /// Override this to read any data that you saved when overriding the <see cref="Serialize(BinaryWriter)"/>
 /// method. Data must be written and read in the same order.
 /// </summary>
 /// <param name="reader"></param>
 public virtual void Deserialize(IOReader reader)
 {
     if (SerializeName)
     {
         Name = reader.ReadString();
     }
     Bounds = reader.ReadBounds();
 }
Ejemplo n.º 2
0
        public void DeserializeAllNew(IOReader reader)
        {
            ClearEntities();

            // Read name map...
            Type[]   blankParams = new Type[0];
            object[] blankArgs   = new object[0];
            int      count       = reader.ReadInt32();

            Type[]            types        = new Type[count];
            ConstructorInfo[] constructors = new ConstructorInfo[count];
            for (int i = 0; i < count; i++)
            {
                string typeName = reader.ReadString();

                Type t = Type.GetType(typeName, false, false);
                if (t == null)
                {
                    throw new Exception($"Entity of type {typeName} could not be found, so cannot be loaded from disk! Reading cannot continue because data length is unknown.");
                }

                var constructor = t.GetConstructor(blankParams);
                if (constructor == null)
                {
                    throw new Exception($"Entity of type {typeName} does not have a no-parameter constructor, so cannot be loaded. Reading cannot continue since data length is unknown.");
                }
                types[i]        = t;
                constructors[i] = constructor;
            }



            count = reader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                ushort typeMapIndex = reader.ReadUInt16();
                var    constructor  = constructors[typeMapIndex];

                var obj = constructor.Invoke(blankArgs);

                // No need to register: Entities should auto register, especially since the default constructor is being used.
                Entity e = obj as Entity;
                e.Deserialize(reader);
            }
        }