Ejemplo n.º 1
0
        public void Parse(GomBinaryReader reader)
        {
            switch (this.ValueType)
            {
            case TypedValueType.Null:
                this.Value = null;
                break;

            case TypedValueType.Int8:
            case TypedValueType.Int16:
            case TypedValueType.Int24:
            case TypedValueType.Int32:
            case TypedValueType.Int40:
            case TypedValueType.Int48:
            case TypedValueType.Int56:
            case TypedValueType.Int64:
            {
                int numBytesToRead = ((byte)this.ValueType) - 0xB0 + 1;
                this.Value = reader.ReadVariableWidthUInt64(numBytesToRead);
            }
            break;

            case TypedValueType.UnicodeString:
            {
                int strLen = (int)reader.ReadByte();
                if ((strLen >= 0xB0) && (strLen <= 0xB7))
                {
                    strLen = (int)reader.ReadVariableWidthUInt64(strLen - 0xB0 + 1);
                }

                byte[] charData = reader.ReadBytes((int)strLen * 2);
                this.Value = Encoding.Unicode.GetString(charData);
            }
            break;

            case TypedValueType.CString:
            {
                int strLen = (int)reader.ReadByte();
                if ((strLen >= 0xB0) && (strLen <= 0xB7))
                {
                    strLen = (int)reader.ReadVariableWidthUInt64(strLen - 0xB0 + 1);
                }

                byte[] charData = reader.ReadBytes(strLen);
                this.Value = Encoding.UTF8.GetString(charData);
            }
            break;

            case TypedValueType.Array:
            case TypedValueType.Array2:
            {
                int arrayLen = (int)reader.ReadByte();
                if ((arrayLen >= 0xB0) && (arrayLen <= 0xB7))
                {
                    arrayLen = (int)reader.ReadVariableWidthUInt64(arrayLen - 0xB0 + 1);
                }

                List <TypedValue> arrayValues = new List <TypedValue>(arrayLen);
                for (var i = 0; i < arrayLen; i++)
                {
                    var val = reader.ReadTypedValue();
                    arrayValues.Add(val);
                }
                this.Value = arrayValues;

                // Array type 2 has a trailing byte for some reason
                if (this.ValueType == TypedValueType.Array2)
                {
                    reader.ReadByte();
                }
            }
            break;

            case TypedValueType.BizarroFieldType:
            case TypedValueType.FieldType:
            {
                this.Value = reader.ReadGomType();
            }
            break;

            default:
                throw new InvalidOperationException(String.Format("Unexpected TypedValue Type: {0:X}", this.ValueType));
            }
        }