Exemplo n.º 1
0
        public void Encode(BinaryWriter writer)
        {
            EsfType containedTypeCode = ContainedTypeCode;
            EsfType myRealType        = (EsfType)(containedTypeCode + 0x40);

#if DEBUG
            Console.WriteLine("encoding array with {2} bytes, code {0} containing {1}", myRealType, containedTypeCode, Value.Length);
#endif

            writer.Write((byte)myRealType);
            byte[] encodedArray = new byte[0];
            using (var stream = new MemoryStream()) {
                using (var memWriter = new BinaryWriter(stream)) {
                    CodecNode <T> valueNode = Codec.CreateValueNode(containedTypeCode, false) as CodecNode <T>;
                    valueNode.TypeCode = containedTypeCode;
                    foreach (T item in Value)
                    {
                        valueNode.Value = item;
                        valueNode.WriteValue(memWriter);
                    }
                    encodedArray = stream.ToArray();
#if DEBUG
                    Console.WriteLine("size is {0}", encodedArray.Length);
#endif
                    Codec.WriteOffset(writer, encodedArray.Length);
                    writer.Write(encodedArray);
                }
            }
        }
Exemplo n.º 2
0
        public override EsfNode CreateValueNode(EsfType typeCode, bool optimize = false)
        {
            StringNode stringNode;

            switch (typeCode)
            {
            case EsfType.UTF16:
                stringNode = new StringNode(ReadUtf16String, WriteUtf16Reference);
                break;

            case EsfType.ASCII:
                stringNode = new StringNode(ReadAsciiString, WriteAsciiReference);
                break;

            case EsfType.ASCII_W21:
                stringNode = new StringNode(ReadAsciiString, WriteAsciiReference);
                break;

            case EsfType.ASCII_W25:
                stringNode = new StringNode(ReadAsciiString, WriteAsciiReference);
                break;

            default:
                return(base.CreateValueNode(typeCode));
            }

            stringNode.TypeCode = typeCode;
            return(stringNode);
        }
Exemplo n.º 3
0
        public EsfNode ReadValueNode(BinaryReader reader, EsfType typeCode)
        {
            EsfNode esfNode = CreateValueNode(typeCode);

            (esfNode as ICodecNode).Decode(reader, typeCode);
            return(esfNode);
        }
Exemplo n.º 4
0
        public void Encode(BinaryWriter writer)
        {
            EsfType containedTypeCode = ContainedTypeCode;
            EsfType esfType           = containedTypeCode + 64;

            writer.Write((byte)esfType);
            byte[] array = new byte[0];
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (BinaryWriter writer2 = new BinaryWriter(memoryStream))
                {
                    CodecNode <T> codecNode =
                        base.Codec.CreateValueNode(containedTypeCode, optimize: false) as CodecNode <T>;
                    codecNode.TypeCode = containedTypeCode;
                    T[] value = Value;
                    for (int i = 0; i < value.Length; i++)
                    {
                        T val2 = codecNode.Value = value[i];
                        codecNode.WriteValue(writer2);
                    }

                    array = memoryStream.ToArray();
                    base.Codec.WriteOffset(writer, array.Length);
                    writer.Write(array);
                }
            }
        }
Exemplo n.º 5
0
        public override EsfNode CreateValueNode(EsfType typeCode, bool optimize = false)
        {
            StringNode result;

            switch (typeCode)
            {
            case EsfType.UTF16:
                result = new StringNode(ReadUtf16String, WriteUtf16Reference);
                break;

            case EsfType.ASCII:
                result = new StringNode(ReadAsciiString, WriteAsciiReference);
                break;

            // HACK: RoninX
            case EsfType.ASCII_W21:
                result = new StringNode(ReadAsciiString, WriteAsciiReference);
                break;

            case EsfType.ASCII_W25:
                result = new StringNode(ReadAsciiString, WriteAsciiReference);
                break;

            default:
                return(base.CreateValueNode(typeCode));
            }
            result.TypeCode = typeCode;
            return(result);
        }
        protected override int ReadValue(BinaryReader reader, EsfType readAs)
        {
            int result;

            switch (readAs)
            {
            case EsfType.INT32_ZERO:
                result = 0;
                break;

            case EsfType.INT32_BYTE:
                result = reader.ReadSByte();
                break;

            case EsfType.INT32_SHORT:
                result = reader.ReadInt16();
                break;

            case EsfType.INT32_24BIT:
                result = ReadInt24(reader);
                break;

            case EsfType.INT32:
                result = reader.ReadInt32();
                break;

            default:
                throw new InvalidOperationException();
            }
            return(result);
        }
