예제 #1
0
        private static void SaveArrayWhenAllElementsAreTheSameType(IList list, BinaryWriter writer)
        {
            var arrayType = list[0] != null && list[0].GetType().Name == "RuntimeType"
                                ? ArrayElementType.AllTypesAreTypes : ArrayElementType.AllTypesAreTheSame;
            var firstElementType = BinaryDataExtensions.GetTypeOrObjectType(list[0]);

            if (arrayType == ArrayElementType.AllTypesAreTheSame && firstElementType == typeof(object) &&
                list[0] == null)
            {
                arrayType = ArrayElementType.AllTypesAreNull;
            }
            writer.Write((byte)arrayType);
            if (arrayType == ArrayElementType.AllTypesAreNull)
            {
                return;
            }
            if (arrayType == ArrayElementType.AllTypesAreTheSame)
            {
                writer.Write(list[0].GetShortNameOrFullNameIfNotFound());
            }
            foreach (object value in list)
            {
                SaveData(value, firstElementType, writer);
            }
        }
예제 #2
0
 private static void LoadClassData(object data, Type type, BinaryReader reader)
 {
     foreach (FieldInfo field in GetBackingFields(type))
     {
         Type fieldType = field.FieldType;
         if (fieldType.DoNotNeedToSaveType(field.Attributes) || fieldType == type)
         {
             continue;
         }
         if (field.FieldType.IsClass)
         {
             bool isNull = !reader.ReadBoolean();
             if (isNull)
             {
                 continue;
             }
             if (fieldType.NeedToSaveTypeName())
             {
                 fieldType =
                     BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(reader.ReadString());
             }
         }
         field.SetValue(data, CreateAndLoad(fieldType, reader));
     }
 }
예제 #3
0
        private static object LoadArray(IList list, Type arrayType, BinaryReader reader)
        {
            var count = reader.ReadNumberMostlyBelow255();

            if (list == null)
            {
                list = Activator.CreateInstance(arrayType, new object[] { count }) as IList;
            }
            if (count == 0)
            {
                return(list);
            }
            var arrayElementType = (BinaryDataSaver.ArrayElementType)reader.ReadByte();

            if (arrayElementType == BinaryDataSaver.ArrayElementType.AllTypesAreNull)
            {
                return(list);
            }
            if (arrayElementType == BinaryDataSaver.ArrayElementType.AllTypesAreDifferent)
            {
                LoadArrayWhereAllElementsAreNotTheSameType(list, arrayType, reader, count);
            }
            else
            {
                Type elementType = arrayElementType == BinaryDataSaver.ArrayElementType.AllTypesAreTypes
                                        ? typeof(Type)
                                        : BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(reader.ReadString());
                LoadArrayWhereAllElementsAreTheSameType(list, arrayType, reader, count, elementType);
            }
            return(list);
        }
예제 #4
0
 private static void SaveElementWithItsType(BinaryWriter writer, object value)
 {
     writer.Write(value.GetShortNameOrFullNameIfNotFound());
     writer.Write(value != null);
     if (value != null)
     {
         SaveData(value, BinaryDataExtensions.GetTypeOrObjectType(value), writer);
     }
 }
예제 #5
0
        private static void LoadEntityBehaviors(BinaryReader reader, Entity entity)
        {
            var behaviors = LoadArray(null, typeof(List <string>), reader) as List <string>;

            foreach (string behavior in behaviors)
            {
                entity.Start(BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(behavior));
            }
        }
예제 #6
0
        private static void LoadDrawableEntityDrawBehaviors(BinaryReader reader,
                                                            DrawableEntity drawable)
        {
            var drawBehaviors = LoadArray(null, typeof(List <string>), reader) as List <string>;

            foreach (string behavior in drawBehaviors)
            {
                drawable.OnDraw(BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(behavior));
            }
        }
예제 #7
0
        private static void LoadListElement(IList list, BinaryReader reader)
        {
            Type elementType =
                BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(reader.ReadString());
            bool isNotNull = reader.ReadBoolean();

            if (isNotNull)
            {
                list.Add(CreateAndLoad(elementType, reader));
            }
        }
예제 #8
0
        private static bool AreAllElementsTheSameType(IList list)
        {
            var firstType = BinaryDataExtensions.GetTypeOrObjectType(list[0]);

            foreach (object element in list)
            {
                if (BinaryDataExtensions.GetTypeOrObjectType(element) != firstType)
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #9
0
        private static void LoadDictionaryWhereAllValuesAreTheSameType(IDictionary data,
                                                                       BinaryReader reader, int count)
        {
            Type everyKeyType =
                BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(reader.ReadString());
            Type everyValueType =
                BinaryDataExtensions.GetTypeFromShortNameOrFullNameIfNotFound(reader.ReadString());

            for (int i = 0; i < count; i++)
            {
                data.Add(CreateAndLoad(everyKeyType, reader), CreateAndLoad(everyValueType, reader));
            }
        }
예제 #10
0
        private static bool AreAllDictionaryValuesTheSameType(IDictionary data)
        {
            Type firstType = null;

            foreach (object element in data.Values)
            {
                if (firstType == null)
                {
                    firstType = BinaryDataExtensions.GetTypeOrObjectType(element);
                }
                else if (BinaryDataExtensions.GetTypeOrObjectType(element) != firstType)
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #11
0
        private static void SaveDictionaryWhenAllValuesAreNotTheSameType(IDictionary data,
                                                                         BinaryWriter writer)
        {
            writer.Write((byte)ArrayElementType.AllTypesAreDifferent);
            Type keyType = null;
            var  pair    = data.GetEnumerator();

            while (pair.MoveNext())
            {
                if (keyType == null)
                {
                    keyType = BinaryDataExtensions.GetTypeOrObjectType(pair.Key);
                    writer.Write(pair.Key.GetShortNameOrFullNameIfNotFound());
                }
                writer.Write(pair.Value.GetShortNameOrFullNameIfNotFound());
                SaveData(pair.Key, keyType, writer);
                SaveData(pair.Value, BinaryDataExtensions.GetTypeOrObjectType(pair.Value), writer);
            }
        }