示例#1
0
        public void TestPrimitives([Values(Endianness.Little, Endianness.Big)] Endianness endianness)
        {
            var  testData  = new byte[] { 0x04, 0x03, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF };
            bool bigEndian = endianness == Endianness.Big;

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream, endianness)) {
                    // Raw bytes
                    reader.Position = 0;

                    var bytes = reader.ReadBytes(8);
                    Assert.AreEqual(8, bytes.Length);
                    for (var i = 0; i < bytes.Length; i++)
                    {
                        if (bigEndian)
                        {
                            Assert.AreEqual(testData[i], bytes[bytes.Length - i - 1]);
                        }
                        else
                        {
                            Assert.AreEqual(testData[i], bytes[i]);
                        }
                    }

                    // Primitives with endianness
                    reader.Position = 0;

                    try {
                        Assert.AreEqual(bigEndian ? 0x04030201FFFFFFFF : 0xFFFFFFFF01020304, reader.ReadInt64());
                    }
                    catch (OverflowException) { }
                    reader.Position -= 8;
                    try {
                        Assert.AreEqual(bigEndian ? 0x04030201FFFFFFFF : 0xFFFFFFFF01020304, reader.ReadUInt64());
                    }
                    catch (OverflowException) { }
                    reader.Position -= 8;
                    Assert.AreEqual(bigEndian ? 0x04030201 : 0x01020304, reader.ReadInt32());
                    reader.Position -= 4;
                    Assert.AreEqual(bigEndian ? 0x04030201 : 0x01020304, reader.ReadUInt32());
                    reader.Position -= 4;
                    Assert.AreEqual(bigEndian ? 0x0403 : 0x0304, reader.ReadInt16());
                    reader.Position -= 2;
                    Assert.AreEqual(bigEndian ? 0x0403 : 0x0304, reader.ReadUInt16());

                    // Signedness
                    reader.Position = 4;

                    Assert.AreEqual(-1, reader.ReadInt32());
                    reader.Position -= 4;
                    Assert.AreEqual(0xFFFFFFFF, reader.ReadUInt32());

                    reader.Position = 6;

                    Assert.AreEqual(-1, reader.ReadInt16());
                    reader.Position -= 2;
                    Assert.AreEqual(0xFFFF, reader.ReadUInt16());
                }
        }
示例#2
0
        public void TestNullTerminatedString()
        {
            var testData = new byte[] { 0x41, 0x42, 0x43, 0x44, 0x00 }; // ABCD\0

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream)) {
                    var str = reader.ReadNullTerminatedString();
                    Assert.AreEqual("ABCD", str);
                }
        }
示例#3
0
        public void Read(BinaryObjectReader reader)
        {
            ArchiveName = reader.ReadString(StringBinaryFormat.FixedLength, 112);
            var waveInfoCount = reader.ReadUInt32();

            FileWaveInfo = new FileWaveInfo[waveInfoCount];
            for (int j = 0; j < waveInfoCount; j++)
            {
                FileWaveInfo[j] = reader.ReadObjectOffset <FileWaveInfo>();
            }
        }
        public object Deserialize(byte[] bytes, Type objectType)
        {
            using (var ms = new MemoryStream(bytes))
            {
                using (var reader = new BinaryReader(ms))
                {
                    var objectReader = new BinaryObjectReader(reader);
                    var context      = NewContext(objectReader);

                    return(context.Read(objectType));
                }
            }
        }
示例#5
0
        public void TestObjectWithStrings()
        {
            var testData = new byte[]
            { 0x41, 0x42, 0x43, 0x44, 0x45, 0x00, 0x00, 0x00, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x00 };

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream)) {
                    var obj = reader.ReadObject <TestObjectWithStrings>();

                    Assert.AreEqual("ABCDE", obj.eightCharString);
                    Assert.AreEqual("FGHI", obj.fourCharString);
                    Assert.AreEqual("JK", obj.nullTerminatedString);
                }
        }
示例#6
0
        public static new ArtTodData FromData(ArtNetData data)
        {
            var stream = new MemoryStream(data.Buffer);
            var reader = new BinaryObjectReader(stream)
            {
                Position = 10
            };

            ArtTodData packet = reader.ReadObject <ArtTodData>();

            packet.PacketData = data;

            return(packet);
        }
示例#7
0
        public void TestArray()
        {
            var testData = new byte[] { 0x34, 0x12, 0x78, 0x56, 0xFF, 0xFF };

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream)) {
                    var arr = reader.ReadArray <short>(3);

                    Assert.AreEqual(3, arr.Length);
                    Assert.AreEqual(0x1234, arr[0]);
                    Assert.AreEqual(0x5678, arr[1]);
                    Assert.AreEqual(-1, arr[2]);
                }
        }