Exemplo n.º 7
0
        protected override uint ReadValue(BinaryReader reader, EsfType readAs)
        {
            switch (readAs)
            {
            case EsfType.UINT32_ZERO:
                return(0u);

            case EsfType.UINT32_ONE:
                return(1u);

            case EsfType.UINT32_BYTE:
                return(reader.ReadByte());

            case EsfType.UINT32_SHORT:
                return(reader.ReadUInt16());

            case EsfType.UINT32_24BIT:
                return(ReadUInt24(reader));

            case EsfType.UINT32:
                return(reader.ReadUInt32());

            default:
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 8
0
 public EsfArrayNode(EsfCodec codec, EsfType code) : base(delegate(string s) { throw new InvalidOperationException(); }) {
     Codec       = codec;
     Separator   = " ";
     TypeCode    = code;
     ConvertItem = DefaultFromString;
     Value       = new T[0];
 }
Exemplo n.º 9
0
        private void TestIntNode(int val, EsfType expectedTypeCode = EsfType.INVALID)
        {
            EsfValueNode <int> node = new OptimizedIntNode {
                Value = val
            };

            TestNode(node, expectedTypeCode);
        }
Exemplo n.º 10
0
        private void TestUIntNode(uint val, EsfType typeCode = EsfType.UINT32)
        {
            EsfValueNode <uint> node = new OptimizedUIntNode {
                Value = val, TypeCode = typeCode
            };

            TestNode(node);
        }
Exemplo n.º 11
0
 public virtual void Decode(BinaryReader reader, EsfType unused)
 {
     base.Codec.ReadRecordInfo(reader, base.OriginalTypeCode, out string name, out byte version);
     base.Name    = name;
     base.Version = version;
     base.Size    = base.Codec.ReadSize(reader);
     Value        = base.Codec.ReadToOffset(reader, reader.BaseStream.Position + base.Size);
 }
Exemplo n.º 12
0
        // Adds readers for optimized values
        public override EsfNode CreateValueNode(EsfType typeCode, bool optimize = true)
        {
            EsfNode result;

            switch (typeCode)
            {
            case EsfType.BOOL:
            case EsfType.BOOL_TRUE:
            case EsfType.BOOL_FALSE:
                if (optimize)
                {
                    return(new OptimizedBoolNode());
                }
                else
                {
                    result = new BoolNode();
                }
                break;

            case EsfType.UINT32:
            case EsfType.UINT32_ZERO:
            case EsfType.UINT32_ONE:
            case EsfType.UINT32_BYTE:
            case EsfType.UINT32_SHORT:
            case EsfType.UINT32_24BIT:
                return(new OptimizedUIntNode
                {
                    SingleByteMin = !optimize
                });

            case EsfType.INT32:
            case EsfType.INT32_ZERO:
            case EsfType.INT32_BYTE:
            case EsfType.INT32_SHORT:
            case EsfType.INT32_24BIT:
                return(new OptimizedIntNode
                {
                    SingleByteMin = !optimize
                });

            case EsfType.SINGLE:
            case EsfType.SINGLE_ZERO:
                if (optimize)
                {
                    return(new OptimizedFloatNode());
                }
                else
                {
                    result = new FloatNode();
                }
                break;

            default:
                return(base.CreateValueNode(typeCode));
            }
            result.TypeCode = typeCode;
            return(result);
        }
Exemplo n.º 13
0
        protected override float ReadValue(BinaryReader reader, EsfType readAs)
        {
            if (readAs != EsfType.SINGLE_ZERO)
            {
                return(reader.ReadSingle());
            }

            return(0f);
        }
Exemplo n.º 14
0
 // read
 public EsfNode ReadValueNode(BinaryReader reader, EsfType typeCode)
 {
     try {
         EsfNode result = CreateValueNode(typeCode);
         (result as ICodecNode).Decode(reader, typeCode);
         return(result);
     } catch (Exception e) {
         throw new InvalidDataException(string.Format("{0} at {1:x}", e.Message, reader.BaseStream.Position));
     }
 }
Exemplo n.º 15
0
    public override void Decode(BinaryReader reader, EsfType type)
    {
        base.Codec.ReadRecordInfo(reader, base.OriginalTypeCode, out string name, out byte version);
        base.Name    = name;
        base.Version = version;
        int num  = base.Codec.ReadSize(reader);
        int num2 = (int)(reader.BaseStream.Position - mapStart);

        byteCount = num + num2;
        reader.BaseStream.Seek(num, SeekOrigin.Current);
    }
Exemplo n.º 16
0
        public virtual void Decode(BinaryReader reader, EsfType unused)
        {
            string outName;
            byte   outVersion;

            Codec.ReadRecordInfo(reader, OriginalTypeCode, out outName, out outVersion);
            Name    = outName;
            Version = outVersion;
            Size    = Codec.ReadSize(reader);
            Value   = Codec.ReadToOffset(reader, reader.BaseStream.Position + Size);
        }
Exemplo n.º 17
0
        protected virtual EsfNode ReadArrayNode(BinaryReader reader, EsfType typeCode)
        {
            EsfNode result;

            try {
                result = CreateArrayNode(typeCode);
            } catch (Exception) {
                throw new InvalidDataException(string.Format("{0} at {1:x}", reader.BaseStream.Position));
            }
            (result as ICodecNode).Decode(reader, typeCode);
            return(result);
        }
Exemplo n.º 18
0
        private EsfValueNode <T> TestNode <T>(EsfValueNode <T> node, EsfType expectedTypeCode = EsfType.INVALID)
        {
            byte[]           data    = encodeNode(node);
            EsfNode          decoded = decodeNode(data);
            EsfValueNode <T> node2   = decoded as EsfValueNode <T>;

            assertEqual(node, node2);
            if (expectedTypeCode != EsfType.INVALID)
            {
                assertEqual(node2.TypeCode, expectedTypeCode);
            }
            encodeNode(node2);
            return(node2);
        }
        public override void Decode(BinaryReader reader, EsfType type)
        {
            // we need to keep track of the original data to give it to the actual parser later
            string name;
            byte   remember;

            Codec.ReadRecordInfo(reader, OriginalTypeCode, out name, out remember);
            Name    = name;
            Version = remember;
            int size     = Codec.ReadSize(reader);
            int infoSize = (int)(reader.BaseStream.Position - mapStart);

            byteCount = size + infoSize;
            reader.BaseStream.Seek(size, SeekOrigin.Current);
        }
Exemplo n.º 20
0
        public void Decode(BinaryReader reader, EsfType type)
        {
            EsfType  containedTypeCode = ContainedTypeCode;
            int      num  = base.Codec.ReadSize(reader);
            List <T> list = new List <T>();

            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(reader.ReadBytes(num))))
            {
                while (binaryReader.BaseStream.Position < num)
                {
                    list.Add(ReadFromCodec(binaryReader, containedTypeCode));
                }
            }

            Value = list.ToArray();
        }
