Esempio n. 1
0
        public XeXHeader(MemoryStream file)
        {
            MyBinaryReader reader;

            try
            {
                reader = new MyBinaryReader(file, EndianType.BigEndian);

                reader.BaseStream.Seek(0L, SeekOrigin.Begin);
                if (Encoding.ASCII.GetString(reader.ReadBytes(4)) == "XEX2")
                {
                    reader.BaseStream.Seek(20L, SeekOrigin.Begin);

                    uint   headerCount = reader.ReadUInt32();
                    byte[] infoArray   = new byte[] { 0, 4, 0, 6 };

                    for (int i = 0; i < headerCount; i++)
                    {
                        byte[] headerIdArray = reader.ReadBytes(4);

                        uint headerId = BitConverter.ToUInt32(headerIdArray, 0);

                        if (headerId == BitConverter.ToUInt32(infoArray, 0))
                        {
                            uint dataAddress = reader.ReadUInt32();

                            reader.BaseStream.Seek((long)dataAddress, SeekOrigin.Begin);
                            this.mediaId        = reader.ReadBytes(4);
                            this.version        = reader.ReadUInt32();
                            this.baseVersion    = reader.ReadUInt32();
                            this.titleId        = reader.ReadBytes(4);
                            this.platform       = reader.ReadByte();
                            this.executableType = reader.ReadByte();
                            this.discNumber     = reader.ReadByte();
                            this.discCount      = reader.ReadByte();
                            break;
                        }
                        else
                        {
                            reader.ReadUInt32();
                        }
                    }
                }
                else
                {
                    throw new Exception("Extracted file is not XEX file");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            reader.Close();
        }
Esempio n. 2
0
 // Constructor.
 public SectionWithLengthNoName(MyBinaryReader saveStream)
 {
     Console.WriteLine("SectionWithLengthNoName constrcutor");
     this.Length = saveStream.ReadInt32();
     Console.WriteLine(this.Length);
     buffer = saveStream.ReadBytes(this.Length);
 }
Esempio n. 3
0
 // Constructor with specified data length.
 public GenericSection(MyBinaryReader saveStream, int length)
 {
     Console.WriteLine("GenericSection constrcutor");
     this.Name = saveStream.ReadCharString(4);
     Console.WriteLine(this.Name);
     buffer = saveStream.ReadBytes(length);
 }
Esempio n. 4
0
        public List <byte[]> FillListWithMenuSectors()
        {
            List <byte[]> result = new List <byte[]>();
            Dictionary <string, byte[]> files = GetFilesWithSectors();

            foreach (KeyValuePair <string, byte[]> kvp in files)
            {
                if (kvp.Key.ToUpper().Contains("_0.IFO"))
                {
                    //sector of ifo file
                    int  sector  = BitConverter.ToInt32(kvp.Value, 0);
                    long address = (long)sector * this.sectorSize;

                    //sector of vob file
                    int sectorVob = GetSectorOfVtsVob(kvp.Key);

                    if (sector > 0 && sectorVob > 0)
                    {
                        // jumt to 0x0D to read start offset of VTSM_PGCI_UT
                        br.BaseStream.Seek(address + 0xD0, SeekOrigin.Begin);

                        // get sector
                        int  startOffsetVTSM_PGCI_UT = br.ReadInt32B();
                        long addressVTSM_PGCI_UT     = address + ((long)startOffsetVTSM_PGCI_UT * this.sectorSize);

                        // jump to VTSM_PGCI_UT
                        br.BaseStream.Seek(addressVTSM_PGCI_UT, SeekOrigin.Begin);
                        br.ReadInt32B();
                        int  endByteVTSM_PGCI_UT    = br.ReadInt32B();
                        long addressEndVTSM_PGCI_UT = addressVTSM_PGCI_UT + endByteVTSM_PGCI_UT;

                        br.BaseStream.Seek(addressVTSM_PGCI_UT, SeekOrigin.Begin);
                        VTSM_PGCI_UT titleSetPGCI = new VTSM_PGCI_UT(br.ReadBytes(endByteVTSM_PGCI_UT));

                        for (int i = 0; i < titleSetPGCI.Menus.Count; i++)
                        {
                            if (i % 2 == 1 && titleSetPGCI.Menus[i].IsGameMenu)
                            {
                                int gameSector = sectorVob + titleSetPGCI.Menus[i].Sector;
                                result.Add(BitConverter.GetBytes(gameSector));
                            }
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 5
0
 public VTSM_PGCI_UT(byte[] array)
 {
     try
     {
         MemoryStream   s      = new MemoryStream(array);
         MyBinaryReader reader = new MyBinaryReader(s);
         this.numberOfVTSM_PGCI_LU = reader.ReadInt16B();
         reader.Skip(2);
         this.endByteOfVTSM_PGCI_LU = reader.ReadInt32B();
         reader.Skip(4);
         int num = reader.ReadInt32B();
         reader.BaseStream.Seek((long)num, SeekOrigin.Begin);
         this.menus = new VTSM_PGCI_LU_MENUS(reader.ReadBytes(array.Length - num));
     }
     catch (Exception exception)
     {
         throw new Exception("Error searching sectors in DVD MENU", exception);
     }
 }
Esempio n. 6
0
        public VTSM_PGCI_UT(byte[] array)
        {
            try
            {
                MemoryStream   ms = new MemoryStream(array);
                MyBinaryReader br = new MyBinaryReader(ms);

                numberOfVTSM_PGCI_LU = br.ReadInt16B();
                br.Skip(2);
                endByteOfVTSM_PGCI_LU = br.ReadInt32B();
                br.Skip(4);
                int LU_1startByte = br.ReadInt32B();
                br.BaseStream.Seek(LU_1startByte, SeekOrigin.Begin);
                menus = new VTSM_PGCI_LU_MENUS(br.ReadBytes(array.Length - LU_1startByte));
            }
            catch (Exception ex)
            {
                throw new Exception("Error searching sectors in DVD MENU", ex);
            }
        }
Esempio n. 7
0
    // Constructor.
    public civ3Game(MyBinaryReader saveStream)
    {
        // Initialize my list of sections I don't know what to do with yet.
        this.pileOfSectionsToSortLater = new List <SectionWithLength>();

        // Initialize empty byte[] pad list.
        this.bytePads = new List <byte[]>();

        // Initialize empty tile list.
        this.tileSection = new List <SectionWithLength>();

        // Read save file stream assuming fixed order and general structure.
        // Trying to use class constructors but reading some padding in manually.
        // FIX: hard-coding CIV3 length of 0x1a
        this.civ3Section = new GenericSection(saveStream, 0x1a);
        this.bicSection  = new SectionWithLength(saveStream);
        this.bicqSection = new BicqSection(saveStream);
        this.gameSection = new GenericSection(saveStream, 0x0ecf);

        // reading in five sections (DATE, PLGI, PLGI, DATE and DATE)
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));

        // FIX: hard-coded byte padding here. Is it algined? Other way to realign?
        this.bytePads.Add(saveStream.ReadBytes(8));

        // reading CNSL section
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));

        // reading three WRLD sections
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));
        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));

        // FIX: Ugly hack to grab presumed map width and height.
        int mapWidth  = BitConverter.ToInt32(pileOfSectionsToSortLater[pileOfSectionsToSortLater.Count - 1].buffer, 0x04);
        int mapHeight = BitConverter.ToInt32(pileOfSectionsToSortLater[pileOfSectionsToSortLater.Count - 1].buffer, 0x18);

        Console.WriteLine(mapWidth);
        Console.WriteLine(mapHeight);

        this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream));

        // FIX: hard-seeking to first TILE section of my test file.
        // saveStream.BaseStream.Seek(0x34a4, SeekOrigin.Begin);

        // Read all TILE sections. There are mapWidth/2 * mapHeight * 4 of them
        // FIX: I really want one entity per game tile, but there are four TILE sections per game tile
        for (int i = 0; i < (mapWidth / 2) * mapHeight * 4; i++)
        {
            this.tileSection.Add(new SectionWithLength(saveStream));
        }
        Console.WriteLine(tileSection.Count);


