public static EventData DeserializeEventData(Protocol16Stream input)
        {
            byte code = DeserializeByte(input);
            Dictionary <byte, object> parameters = DeserializeParameterTable(input);

            return(new EventData(code, parameters));
        }
        public static OperationRequest DeserializeOperationRequest(Protocol16Stream input)
        {
            byte operationCode = DeserializeByte(input);
            Dictionary <byte, object> parameters = DeserializeParameterTable(input);

            return(new OperationRequest(operationCode, parameters));
        }
示例#3
0
        private static void SerializeDouble(Protocol16Stream output, double value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Double, writeTypeCode);
            var doubleBuffer = _doubleBuffer.Value;

            doubleBuffer[0] = value;
            var buffer = _byteBuffer.Value;

            Buffer.BlockCopy(doubleBuffer, 0, buffer, 0, sizeof(double));
            if (BitConverter.IsLittleEndian)
            {
                byte b0 = buffer[0];
                byte b1 = buffer[1];
                byte b2 = buffer[2];
                byte b3 = buffer[3];
                buffer[0] = buffer[7];
                buffer[1] = buffer[6];
                buffer[2] = buffer[5];
                buffer[3] = buffer[4];
                buffer[4] = b3;
                buffer[5] = b2;
                buffer[6] = b1;
                buffer[7] = b0;
            }
            output.Write(buffer, 0, sizeof(double));
        }
示例#4
0
        private static void SerializeDictionary(Protocol16Stream output, IDictionary dictionary, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Dictionary, writeTypeCode);

            SerializeDictionaryHeader(output, dictionary.GetType(), out var writeKeyCode, out var writeValueCode);
            SerializeDictionaryElements(output, dictionary, writeKeyCode, writeValueCode);
        }
        public static short DeserializeShort(Protocol16Stream input)
        {
            var buffer = _byteBuffer.Value;

            input.Read(buffer, 0, sizeof(short));

            return((short)(buffer[0] << 8 | buffer[1]));
        }
        private static int DeserializeInteger(Protocol16Stream input)
        {
            var buffer = _byteBuffer.Value;

            input.Read(buffer, 0, sizeof(int));

            return(buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]);
        }
        private static object DeserializeHashtable(Protocol16Stream input)
        {
            int       size   = DeserializeShort(input);
            Hashtable output = new Hashtable(size);

            DeserializeDictionaryElements(input, output, size, (byte)Protocol16Type.Unknown, (byte)Protocol16Type.Unknown);
            return(output);
        }
        public static OperationResponse DeserializeOperationResponse(Protocol16Stream input)
        {
            byte   operationCode = DeserializeByte(input);
            short  returnCode    = DeserializeShort(input);
            string debugMessage  = (Deserialize(input, DeserializeByte(input)) as string);
            Dictionary <byte, object> parameters = DeserializeParameterTable(input);

            return(new OperationResponse(operationCode, returnCode, debugMessage, parameters));
        }
示例#9
0
        private static void SerializeShort(Protocol16Stream output, short value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Short, writeTypeCode);
            var buffer = _byteBuffer.Value;

            buffer[0] = (byte)(value >> 8);
            buffer[1] = (byte)(value);
            output.Write(buffer, 0, sizeof(short));
        }
 private static void DeserializeDictionaryElements(Protocol16Stream input, IDictionary output, int dictionarySize, byte keyTypeCode, byte valueTypeCode)
 {
     for (int i = 0; i < dictionarySize; i++)
     {
         object key   = Deserialize(input, (keyTypeCode == 0 || keyTypeCode == 42) ? ((byte)input.ReadByte()) : keyTypeCode);
         object value = Deserialize(input, (valueTypeCode == 0 || valueTypeCode == 42) ? ((byte)input.ReadByte()) : valueTypeCode);
         output.Add(key, value);
     }
 }
        private static byte[] DeserializeByteArray(Protocol16Stream input)
        {
            int arraySize = DeserializeInteger(input);

            var buffer = new byte[arraySize];

            input.Read(buffer, 0, arraySize);

            return(buffer);
        }