Exemplo n.º 21
0
        protected override EsfNode CreateArrayNode(EsfType typeCode)
        {
            EsfNode result;

            // support array types for new primitives
            // this sets the type code of the base type to later have an easier time
            switch (typeCode)
            {
            case EsfType.BOOL_TRUE_ARRAY:
            case EsfType.BOOL_FALSE_ARRAY:
            case EsfType.UINT_ZERO_ARRAY:
            case EsfType.UINT_ONE_ARRAY:
            case EsfType.INT32_ZERO_ARRAY:
            case EsfType.SINGLE_ZERO_ARRAY:
                // trying to read this should result in an infinite loop
                throw new InvalidDataException(string.Format("Array {0:x} of zero-byte entries makes no sense", typeCode));

            case EsfType.UINT32_BYTE_ARRAY:
            case EsfType.UINT32_SHORT_ARRAY:
            case EsfType.UINT32_24BIT_ARRAY:
                result = new OptimizedArrayNode <uint>(this, typeCode, delegate(uint val) {
                    return(new OptimizedUIntNode
                    {
                        Value = val,
                        SingleByteMin = true
                    });
                });
                break;

            case EsfType.INT32_BYTE_ARRAY:
            case EsfType.INT32_SHORT_ARRAY:
                result = new OptimizedArrayNode <int>(this, typeCode, delegate(int val) {
                    return(new OptimizedIntNode
                    {
                        Value = val,
                        SingleByteMin = true
                    });
                });
                break;

            default:
                result = base.CreateArrayNode(typeCode);
                return(result);
            }
            result.TypeCode = (EsfType)typeCode;
            return(result);
        }
