Esempio n. 1
0
        /// <summary>
        ///     Fixes the LBA table on the main ELF executable of the Harvest Moon: Save the Homeland game.
        /// </summary>
        /// <param name="Elf">The Stream with the ELF data</param>
        /// <param name="LBA">The LBA of the modified file</param>
        /// <param name="NewSize">The new size of the file</param>
        public static void Fix(Stream Elf, uint LBA, uint NewSize)
        {
            BinaryReader Reader = new BinaryReader(Elf);
            BinaryWriter Writer = new BinaryWriter(Elf);

            int  Difference = 0;
            bool Found      = false;

            Elf.Seek(LBATableStart, SeekOrigin.Begin);
            while (Elf.Position < LBATableEnd)
            {
                uint LBAStart = Reader.ReadUInt32();
                uint LBAEnd   = Reader.ReadUInt32();

                Elf.Seek(-8, SeekOrigin.Current);
                Writer.Write((uint)(LBAStart + Difference));
                Writer.Write((uint)(LBAEnd + Difference));

                if (LBAStart == LBA)
                {
                    Found = true;
                    uint Size = NewSize / BytesPerSector;
                    if ((NewSize % BytesPerSector) != 0)
                    {
                        Size++;
                    }

                    Elf.Seek(-4, SeekOrigin.Current);
                    uint NewEnd = (LBAStart + Size) - 1;
                    Writer.Write(NewEnd);
                    Difference = (int)(NewEnd - LBAEnd);
                }
            }

            if (!Found)
            {
                TextOut.PrintWarning("The LBA you entered was not found on the table!");
                TextOut.Print("Make sure you typed it in DECIMAL format.");
            }
            else
            {
                TextOut.PrintSuccess("LBA found and values patched successfully!");
            }
        }