示例#8
0
        public static ArtNetPacket FromData(ArtNetData data)
        {
            var stream = new MemoryStream(data.Buffer);
            var reader = new BinaryObjectReader(stream);

            byte[]  id   = reader.ReadBytes(8);
            OpCodes code = (OpCodes)reader.ReadInt16();

            ArtNetPacket packet = new(code);

            packet.ID         = id;
            packet.PacketData = data;

            return(packet);
        }
示例#9
0
文件: ArtRdm.cs 项目: OsOmE1/Art.Net
        public static new ArtRdm FromData(ArtNetData data)
        {
            var stream = new MemoryStream(data.Buffer);
            var reader = new BinaryObjectReader(stream)
            {
                Position = 10
            };

            ArtRdm packet = reader.ReadObject <ArtRdm>();

            packet.RdmPacket = reader.ReadBytes((int)(stream.Length - reader.Position));

            packet.PacketData = data;

            return(packet);
        }
示例#10
0
        private List <FileWaveGroup> ReadWsChunk(BinaryObjectReader reader)
        {
            var waveGroups = new List <FileWaveGroup>();

            reader.Skip(4);
            var wsysOffset = reader.ReadUInt32();

            reader.Skip(4);

            using (reader.AtOffset(wsysOffset))
            {
                using (reader.WithOffsetOrigin())
                    ReadWSYS(reader, waveGroups);
            };

            return(waveGroups);
        }
示例#11
0
        public void TestMappedObject()
        {
            var testData = new byte[]
            { 0xCC, 0xCC, 0xCC, 0xCC, 0x02, 0x01, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12 };

            using var stream = new MemoryStream(testData);
            using var reader = new BinaryObjectReader(stream);

            reader.ObjectMappings.Add(typeof(TestObject), typeof(TestMappedObject));

            var obj = reader.ReadObject <TestObject>();

            Assert.AreEqual(0x0102, obj.a);
            Assert.AreEqual(0x1234, obj.b);
            Assert.That("XYZ" == obj.c);
            Assert.AreEqual(0x99, obj.d);
        }
示例#12
0
        public static new ArtRdmSub FromData(ArtNetData data)
        {
            var stream = new MemoryStream(data.Buffer);
            var reader = new BinaryObjectReader(stream)
            {
                Position = 10
            };

            ArtRdmSub packet = reader.ReadObject <ArtRdmSub>();

            packet.SubCount  = (short)((packet.SubCount << 8) + (packet.SubCount >> 8));
            packet.SubDevice = (short)((packet.SubDevice << 8) + (packet.SubDevice >> 8));
            packet.Data      = packet.Data.Reverse().ToArray();

            packet.PacketData = data;

            return(packet);
        }
示例#13
0
        public void TestObjectWithVersioning()
        {
            var testData = new byte[]
            { 0x34, 0x12, 0x00, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00 };

            for (int v = 1; v <= 3; v++)
            {
                using (var stream = new MemoryStream(testData))
                    using (var reader = new BinaryObjectReader(stream)) {
                        reader.Version = v;
                        var obj = reader.ReadObject <TestObjectWithVersioning>();

                        Assert.AreEqual(0x1234, obj.allVersionsItem);
                        Assert.AreEqual((v == 1 ? "A" : null), obj.version1Item);
                        Assert.AreEqual((v >= 2 ? "A" : null), obj.version2AndHigherItem);
                        Assert.AreEqual((v < 3 ? "B" : null), obj.version1And2Item);
                    }
            }
        }
示例#14
0
        public void TestMappedPrimitives()
        {
            var testData = new byte[]
            { 0x01, 0x02, 0x01, 0x87, 0x65, 0x43, 0x21 };

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream)) {
                    reader.PrimitiveMappings.Add(typeof(int).Name, typeof(byte));
                    reader.PrimitiveMappings.Add(typeof(bool).Name, typeof(byte));
                    reader.PrimitiveMappings.Add(typeof(long).Name, typeof(int));

                    var obj = reader.ReadObject <TestObjectWithPrimitiveMapping>();

                    Assert.AreEqual(obj.int1, 1);
                    Assert.AreEqual(obj.int2, 2);
                    Assert.AreEqual(obj.bool1, true);
                    Assert.AreEqual(obj.long1, 0x21436587);
                }
        }
