public static int GetObjectSize(object obj, Type type) { int size; if (TypeSizes.TryGetValue(type, out size)) { return(size); } else if (type.GetInterfaces().Contains(typeof(IPacketSerializable))) { return(((IPacketSerializable)obj).GetSize()); } else if (type.IsEnum) { EnumPackageType packageType = (EnumPackageType)type.GetCustomAttribute(typeof(EnumPackageType), false); if (packageType == null) { Console.WriteLine("Error: Enum \"" + type.FullName + "\" doesn't have EnumPackageType attribute"); return(0); } return(GetObjectSize(DynamicCast(DynamicCast(obj, type.GetEnumUnderlyingType()), packageType.type), packageType.type)); } else { Console.WriteLine("Error: Unsupported type \"" + type.FullName + "\""); return(0); } }
public static bool Deserialize(Type type, byte[] data, ref int position, out object value) { if (type == typeof(byte)) { value = data[position]; position++; } else if (type.GetInterfaces().Contains(typeof(IPacketSerializable))) { IPacketSerializable serializable = (IPacketSerializable)Activator.CreateInstance(type); serializable.Deserialize(data, ref position); value = serializable; } else if (type.IsEnum) { EnumPackageType packageType = (EnumPackageType)type.GetCustomAttribute(typeof(EnumPackageType), false); if (packageType == null) { Console.WriteLine("Error: Enum \"" + type.FullName + "\" doesn't have EnumPackageType attribute"); value = null; return(false); } object obj; if (!Deserialize(packageType.type, data, ref position, out obj)) { value = null; return(false); } value = DynamicCast(DynamicCast(obj, type.GetEnumUnderlyingType()), type); } else if (type == typeof(bool)) { value = BitConverter.ToBoolean(data, position); position++; } else if (type == typeof(short)) { value = BitConverter.ToInt16(data, position); position += 2; } else if (type == typeof(ushort)) { value = BitConverter.ToUInt16(data, position); position += 2; } else if (type == typeof(int)) { value = BitConverter.ToInt32(data, position); position += 4; } else if (type == typeof(uint)) { value = BitConverter.ToUInt32(data, position); position += 4; } else if (type == typeof(long)) { value = BitConverter.ToInt64(data, position); position += 8; } else if (type == typeof(ulong)) { value = BitConverter.ToUInt64(data, position); position += 8; } else if (type == typeof(float)) { value = BitConverter.ToSingle(data, position); position += 4; } else if (type == typeof(double)) { value = BitConverter.ToDouble(data, position); position += 8; } else { value = null; return(false); } return(true); }
public static bool Serialize(Type type, object value, byte[] data, ref int position) { if (type == typeof(byte)) { data[position] = (byte)value; position++; } else if (type.GetInterfaces().Contains(typeof(IPacketSerializable))) { return(((IPacketSerializable)value).Serialize(data, ref position)); } else if (type.IsEnum) { EnumPackageType packageType = (EnumPackageType)type.GetCustomAttribute(typeof(EnumPackageType), false); if (packageType == null) { Console.WriteLine("Error: Enum \"" + type.FullName + "\" doesn't have EnumPackageType attribute"); return(false); } return(Serialize(packageType.type, DynamicCast(DynamicCast(value, type.GetEnumUnderlyingType()), packageType.type), data, ref position)); } else if (type == typeof(bool)) { byte[] bytes = BitConverter.GetBytes((bool)value); Array.Copy(bytes, 0, data, position, 1); position += 1; } else if (type == typeof(short)) { byte[] bytes = BitConverter.GetBytes((short)value); Array.Copy(bytes, 0, data, position, 2); position += 2; } else if (type == typeof(ushort)) { byte[] bytes = BitConverter.GetBytes((ushort)value); Array.Copy(bytes, 0, data, position, 2); position += 2; } else if (type == typeof(int)) { byte[] bytes = BitConverter.GetBytes((int)value); Array.Copy(bytes, 0, data, position, 4); position += 4; } else if (type == typeof(uint)) { byte[] bytes = BitConverter.GetBytes((uint)value); Array.Copy(bytes, 0, data, position, 4); position += 4; } else if (type == typeof(long)) { byte[] bytes = BitConverter.GetBytes((long)value); Array.Copy(bytes, 0, data, position, 8); position += 8; } else if (type == typeof(ulong)) { byte[] bytes = BitConverter.GetBytes((ulong)value); Array.Copy(bytes, 0, data, position, 8); position += 8; } else if (type == typeof(float)) { byte[] bytes = BitConverter.GetBytes((float)value); Array.Copy(bytes, 0, data, position, 4); position += 4; } else if (type == typeof(double)) { byte[] bytes = BitConverter.GetBytes((double)value); Array.Copy(bytes, 0, data, position, 8); position += 8; } else { return(false); } return(true); }