public BMSByte Serialized() { serializeBuffer.Clear(); foreach (var property in Properties) { object val = property.GetValue(this, null); if (property.PropertyType == typeof(string)) { serializeBuffer.Append(BitConverter.GetBytes(((string)val).Length)); } ObjectMapper.MapBytes(serializeBuffer, val); } return(serializeBuffer); }
/// <summary> /// Gets the bytes for the Instance of an Object and appends them to a <c>BMSByte</c>. /// </summary> /// <param name="o">The Instance of the Object.</param> /// <param name="type">The Type of the Object.</param> /// <param name="bytes"><c>BMSByte</c> to which the bytes should be added.</param> private static void GetBytes(object o, Type type, ref BMSByte bytes) { if (type == typeof(string)) { var strBytes = Encoding.UTF8.GetBytes((string)o); // TODO: Need to make custom string serialization to binary bytes.Append(BitConverter.GetBytes(strBytes.Length)); if (strBytes.Length > 0) { bytes.Append(strBytes); } } else if (type == typeof(sbyte)) { bytes.BlockCopy <sbyte>(o, 1); } else if (type == typeof(byte)) { bytes.BlockCopy <byte>(o, 1); } else if (type == typeof(char)) { bytes.BlockCopy <char>(o, 1); } else if (type == typeof(bool)) { bytes.Append(BitConverter.GetBytes((bool)o)); } else if (type == typeof(short)) { bytes.Append(BitConverter.GetBytes((short)o)); } else if (type == typeof(ushort)) { bytes.Append(BitConverter.GetBytes((ushort)o)); } else if (type == typeof(int)) { bytes.Append(BitConverter.GetBytes((int)o)); } else if (type == typeof(uint)) { bytes.Append(BitConverter.GetBytes((uint)o)); } else if (type == typeof(long)) { bytes.Append(BitConverter.GetBytes((long)o)); } else if (type == typeof(ulong)) { bytes.Append(BitConverter.GetBytes((ulong)o)); } else if (type == typeof(float)) { bytes.Append(BitConverter.GetBytes((float)o)); } else if (type == typeof(double)) { bytes.Append(BitConverter.GetBytes((double)o)); } else if (type == typeof(Vector2)) { bytes.Append(BitConverter.GetBytes(((Vector2)o).x)); bytes.Append(BitConverter.GetBytes(((Vector2)o).y)); } else if (type == typeof(Vector3)) { bytes.Append(BitConverter.GetBytes(((Vector3)o).x)); bytes.Append(BitConverter.GetBytes(((Vector3)o).y)); bytes.Append(BitConverter.GetBytes(((Vector3)o).z)); } else if (type == typeof(Vector4)) { bytes.Append(BitConverter.GetBytes(((Vector4)o).x)); bytes.Append(BitConverter.GetBytes(((Vector4)o).y)); bytes.Append(BitConverter.GetBytes(((Vector4)o).z)); bytes.Append(BitConverter.GetBytes(((Vector4)o).w)); } else if (type == typeof(Color)) { bytes.Append(BitConverter.GetBytes(((Color)o).r)); bytes.Append(BitConverter.GetBytes(((Color)o).g)); bytes.Append(BitConverter.GetBytes(((Color)o).b)); bytes.Append(BitConverter.GetBytes(((Color)o).a)); } else if (type == typeof(Quaternion)) { bytes.Append(BitConverter.GetBytes(((Quaternion)o).x)); bytes.Append(BitConverter.GetBytes(((Quaternion)o).y)); bytes.Append(BitConverter.GetBytes(((Quaternion)o).z)); bytes.Append(BitConverter.GetBytes(((Quaternion)o).w)); } else if (type == typeof(byte[])) { bytes.Append(BitConverter.GetBytes(((byte[])o).Length)); bytes.Append((byte[])o); } else if (type == typeof(BMSByte)) { bytes.Append(BitConverter.GetBytes(((BMSByte)o).Size)); bytes.BlockCopy(((BMSByte)o).byteArr, ((BMSByte)o).StartIndex(), ((BMSByte)o).Size); } else if (type.IsEnum()) { GetBytes(o, Enum.GetUnderlyingType(type), ref bytes); } else { throw new NetworkException(11, "The type " + type.ToString() + " is not allowed to be sent over the Network (yet)"); } }
/// <summary> /// Get a byte[] out of a Networking Stream /// </summary> /// <param name="args">Arguments passed through to get mapped</param> /// <returns>A byte[] of the mapped arguments</returns> public static BMSByte MapBytes(BMSByte bytes, params object[] args) { foreach (object o in args) { #if UNITY_EDITOR if (o == null) { throw new NetworkException("You are trying to serialize a null object, null objects have no dimentions and can not be mapped across the network"); } #endif Type type = o.GetType(); if (type == typeof(string)) { var strBytes = Encoding.UTF8.GetBytes((string)o); // TODO: Need to make custom string serialization to binary bytes.Append(BitConverter.GetBytes(strBytes.Length)); if (strBytes.Length > 0) { bytes.Append(strBytes); } } else if (type == typeof(sbyte)) { bytes.BlockCopy <sbyte>(o, 1); } else if (type == typeof(byte)) { bytes.BlockCopy <byte>(o, 1); } else if (type == typeof(char)) { bytes.BlockCopy <char>(o, 1); } else if (type == typeof(bool)) { bytes.Append(BitConverter.GetBytes((bool)o)); } else if (type == typeof(short)) { bytes.Append(BitConverter.GetBytes((short)o)); } else if (type == typeof(ushort)) { bytes.Append(BitConverter.GetBytes((ushort)o)); } else if (type == typeof(int)) { bytes.Append(BitConverter.GetBytes((int)o)); } else if (type == typeof(uint)) { bytes.Append(BitConverter.GetBytes((uint)o)); } else if (type == typeof(long)) { bytes.Append(BitConverter.GetBytes((long)o)); } else if (type == typeof(ulong)) { bytes.Append(BitConverter.GetBytes((ulong)o)); } else if (type == typeof(float)) { bytes.Append(BitConverter.GetBytes((float)o)); } else if (type == typeof(double)) { bytes.Append(BitConverter.GetBytes((double)o)); } else if (type == typeof(Vector2)) { bytes.Append(BitConverter.GetBytes(((Vector2)o).x)); bytes.Append(BitConverter.GetBytes(((Vector2)o).y)); } else if (type == typeof(Vector3)) { bytes.Append(BitConverter.GetBytes(((Vector3)o).x)); bytes.Append(BitConverter.GetBytes(((Vector3)o).y)); bytes.Append(BitConverter.GetBytes(((Vector3)o).z)); } else if (type == typeof(Vector4)) { bytes.Append(BitConverter.GetBytes(((Vector4)o).x)); bytes.Append(BitConverter.GetBytes(((Vector4)o).y)); bytes.Append(BitConverter.GetBytes(((Vector4)o).z)); bytes.Append(BitConverter.GetBytes(((Vector4)o).w)); } else if (type == typeof(Color)) { bytes.Append(BitConverter.GetBytes(((Color)o).r)); bytes.Append(BitConverter.GetBytes(((Color)o).g)); bytes.Append(BitConverter.GetBytes(((Color)o).b)); bytes.Append(BitConverter.GetBytes(((Color)o).a)); } else if (type == typeof(Quaternion)) { bytes.Append(BitConverter.GetBytes(((Quaternion)o).x)); bytes.Append(BitConverter.GetBytes(((Quaternion)o).y)); bytes.Append(BitConverter.GetBytes(((Quaternion)o).z)); bytes.Append(BitConverter.GetBytes(((Quaternion)o).w)); } else if (type == typeof(byte[])) { bytes.Append(BitConverter.GetBytes(((byte[])o).Length)); bytes.Append((byte[])o); } else if (type == typeof(BMSByte)) { bytes.Append(BitConverter.GetBytes(((BMSByte)o).Size)); bytes.BlockCopy(((BMSByte)o).byteArr, ((BMSByte)o).StartIndex(), ((BMSByte)o).Size); } else { throw new NetworkException(11, "The type " + type.ToString() + " is not allowed to be sent over the network (yet)"); } } return(bytes); }