示例#15
0
        public override ulong[] GetFunctionTable()
        {
            ulong globalOffsetTable = 0;
            ulong arrayOffset       = 0;
            ulong arraySize         = 0;

            foreach (NSODynamic dynamic in _dynamics)
            {
                if (dynamic.Tag == NSODynamicTag.DT_PLTGOT)
                {
                    globalOffsetTable = dynamic.Value;
                }
                else if (dynamic.Tag == NSODynamicTag.DT_INIT_ARRAY)
                {
                    arrayOffset = dynamic.Value;
                }
                else if (dynamic.Tag == NSODynamicTag.DT_INIT_ARRAYSZ)
                {
                    arraySize = dynamic.Value;
                }
            }
            if (globalOffsetTable == 0)
            {
                throw new InvalidOperationException("Unable to get GLOBAL_OFFSET_TABLE from PT_DYNAMIC");
            }

            GlobalOffset = globalOffsetTable;

            using (var codeStream = new MemoryStream(_fullCode, false))
                using (var codeReader = new BinaryObjectReader(codeStream))
                {
                    if (_arm32)
                    {
                        return(ArrayUtil.ConvertArray <ulong, uint>(codeReader.ReadArray <uint>((uint)arrayOffset,
                                                                                                (int)(arraySize / 4))));
                    }
                    else
                    {
                        return(codeReader.ReadArray <ulong>((uint)arrayOffset, (int)(arraySize / 8)));
                    }
                }
        }
示例#16
0
        public List <FileWaveGroup> Parse(Stream stream)
        {
            var waveGroups = new List <FileWaveGroup>();

            using (var reader = new BinaryObjectReader(stream, StreamOwnership.Retain, Endianness.Big, Encoding.ASCII))
            {
                while (reader.Position + 4 < reader.Length)
                {
                    var value = reader.ReadUInt32();
                    if (value == 0x57535953)
                    {
                        reader.Seek(-4, SeekOrigin.Current);
                        using (reader.WithOffsetOrigin())
                            ReadWSYS(reader, waveGroups);
                    }
                }
            }

            return(waveGroups);
        }
示例#17
0
        public void TestObjectWithArrays()
        {
            var testData = new byte[]
            { 0x03, 0x00, 0x00, 0x00, 0x34, 0x12, 0x78, 0x56, 0xBC, 0x9A, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E };

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream)) {
                    var obj = reader.ReadObject <TestObjectWithArrays>();

                    Assert.AreEqual(3, obj.numberOfItems);
                    Assert.AreEqual(3, obj.itemArray.Length);
                    Assert.AreEqual(0x1234, obj.itemArray[0]);
                    Assert.AreEqual(0x5678, obj.itemArray[1]);
                    Assert.AreEqual(0x9ABC, obj.itemArray[2]);
                    for (int i = 0; i < 5; i++)
                    {
                        Assert.AreEqual(i + 0x0A, obj.fiveItems[i]);
                    }
                }
        }
示例#18
0
        private void ReadWSYS(BinaryObjectReader reader, List <FileWaveGroup> waveGroups)
        {
            var start = reader.Position;

            mLogger.Information($"Reading WSYS at 0x{start:X8}");

            reader.Skip(16);
            reader.ReadOffset(() =>
            {
                reader.Skip(4);
                var waveGroupCount = reader.ReadUInt32();

                for (int i = 0; i < waveGroupCount; i++)
                {
                    using (reader.ReadOffset())
                    {
                        var grp = reader.ReadObject <FileWaveGroup>();
                        waveGroups.Add(grp);
                    }
                }
            });
        }
示例#19
0
        public void TestArrayOfObject([Values(Endianness.Little, Endianness.Big)] Endianness endianness)
        {
            byte[] testData;

            if (endianness == Endianness.Little)
            {
                testData = new byte[] {
                    0x04, 0x03, 0x02, 0x01, 0x34, 0x12, 0x41, 0x42, 0x43, 0x44, 0x00, 0x99,
                    0x05, 0x03, 0x02, 0x01, 0x35, 0x12, 0x41, 0x42, 0x43, 0x45, 0x00, 0x9A
                }
            }
            ;
            else
            {
                testData = new byte[] {
                    0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x41, 0x42, 0x43, 0x44, 0x00, 0x99,
                    0x01, 0x02, 0x03, 0x05, 0x12, 0x35, 0x41, 0x42, 0x43, 0x45, 0x00, 0x9A,
                };
            }

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream, endianness)) {
                    var objArr = reader.ReadArray <TestObject>(2);

                    Assert.AreEqual(2, objArr.Length);

                    Assert.AreEqual(0x01020304, objArr[0].a);
                    Assert.AreEqual(0x1234, objArr[0].b);
                    Assert.That("ABCD" == objArr[0].c);
                    Assert.AreEqual(0x99, objArr[0].d);

                    Assert.AreEqual(0x01020305, objArr[1].a);
                    Assert.AreEqual(0x1235, objArr[1].b);
                    Assert.That("ABCE" == objArr[1].c);
                    Assert.AreEqual(0x9A, objArr[1].d);
                }
        }