/*
 *              // FIX: Need to load all tiles. Load a single tile into the list.
 *              this.tileSection.Add(new SectionWithLength(saveStream));
 *              this.tileSection.Add(new SectionWithLength(saveStream));
 *              this.tileSection.Add(new SectionWithLength(saveStream));
 *              this.tileSection.Add(new SectionWithLength(saveStream));
 *              this.tileSection.Add(new SectionWithLength(saveStream));
 *              this.tileSection.Add(new SectionWithLength(saveStream));
 */
        // Iterating and printing out section names of my junk pile.
        foreach (SectionWithLength mySection in this.pileOfSectionsToSortLater)
        {
            Console.WriteLine(mySection.Name);
        }
    }
Esempio n. 8
0
        public XeXHeader(MyBinaryReader reader)
        {
            long position = reader.BaseStream.Position;

            try
            {
                byte[] b = reader.ReadBytes(4);
                if (Encoding.ASCII.GetString(b) == "XEX2")
                {
                    reader.BaseStream.Seek(position + 20L, SeekOrigin.Begin);
                    uint num = reader.ReadUInt32();

                    UInt32 einfo  = BitConverter.ToUInt32(executioninfo, 0);
                    UInt32 tstamp = BitConverter.ToUInt32(basefiletimestamp, 0);

                    for (int i = 0; i < num; i++)
                    {
                        UInt32 val     = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
                        uint   pos     = reader.ReadUInt32();
                        long   savepos = reader.BaseStream.Position;
                        if (val == einfo)
                        {
                            reader.BaseStream.Seek((long)position + pos, SeekOrigin.Begin);
                            this.short_mediaId  = reader.ReadBytes(4);
                            this.version        = reader.ReadUInt32();
                            this.baseVersion    = reader.ReadUInt32();
                            this.titleId        = reader.ReadBytes(4);
                            this.platform       = reader.ReadByte();
                            this.executableType = reader.ReadByte();
                            this.discNumber     = reader.ReadByte();
                            this.discCount      = reader.ReadByte();
                            reader.BaseStream.Seek(savepos, SeekOrigin.Begin);
                        }
                        else if (val == tstamp)
                        {
                            reader.BaseStream.Seek((long)position + pos + 4, SeekOrigin.Begin);
                            this.date =
                                new DateTime(1970, 1, 1, 0, 0, 0).Add(
                                    TimeSpan.FromTicks((long)reader.ReadUInt32() * TimeSpan.TicksPerSecond));
                            reader.BaseStream.Seek(savepos, SeekOrigin.Begin);
                        }
                    }
                    //Read cert
                    reader.BaseStream.Seek((long)position + 0x10, SeekOrigin.Begin);
                    uint offset = reader.ReadUInt32();
                    reader.BaseStream.Seek((long)position + (long)offset + 0x140, SeekOrigin.Begin);
                    mediaid = reader.ReadBytes(16);
                    reader.BaseStream.Seek((long)position + (long)offset + 0x178, SeekOrigin.Begin);
                    regioncode = reader.ReadUInt32();
                }
                else if (Encoding.ASCII.GetString(b) == "XBEH")
                {
                    reader.EndianType = EndianType.LittleEndian;
                    reader.BaseStream.Seek(position + 0x110, SeekOrigin.Begin);
                    uint pos = reader.ReadUInt32();
                    this.date = new DateTime(1970, 1, 1, 0, 0, 0).Add(TimeSpan.FromTicks((long)reader.ReadUInt32() * TimeSpan.TicksPerSecond));
                    reader.BaseStream.Seek(position + pos + 0x8, SeekOrigin.Begin);
                    this.titleId    = new byte[4];
                    this.titleId[3] = reader.ReadByte();
                    this.titleId[2] = reader.ReadByte();
                    this.titleId[1] = reader.ReadByte();
                    this.titleId[0] = reader.ReadByte();
                    byte[] b2 = reader.ReadBytes(0x40);
                    this.title = Encoding.Unicode.GetString(b2).Replace("\0", string.Empty).Trim();
                    reader.BaseStream.Seek(position + pos + 0xA8, SeekOrigin.Begin);
                    this.discNumber = this.discCount = (byte)(reader.ReadUInt32() + 1);
                    this.version    = reader.ReadUInt32();
                }
                else
                {
                    throw new Exception("Not XEX/XEB file");
                }
            }
            catch (Exception exception)
            {
                if (reader != null)
                {
                    reader.BaseStream.Seek(position, SeekOrigin.Begin);
                }
                throw exception;
            }
            reader.BaseStream.Seek(position, SeekOrigin.Begin);
        }
