public static AllSprites FromPsxIso(Stream iso, bool expand)
        {
            if (expand && !DetectExpansionOfPsxIso(iso))
            {
                ExpandPsxIso(iso);
            }

            return(new AllSprites(Context.US_PSX, AllSpriteAttributes.FromPsxIso(iso), SpriteFileLocations.FromPsxIso(iso),
                                  new Sprite[] {
                new WepSprite(Context.US_PSX, WepSprite.Wep.WEP1, "WEP1", new PatcherLib.Iso.PsxIso.KnownPosition(PatcherLib.Iso.PsxIso.Sectors.BATTLE_WEP_SPR, 0, 256 * 256 / 2 + 0x200)),
                new WepSprite(Context.US_PSX, WepSprite.Wep.WEP2, "WEP2", new PatcherLib.Iso.PsxIso.KnownPosition(PatcherLib.Iso.PsxIso.Sectors.BATTLE_WEP_SPR, 0, 256 * 256 / 2 + 0x200)),
                new WepSprite(Context.US_PSX, WepSprite.Wep.EFF1, "EFF1", new PatcherLib.Iso.PsxIso.KnownPosition(PatcherLib.Iso.PsxIso.Sectors.BATTLE_WEP_SPR, 256 * 256 / 2 + 0x200, 256 * 256 / 2 + 0x200)),
                new WepSprite(Context.US_PSX, WepSprite.Wep.EFF2, "EFF2", new PatcherLib.Iso.PsxIso.KnownPosition(PatcherLib.Iso.PsxIso.Sectors.BATTLE_WEP_SPR, 256 * 256 / 2 + 0x200, 256 * 256 / 2 + 0x200)),
                new WepSprite(Context.US_PSX, WepSprite.Wep.EFF1, "TRAP1", new PatcherLib.Iso.PsxIso.KnownPosition(PatcherLib.Iso.PsxIso.Sectors.BATTLE_WEP_SPR, (256 * 256 / 2 + 0x200) * 2, 144 * 256 / 2 + 0x200)),
            }
                                  ));
        }
        public static void ExpandPsxIso(Stream iso)
        {
            byte[] expandedBytes = expandedSectorCount.ToBytes();
            byte[] reverseBytes  = new byte[4] {
                expandedBytes[3], expandedBytes[2], expandedBytes[1], expandedBytes[0]
            };
            PatcherLib.Iso.PsxIso.PatchPsxIso(iso, PatcherLib.Iso.PsxIso.NumberOfSectorsLittleEndian.GetPatchedByteArray(expandedBytes));
            PatcherLib.Iso.PsxIso.PatchPsxIso(iso, PatcherLib.Iso.PsxIso.NumberOfSectorsBigEndian.GetPatchedByteArray(reverseBytes));
            //PatcherLib.Iso.PsxIso.PatchPsxIso( iso,
            //    new PatchedByteArray(
            //        (PatcherLib.Iso.PsxIso.Sectors)22,
            //        0xDC,
            //        new byte[] { 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00 } ) );

            // Build directory entry for /DUMMY
            //iso.Seek(0x203E6500, SeekOrigin.Begin);
            //iso.Write(Properties.Resources.PatchedDummyFolder, 0, Properties.Resources.PatchedDummyFolder.Length);

            // Read old sprites
            var locs = SpriteFileLocations.FromPsxIso(iso);

            byte[][] oldSprites = new byte[NumPsxSprites][];
            for (int i = 0; i < NumPsxSprites; i++)
            {
                var loc = locs[i];
                oldSprites[i] = PatcherLib.Iso.PsxIso.ReadFile(iso, (PatcherLib.Iso.PsxIso.Sectors)loc.Sector, 0, (int)loc.Size);
            }

            Set <string> allowedEntries = new Set <string>(new string[] {
                "\0", "\x01",
                "ARUTE.SEQ;1", "ARUTE.SHP;1",
                "CYOKO.SEQ;1", "CYOKO.SHP;1",
                "EFC_FNT.TIM;1", "EFF1.SEQ;1", "EFF1.SHP;1", "EFF2.SEQ;1",
                "EFF2.SHP;1", "ENTD1.ENT;1", "ENTD2.ENT;1", "ENTD3.ENT;1",
                "ENTD4.ENT;1",

                "KANZEN.SEQ;1", "KANZEN.SHP;1",
                "MON.SEQ;1", "MON.SHP;1",
                "OTHER.SEQ;1", "OTHER.SHP;1", "OTHER.SPR;1", "RUKA.SEQ;1",
                "TYPE1.SEQ;1", "TYPE1.SHP;1", "TYPE2.SEQ;1", "TYPE2.SHP;1",
                "TYPE3.SEQ;1", "TYPE4.SEQ;1", "WEP.SPR;1", "WEP1.SEQ;1",
                "WEP1.SHP;1", "WEP2.SEQ;1", "WEP2.SHP;1", "ZODIAC.BIN;1"
            });

            List <PatcherLib.Iso.DirectoryEntry> battleDir = new List <PatcherLib.Iso.DirectoryEntry>(PatcherLib.Iso.DirectoryEntry.GetPsxBattleEntries(iso));

            byte[] extBytes = battleDir[2].ExtendedBytes;
            System.Diagnostics.Debug.Assert(battleDir.Sub(2).TrueForAll(ent => PatcherLib.Utilities.Utilities.CompareArrays(extBytes, ent.ExtendedBytes)));
            byte[] midBytes = battleDir[2].MiddleBytes;
            System.Diagnostics.Debug.Assert(battleDir.Sub(2).TrueForAll(ent => PatcherLib.Utilities.Utilities.CompareArrays(midBytes, ent.MiddleBytes)));
            battleDir.RemoveAll(dirent => !allowedEntries.Contains(dirent.Filename));

            // Expand length of ISO
            byte[] anchorBytes = new byte[] {
                0x00, 0xFF, 0xFF, 0xFF,
                0xFF, 0xFF, 0xFF, 0xFF,
                0xFF, 0xFF, 0xFF, 0x00
            };
            byte[] sectorBytes = new byte[] {
                0x00, 0x00, 0x08, 0x00,
                0x00, 0x00, 0x08, 0x00
            };
            byte[] endOfFileBytes = new byte[] {
                0x00, 0x00, 0x89, 0x00,
                0x00, 0x00, 0x89, 0x00
            };
            //byte[] sectorBytes = new byte[8];
            //byte[] endOfFileBytes = new byte[8];
            byte[] emptySector = new byte[2328];
            Time   t           = new Time(51, 9, 39);

            for (long l = 0x2040B100; l < 0x20F18D00; l += 2352)
            {
                // write 0x00FFFFFF FFFFFFFF FFFFFF00 MM SS FF 02
                // write 0x00000800 00000800 for sector of file
                // write 0x00008900 00008900 for last sector of file
                iso.Seek(l, SeekOrigin.Begin);
                iso.Write(anchorBytes, 0, anchorBytes.Length);
                iso.Write(t.ToBCD(), 0, 3);
                t = t.AddFrame();
                iso.WriteByte(0x02);
                if ((l - 0x2040B100 + 2352) % 0x12600 != 0)
                {
                    iso.Write(sectorBytes, 0, 8);
                }
                else
                {
                    iso.Write(endOfFileBytes, 0, 8);
                }
                iso.Write(emptySector, 0, 2328);
            }


            // Copy old sprites to new locations
            List <byte> posBytes    = new List <byte>(NumPsxSprites * 8);
            const long  startSector = 0x2040B100 / 2352;

            for (int i = 0; i < NumPsxSprites; i++)
            {
                uint   sector    = (uint)(startSector + i * 65536 / 2048);
                byte[] bytes     = oldSprites[i];
                byte[] realBytes = new byte[65536];
                bytes.CopyTo(realBytes, 0);
                PatcherLib.Iso.PsxIso.PatchPsxIso(iso, new PatchedByteArray((int)sector, 0, realBytes));
                posBytes.AddRange(sector.ToBytes());
                posBytes.AddRange(((uint)realBytes.Length).ToBytes());

                battleDir.Add(new PatcherLib.Iso.DirectoryEntry(sector, 65536, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                                                                string.Format("{0:X2}.SPR;1", i + 1), battleDir[2].ExtendedBytes));
            }

            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_ARLI2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "8D.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_BIBU2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "96.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_BOM2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "88.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_BEHI2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "93.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_DEMON2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "99.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_DORA22_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "95.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_HYOU2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "89.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_IRON5_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "9A_2.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_IRON4_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "9A_3.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_IRON2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "9A_4.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_IRON3_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "9A_5.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_MINOTA2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "91.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_MOL2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "92.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_TORI2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "8E.SP2;1", battleDir[2].ExtendedBytes));
            battleDir.Add(new PatcherLib.Iso.DirectoryEntry(
                              (uint)PatcherLib.Iso.PsxIso.Sectors.BATTLE_URI2_SP2, 32768, DateTime.Now, battleDir[2].GMTOffset, battleDir[2].MiddleBytes,
                              "8F.SP2;1", battleDir[2].ExtendedBytes));

            //"ARLI2.SP2;1",  // 0x8c
            //"BIBU2.SP2;1", // 0x95
            //"BOM2.SP2;1", // 0x87
            //"BEHI2.SP2;1", // 0x92
            //"DEMON2.SP2;1", // 0x98
            //"DORA22.SP2;1", // 0x94
            //"HYOU2.SP2;1",  // 0x88
            //"IRON5.SP2;1",
            //"IRON4.SP2;1",
            //"IRON2.SP2;1",
            //"IRON3.SP2;1",
            //"MINOTA2.SP2;1", // 0x90
            //"MOL2.SP2;1",  // 0x91
            //"TORI2.SP2;1", // 0x8d
            //"UR2.SP2;1", // 0x8e

            battleDir.Sort((a, b) => a.Filename.CompareTo(b.Filename));

            // Patch direntry
            PatcherLib.Iso.DirectoryEntry.WritePsxDirectoryEntries(
                iso,
                PatcherLib.Iso.PsxIso.BattleDirectoryEntrySector,
                PatcherLib.Iso.PsxIso.BattleDirectoryEntryLength,
                battleDir);

            // Erase the dummy directory, just to be sure
            PatcherLib.Iso.DirectoryEntry.WritePsxDirectoryEntries(
                iso,
                PatcherLib.Iso.PsxIso.DummyDirectoryEntrySector,
                PatcherLib.Iso.PsxIso.DummyDirectoryEntryLength,
                new PatcherLib.Iso.DirectoryEntry[0]);

            // Update battle.bin
            PatcherLib.Iso.PsxIso.PatchPsxIso(iso, SpriteFileLocations.SpriteLocationsPosition.GetPatchedByteArray(posBytes.ToArray()));
        }