示例#20
0
        public void TestObject([Values(Endianness.Little, Endianness.Big)] Endianness endianness)
        {
            byte[] testData;

            if (endianness == Endianness.Little)
            {
                testData = new byte[] { 0x04, 0x03, 0x02, 0x01, 0x34, 0x12, 0x41, 0x42, 0x43, 0x44, 0x00, 0x99 }
            }
            ;
            else
            {
                testData = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x41, 0x42, 0x43, 0x44, 0x00, 0x99 };
            }

            using (var stream = new MemoryStream(testData))
                using (var reader = new BinaryObjectReader(stream, endianness)) {
                    var obj = reader.ReadObject <TestObject>();

                    Assert.AreEqual(0x01020304, obj.a);
                    Assert.AreEqual(0x1234, obj.b);
                    Assert.That("ABCD" == obj.c);
                    Assert.AreEqual(0x99, obj.d);
                }
        }
示例#21
0
        protected override bool Init()
        {
            // Check for NSO0 signature "NSO0"
            if (ReadUInt32() != 0x304F534E)
            {
                return(false);
            }

            // NSO Version is always 0
            _nsoVersion = ReadUInt32();
            if (_nsoVersion != 0x00)
            {
                return(false);
            }

            // Reserved (Unused)
            _reserved1 = ReadUInt32();

            // Flags
            _flags = ReadUInt32();

            // .text SegmentHeader
            _textSegmentHeader                  = new NSOSegmentHeader();
            _textSegmentHeader.FileOffset       = ReadUInt32();
            _textSegmentHeader.MemoryOffset     = ReadUInt32();
            _textSegmentHeader.DecompressedSize = ReadUInt32();

            // Module offset (calculated by sizeof(header))
            _moduleOffset = ReadUInt32();

            // .rodata SegmentHeader
            _roDataSegmentHeader                  = new NSOSegmentHeader();
            _roDataSegmentHeader.FileOffset       = ReadUInt32();
            _roDataSegmentHeader.MemoryOffset     = ReadUInt32();
            _roDataSegmentHeader.DecompressedSize = ReadUInt32();

            // Module file size
            _moduleFileSize = ReadUInt32();

            // .data SegmentHeader
            _dataSegmentHeader                  = new NSOSegmentHeader();
            _dataSegmentHeader.FileOffset       = ReadUInt32();
            _dataSegmentHeader.MemoryOffset     = ReadUInt32();
            _dataSegmentHeader.DecompressedSize = ReadUInt32();

            // bssSize
            _bssSize = ReadUInt32();

            // Value of "build id" from ELF's GNU .note section. Contains variable sized digest, up to 32bytes.
            _buildId = ReadBytes(32);

            // Compressed Sizes
            _textSegmentHeader.CompressedSize   = ReadUInt32();
            _roDataSegmentHeader.CompressedSize = ReadUInt32();
            _dataSegmentHeader.CompressedSize   = ReadUInt32();

            // Reserved (Padding)
            _reserved2 = ReadBytes(28);

            // Relative Extents
            _roDataRelativeExtentsOfApiInfo = new NSORoDataRelativeExtent();
            _roDataRelativeExtentsOfApiInfo.RegionRoDataOffset = ReadUInt32();
            _roDataRelativeExtentsOfApiInfo.RegionSize         = ReadUInt32();

            _roDataRelativeExtentsOfDynstr = new NSORoDataRelativeExtent();
            _roDataRelativeExtentsOfDynstr.RegionRoDataOffset = ReadUInt32();
            _roDataRelativeExtentsOfDynstr.RegionSize         = ReadUInt32();

            _roDataRelativeExtentsOfDynsym = new NSORoDataRelativeExtent();
            _roDataRelativeExtentsOfDynsym.RegionRoDataOffset = ReadUInt32();
            _roDataRelativeExtentsOfDynsym.RegionSize         = ReadUInt32();

            // Section Hashes
            _textSegmentHeader.SHA256Hash   = ReadBytes(32);
            _roDataSegmentHeader.SHA256Hash = ReadBytes(32);
            _dataSegmentHeader.SHA256Hash   = ReadBytes(32);

            // Decompress .text Section
            Position = _textSegmentHeader.FileOffset;
            if ((_flags & 1) != 0)
            {
                _textSection = Decompress(ReadBytes((int)_textSegmentHeader.CompressedSize), (int)_textSegmentHeader.DecompressedSize);
            }
            else
            {
                _textSection = ReadBytes((int)_textSegmentHeader.DecompressedSize);
            }

            // Decompress .rodata Section
            Position = _roDataSegmentHeader.FileOffset;
            if ((_flags & 2) != 0)
            {
                _roDataSection = Decompress(ReadBytes((int)_roDataSegmentHeader.CompressedSize), (int)_roDataSegmentHeader.DecompressedSize);
            }
            else
            {
                _roDataSection = ReadBytes((int)_roDataSegmentHeader.DecompressedSize);
            }

            // Decompress .data Section
            Position = _dataSegmentHeader.FileOffset;
            if ((_flags & 4) != 0)
            {
                _dataSection = Decompress(ReadBytes((int)_dataSegmentHeader.CompressedSize), (int)_dataSegmentHeader.DecompressedSize);
            }
            else
            {
                _dataSection = ReadBytes((int)_dataSegmentHeader.DecompressedSize);
            }

            // Map Sections to Memory
            using (var fullCodeStream = new MemoryStream())
                using (var fullCodeWriter = new BinaryWriter(fullCodeStream))
                {
                    fullCodeWriter.Write(_textSection);
                    uint roOffset = _roDataSegmentHeader.MemoryOffset;
                    if (roOffset >= fullCodeWriter.BaseStream.Position)
                    {
                        fullCodeWriter.Seek((int)(roOffset - fullCodeWriter.BaseStream.Position), SeekOrigin.Current);
                    }
                    else
                    {
                        Console.WriteLine("Truncating .text?");
                        fullCodeWriter.Seek((int)roOffset, SeekOrigin.Begin);
                    }

                    fullCodeWriter.Write(_roDataSection);
                    uint dataOffset = _dataSegmentHeader.MemoryOffset;
                    if (dataOffset > fullCodeWriter.BaseStream.Position)
                    {
                        fullCodeWriter.Seek((int)(dataOffset - fullCodeWriter.BaseStream.Position), SeekOrigin.Current);
                    }
                    else
                    {
                        Console.WriteLine("Truncating .rodata?");
                    }

                    fullCodeWriter.Write(_dataSection);
                    fullCodeWriter.Flush();
                    _fullCode = fullCodeStream.ToArray();
                }

            using (var codeStream = new MemoryStream(_fullCode, false))
                using (var codeReader = new BinaryObjectReader(codeStream))
                {
                    uint modOffset = codeReader.ReadUInt32(0x4);

                    // Mod
                    codeReader.BaseStream.Seek(modOffset, SeekOrigin.Begin);
                    uint modMagic = codeReader.ReadUInt32();
                    if (modMagic != 0x30444F4D)
                    {
                        return(false);
                    }

                    _mod = codeReader.ReadObject <NSOMod>();
                    _mod.DynamicOffset   += modOffset;
                    _mod.BssStartOffset  += modOffset;
                    _mod.BssEndOffset    += modOffset;
                    _mod.UnwindOffset    += modOffset;
                    _mod.UnwindEndOffset += modOffset;
                    _mod.ModuleOffset    += modOffset;

                    _arm32 = (codeReader.ReadUInt64(_mod.DynamicOffset) > 0xFFFFFFFF || codeReader.ReadUInt64(_mod.DynamicOffset + 0x10) > 0xFFFFFFFF);

                    //dynamic = codeReader.ReadObject<NSODynamic>(mod.DynamicOffset);

                    uint flatSize = _dataSegmentHeader.MemoryOffset + _dataSegmentHeader.DecompressedSize;

                    _dynamics = new List <NSODynamic>();
                    codeReader.BaseStream.Seek(_mod.DynamicOffset, SeekOrigin.Begin);
                    for (uint i = 0; i < (flatSize - _mod.DynamicOffset) / 0x10; i++)
                    {
                        NSODynamic dynamic = new NSODynamic();
                        dynamic.Tag = (NSODynamicTag)(_arm32 ? codeReader.ReadUInt32() : codeReader.ReadUInt64());
                        if (dynamic.Tag == NSODynamicTag.DT_NULL)
                        {
                            break;
                        }
                        dynamic.Value = _arm32 ? codeReader.ReadUInt32() : codeReader.ReadUInt64();
                        //if ((int)dynamic.Tag > 0 && (int)dynamic.Tag < 31)
                        _dynamics.Add(dynamic);
                    }
                }

            return(true);
        }