示例#12
0
        private static void SerializeInteger(Protocol16Stream output, int value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Integer, writeTypeCode);
            var buffer = _byteBuffer.Value;

            buffer[0] = (byte)(value >> 24);
            buffer[1] = (byte)(value >> 16);
            buffer[2] = (byte)(value >> 8);
            buffer[3] = (byte)(value);
            output.Write(buffer, 0, sizeof(int));
        }
示例#13
0
 private static void SerializeString(Protocol16Stream output, string value, bool writeTypeCode)
 {
     output.WriteTypeCodeIfTrue(Protocol16Type.String, writeTypeCode);
     byte[] bytes = Encoding.UTF8.GetBytes(value);
     if (bytes.Length > short.MaxValue)
     {
         throw new NotSupportedException($"Strings that exceed a UTF8-encoded byte-length of {short.MaxValue} (short.MaxValue) are not supported. Yours is: {bytes.Length}");
     }
     SerializeShort(output, (short)bytes.Length, false);
     output.Write(bytes, 0, bytes.Length);
 }
        private static Array DeserializeArray(Protocol16Stream input)
        {
            short size     = DeserializeShort(input);
            byte  typeCode = (byte)input.ReadByte();

            switch ((Protocol16Type)typeCode)
            {
            case Protocol16Type.Array:
            {
                Array array     = DeserializeArray(input);
                Type  arrayType = array.GetType();
                Array result    = Array.CreateInstance(arrayType, size);
                result.SetValue(array, 0);
                for (short i = 1; i < size; i++)
                {
                    array = DeserializeArray(input);
                    result.SetValue(array, i);
                }

                return(result);
            }

            case Protocol16Type.ByteArray:
            {
                byte[][] array = new byte[size][];
                for (short i = 0; i < size; i++)
                {
                    array[i] = DeserializeByteArray(input);
                }

                return(array);
            }

            case Protocol16Type.Dictionary:
            {
                DeserializeDictionaryArray(input, size, out Array result);

                return(result);
            }

            default:
            {
                Type  arrayType = GetTypeOfCode(typeCode);
                Array result    = Array.CreateInstance(arrayType, size);

                for (short i = 0; i < size; i++)
                {
                    result.SetValue(Deserialize(input, typeCode), i);
                }

                return(result);
            }
            }
        }
示例#15
0
 private static void SerializeStringArray(Protocol16Stream output, string[] strings, bool writeTypeCode)
 {
     if (strings.Length > short.MaxValue)
     {
         throw new NotSupportedException($"string[] can only have a maximum size of {short.MaxValue} (short.MaxValue). Yours is: {strings.Length}");
     }
     output.WriteTypeCodeIfTrue(Protocol16Type.StringArray, writeTypeCode);
     SerializeShort(output, (short)strings.Length, false);
     foreach (var s in strings)
     {
         SerializeString(output, s, false);
     }
 }
        private static int[] DeserializeIntArray(Protocol16Stream input)
        {
            int arraySize = DeserializeInteger(input);

            var array = new int[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                array[i] = DeserializeInteger(input);
            }

            return(array);
        }
        private static long DeserializeLong(Protocol16Stream input)
        {
            var buffer = _byteBuffer.Value;

            input.Read(buffer, 0, sizeof(long));

            if (BitConverter.IsLittleEndian)
            {
                return((long)buffer[0] << 56 | (long)buffer[1] << 48 | (long)buffer[2] << 40 | (long)buffer[3] << 32 | (long)buffer[4] << 24 | (long)buffer[5] << 16 | (long)buffer[6] << 8 | buffer[7]);
            }

            return(BitConverter.ToInt64(buffer, 0));
        }
        private static Type DeserializeDictionaryType(Protocol16Stream input, out byte keyTypeCode, out byte valueTypeCode)
        {
            keyTypeCode   = (byte)input.ReadByte();
            valueTypeCode = (byte)input.ReadByte();
            Type keyType   = GetTypeOfCode(keyTypeCode);
            Type valueType = GetTypeOfCode(valueTypeCode);

            return(typeof(Dictionary <,>).MakeGenericType(new Type[]
            {
                keyType,
                valueType
            }));
        }
