Beispiel #1
0
        /// <summary>
        /// Read the internal info of a ROM file.
        /// </summary>
        /// <param name="str">Stream to read from.</param>
        public override void Read(DataStream str)
        {
            // Read header
            str.Seek(0, SeekMode.Origin);
            this.header = new RomHeader();
            this.header.Read(str);

            // Read banner
            var bannerStream = new DataStream(str, this.header.BannerOffset, Banner.Size);
            var bannerFile   = new GameFile("Banner", bannerStream);

            this.banner = new Banner();
            this.banner.Initialize(bannerFile);
            this.banner.Read();

            // Read file system: FAT and FNT
            this.fileSys = new FileSystem();
            this.fileSys.Initialize(null, this.header);
            this.fileSys.Read(str);

            // Assign common tags (they will be assigned recursively)
            this.File.Tags["_Device_"]    = "NDS";
            this.File.Tags["_MakerCode_"] = this.header.MakerCode;
            this.File.Tags["_GameCode_"]  = this.header.GameCode;

            // Get the ROM folders with files and system files.
            this.fileSys.SystemFolder.AddFile(bannerFile);

            this.File.AddFolder(this.fileSys.Root);
            this.File.AddFolder(this.fileSys.SystemFolder);
        }
Beispiel #2
0
        /// <summary>
        /// Update and write the Arm file
        /// </summary>
        /// <param name="str">Stream to write</param>
        /// <param name="header">Rom header to update.</param>
        /// <param name="shiftOffset">Amount of data before that stream.</param>
        public void UpdateAndWrite(DataStream str, RomHeader header, uint shiftOffset)
        {
            if (this.IsArm9)
            {
                header.Arm9Autoload     = this.Autoload;
                header.Arm9EntryAddress = this.EntryAddress;
                header.Arm9RamAddress   = this.RamAddress;
                header.Arm9Size         = (uint)this.Length;
                if (this.Length > 0x00)
                {
                    header.Arm9Offset = (uint)(shiftOffset + str.Position);
                }
                else
                {
                    header.Arm9Offset = 0x00;
                }
            }
            else
            {
                header.Arm7Autoload     = this.Autoload;
                header.Arm7EntryAddress = this.EntryAddress;
                header.Arm7RamAddress   = this.RamAddress;
                header.Arm7Size         = (uint)this.Length;
                if (this.Length > 0x00)
                {
                    header.Arm7Offset = (uint)(shiftOffset + str.Position);
                }
                else
                {
                    header.Arm7Offset = 0x00;
                }
            }

            // Write the file
            this.Stream.WriteTo(str);

            if (this.IsArm9)
            {
                // Update encoded size if it was set
                uint encodeSize = this.SearchEncodedSizeAddress();
                if (encodeSize != 0 && encodeSize != 1)
                {
                    this.Stream.Seek(encodeSize, SeekMode.Origin);
                    if (new DataReader(this.Stream).ReadUInt32() != 0x00)
                    {
                        str.Seek(encodeSize, SeekMode.Origin);
                        new DataWriter(str).Write((uint)(this.Length + this.RamAddress));
                        str.Seek(0, SeekMode.End);
                    }
                }
            }

            // Write the unknown tail
            if (this.unknownTail != null)
            {
                str.Write(this.unknownTail, 0, this.unknownTail.Length);
            }
        }
Beispiel #3
0
        public override void Initialize(GameFile file, params object[] parameters)
        {
            base.Initialize(file, parameters);

            if (parameters.Length == 1)
            {
                this.header = (RomHeader)parameters[0];
            }
        }