示例#22
0
 public static OffsetOriginToken WithOffsetOrigin(this BinaryObjectReader reader, long origin)
 => reader.OffsetHandler.WithOffsetOrigin(origin);
示例#23
0
 public static void PushOffsetOrigin(this BinaryObjectReader reader)
 => reader.OffsetHandler.PushOffsetOrigin(reader.Position);
示例#24
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                System.Console.WriteLine("Usage:\nKTID_Splitter [path to g1t] [path to ktid] [path to kidssingletondb] ");
            }
            else
            {
                FileInfo arg0 = new FileInfo(args[0]);

                if (arg0.Extension == ".g1t")
                {
                    List <Int32>  g1tPointers   = new List <Int32>();
                    List <Int32>  g1tNormalFlag = new List <Int32>();
                    List <UInt32> texHashes     = new List <UInt32>();

                    byte[] SectionBytes;

                    var g1tmagic    = 0;
                    var g1tversion  = 0;
                    var texcount    = 0;
                    var offsetTable = 0;
                    var platformID  = 0;
                    var unk         = 0;

                    // Read hash list from KITD
                    FileInfo arg1 = new FileInfo(args[1]);
                    if (arg1.Extension == ".ktid")
                    {
                        Console.WriteLine("\nReading " + arg1.Name);
                        using (BinaryObjectReader ktidFile = new BinaryObjectReader(args[1], Endianness.Little, Encoding.GetEncoding(65001)))
                        {
                            var numOfTex = arg1.Length / 8;

                            Console.WriteLine("Number of Entries in KTID: " + numOfTex.ToString());

                            for (var i = 0; i < numOfTex; i++)
                            {
                                ktidFile.Seek(4, SeekOrigin.Current);
                                texHashes.Add(ktidFile.ReadUInt32());
                                //Console.WriteLine("Reading Hash: " + i.ToString() + " = " + texHashes[i]);
                            }
                        }
                    }

                    // Parse kidsobj database to get name hashes
                    FileInfo arg2 = new FileInfo(args[2]);
                    Console.WriteLine("\nParsing " + arg2.Name);
                    List <UInt32> ktidEntryID     = new List <UInt32>();
                    List <UInt32> textureNameHash = new List <UInt32>();

                    using (BinaryObjectReader kidsobj = new BinaryObjectReader(args[2], Endianness.Little, Encoding.GetEncoding(65001)))
                    {
                        var fileMagic     = kidsobj.ReadInt64();
                        var headerSize    = kidsobj.ReadInt32();
                        var field_0xC     = kidsobj.ReadInt32();
                        var numOfEntries  = kidsobj.ReadInt32();
                        var kidsobjfileID = kidsobj.ReadUInt32();
                        var totalFileSize = kidsobj.ReadInt32();

                        Console.WriteLine("Number of Entries in KIDSOBJ: " + numOfEntries.ToString());

                        Console.WriteLine("Parsing texture name hash list");
                        for (var i = 0; i < numOfEntries; i++)
                        {
                            fileMagic = kidsobj.ReadInt64(); //0x0
                            var entrySize = kidsobj.ReadUInt32();
                            kidsobjfileID = kidsobj.ReadUInt32();
                            var typeHash        = kidsobj.ReadUInt32();
                            var numOfProperties = kidsobj.ReadUInt32();
                            //Console.WriteLine("entryID 0x" + kidsobjfileID.ToString("X8"));
                            //Console.WriteLine("entry Size: " + entrySize.ToString() + " : Expected Size " + ( 24 +  (numOfProperties * 16 )).ToString());
                            if (typeHash == 4286431188) // 0xFF7DBFD4 TypeInfo::Object::Render::Texture::Static
                            {
                                //Console.WriteLine("Found TypeInfo::Object::Render::Texture::Static");
                                ktidEntryID.Add(kidsobjfileID);
                                for (var h = 0; h < numOfProperties; h++)
                                {
                                    var propertyType = kidsobj.ReadInt32();
                                    var count        = kidsobj.ReadInt32();
                                    var kidsTypeHash = kidsobj.ReadInt32();
                                }
                                textureNameHash.Add(kidsobj.ReadUInt32());
                                for (var h = 0; h < numOfProperties - 1; h++)
                                {
                                    var property = kidsobj.ReadInt32();
                                }
                            }
                            else
                            {
                                var dataPadding = (4 - entrySize % 4) % 4;
                                //Console.WriteLine("Property 0x" + typeHash.ToString("X8"));
                                kidsobj.Seek((entrySize + dataPadding) - 24, SeekOrigin.Current);
                            }
                        }
                    }

                    // Read G1T
                    using (BinaryObjectReader g1tFile = new BinaryObjectReader(args[0], Endianness.Little, Encoding.GetEncoding(65001)))
                    {
                        Console.WriteLine("\nProcessing " + arg0.Name);
                        g1tmagic   = g1tFile.ReadInt32();
                        g1tversion = g1tFile.ReadInt32();
                        var filesize = g1tFile.ReadInt32();//irrelevant
                        offsetTable = g1tFile.ReadInt32();
                        texcount    = g1tFile.ReadInt32();
                        platformID  = g1tFile.ReadInt32();
                        unk         = g1tFile.ReadInt32();
                        Console.WriteLine("Number of Textures in G1T: " + texcount.ToString());

                        for (var i = 0; i < texcount; i++)
                        {
                            g1tNormalFlag.Add(g1tFile.ReadInt32());
                        }

                        g1tFile.Seek(offsetTable, SeekOrigin.Begin);

                        for (var i = 0; i < texcount; i++)
                        {
                            g1tPointers.Add(g1tFile.ReadInt32() + offsetTable);
                            Console.WriteLine("Pointer # " + (i + 1).ToString() + " -> 0x" + g1tPointers[i].ToString("X8"));
                        }
                        g1tPointers.Add((int)arg0.Length);
                        Console.WriteLine("\n");
                        for (var i = 0; i < texcount; i++)
                        {
                            g1tFile.Seek(g1tPointers[i], SeekOrigin.Begin);
                            var dataSize = (g1tPointers[i + 1] - g1tPointers[i]);
                            //Console.WriteLine("Current Est. Size: 0x" + dataSize.ToString("X8"));

                            SectionBytes = g1tFile.ReadArray <byte>(dataSize);
                            //Console.WriteLine("Actual amount of bytes read: 0x" + SectionBytes.Length.ToString("X8"));
                            String savePath;
                            //try to write individual G1T
                            if (args.Length > 3)
                            {
                                savePath = args[3];
                                System.IO.Directory.CreateDirectory(args[3]);
                            }
                            else
                            {
                                savePath = arg0.DirectoryName;
                            }

                            var hashFileName = 0;

                            for (var h = 0; h < ktidEntryID.Count; h++)
                            {
                                if (texHashes[i] == ktidEntryID[h])
                                {
                                    Console.WriteLine("KTID Match Found: " + ktidEntryID[h].ToString("X8") + " => " + textureNameHash[h].ToString("X8"));
                                    hashFileName = h;
                                }
                            }

                            Console.WriteLine("Writing G1T File # " + (i + 1).ToString() + " -> " + textureNameHash[hashFileName].ToString("X8"));
                            using (BinaryObjectWriter newG1TFile = new BinaryObjectWriter(Path.Combine(savePath, "0x" + textureNameHash[hashFileName].ToString("X8") + ".file"), Endianness.Little, Encoding.GetEncoding(65001)))
                            {
                                newG1TFile.WriteInt32(g1tmagic);
                                newG1TFile.WriteInt32(g1tversion);
                                newG1TFile.WriteInt32(0);  // TODO Fix this, its filesize
                                newG1TFile.WriteInt32(32); // offset table, only 1
                                newG1TFile.WriteInt32(1);  // only 1 tex per g1t
                                newG1TFile.WriteInt32(platformID);
                                newG1TFile.WriteInt32(0);  // padding?
                                newG1TFile.WriteInt32(g1tNormalFlag[i]);
                                newG1TFile.WriteInt32(4);  //offset table
                                newG1TFile.WriteBytes(SectionBytes);
                                // Go back and fix filesize
                                newG1TFile.Seek(8, SeekOrigin.Begin);
                                newG1TFile.WriteInt32((int)newG1TFile.Length);
                            }
                        }
                    }
                }
                else if (arg0.Extension == ".ktid")
                {
                    Console.WriteLine("Dumping KTID list to txt");
                    using (BinaryObjectReader ktidFile = new BinaryObjectReader(args[0], Endianness.Little, Encoding.GetEncoding(65001)))
                    {
                        List <string> texHashes = new List <string>();
                        var           numOfTex  = arg0.Length / 8;

                        Console.WriteLine("Number of textures: " + numOfTex.ToString());

                        for (var i = 0; i < numOfTex; i++)
                        {
                            ktidFile.Seek(4, SeekOrigin.Current);
                            texHashes.Add(ktidFile.ReadUInt32().ToString("X8"));
                            Console.WriteLine("Reading Hash: " + i.ToString() + " = " + texHashes[i]);
                        }

                        var savePath = arg0.FullName + ".txt";
                        File.WriteAllLines(savePath, texHashes);
                        Console.WriteLine("File saved to: " + savePath, "File saved");
                    }
                }
                else if (arg0.Extension == ".txt")
                {
                    Console.WriteLine("Converting KTID List to KTID file");

                    string[] readText = File.ReadAllLines(args[0], Encoding.UTF8);

                    List <string> texHashes = new List <string>();

                    int k = 0;
                    foreach (string s in readText)
                    {
                        texHashes.Add(readText[k]);
                        k++;
                    }

                    var savePath = arg0.FullName + ".ktid";
                    using (BinaryObjectWriter newKTIDFile = new BinaryObjectWriter(savePath, Endianness.Little, Encoding.GetEncoding(65001)))
                    {
                        for (var i = 0; i < texHashes.Count; i++)
                        {
                            newKTIDFile.WriteInt32(i);
                            newKTIDFile.WriteInt32(Convert.ToInt32(texHashes[i], 16));
                        }
                        Console.WriteLine("File saved to: " + savePath, "File saved");
                    }
                }
                else if (arg0.Extension == ".kidssingletondb")
                {
                    Console.WriteLine("Parsing CharacterEditor.kidssingletondb");
                    List <UInt32> ktidEntryID     = new List <UInt32>();
                    List <UInt32> textureNameHash = new List <UInt32>();

                    using (BinaryObjectReader kidsobj = new BinaryObjectReader(args[0], Endianness.Little, Encoding.GetEncoding(65001)))
                    {
                        var fileMagic     = kidsobj.ReadInt64();
                        var headerSize    = kidsobj.ReadInt32();
                        var field_0xC     = kidsobj.ReadInt32();
                        var numOfEntries  = kidsobj.ReadInt32();
                        var kidsobjfileID = kidsobj.ReadUInt32();
                        var totalFileSize = kidsobj.ReadInt32();

                        Console.WriteLine("Number of Entries in KIDSOBJ: " + numOfEntries.ToString());

                        Console.WriteLine("Parsing and dumping texture name hash list");
                        for (var i = 0; i < numOfEntries; i++)
                        {
                            fileMagic = kidsobj.ReadInt64(); //0x0
                            var entrySize = kidsobj.ReadUInt32();
                            kidsobjfileID = kidsobj.ReadUInt32();
                            var typeHash        = kidsobj.ReadUInt32();
                            var numOfProperties = kidsobj.ReadUInt32();
                            //Console.WriteLine("entryID 0x" + kidsobjfileID.ToString("X8"));
                            //Console.WriteLine("entry Size: " + entrySize.ToString() + " : Expected Size " + ( 24 +  (numOfProperties * 16 )).ToString());
                            if (typeHash == 4286431188) // 0xFF7DBFD4 TypeInfo::Object::Render::Texture::Static
                            {
                                //Console.WriteLine("Found TypeInfo::Object::Render::Texture::Static");
                                ktidEntryID.Add(kidsobjfileID);
                                for (var h = 0; h < numOfProperties; h++)
                                {
                                    var propertyType = kidsobj.ReadInt32();
                                    var count        = kidsobj.ReadInt32();
                                    var kidsTypeHash = kidsobj.ReadInt32();
                                }
                                textureNameHash.Add(kidsobj.ReadUInt32());
                                for (var h = 0; h < numOfProperties - 1; h++)
                                {
                                    var property = kidsobj.ReadInt32();
                                }
                            }
                            else
                            {
                                var dataPadding = (4 - entrySize % 4) % 4;
                                //Console.WriteLine("Property 0x" + typeHash.ToString("X8"));
                                kidsobj.Seek((entrySize + dataPadding) - 24, SeekOrigin.Current);
                            }
                        }

                        List <String> combination = new List <String>();

                        var savePath = arg0.FullName + ".txt";
                        for (var h = 0; h < textureNameHash.Count; h++)
                        {
                            combination.Add(ktidEntryID[h].ToString("X8") + " " + textureNameHash[h].ToString("X8"));
                        }
                        File.WriteAllLines(savePath, combination);
                        Console.WriteLine("File saved to: " + savePath, "File saved");
                    }
                }
                else
                {
                    Console.WriteLine("Error: First file is not a ktid or g1t file.");
                }
            }
        }
示例#25
0
 public static OffsetOriginToken WithOffsetOrigin(this BinaryObjectReader reader)
 => reader.OffsetHandler.WithOffsetOrigin(reader.Position);
示例#26
0
 public void Read(BinaryObjectReader reader)
 => reader.Read(out WaveInfo);
示例#27
0
 public static void PopOffsetOrigin(this BinaryObjectReader reader)
 => reader.OffsetHandler.PopOffsetOrigin();