示例#19
0
 private static void SerializeObjectArray(Protocol16Stream output, object[] objects, bool writeTypeCode)
 {
     if (objects.Length > short.MaxValue)
     {
         throw new NotSupportedException($"objects[] can only have a maximum size of {short.MaxValue} (short.MaxValue). Yours is: {objects.Length}");
     }
     output.WriteTypeCodeIfTrue(Protocol16Type.ObjectArray, writeTypeCode);
     SerializeShort(output, (short)objects.Length, false);
     foreach (var s in objects)
     {
         Serialize(output, s, true);
     }
 }
        private static string[] DeserializeStringArray(Protocol16Stream input)
        {
            int arraySize = DeserializeShort(input);

            var array = new string[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                array[i] = DeserializeString(input);
            }

            return(array);
        }
        private static object[] DeserializeObjectArray(Protocol16Stream input)
        {
            int arraySize = DeserializeShort(input);

            var array = new object[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                byte typeCode = (byte)input.ReadByte();
                array[i] = Deserialize(input, typeCode);
            }

            return(array);
        }
        private static Dictionary <byte, object> DeserializeParameterTable(Protocol16Stream input)
        {
            int dictionarySize = DeserializeShort(input);
            var dictionary     = new Dictionary <byte, object>(dictionarySize);

            for (int i = 0; i < dictionarySize; i++)
            {
                byte   key           = (byte)input.ReadByte();
                byte   valueTypeCode = (byte)input.ReadByte();
                object value         = Deserialize(input, valueTypeCode);
                dictionary[key] = value;
            }

            return(dictionary);
        }
示例#23
0
        private static void SerializeParameterTable(Protocol16Stream output, Dictionary <byte, object> parameters)
        {
            if (parameters == null || parameters.Count == 0)
            {
                SerializeShort(output, 0, false);
                return;
            }

            SerializeShort(output, (short)parameters.Count, false);
            foreach (KeyValuePair <byte, object> keyValuePair in parameters)
            {
                output.WriteByte(keyValuePair.Key);
                Serialize(output, keyValuePair.Value, true);
            }
        }
        private static string DeserializeString(Protocol16Stream input)
        {
            int stringSize = DeserializeShort(input);

            if (stringSize == 0)
            {
                return(string.Empty);
            }

            var buffer = new byte[stringSize];

            input.Read(buffer, 0, stringSize);

            return(Encoding.UTF8.GetString(buffer, 0, stringSize));
        }
示例#25
0
        private static void SerializeAnyArray(Protocol16Stream output, Array array, bool writeTypeCode, Protocol16Type arrayType)
        {
            if (arrayType == Protocol16Type.ObjectArray)
            {
                SerializeObjectArray(output, (object[])array, writeTypeCode);
                return;
            }

            // Fallback to object array if null is included
            var containsNull = false;

            foreach (var element in array)
            {
                if (element == null)
                {
                    containsNull = true;
                    break;
                }
            }

            if (containsNull)
            {
                SerializeObjectArray(output, (object[])array, writeTypeCode);
                return;
            }

            switch (arrayType)
            {
            case Protocol16Type.StringArray:
                SerializeStringArray(output, (string[])array, writeTypeCode);
                return;

            case Protocol16Type.IntegerArray:
                SerializeIntArray(output, (int[])array, writeTypeCode);
                break;

            case Protocol16Type.ByteArray:
                SerializeByteArray(output, (byte[])array, writeTypeCode);
                break;

            case Protocol16Type.Array:
                SerializeArrayWithSameElements(output, array, writeTypeCode);
                break;

            default:
                throw new Exception("Unknown array type");
            }
        }