Exemplo n.º 22
0
        protected override bool ReadValue(BinaryReader reader, EsfType readAs)
        {
            switch (readAs)
            {
            case EsfType.BOOL:
                return(reader.ReadBoolean());

            case EsfType.BOOL_TRUE:
                return(true);

            case EsfType.BOOL_FALSE:
                return(false);

            default:
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 23
0
        public virtual EsfNode Decode(BinaryReader reader, byte code)
        {
            // create node appropriate to type code
            EsfNode result;

            try {
                long    position = reader.BaseStream.Position;
                EsfType typeCode = (EsfType)code;
                // writeDebug = reader.BaseStream.Position > 0xd80000;
                if (code < (byte)EsfType.BOOL_ARRAY)
                {
                    result = ReadValueNode(reader, typeCode);
                    // if (Log != null) { Log(result.ToXml()); };
                }
                else if (typeCode < EsfType.RECORD)
                {
                    result = ReadArrayNode(reader, typeCode);
                }
                else if (typeCode == EsfType.RECORD)
                {
                    result = ReadRecordNode(reader, code);
                }
                else if (typeCode == EsfType.RECORD_BLOCK)
                {
                    result = ReadRecordArrayNode(reader, code);
                }
                else
                {
                    throw new InvalidDataException(string.Format("Type code {0:x} at {1:x} invalid", typeCode, reader.BaseStream.Position - 1));
                }
                // if (Log != null) { Log(string.Format("Read node {0} / {1}", result, result.TypeCode));
                if (NodeReadFinished != null)
                {
                    NodeReadFinished(result, position);
                }
            } catch (Exception e) {
                Console.WriteLine(string.Format("Exception at {0:x}: {1}", reader.BaseStream.Position, e));
                throw e;
            }
            return(result);
        }
Exemplo n.º 24
0
        public void Decode(BinaryReader reader, EsfType type)
        {
            EsfType containedTypeCode = ContainedTypeCode;

#if DEBUG
            // Console.WriteLine("decoding array type code {0} containing {1}", type, containedTypeCode);
#endif

            int size = Codec.ReadSize(reader);
#if DEBUG
            // Console.WriteLine("Reading array[{0}] with {1} elements", type, size);
#endif
            List <T> read = new List <T>();
            using (var itemReader = new BinaryReader(new MemoryStream(reader.ReadBytes(size)))) {
                while (itemReader.BaseStream.Position < size)
                {
                    read.Add(ReadFromCodec(itemReader, containedTypeCode));
                }
            }
            Value = read.ToArray();
        }
Exemplo n.º 25
0
        protected override EsfNode CreateArrayNode(EsfType typeCode)
        {
            EsfNode esfNode;

            switch (typeCode)
            {
            case EsfType.BOOL_TRUE_ARRAY:
            case EsfType.BOOL_FALSE_ARRAY:
            case EsfType.UINT_ZERO_ARRAY:
            case EsfType.UINT_ONE_ARRAY:
            case EsfType.INT32_ZERO_ARRAY:
            case EsfType.SINGLE_ZERO_ARRAY:
                throw new InvalidDataException($"Array {typeCode:x} of zero-byte entries makes no sense");

            case EsfType.UINT32_BYTE_ARRAY:
            case EsfType.UINT32_SHORT_ARRAY:
            case EsfType.UINT32_24BIT_ARRAY:
                esfNode = new OptimizedArrayNode <uint>(this, typeCode, (uint val) => new OptimizedUIntNode
                {
                    Value         = val,
                    SingleByteMin = true
                });
                break;

            case EsfType.INT32_BYTE_ARRAY:
            case EsfType.INT32_SHORT_ARRAY:
                esfNode = new OptimizedArrayNode <int>(this, typeCode, (int val) => new OptimizedIntNode
                {
                    Value         = val,
                    SingleByteMin = true
                });
                break;

            default:
                return(base.CreateArrayNode(typeCode));
            }

            esfNode.TypeCode = typeCode;
            return(esfNode);
        }
Exemplo n.º 26
0
        public void Decode(BinaryReader reader, EsfType unused)
        {
            base.Codec.ReadRecordInfo(reader, base.OriginalTypeCode, out string name, out byte version);
            base.Name    = name;
            base.Version = version;
            base.Size    = base.Codec.ReadSize(reader);
            int            num  = base.Codec.ReadCount(reader);
            List <EsfNode> list = new List <EsfNode>(num);

            for (int i = 0; i < num; i++)
            {
                RecordEntryNode recordEntryNode = new RecordEntryNode(base.Codec)
                {
                    Name     = $"{base.Name} - {i}",
                    TypeCode = EsfType.RECORD_BLOCK_ENTRY
                };
                recordEntryNode.Decode(reader, EsfType.RECORD_BLOCK_ENTRY);
                list.Add(recordEntryNode);
            }

            Value = list;
        }
        protected override bool ReadValue(BinaryReader reader, EsfType readAs)
        {
            bool result;

            switch (readAs)
            {
            case EsfType.BOOL:
                result = reader.ReadBoolean();
                break;

            case EsfType.BOOL_TRUE:
                result = true;
                break;

            case EsfType.BOOL_FALSE:
                result = false;
                break;

            default:
                throw new InvalidOperationException();
            }
            return(result);
        }
Exemplo n.º 28
0
        public void Decode(BinaryReader reader, EsfType unused)
        {
            string name;
            byte   version;

            Codec.ReadRecordInfo(reader, OriginalTypeCode, out name, out version);
            Name    = name;
            Version = version;
            Size    = (int)Codec.ReadSize(reader);
            int            itemCount      = Codec.ReadCount(reader);
            List <EsfNode> containedNodes = new List <EsfNode>(itemCount);

            for (int i = 0; i < itemCount; i++)
            {
                RecordEntryNode contained = new RecordEntryNode(Codec)
                {
                    Name     = string.Format("{0} - {1}", Name, i),
                    TypeCode = EsfType.RECORD_BLOCK_ENTRY
                };
                contained.Decode(reader, EsfType.RECORD_BLOCK_ENTRY);
                containedNodes.Add(contained);
            }
            Value = containedNodes;
        }
Exemplo n.º 29
0
        // Adds readers for optimized values
        public override EsfNode CreateValueNode(EsfType typeCode, bool optimize = true)
        {
            EsfNode result;

            switch (typeCode)
            {
            case EsfType.BOOL:
            case EsfType.BOOL_TRUE:
            case EsfType.BOOL_FALSE:
                if (optimize)
                {
                    return(new OptimizedBoolNode());
                }
                else
                {
                    result = new BoolNode();
                }
                break;

            case EsfType.UINT32:
            case EsfType.UINT32_ZERO:
            case EsfType.UINT32_ONE:
            case EsfType.UINT32_BYTE:
            case EsfType.UINT32_SHORT:
            case EsfType.UINT32_24BIT:
                return(new OptimizedUIntNode
                {
                    SingleByteMin = !optimize
                });

            case EsfType.INT32:
            case EsfType.INT32_ZERO:
            case EsfType.INT32_BYTE:
            case EsfType.INT32_SHORT:
            case EsfType.INT32_24BIT:
                return(new OptimizedIntNode
                {
                    SingleByteMin = !optimize
                });

            case EsfType.SINGLE:
            case EsfType.SINGLE_ZERO:
                if (optimize)
                {
                    return(new OptimizedFloatNode());
                }
                else
                {
                    result = new FloatNode();
                }
                break;

            //TODO confirm intended data types of types indicated by unknown enum values
            case EsfType.UNKNOWN_23:
                result = new SByteNode();
                break;

            case EsfType.UNKNOWN_24:
                result = new ShortNode();
                break;

            default:
                return(base.CreateValueNode(typeCode));
            }
            result.TypeCode = typeCode;
            return(result);
        }
Exemplo n.º 30
0
 ///<inheritdoc />
 protected override Type26 ReadValue(BinaryReader reader, EsfType readAs)
 {
     return(new Type26(reader));
 }