Beispiel #4
0
        /// <summary>
        /// Create a folder with all the overlays of the same ARM.
        /// </summary>
        /// <param name="str">Stream to read the overlay table.</param>
        /// <param name="header">Header of the current ROM.</param>
        /// <param name="isArm9">If must read overlays from the ARM9 or ARM7.</param>
        /// <param name="listFiles">List with all the files in the ROM.</param>
        /// <returns>Folder with overlays.</returns>
        public static OverlayFolder FromTable(DataStream str, RomHeader header, bool isArm9, GameFile[] listFiles)
        {
            OverlayFolder overlays = new OverlayFolder(isArm9);

            int numOverlays;
            if (isArm9) {
                str.Seek(header.Ov9TableOffset, SeekMode.Origin);
                numOverlays = (int)(header.Ov9TableSize / OverlayFile.TableEntrySize);
            } else {
                str.Seek(header.Ov7TableOffset, SeekMode.Origin);
                numOverlays = (int)(header.Ov7TableSize / OverlayFile.TableEntrySize);
            }

            for (int i = 0; i < numOverlays; i++)
                overlays.AddFile(OverlayFile.FromTable(str, isArm9, listFiles));

            return overlays;
        }
Beispiel #5
0
        /// <summary>
        /// Write the table of overlays info.
        /// </summary>
        /// <param name="str">Stream to write to.</param>
        /// <param name="header">Header of the ROM to update.</param>
        /// <param name="shiftOffset">Amount of data before the current stream.</param>
        public void WriteTable(DataStream str, RomHeader header, uint shiftOffset)
        {
            long startOffset = str.Position;
            int  numOverlays = 0;

            // For each overlay, writes its info
            foreach (GameFile file in this.Files)
            {
                OverlayFile ov = file as OverlayFile;
                if (ov == null)
                {
                    continue;
                }

                ov.WriteTable(str);
                numOverlays++;
            }

            // Updates RomHeader
            if (this.isArm9)
            {
                header.Ov9TableSize = (uint)(numOverlays * OverlayFile.TableEntrySize);
                if (numOverlays > 0)
                {
                    header.Ov9TableOffset = (uint)(shiftOffset + startOffset);
                }
                else
                {
                    header.Ov9TableOffset = 0x00;
                }
            }
            else
            {
                header.Ov7TableSize = (uint)(numOverlays * OverlayFile.TableEntrySize);
                if (numOverlays > 0)
                {
                    header.Ov7TableOffset = (uint)(shiftOffset + startOffset);
                }
                else
                {
                    header.Ov7TableOffset = 0x00;
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Create a new ARM file from the info of the ROM header.
        /// </summary>
        /// <param name="str">Stream to read the unknown tail.</param>
        /// <param name="header">Header of the current ROM.</param>
        /// <param name="romPath">Path to the current ROM.</param>
        /// <param name="isArm9">Indicates if must create an ARM9 or ARM7 file.</param>
        /// <returns>ARM file.</returns>
        public static ArmFile FromStream(DataStream str, RomHeader header, bool isArm9)
        {
            ArmFile arm;

            if (isArm9)
            {
                arm              = new ArmFile("ARM9.bin", new DataStream(str, header.Arm9Offset, header.Arm9Size));
                arm.IsArm9       = true;
                arm.EntryAddress = header.Arm9EntryAddress;
                arm.Autoload     = header.Arm9Autoload;
                arm.RamAddress   = header.Arm9RamAddress;
            }
            else
            {
                arm              = new ArmFile("ARM7.bin", new DataStream(str, header.Arm7Offset, header.Arm7Size));
                arm.IsArm9       = false;
                arm.EntryAddress = header.Arm7EntryAddress;
                arm.Autoload     = header.Arm7Autoload;
                arm.RamAddress   = header.Arm7RamAddress;
            }

            // Set the unknown tail
            if (isArm9)
            {
                // It's after the ARM9 file.
                str.Seek(header.Arm9Offset + header.Arm9Size, SeekMode.Origin);

                // Read until reachs padding byte
                List <byte> tail = new List <byte>();

                /*byte b = (byte)str.ReadByte();
                 *              while (b != 0xFF) {
                 *  tail.Add(b);
                 *  b = (byte)str.ReadByte();
                 * }*/

                arm.unknownTail = tail.ToArray();
            }

            return(arm);
        }
Beispiel #7
0
        /// <summary>
        /// Create a folder with all the overlays of the same ARM.
        /// </summary>
        /// <param name="str">Stream to read the overlay table.</param>
        /// <param name="header">Header of the current ROM.</param>
        /// <param name="isArm9">If must read overlays from the ARM9 or ARM7.</param>
        /// <param name="listFiles">List with all the files in the ROM.</param>
        /// <returns>Folder with overlays.</returns>
        public static OverlayFolder FromTable(DataStream str, RomHeader header, bool isArm9, GameFile[] listFiles)
        {
            OverlayFolder overlays = new OverlayFolder(isArm9);

            int numOverlays;

            if (isArm9)
            {
                str.Seek(header.Ov9TableOffset, SeekMode.Origin);
                numOverlays = (int)(header.Ov9TableSize / OverlayFile.TableEntrySize);
            }
            else
            {
                str.Seek(header.Ov7TableOffset, SeekMode.Origin);
                numOverlays = (int)(header.Ov7TableSize / OverlayFile.TableEntrySize);
            }

            for (int i = 0; i < numOverlays; i++)
            {
                overlays.AddFile(OverlayFile.FromTable(str, isArm9, listFiles));
            }

            return(overlays);
        }
Beispiel #8
0
        /// <summary>
        /// Write the table of overlays info.
        /// </summary>
        /// <param name="str">Stream to write to.</param>
        /// <param name="header">Header of the ROM to update.</param>
        /// <param name="shiftOffset">Amount of data before the current stream.</param>
        public void WriteTable(DataStream str, RomHeader header, uint shiftOffset)
        {
            long startOffset = str.Position;
            int numOverlays = 0;

            // For each overlay, writes its info
            foreach (GameFile file in this.Files) {
                OverlayFile ov = file as OverlayFile;
                if (ov == null)
                    continue;

                ov.WriteTable(str);
                numOverlays++;
            }

            // Updates RomHeader
            if (this.isArm9) {
                header.Ov9TableSize = (uint)(numOverlays * OverlayFile.TableEntrySize);
                if (numOverlays > 0)
                    header.Ov9TableOffset = (uint)(shiftOffset + startOffset);
                else
                    header.Ov9TableOffset = 0x00;
            } else {
                header.Ov7TableSize = (uint)(numOverlays * OverlayFile.TableEntrySize);
                if (numOverlays > 0)
                    header.Ov7TableOffset = (uint)(shiftOffset + startOffset);
                else
                    header.Ov7TableOffset = 0x00;
            }
        }
Beispiel #9
0
        /// <summary>
        /// Update and write the Arm file
        /// </summary>
        /// <param name="str">Stream to write</param>
        /// <param name="header">Rom header to update.</param>
        /// <param name="shiftOffset">Amount of data before that stream.</param>
        public void UpdateAndWrite(DataStream str, RomHeader header, uint shiftOffset)
        {
            if (this.IsArm9) {
                header.Arm9Autoload     = this.Autoload;
                header.Arm9EntryAddress = this.EntryAddress;
                header.Arm9RamAddress   = this.RamAddress;
                header.Arm9Size         = (uint)this.Length;
                if (this.Length > 0x00)
                    header.Arm9Offset = (uint)(shiftOffset + str.Position);
                else
                    header.Arm9Offset = 0x00;
            } else {
                header.Arm7Autoload     = this.Autoload;
                header.Arm7EntryAddress = this.EntryAddress;
                header.Arm7RamAddress   = this.RamAddress;
                header.Arm7Size         = (uint)this.Length;
                if (this.Length > 0x00)
                    header.Arm7Offset = (uint)(shiftOffset + str.Position);
                else
                    header.Arm7Offset = 0x00;
            }

            // Write the file
            this.Stream.WriteTo(str);

            if (this.IsArm9) {
                // Update encoded size if it was set
                uint encodeSize = this.SearchEncodedSizeAddress();
                if (encodeSize != 0 && encodeSize != 1) {
                    this.Stream.Seek(encodeSize, SeekMode.Origin);
                    if (new DataReader(this.Stream).ReadUInt32() != 0x00) {
                        str.Seek(encodeSize, SeekMode.Origin);
                        new DataWriter(str).Write((uint)(this.Length + this.RamAddress));
                        str.Seek(0, SeekMode.End);
                    }
                }
            }

            // Write the unknown tail
            if (this.unknownTail != null)
                str.Write(this.unknownTail, 0, this.unknownTail.Length);
        }
Beispiel #10
0
        /// <summary>
        /// Create a new ARM file from the info of the ROM header.
        /// </summary>
        /// <param name="str">Stream to read the unknown tail.</param> 
        /// <param name="header">Header of the current ROM.</param>
        /// <param name="romPath">Path to the current ROM.</param>
        /// <param name="isArm9">Indicates if must create an ARM9 or ARM7 file.</param>
        /// <returns>ARM file.</returns>
        public static ArmFile FromStream(DataStream str, RomHeader header, bool isArm9)
        {
            ArmFile arm;

            if (isArm9) {
                arm = new ArmFile("ARM9.bin", new DataStream(str, header.Arm9Offset, header.Arm9Size));
                arm.IsArm9 = true;
                arm.EntryAddress = header.Arm9EntryAddress;
                arm.Autoload     = header.Arm9Autoload;
                arm.RamAddress   = header.Arm9RamAddress;
            } else {
                arm = new ArmFile("ARM7.bin", new DataStream(str, header.Arm7Offset, header.Arm7Size));
                arm.IsArm9 = false;
                arm.EntryAddress = header.Arm7EntryAddress;
                arm.Autoload     = header.Arm7Autoload;
                arm.RamAddress   = header.Arm7RamAddress;
            }

            // Set the unknown tail
            if (isArm9) {
                // It's after the ARM9 file.
                str.Seek(header.Arm9Offset + header.Arm9Size, SeekMode.Origin);

                // Read until reachs padding byte
                List<byte> tail = new List<byte>();
                /*byte b = (byte)str.ReadByte();
                while (b != 0xFF) {
                    tail.Add(b);
                    b = (byte)str.ReadByte();
                }*/

                arm.unknownTail = tail.ToArray();
            }

            return arm;
        }
Beispiel #11
0
        public override void Initialize(GameFile file, params object[] parameters)
        {
            base.Initialize(file, parameters);

            if (parameters.Length == 1)
                this.header = (RomHeader)parameters[0];
        }
Beispiel #12
0
        /// <summary>
        /// Read the internal info of a ROM file.
        /// </summary>
        /// <param name="str">Stream to read from.</param>
        public override void Read(DataStream str)
        {
            // Read header
            str.Seek(0, SeekMode.Origin);
            this.header = new RomHeader();
            this.header.Read(str);

            // Read banner
            var bannerStream = new DataStream(str, this.header.BannerOffset, Banner.Size);
            var bannerFile   = new GameFile("Banner", bannerStream);
            this.banner = new Banner();
            this.banner.Initialize(bannerFile);
            this.banner.Read();

            // Read file system: FAT and FNT
            this.fileSys = new FileSystem();
            this.fileSys.Initialize(null, this.header);
            this.fileSys.Read(str);

            // Assign common tags (they will be assigned recursively)
            this.File.Tags["_Device_"]    = "NDS";
            this.File.Tags["_MakerCode_"] = this.header.MakerCode;
            this.File.Tags["_GameCode_"]  = this.header.GameCode;

            // Get the ROM folders with files and system files.
            this.fileSys.SystemFolder.AddFile(bannerFile);

            this.File.AddFolder(this.fileSys.Root);
            this.File.AddFolder(this.fileSys.SystemFolder);
        }