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); } }
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)); } }
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); }
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); } }
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)); } }
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)); } }
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)); } }
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); }
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)); } }
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); }
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); } }