示例#26
0
        private static void SerializeIntArray(Protocol16Stream output, int[] ints, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.IntegerArray, writeTypeCode);
            SerializeInteger(output, ints.Length, false);
            var array = new byte[ints.Length * sizeof(int)];
            var idx   = 0;

            foreach (var number in ints)
            {
                array[idx++] = (byte)(number >> 24);
                array[idx++] = (byte)(number >> 16);
                array[idx++] = (byte)(number >> 8);
                array[idx++] = (byte)(number);
            }
            output.Write(array, 0, array.Length);
        }
示例#27
0
 private static void SerializeOperationResponse(Protocol16Stream output, OperationResponse data, bool writeTypeCode)
 {
     output.WriteTypeCodeIfTrue(Protocol16Type.OperationResponse, writeTypeCode);
     output.WriteByte(data.OperationCode);
     SerializeShort(output, data.ReturnCode, false);
     if (string.IsNullOrEmpty(data.DebugMessage))
     {
         output.WriteTypeCodeIfTrue(Protocol16Type.Null, true);
     }
     else
     {
         // WTF ExitGames, why did you set the writeCode to false?!
         SerializeString(output, data.DebugMessage, true);
     }
     SerializeParameterTable(output, data.Parameters);
 }
        private static IDictionary DeserializeDictionary(Protocol16Stream input)
        {
            byte keyTypeCode    = (byte)input.ReadByte();
            byte valueTypeCode  = (byte)input.ReadByte();
            int  dictionarySize = DeserializeShort(input);
            Type keyType        = GetTypeOfCode(keyTypeCode);
            Type valueType      = GetTypeOfCode(valueTypeCode);
            Type dictionaryType = typeof(Dictionary <,>).MakeGenericType(new Type[]
            {
                keyType,
                valueType
            });

            IDictionary output = Activator.CreateInstance(dictionaryType) as IDictionary;

            DeserializeDictionaryElements(input, output, dictionarySize, keyTypeCode, valueTypeCode);
            return(output);
        }
        private static float DeserializeFloat(Protocol16Stream input)
        {
            var buffer = _byteBuffer.Value;

            input.Read(buffer, 0, sizeof(float));

            if (BitConverter.IsLittleEndian)
            {
                byte b0 = buffer[0];
                byte b1 = buffer[1];
                buffer[0] = buffer[3];
                buffer[1] = buffer[2];
                buffer[2] = b1;
                buffer[3] = b0;
            }

            return(BitConverter.ToSingle(buffer, 0));
        }
        private static bool DeserializeDictionaryArray(Protocol16Stream input, short size, out Array result)
        {
            Type type = DeserializeDictionaryType(input, out byte keyTypeCode, out byte valueTypeCode);

            result = Array.CreateInstance(type, size);

            for (short i = 0; i < size; i++)
            {
                if (!(Activator.CreateInstance(type) is IDictionary dictionary))
                {
                    return(false);
                }
                short arraySize = DeserializeShort(input);
                for (int j = 0; j < arraySize; j++)
                {
                    object key;
                    if (keyTypeCode > 0)
                    {
                        key = Deserialize(input, keyTypeCode);
                    }
                    else
                    {
                        byte nextKeyTypeCode = (byte)input.ReadByte();
                        key = Deserialize(input, nextKeyTypeCode);
                    }
                    object value;
                    if (valueTypeCode > 0)
                    {
                        value = Deserialize(input, valueTypeCode);
                    }
                    else
                    {
                        byte nextValueTypeCode = (byte)input.ReadByte();
                        value = Deserialize(input, nextValueTypeCode);
                    }
                    dictionary.Add(key, value);
                }
                result.SetValue(dictionary, i);
            }

            return(true);
        }