public static object Deserialize(byte[] data, Type t) { if (t == null) { return(null); } if (((data != null) ? data.Length : 0) != 0) { if (t.IsEnum) { return(Enum.ToObject(t, BloxSerializer.ToInt(data))); } if (t.IsArray) { return(BloxSerializer.DeserializeArray(data, t)); } if (t.IsGenericType) { if (t.GetGenericTypeDefinition() == typeof(List <>)) { return(BloxSerializer.DeserializeList(data, t)); } return(new byte[0]); } if (BloxSerializer.readers.ContainsKey(t)) { return(BloxSerializer.readers[t](data)); } return(null); } return(BloxMemberInfo.GetDefaultValue(t)); }
public static byte[] SerializeArray(object obj) { Array array = obj as Array; if (array == null) { return(new byte[0]); } MemoryStream memoryStream = new MemoryStream(); using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream)) { binaryWriter.Write(array.Length); for (int i = 0; i < array.Length; i++) { byte[] array2 = BloxSerializer.Serialize(array.GetValue(i)); binaryWriter.Write(array2.Length); binaryWriter.Write(array2); } } memoryStream.Flush(); byte[] buffer = memoryStream.GetBuffer(); memoryStream.Close(); return(buffer); }
public static object DeserializeList(byte[] data, Type t) { IList list = (IList)Activator.CreateInstance(t); if (((data != null) ? data.Length : 0) != 0) { t = t.GetGenericArguments()[0]; using (MemoryStream input = new MemoryStream(data, false)) { using (BinaryReader binaryReader = new BinaryReader(input)) { int num = binaryReader.ReadInt32(); for (int i = 0; i < num; i++) { int num2 = binaryReader.ReadInt32(); byte[] array = new byte[num2]; binaryReader.Read(array, 0, num2); object value = BloxSerializer.Deserialize(array, t); list.Add(value); } return(list); } } } return(list); }
public static byte[] Serialize(object obj) { if (obj == null) { return(new byte[0]); } Type type = obj.GetType(); if (type.IsEnum) { return(BloxSerializer.GetBytes((int)obj)); } if (type.IsArray) { return(BloxSerializer.SerializeArray(obj)); } if (type.IsGenericType) { if (type.GetGenericTypeDefinition() == typeof(List <>)) { return(BloxSerializer.SerializeList(obj)); } return(new byte[0]); } if (BloxSerializer.writers.ContainsKey(type)) { return(BloxSerializer.writers[type](obj)); } return(new byte[0]); }
public static object DeserializeArray(byte[] data, Type t) { t = t.GetElementType(); if (((data != null) ? data.Length : 0) != 0) { Array array = null; using (MemoryStream input = new MemoryStream(data, false)) { using (BinaryReader binaryReader = new BinaryReader(input)) { int num = binaryReader.ReadInt32(); array = Array.CreateInstance(t, num); for (int i = 0; i < num; i++) { int num2 = binaryReader.ReadInt32(); byte[] array2 = new byte[num2]; binaryReader.Read(array2, 0, num2); object value = BloxSerializer.Deserialize(array2, t); array.SetValue(value, i); } return(array); } } } return(Array.CreateInstance(t, 0)); }
public static byte[] SerializeList(object obj) { IList list = obj as IList; if (list == null) { return(new byte[0]); } MemoryStream memoryStream = new MemoryStream(); using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream)) { binaryWriter.Write(list.Count); for (int i = 0; i < list.Count; i++) { byte[] array = BloxSerializer.Serialize(list[i]); binaryWriter.Write(array.Length); binaryWriter.Write(array); } } memoryStream.Flush(); byte[] buffer = memoryStream.GetBuffer(); memoryStream.Close(); return(buffer); }
static BloxSerializer() { BloxSerializer.writers = new Dictionary <Type, Func <object, byte[]> >(); BloxSerializer.readers = new Dictionary <Type, Func <byte[], object> >(); BloxSerializer.writers[typeof(bool)] = ((object value) => BloxSerializer.GetBytes((bool)value)); BloxSerializer.writers[typeof(byte)] = ((object value) => BloxSerializer.GetBytes((byte)value)); BloxSerializer.writers[typeof(sbyte)] = ((object value) => BloxSerializer.GetBytes((sbyte)value)); BloxSerializer.writers[typeof(char)] = ((object value) => BloxSerializer.GetBytes((char)value)); BloxSerializer.writers[typeof(int)] = ((object value) => BloxSerializer.GetBytes((int)value)); BloxSerializer.writers[typeof(uint)] = ((object value) => BloxSerializer.GetBytes((uint)value)); BloxSerializer.writers[typeof(short)] = ((object value) => BloxSerializer.GetBytes((short)value)); BloxSerializer.writers[typeof(ushort)] = ((object value) => BloxSerializer.GetBytes((ushort)value)); BloxSerializer.writers[typeof(long)] = ((object value) => BloxSerializer.GetBytes((long)value)); BloxSerializer.writers[typeof(ulong)] = ((object value) => BloxSerializer.GetBytes((ulong)value)); BloxSerializer.writers[typeof(float)] = ((object value) => BloxSerializer.GetBytes((float)value)); BloxSerializer.writers[typeof(double)] = ((object value) => BloxSerializer.GetBytes((double)value)); BloxSerializer.writers[typeof(decimal)] = ((object value) => BloxSerializer.GetBytes((decimal)value)); BloxSerializer.writers[typeof(string)] = ((object value) => BloxSerializer.GetBytes((string)value)); BloxSerializer.writers[typeof(Vector2)] = ((object value) => BloxSerializer.GetBytes((Vector2)value)); BloxSerializer.writers[typeof(Vector3)] = ((object value) => BloxSerializer.GetBytes((Vector3)value)); BloxSerializer.writers[typeof(Vector4)] = ((object value) => BloxSerializer.GetBytes((Vector4)value)); BloxSerializer.writers[typeof(Quaternion)] = ((object value) => BloxSerializer.GetBytes((Quaternion)value)); BloxSerializer.writers[typeof(Rect)] = ((object value) => BloxSerializer.GetBytes((Rect)value)); BloxSerializer.writers[typeof(Color)] = ((object value) => BloxSerializer.GetBytes((Color)value)); BloxSerializer.writers[typeof(Color32)] = ((object value) => BloxSerializer.GetBytes((Color32)value)); BloxSerializer.readers[typeof(bool)] = ((byte[] data) => BloxSerializer.ToBool(data)); BloxSerializer.readers[typeof(byte)] = ((byte[] data) => BloxSerializer.ToByte(data)); BloxSerializer.readers[typeof(sbyte)] = ((byte[] data) => BloxSerializer.ToSByte(data)); BloxSerializer.readers[typeof(char)] = ((byte[] data) => BloxSerializer.ToChar(data)); BloxSerializer.readers[typeof(int)] = ((byte[] data) => BloxSerializer.ToInt(data)); BloxSerializer.readers[typeof(uint)] = ((byte[] data) => BloxSerializer.ToUInt(data)); BloxSerializer.readers[typeof(short)] = ((byte[] data) => BloxSerializer.ToShort(data)); BloxSerializer.readers[typeof(ushort)] = ((byte[] data) => BloxSerializer.ToUShort(data)); BloxSerializer.readers[typeof(long)] = ((byte[] data) => BloxSerializer.ToLong(data)); BloxSerializer.readers[typeof(ulong)] = ((byte[] data) => BloxSerializer.ToULong(data)); BloxSerializer.readers[typeof(float)] = ((byte[] data) => BloxSerializer.ToFloat(data)); BloxSerializer.readers[typeof(double)] = ((byte[] data) => BloxSerializer.ToDouble(data)); BloxSerializer.readers[typeof(decimal)] = ((byte[] data) => BloxSerializer.ToDecimal(data)); BloxSerializer.readers[typeof(string)] = ((byte[] data) => BloxSerializer.ToString(data)); BloxSerializer.readers[typeof(Vector2)] = ((byte[] data) => BloxSerializer.ToVector2(data)); BloxSerializer.readers[typeof(Vector3)] = ((byte[] data) => BloxSerializer.ToVector3(data)); BloxSerializer.readers[typeof(Vector4)] = ((byte[] data) => BloxSerializer.ToVector4(data)); BloxSerializer.readers[typeof(Quaternion)] = ((byte[] data) => BloxSerializer.ToQuaternion(data)); BloxSerializer.readers[typeof(Rect)] = ((byte[] data) => BloxSerializer.ToRect(data)); BloxSerializer.readers[typeof(Color)] = ((byte[] data) => BloxSerializer.ToColor(data)); BloxSerializer.readers[typeof(Color32)] = ((byte[] data) => BloxSerializer.ToColor32(data)); }
public BloxBlockData(BloxBlock b) { if (b != null) { Type type = b.GetType(); this.blockSysType = type.AssemblyQualifiedName; this.blockType = b.blockType; this.ident = b.ident; this.active = b.active; this._ed_viewOffs = b._ed_viewOffs; if (b.returnType != null) { this.returnType = b.returnType.AssemblyQualifiedName; this.returnValue = BloxSerializer.Serialize(b.returnValue); } if (b.mi != null && b.mi.IsValid) { this.memberReflectedType = b.mi.ReflectedType.AssemblyQualifiedName; this.memberType = b.mi.MemberType; this.memberName = b.mi.Name; ParameterInfo[] parameters = b.mi.GetParameters(); if (((parameters != null) ? parameters.Length : 0) != 0) { this.paramTypes = new string[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { this.paramTypes[i] = parameters[i].ParameterType.AssemblyQualifiedName; } } } if (type != typeof(BloxBlock)) { FieldInfo[] array = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); this.fields = new BlockField[array.Length]; for (int j = 0; j < array.Length; j++) { object value = array[j].GetValue(b); if (value != null) { this.fields[j].fieldName = array[j].Name; this.fields[j].typeName = array[j].FieldType.AssemblyQualifiedName; this.fields[j].data = BloxSerializer.Serialize(value); } } } } }
public BloxBlock CreateBlock() { if (string.IsNullOrEmpty(this.blockSysType)) { return(null); } Type type = Type.GetType(this.blockSysType); if (type == null) { Debug.LogError("Could not find Block type: " + this.blockSysType); return(null); } BloxBlock bloxBlock = (BloxBlock)Activator.CreateInstance(type); bloxBlock.blockType = this.blockType; bloxBlock.ident = this.ident; bloxBlock.active = this.active; bloxBlock._ed_viewOffs = this._ed_viewOffs; bloxBlock.returnType = (string.IsNullOrEmpty(this.returnType) ? null : Type.GetType(this.returnType)); if (bloxBlock.returnType != null) { bloxBlock.returnValue = BloxSerializer.Deserialize(this.returnValue, bloxBlock.returnType); } if (!string.IsNullOrEmpty(this.memberName)) { Type type2 = string.IsNullOrEmpty(this.memberReflectedType) ? null : Type.GetType(this.memberReflectedType); if (type2 != null) { if (this.memberType == MemberTypes.Field) { bloxBlock.mi = new BloxMemberInfo(type2.GetField(this.memberName, BloxBlockData.BindFlags), type2); } else if (this.memberType == MemberTypes.Property) { bloxBlock.mi = new BloxMemberInfo(type2.GetProperty(this.memberName, BloxBlockData.BindFlags), type2); } else if (this.memberType == MemberTypes.Constructor) { if (((this.paramTypes != null) ? this.paramTypes.Length : 0) != 0) { Type[] array = null; array = new Type[this.paramTypes.Length]; for (int i = 0; i < this.paramTypes.Length; i++) { array[i] = Type.GetType(this.paramTypes[i]); } bloxBlock.mi = new BloxMemberInfo(type2.GetConstructor(BloxBlockData.CtorBindFlags, null, array, null), type2); } else { bloxBlock.mi = new BloxMemberInfo(type2.GetConstructor(Type.EmptyTypes), type2); } } else if (this.memberType == MemberTypes.Method) { if (((this.paramTypes != null) ? this.paramTypes.Length : 0) != 0) { Type[] array2 = null; array2 = new Type[this.paramTypes.Length]; for (int j = 0; j < this.paramTypes.Length; j++) { array2[j] = Type.GetType(this.paramTypes[j]); } bloxBlock.mi = new BloxMemberInfo(type2.GetMethod(this.memberName, BloxBlockData.BindFlags, null, array2, null), type2); } else { bloxBlock.mi = new BloxMemberInfo(type2.GetMethod(this.memberName, BloxBlockData.BindFlags, null, new Type[0], null), type2); } } } } if (((type != typeof(BloxBlock)) ? this.fields.Length : 0) != 0) { FieldInfo[] array3 = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); for (int k = 0; k < this.fields.Length; k++) { if (((this.fields[k].data != null) ? this.fields[k].data.Length : 0) != 0) { FieldInfo fieldInfo = null; if (k < array3.Length && array3[k].Name == this.fields[k].fieldName) { fieldInfo = array3[k]; } else { int num = 0; while (num < array3.Length) { if (!(array3[num].Name == this.fields[k].fieldName)) { num++; continue; } fieldInfo = array3[num]; break; } } if (fieldInfo != null) { Type type3 = Type.GetType(this.fields[k].typeName); if (type3 != null && type3 == fieldInfo.FieldType) { object obj = BloxSerializer.Deserialize(this.fields[k].data, type3); if (obj != null) { fieldInfo.SetValue(bloxBlock, obj); } } } } } } return(bloxBlock); }