Esempio n. 9
0
        private object?LoadObjectV2(int dataOffset)
        {
            Debug.Assert(System.Threading.Monitor.IsEntered(reader));
            reader.Seek(dataSectionPosition + dataOffset, SeekOrigin.Begin);
            var typeCode = (ResourceTypeCode)reader.Read7BitEncodedInt();

            switch (typeCode)
            {
            case ResourceTypeCode.Null:
                return(null);

            case ResourceTypeCode.String:
                return(reader.ReadString());

            case ResourceTypeCode.Boolean:
                return(reader.ReadBoolean());

            case ResourceTypeCode.Char:
                return((char)reader.ReadUInt16());

            case ResourceTypeCode.Byte:
                return(reader.ReadByte());

            case ResourceTypeCode.SByte:
                return(reader.ReadSByte());

            case ResourceTypeCode.Int16:
                return(reader.ReadInt16());

            case ResourceTypeCode.UInt16:
                return(reader.ReadUInt16());

            case ResourceTypeCode.Int32:
                return(reader.ReadInt32());

            case ResourceTypeCode.UInt32:
                return(reader.ReadUInt32());

            case ResourceTypeCode.Int64:
                return(reader.ReadInt64());

            case ResourceTypeCode.UInt64:
                return(reader.ReadUInt64());

            case ResourceTypeCode.Single:
                return(reader.ReadSingle());

            case ResourceTypeCode.Double:
                return(reader.ReadDouble());

            case ResourceTypeCode.Decimal:
                return(reader.ReadDecimal());

            case ResourceTypeCode.DateTime:
                // Use DateTime's ToBinary & FromBinary.
                long data = reader.ReadInt64();
                return(DateTime.FromBinary(data));

            case ResourceTypeCode.TimeSpan:
                long ticks = reader.ReadInt64();
                return(new TimeSpan(ticks));

            // Special types
            case ResourceTypeCode.ByteArray:
            {
                int len = reader.ReadInt32();
                if (len < 0)
                {
                    throw new BadImageFormatException("Resource with negative length");
                }
                return(reader.ReadBytes(len));
            }

            case ResourceTypeCode.Stream:
            {
                int len = reader.ReadInt32();
                if (len < 0)
                {
                    throw new BadImageFormatException("Resource with negative length");
                }
                byte[] bytes = reader.ReadBytes(len);
                return(new MemoryStream(bytes, writable: false));
            }

            default:
                if (typeCode < ResourceTypeCode.StartOfUserTypes)
                {
                    throw new BadImageFormatException("Invalid typeCode");
                }
                return(new ResourceSerializedObject(FindType(typeCode - ResourceTypeCode.StartOfUserTypes), this, reader.BaseStream.Position));
            }
        }
        public List <IConstant> Parse(MyBinaryReader reader)
        {
            // заполняем первый индекс заглушкой, т.к. здесь нумерация с единицы
            var pool = new List <IConstant>(_poolCount)
            {
                null
            };

            while (_poolCount - pool.Count > 0)
            {
                switch ((Tags)reader.ReadByte())
                { // Здесь всем методам будем передавать index + 1
                case Tags.CONSTANT_Class:
                    // parse 2byte name_index
                    pool.Add(new ClassInfo(reader.ReadUInt16()));
                    break;

                case Tags.CONSTANT_Fieldref:
                case Tags.CONSTANT_Methodref:
                case Tags.CONSTANT_InterfaceMethodref:
                    // u2 class_index
                    // u2 name_and_type_index
                    pool.Add(new FiMeInRef(reader.ReadUInt16(), reader.ReadUInt16()));
                    break;

                case Tags.CONSTANT_String:
                    // u2 string_index
                    pool.Add(new StringRef(reader.ReadUInt16()));
                    break;

                case Tags.CONSTANT_Integer:
                    // u4 bytes
                    pool.Add(ParseIntegerConstant(reader));
                    //index += 4;
                    break;

                case Tags.CONSTANT_Float:     // Не знаю, что будет с float, попробую не пользоваться формулой s * m * 2**(e-150)
                    // u4 bytes
                    pool.Add(ParseFloatConstant(reader));
                    //index += 4;
                    break;

                case Tags.CONSTANT_Long:
                case Tags.CONSTANT_Double:
                    var pair = ParseLongOrDoubleConstant(reader);
                    pool.Add(pair.Item1);
                    pool.Add(pair.Item2);
                    //index += 8;
                    break;

                case Tags.CONSTANT_NameAndType:
                    // u2 name_index
                    // u2 descriptor_index
                    pool.Add(new NameAndTypeInfo(reader.ReadUInt16(), reader.ReadUInt16()));
                    break;

                case Tags.CONSTANT_Utf8:
                    var length   = reader.ReadUInt16();
                    var fragment = reader.ReadBytes(length);
                    pool.Add(ParseUtfStrings(fragment, length));
                    break;

                case Tags.CONSTANT_MethodHandle:
                    // u1 reference_kind in range(1 to 9)
                    // u2 reference_index
                    pool.Add(new MethodHandle(reader.ReadByte(), reader.ReadUInt16()));     // Надо бы валидацию
                    break;

                case Tags.CONSTANT_MethodType:
                    // u2 descriptor_index
                    pool.Add(new MethodType(reader.ReadUInt16()));
                    break;

                case Tags.CONSTANT_InvokeDynamic:
                    // u2 bootstrap_method_attr_index
                    // u2 name_and_type_index
                    pool.Add(new InvokeDynamic(reader.ReadUInt16(), reader.ReadUInt16()));
                    break;
                }
            }

            return(pool);
        }