Beispiel #1
0
        private void SerializeArray(StreamBuffer dout, Array serObject, bool setType)
        {
            if (setType)
            {
                dout.WriteByte(121);
            }
            if (serObject.Length > 32767)
            {
                throw new NotSupportedException("String[] that exceed 32767 (short.MaxValue) entries are not supported. Yours is: " + serObject.Length);
            }
            this.SerializeShort(dout, (short)serObject.Length, false);
            Type   elementType = serObject.GetType().GetElementType();
            GpType codeOfType  = this.GetCodeOfType(elementType);

            if (codeOfType != 0)
            {
                dout.WriteByte((byte)codeOfType);
                if (codeOfType == GpType.Dictionary)
                {
                    bool setKeyType   = default(bool);
                    bool setValueType = default(bool);
                    this.SerializeDictionaryHeader(dout, (object)serObject, out setKeyType, out setValueType);
                    for (int i = 0; i < serObject.Length; i++)
                    {
                        object value = serObject.GetValue(i);
                        this.SerializeDictionaryElements(dout, value, setKeyType, setValueType);
                    }
                }
                else
                {
                    for (int j = 0; j < serObject.Length; j++)
                    {
                        object value2 = serObject.GetValue(j);
                        this.Serialize(dout, value2, false);
                    }
                }
                return;
            }
            CustomType customType = default(CustomType);

            if (Protocol.TypeDict.TryGetValue(elementType, out customType))
            {
                dout.WriteByte(99);
                dout.WriteByte(customType.Code);
                int   num = 0;
                short num2;
                long  num3;
                while (true)
                {
                    if (num < serObject.Length)
                    {
                        object value3 = serObject.GetValue(num);
                        if (customType.SerializeStreamFunction == null)
                        {
                            byte[] array = customType.SerializeFunction(value3);
                            this.SerializeShort(dout, (short)array.Length, false);
                            dout.Write(array, 0, array.Length);
                        }
                        else
                        {
                            int position = dout.Position;
                            dout.Position += 2;
                            num2           = customType.SerializeStreamFunction(dout, value3);
                            num3           = dout.Position;
                            dout.Position  = position;
                            this.SerializeShort(dout, num2, false);
                            dout.Position += num2;
                            if (dout.Position != num3)
                            {
                                break;
                            }
                        }
                        num++;
                        continue;
                    }
                    return;
                }
                throw new Exception("Serialization failed. Stream position corrupted. Should be " + num3 + " is now: " + dout.Position + " serializedLength: " + num2);
            }
            throw new NotSupportedException("cannot serialize array of type " + elementType);
        }
Beispiel #2
0
        private Array DeserializeArray(StreamBuffer din)
        {
            short num   = this.DeserializeShort(din);
            byte  b     = din.ReadByte();
            Array array = null;

            switch (b)
            {
            case 121:
            {
                Array array3 = this.DeserializeArray(din);
                Type  type   = array3.GetType();
                array = Array.CreateInstance(type, num);
                array.SetValue(array3, 0);
                for (short num4 = 1; num4 < num; num4 = (short)(num4 + 1))
                {
                    array3 = this.DeserializeArray(din);
                    array.SetValue(array3, num4);
                }
                goto IL_0226;
            }

            case 120:
                array = Array.CreateInstance(typeof(byte[]), num);
                for (short num5 = 0; num5 < num; num5 = (short)(num5 + 1))
                {
                    Array value = this.DeserializeByteArray(din, -1);
                    array.SetValue(value, num5);
                }
                goto IL_0226;

            case 98:
                array = this.DeserializeByteArray(din, num);
                goto IL_0226;

            case 105:
                array = this.DeserializeIntArray(din, num);
                goto IL_0226;

            case 99:
            {
                byte       b2         = din.ReadByte();
                CustomType customType = default(CustomType);
                if (Protocol.CodeDict.TryGetValue(b2, out customType))
                {
                    array = Array.CreateInstance(customType.Type, num);
                    for (int i = 0; i < num; i++)
                    {
                        short num3 = this.DeserializeShort(din);
                        if (customType.DeserializeStreamFunction == null)
                        {
                            byte[] array2 = new byte[num3];
                            din.Read(array2, 0, num3);
                            array.SetValue(customType.DeserializeFunction(array2), i);
                        }
                        else
                        {
                            array.SetValue(customType.DeserializeStreamFunction(din, num3), i);
                        }
                    }
                    goto IL_0226;
                }
                throw new Exception("Cannot find deserializer for custom type: " + b2);
            }

            case 68:
            {
                Array result = null;
                this.DeserializeDictionaryArray(din, num, out result);
                return(result);
            }

            default:
            {
                array = this.CreateArrayByType(b, num);
                for (short num2 = 0; num2 < num; num2 = (short)(num2 + 1))
                {
                    array.SetValue(this.Deserialize(din, b), num2);
                }
                goto IL_0226;
            }
IL_0226:
                return(array);
            }
        }