Example #1
0
                private static void BuildCharMapFromIso(Stream iso, out GenericCharMap outCharmap, out IList <Glyph> customGlyphs)
                {
                    var rootDirEnt   = DirectoryEntry.GetPsxDirectoryEntries(iso, FFTText.PsxRootDirEntSector, 1);
                    var charMapEntry = rootDirEnt.Find(d => d.Filename == FFTText.PsxCharmapFileName);

                    System.Diagnostics.Debug.Assert(charMapEntry.Sector == FFTText.PsxCharmapSector);
                    var charmapBytes = PsxIso.GetBlock(iso, new PsxIso.KnownPosition((PsxIso.Sectors)FFTText.PsxCharmapSector, 0,
                                                                                     (int)charMapEntry.Size));
                    Dictionary <int, string> myCharmap = new Dictionary <int, string>();

                    using (MemoryStream memStream = new MemoryStream(charmapBytes))
                        using (TextReader reader = new StreamReader(memStream, Encoding.UTF8))
                        {
                            // Get header line
                            reader.ReadLine();
                            string currentLine = string.Empty;
                            while ((currentLine = reader.ReadLine()) != null)
                            {
                                string[] cols  = currentLine.Split('\t');
                                int      index = int.Parse(cols[0], System.Globalization.NumberStyles.HexNumber);
                                myCharmap[index] = cols[1];
                            }
                        }

                    outCharmap   = new NonDefaultCharMap(myCharmap);
                    customGlyphs = null;
                }
Example #2
0
        private void CheckISO(string filepath)
        {
            if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
            {
                pnl_Params.Visible = true;

                using (Stream file = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    if (settings.WorldConditionalsRepoint)
                    {
                        if (context == Context.US_PSX)
                        {
                            int ramOffset        = PsxIso.ReadFile(file, (PsxIso.Sectors)settings.WorldConditionalsPointerSector, settings.WorldConditionalsPointerOffset, 3).ToIntLE();
                            int wldcoreRamOffset = PsxIso.GetRamOffset(PsxIso.Sectors.WORLD_WLDCORE_BIN);
                            int worldRamOffset   = PsxIso.GetRamOffset(PsxIso.Sectors.WORLD_WORLD_BIN);
                            if (ramOffset >= worldRamOffset)
                            {
                                spinner_WorldConditionals_Sector.Value = (int)PsxIso.Sectors.WORLD_WORLD_BIN;
                                spinner_WorldConditionals_Offset.Value = ramOffset - worldRamOffset;
                            }
                            else if (ramOffset >= wldcoreRamOffset)
                            {
                                spinner_WorldConditionals_Sector.Value = (int)PsxIso.Sectors.WORLD_WLDCORE_BIN;
                                spinner_WorldConditionals_Offset.Value = ramOffset - wldcoreRamOffset;
                            }
                        }
                    }
                }

                EnableActionButton();
            }
        }
Example #3
0
        public static IList <DirectoryEntry> GetPsxDirectoryEntries(Stream iso, int sectorOfParentEntry, int numSectors)
        {
            int sector = sectorOfParentEntry;
            int length = numSectors;

            byte[] bytes = PsxIso.GetBlock(iso, new PsxIso.KnownPosition((PatcherLib.Iso.PsxIso.Sectors)sector, 0, length * 2048));
            return(BuildDirectoryEntriesFromBytes(bytes));
        }
Example #4
0
        public static PatchResult PatchPSV(string filename, List <AsmPatch> asmPatches, ASMEncoding.ASMEncodingUtility asmUtility)
        {
            ConflictResolveResult conflictResolveResult = ConflictHelper.ResolveConflicts(asmPatches, asmUtility);

            asmPatches = conflictResolveResult.Patches;

            List <PatchedByteArray> patches = new List <PatchedByteArray>();

            foreach (AsmPatch asmPatch in asmPatches)
            {
                asmPatch.Update(asmUtility);
                foreach (PatchedByteArray innerPatch in asmPatch)
                {
                    patches.Add(innerPatch);
                }
            }

            PatchPsxSaveStateResult patchResult;

            using (BinaryReader reader = new BinaryReader(File.Open(filename, FileMode.Open)))
            {
                patchResult = PatcherLib.Iso.PsxIso.PatchPsxSaveState(reader, patches);
            }

            StringBuilder sbResultMessage = new StringBuilder();

            sbResultMessage.AppendLine("Complete!");
            sbResultMessage.AppendLine();

            if (!string.IsNullOrEmpty(conflictResolveResult.Message))
            {
                sbResultMessage.AppendLine(conflictResolveResult.Message);
                sbResultMessage.AppendLine();
            }

            if (patchResult.UnsupportedFiles.Count > 0)
            {
                sbResultMessage.AppendLine("Files not supported for savestate patching:");
                foreach (PsxIso.Sectors sector in patchResult.UnsupportedFiles)
                {
                    sbResultMessage.AppendFormat("\t{0}{1}", PsxIso.GetSectorName(sector), Environment.NewLine);
                }
                sbResultMessage.AppendLine();
            }
            if (patchResult.AbsentFiles.Count > 0)
            {
                sbResultMessage.AppendLine("Files not present in savestate:");
                foreach (PsxIso.Sectors sector in patchResult.AbsentFiles)
                {
                    sbResultMessage.AppendFormat("\t{0}{1}", PsxIso.GetSectorName(sector), Environment.NewLine);
                }
                sbResultMessage.AppendLine();
            }

            return(new PatchResult(true, sbResultMessage.ToString()));
        }
Example #5
0
        private void GetSectorPairs(Context context)
        {
            _sectorPairs = PsxIso.GetSectorPairs();

            _sectorIndexMap = new Dictionary <Enum, int>();
            for (int index = 0; index < _sectorPairs.Length; index++)
            {
                _sectorIndexMap.Add(_sectorPairs[index].Sector, index);
            }
        }
Example #6
0
 private void UpdateIso(Stream iso)
 {
     if (psxPos != null)
     {
         PsxIso.PatchPsxIso(iso, psxPos.GetPatchedByteArray(ToByteArray()));
     }
     else if (pspPos != null)
     {
         PspIso.ApplyPatch(iso, pspInfo, pspPos.GetPatchedByteArray(ToByteArray()));
     }
 }
Example #7
0
        public static void PatchISO(EntryData entryData, string filepath, Context context, DataHelper dataHelper = null)
        {
            if (!string.IsNullOrEmpty(filepath))
            {
                List <PatchedByteArray> patches = GetISOPatches(entryData, context, dataHelper);

                using (Stream file = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                {
                    PsxIso.PatchPsxIso(file, patches);
                }
            }
        }
Example #8
0
        public static PatchResult PatchISO(string filename, List <AsmPatch> asmPatches, ASMEncoding.ASMEncodingUtility asmUtility)
        {
            ConflictResolveResult conflictResolveResult = ConflictHelper.ResolveConflicts(asmPatches, asmUtility);

            asmPatches = conflictResolveResult.Patches;
            string conflictResolveMessage = conflictResolveResult.Message;

            List <PatchedByteArray> patches = new List <PatchedByteArray>();

            foreach (AsmPatch asmPatch in asmPatches)
            {
                asmPatch.Update(asmUtility);
                foreach (PatchedByteArray innerPatch in asmPatch)
                {
                    patches.Add(innerPatch);

                    if ((asmUtility.EncodingMode == ASMEncodingMode.PSP) && (innerPatch.Sector == (int)PspIso.Sectors.PSP_GAME_SYSDIR_BOOT_BIN))
                    {
                        patches.Add(innerPatch.GetCopyForSector(PspIso.Sectors.PSP_GAME_SYSDIR_EBOOT_BIN));
                    }
                }
            }

            using (Stream file = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                if (asmUtility.EncodingMode == ASMEncoding.ASMEncodingMode.PSX)
                {
                    PsxIso.PatchPsxIso(file, patches);
                }
                else if (asmUtility.EncodingMode == ASMEncoding.ASMEncodingMode.PSP)
                {
                    PspIso.PatchISO(file, patches);
                }
            }

            StringBuilder sbResultMessage = new StringBuilder();

            sbResultMessage.AppendLine("Complete!");
            sbResultMessage.AppendLine();

            if (!string.IsNullOrEmpty(conflictResolveMessage))
            {
                sbResultMessage.AppendLine(conflictResolveMessage);
                sbResultMessage.AppendLine();
            }

            // DEBUG
            //File.WriteAllText("./output.xml", PatchXmlReader.CreatePatchXML(patches), Encoding.UTF8);

            return(new PatchResult(true, sbResultMessage.ToString()));
        }
Example #9
0
        private void btnPatchSaveState_Click(object sender, EventArgs e)
        {
            //Patchbutton copy. Modify to patch byte array right to savestate.
            saveFileDialog1.Filter   = "PSV files (*.psv)|*.psv|All files (*.*)|*.*";
            saveFileDialog1.FileName = string.Empty;

            StringBuilder sbResultMessage = new StringBuilder();

            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                using (BinaryReader reader = new BinaryReader(File.Open(saveFileDialog1.FileName, FileMode.Open)))
                {
                    List <PatchedByteArray> patches = new List <PatchedByteArray>();
                    foreach (AsmPatch asmPatch in clb_Patches.CheckedItems)
                    {
                        //ModifyPatch(asmPatch);
                        asmPatch.Update(asmUtility);
                        foreach (PatchedByteArray innerPatch in asmPatch)
                        {
                            patches.Add(innerPatch);
                        }
                    }

                    PatchPsxSaveStateResult patchResult = PatcherLib.Iso.PsxIso.PatchPsxSaveState(reader, patches);

                    sbResultMessage.AppendLine("Complete!");
                    sbResultMessage.AppendLine();
                    if (patchResult.UnsupportedFiles.Count > 0)
                    {
                        sbResultMessage.AppendLine("Files not supported for savestate patching:");
                        foreach (PsxIso.Sectors sector in patchResult.UnsupportedFiles)
                        {
                            sbResultMessage.AppendFormat("\t{0}{1}", PsxIso.GetSectorName(sector), Environment.NewLine);
                        }
                        sbResultMessage.AppendLine();
                    }
                    if (patchResult.AbsentFiles.Count > 0)
                    {
                        sbResultMessage.AppendLine("Files not present in savestate:");
                        foreach (PsxIso.Sectors sector in patchResult.AbsentFiles)
                        {
                            sbResultMessage.AppendFormat("\t{0}{1}", PsxIso.GetSectorName(sector), Environment.NewLine);
                        }
                        sbResultMessage.AppendLine();
                    }
                }

                PatcherLib.MyMessageBox.Show(this, sbResultMessage.ToString(), "Complete!", MessageBoxButtons.OK);
            }
        }
Example #10
0
        private void btn_Load_Click(object sender, EventArgs e)
        {
            btn_Load.Enabled = false;
            string filepath = txt_ISO.Text;

            if (!string.IsNullOrEmpty(filepath))
            {
                try
                {
                    using (Stream file = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        if (chk_BattleConditionals.Checked)
                        {
                            PsxIso.Sectors battleSector = (PsxIso.Sectors)spinner_BattleConditionals_Sector.Value;
                            int            battleOffset = (int)spinner_BattleConditionals_Offset.Value;
                            int            battleSize   = (int)spinner_BattleConditionals_Size.Value;
                            byte[]         battleBytes  = PsxIso.ReadFile(file, battleSector, battleOffset, battleSize);
                            _entryData.BattleConditionals = _dataHelper.LoadConditionalSetsFromByteArray(CommandType.BattleConditional, battleBytes);
                        }

                        if (chk_WorldConditionals.Checked)
                        {
                            PsxIso.Sectors worldSector = (PsxIso.Sectors)spinner_WorldConditionals_Sector.Value;
                            int            worldOffset = (int)spinner_WorldConditionals_Offset.Value;
                            int            worldSize   = (int)spinner_WorldConditionals_Size.Value;
                            byte[]         worldBytes  = PsxIso.ReadFile(file, worldSector, worldOffset, worldSize);
                            _entryData.WorldConditionals = _dataHelper.LoadConditionalSetsFromByteArray(CommandType.WorldConditional, worldBytes);
                        }

                        if (chk_Events.Checked)
                        {
                            PsxIso.Sectors eventSector = (PsxIso.Sectors)spinner_Events_Sector.Value;
                            int            eventOffset = (int)spinner_Events_Offset.Value;
                            int            eventSize   = (int)spinner_Events_Size.Value;
                            byte[]         eventBytes  = PsxIso.ReadFile(file, eventSector, eventOffset, eventSize);
                            _entryData.Events = _dataHelper.GetEventsFromBytes(eventBytes);
                        }
                    }

                    DialogResult = DialogResult.OK;
                    Close();
                }
                catch (Exception ex)
                {
                    PatcherLib.MyMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);
                    btn_Load.Enabled = true;
                }
            }
        }
Example #11
0
        public static bool DoesPsxIsoHaveDtePatches(System.IO.Stream iso)
        {
            foreach (var pba in psxDtePatches)
            {
                byte[] patchBytes  = pba.GetBytes();
                byte[] streambytes =
                    PsxIso.GetBlock(iso, new PsxIso.KnownPosition((PsxIso.Sectors)pba.Sector, (int)pba.Offset, patchBytes.Length));
                if (!Utilities.CompareArrays(patchBytes, streambytes))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #12
0
        public static uint GetRamOffsetUnsigned(Enum sector, Context context, bool useKSeg0 = true)
        {
            Type type = sector.GetType();

            if (type == typeof(PsxIso.Sectors))
            {
                return(PsxIso.GetRamOffset((PsxIso.Sectors)sector, useKSeg0));
            }
            else if (type == typeof(PspIso.Sectors))
            {
                return(PspIso.GetRamOffsetUnsigned((PspIso.Sectors)sector));
            }
            else
            {
                return(0);
            }
        }
Example #13
0
        public static int GetRamOffset(Enum sector, Context context)
        {
            Type type = sector.GetType();

            if (type == typeof(PsxIso.Sectors))
            {
                return(PsxIso.GetRamOffset((PsxIso.Sectors)sector));
            }
            else if (type == typeof(PspIso.Sectors))
            {
                return(PspIso.GetRamOffset((PspIso.Sectors)sector));
            }
            else
            {
                return(0);
            }
        }
Example #14
0
                public static void GetCharMap(Stream iso, out GenericCharMap outCharmap, out IList <Glyph> customGlyphs)
                {
                    var matchBytes = Encoding.UTF8.GetBytes(FFTText.CharmapHeader);
                    var isoBytes   = PsxIso.GetBlock(iso, new PsxIso.KnownPosition((PsxIso.Sectors)FFTText.PsxCharmapSector, 0,
                                                                                   matchBytes.Length));

                    if (Utilities.CompareArrays(matchBytes, isoBytes))
                    {
                        BuildCharMapFromIso(iso, out outCharmap, out customGlyphs);
                    }
                    else
                    {
                        IList <byte> dteBytes = PsxIso.ReadFile(iso, DTE.PsxDteTable);
                        outCharmap   = GetCharMap(dteBytes);
                        customGlyphs = null;
                    }
                }
Example #15
0
        public static string GetSectorName(Enum sector)
        {
            Type type = sector.GetType();

            if (type == typeof(PsxIso.Sectors))
            {
                return(PsxIso.GetSectorName((PsxIso.Sectors)sector));
            }
            else if (type == typeof(PspIso.Sectors))
            {
                return(PspIso.GetSectorName((PspIso.Sectors)sector));
            }
            else if (type == typeof(FFTPack.Files))
            {
                return(PspIso.GetFileName((FFTPack.Files)sector));
            }
            else
            {
                return(string.Empty);
            }
        }
Example #16
0
        public void RestoreFile(System.IO.Stream iso)
        {
            IList <byte> bytes = null;

            if (Layout.Context == Context.US_PSX)
            {
                KeyValuePair <Enum, int> sect = Layout.Sectors[SectorType.Sector][0];
                bytes = PsxIso.ReadFile(iso, (PsxIso.Sectors)sect.Key, sect.Value, Layout.Size);
            }
            else if (Layout.Context == Context.US_PSP)
            {
                PatcherLib.Iso.PspIso.PspIsoInfo info = PatcherLib.Iso.PspIso.PspIsoInfo.GetPspIsoInfo(iso);
                if (Layout.Sectors.ContainsKey(SectorType.BootBin))
                {
                    KeyValuePair <Enum, int> sect = Layout.Sectors[SectorType.BootBin][0];
                    bytes = PspIso.GetFile(iso, info, (PspIso.Sectors)sect.Key, sect.Value, Layout.Size);
                }
                else if (Layout.Sectors.ContainsKey(SectorType.FFTPack))
                {
                    KeyValuePair <Enum, int> sect = Layout.Sectors[SectorType.FFTPack][0];
                    bytes = PspIso.GetFile(iso, info, (FFTPack.Files)sect.Key, sect.Value, Layout.Size);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
            AbstractFile tempFile = ConstructFile(
                Layout.FileType,
                CharMap,
                Layout,
                bytes);

            this.Sections = tempFile.Sections;
        }
Example #17
0
        public static bool DoesPsxIsoHaveDtePatches(System.IO.Stream iso)
        {
            var matchBytes = Encoding.UTF8.GetBytes(FFTText.CharmapHeader);
            var isoBytes   = PsxIso.GetBlock(iso, new PsxIso.KnownPosition((PsxIso.Sectors)FFTText.PsxCharmapSector, 0,
                                                                           matchBytes.Length));

            if (Utilities.CompareArrays(matchBytes, isoBytes))
            {
                return(true);
            }

            foreach (var pba in psxDtePatches)
            {
                byte[] patchBytes  = pba.GetBytes();
                byte[] streambytes =
                    PsxIso.GetBlock(iso, new PsxIso.KnownPosition((PsxIso.Sectors)pba.Sector, (int)pba.Offset, patchBytes.Length));
                if (!Utilities.CompareArrays(patchBytes, streambytes))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #18
0
                public static GenericCharMap GetCharMap(Stream iso)
                {
                    IList <byte> dteBytes = PsxIso.ReadFile(iso, DTE.PsxDteTable);

                    return(GetCharMap(dteBytes));
                }
Example #19
0
        public static void WritePsxDirectoryEntries(Stream iso, int sector, int numSectors, IList <DirectoryEntry> entries)
        {
            var patches = GetPsxDirectoryEntryPatches(sector, numSectors, entries);

            patches.ForEach(p => PsxIso.PatchPsxIso(iso, patches));
        }
Example #20
0
        private void btn_Patch_Click(object sender, EventArgs e)
        {
            btn_Patch.Enabled = false;
            string filepath = txt_ISO.Text;

            if (!string.IsNullOrEmpty(filepath))
            {
                List <PatchedByteArray> patches = new List <PatchedByteArray>();

                if (chk_BattleConditionals.Checked)
                {
                    PsxIso.Sectors battleSector = (PsxIso.Sectors)spinner_BattleConditionals_Sector.Value;
                    int            battleOffset = (int)spinner_BattleConditionals_Offset.Value;
                    byte[]         battleBytes  = _dataHelper.ConditionalSetsToByteArray(CommandType.BattleConditional, _entryData.BattleConditionals);
                    patches.Add(new PatchedByteArray(battleSector, battleOffset, battleBytes));

                    if ((settings.BattleConditionalsApplyLimitPatch) && (DataHelper.GetMaxBlocks(_entryData.BattleConditionals) > 10))
                    {
                        patches.Add(new PatchedByteArray(settings.BattleConditionalsLimitPatchSector, settings.BattleConditionalsLimitPatchOffset, settings.BattleConditionalsLimitPatchBytes));
                    }
                }

                if (chk_WorldConditionals.Checked)
                {
                    PsxIso.Sectors worldSector = (PsxIso.Sectors)spinner_WorldConditionals_Sector.Value;
                    int            worldOffset = (int)spinner_WorldConditionals_Offset.Value;
                    byte[]         worldBytes  = _dataHelper.ConditionalSetsToByteArray(CommandType.WorldConditional, _entryData.WorldConditionals);
                    patches.Add(new PatchedByteArray(worldSector, worldOffset, worldBytes));

                    if (settings.WorldConditionalsRepoint)
                    {
                        byte[] patchBytes = (((uint)(PsxIso.GetRamOffset(worldSector) + worldOffset)) | PsxIso.KSeg0Mask).ToBytes();
                        patches.Add(new PatchedByteArray(settings.WorldConditionalsPointerSector, settings.WorldConditionalsPointerOffset, patchBytes));
                    }
                }

                if (chk_Events.Checked)
                {
                    PsxIso.Sectors eventSector = (PsxIso.Sectors)spinner_Events_Sector.Value;
                    int            eventOffset = (int)spinner_Events_Offset.Value;
                    byte[]         eventBytes  = _dataHelper.EventsToByteArray(_entryData.Events);
                    patches.Add(new PatchedByteArray(eventSector, eventOffset, eventBytes));
                }

                try
                {
                    using (Stream file = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                    {
                        PsxIso.PatchPsxIso(file, patches);
                    }

                    DialogResult = DialogResult.OK;
                    Close();
                }
                catch (Exception ex)
                {
                    PatcherLib.MyMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);
                    btn_Patch.Enabled = true;
                }
            }
        }
Example #21
0
        private void CheckStateFile(string filepath)
        {
            try
            {
                using (BinaryReader reader = new BinaryReader(File.Open(filepath, FileMode.Open)))
                {
                    Stream stream = reader.BaseStream;

                    pnl_Battle.Visible = false;
                    pnl_World.Visible  = false;

                    if (PsxIso.IsSectorInPsxSaveState(stream, PsxIso.Sectors.BATTLE_BIN))
                    {
                        pnl_Battle.Visible = true;
                        EnableActionButton();

                        spinner_BattleConditionals_RamLocation_Blocks.Value   = settings.BattleConditionalBlockOffsetsRAMLocation;
                        spinner_BattleConditionals_RamLocation_Commands.Value = settings.BattleConditionalsRAMLocation;
                        spinner_Event_RamLocation.Value = settings.EventRAMLocation;

                        chk_BattleConditionals.Checked = (PsxIso.LoadFromPsxSaveState(reader, (uint)settings.BattleConditionalBlockOffsetsRAMLocation, 4).ToIntLE() != 0);
                        chk_Event.Checked = (PsxIso.LoadFromPsxSaveState(reader, (uint)settings.EventRAMLocation, 1).ToIntLE() != 0);

                        int loadedEventID = PsxIso.LoadFromPsxSaveState(reader, (uint)settings.EventIDRAMLocation, 2).ToIntLE();
                        cmb_Event.SelectedIndex = ((loadedEventID >= 0) && (loadedEventID < _entryData.Events.Count)) ? loadedEventID : ((_selectedIndexResult.EventIndex >= 0) ? _selectedIndexResult.EventIndex : 0);

                        int battleConditionalsIndex = 0;
                        if (PsxIso.IsSectorInPsxSaveState(stream, (PsxIso.Sectors)settings.ScenariosSector))
                        {
                            battleConditionalsIndex = PsxIso.LoadFromPsxSaveState(reader, (uint)(settings.ScenariosRAMLocation + (loadedEventID * 24) + 22), 2).ToIntLE();
                        }
                        else
                        {
                            battleConditionalsIndex = (_selectedIndexResult.BattleConditionalIndex >= 0) ? _selectedIndexResult.BattleConditionalIndex : 0;
                        }

                        cmb_BattleConditionals_ConditionalSet.SelectedIndex = battleConditionalsIndex;
                    }
                    else if (PsxIso.IsSectorInPsxSaveState(stream, PsxIso.Sectors.WORLD_WLDCORE_BIN))
                    {
                        pnl_World.Visible = true;
                        EnableActionButton();

                        bool isLoad = (_mode == Mode.Load);
                        lbl_WorldConditionals_Size.Visible     = isLoad;
                        spinner_WorldConditionals_Size.Visible = isLoad;

                        spinner_WorldConditionals_Size.Value        = settings.WorldConditionalsSize;
                        spinner_WorldConditionals_RamLocation.Value = settings.WorldConditionalsRepoint
                            ? PsxIso.LoadFromPsxSaveState(reader, (uint)settings.WorldConditionalsWorkingPointerRAMLocation, 3).ToIntLE()
                            : settings.WorldConditionalsCalcRAMLocation;

                        if (_mode == Mode.Patch)
                        {
                            worldConditionalsBytes = _dataHelper.ConditionalSetsToByteArray(CommandType.WorldConditional, _entryData.WorldConditionals);
                            if (worldConditionalsBytes.Length > settings.WorldConditionalsSize)
                            {
                                chk_WorldConditionals.Checked = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                PatcherLib.MyMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);
            }
        }
Example #22
0
        private void btn_Patch_Click(object sender, EventArgs e)
        {
            btn_Patch.Enabled = false;
            string filepath = txt_File.Text;
            Dictionary <uint, byte[]> ramPatches = new Dictionary <uint, byte[]>();

            if (!string.IsNullOrEmpty(filepath))
            {
                try
                {
                    using (BinaryReader reader = new BinaryReader(File.Open(filepath, FileMode.Open)))
                    {
                        if (pnl_Battle.Visible)
                        {
                            if (chk_BattleConditionals.Checked)
                            {
                                int setIndex = cmb_BattleConditionals_ConditionalSet.SelectedIndex;
                                if (setIndex >= 0)
                                {
                                    uint blockRamOffset   = (uint)spinner_BattleConditionals_RamLocation_Blocks.Value;
                                    uint commandRamOffset = (uint)spinner_BattleConditionals_RamLocation_Commands.Value;

                                    List <byte[]> byteArrays = _dataHelper.ConditionalSetToActiveByteArrays(CommandType.BattleConditional, _entryData.BattleConditionals[setIndex]);
                                    ramPatches.Add(blockRamOffset, byteArrays[0]);
                                    ramPatches.Add(commandRamOffset, byteArrays[1]);

                                    if (settings.BattleConditionalsApplyLimitPatch)
                                    {
                                        int numBlocks = _entryData.BattleConditionals[setIndex].ConditionalBlocks.Count;
                                        if (numBlocks > 10)
                                        {
                                            ramPatches.Add((uint)settings.BattleConditionalsLimitPatchRAMLocation, settings.BattleConditionalsLimitPatchBytes);
                                        }
                                    }
                                }
                            }

                            if (chk_Event.Checked)
                            {
                                int eventIndex = cmb_Event.SelectedIndex;
                                if (eventIndex >= 0)
                                {
                                    uint   eventRamOffset = (uint)spinner_Event_RamLocation.Value;
                                    byte[] eventBytes     = _dataHelper.EventToByteArray(_entryData.Events[eventIndex], true);
                                    ramPatches.Add(eventRamOffset, eventBytes);

                                    uint eventRamOffsetKSeg0 = eventRamOffset | PsxIso.KSeg0Mask;
                                    uint textOffset          = eventBytes.SubLength(0, 4).ToUInt32();
                                    if (textOffset != DataHelper.BlankTextOffsetValue)
                                    {
                                        ramPatches.Add((uint)settings.TextOffsetRAMLocation, (eventRamOffsetKSeg0 + textOffset).ToBytes().ToArray());
                                    }
                                }
                            }
                        }
                        else if (pnl_World.Visible)
                        {
                            if (chk_WorldConditionals.Checked)
                            {
                                uint ramOffset = (uint)spinner_WorldConditionals_RamLocation.Value;
                                ramPatches.Add(ramOffset, worldConditionalsBytes);
                                uint ramOffsetKSeg0 = ramOffset | PsxIso.KSeg0Mask;
                                if ((settings.WorldConditionalsRepoint) && (ramOffsetKSeg0 != PsxIso.LoadFromPsxSaveState(reader, (uint)settings.WorldConditionalsPointerRAMLocation, 4).ToUInt32()))
                                {
                                    ramPatches.Add((uint)settings.WorldConditionalsPointerRAMLocation, ramOffsetKSeg0.ToBytes().ToArray());
                                    ramPatches.Add((uint)settings.WorldConditionalsWorkingPointerRAMLocation, ramOffsetKSeg0.ToBytes().ToArray());
                                }
                            }
                        }

                        PsxIso.PatchPsxSaveState(reader, ramPatches);
                    }

                    DialogResult = DialogResult.OK;
                    Close();
                }
                catch (Exception ex)
                {
                    PatcherLib.MyMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);
                    btn_Patch.Enabled = true;
                }
            }
        }
Example #23
0
        private void btn_Load_Click(object sender, EventArgs e)
        {
            btn_Load.Enabled = false;
            string filepath = txt_File.Text;

            if (!string.IsNullOrEmpty(filepath))
            {
                try
                {
                    using (BinaryReader reader = new BinaryReader(File.Open(filepath, FileMode.Open)))
                    {
                        if (pnl_Battle.Visible)
                        {
                            if (chk_BattleConditionals.Checked)
                            {
                                List <byte[]> byteArrays = PsxIso.LoadFromPsxSaveState(reader, new List <KeyValuePair <uint, int> >()
                                {
                                    new KeyValuePair <uint, int>((uint)spinner_BattleConditionals_RamLocation_Blocks.Value, settings.BattleConditionalBlockOffsetsRAMLength),
                                    new KeyValuePair <uint, int>((uint)spinner_BattleConditionals_RamLocation_Commands.Value, settings.BattleConditionalsRAMLength)
                                });

                                int setIndex = cmb_BattleConditionals_ConditionalSet.SelectedIndex;
                                if (setIndex >= 0)
                                {
                                    _entryData.BattleConditionals[setIndex] = _dataHelper.LoadActiveConditionalSet(setIndex, _entryData.BattleConditionals[setIndex].Name,
                                                                                                                   CommandType.BattleConditional, byteArrays[0], byteArrays[1]);
                                }
                            }

                            if (chk_Event.Checked)
                            {
                                int eventIndex = cmb_Event.SelectedIndex;
                                if (eventIndex >= 0)
                                {
                                    int    eventRamLocation = (int)spinner_Event_RamLocation.Value;
                                    byte[] eventBytes       = PsxIso.LoadFromPsxSaveState(reader, (uint)eventRamLocation, settings.EventSize);

                                    if (eventBytes.SubLength(0, 4).ToUInt32() == DataHelper.BlankTextOffsetValue)
                                    {
                                        int textRamLocation = PsxIso.LoadFromPsxSaveState(reader, (uint)settings.TextOffsetRAMLocation, 3).ToIntLE();
                                        int textOffset      = textRamLocation - eventRamLocation;

                                        if (textOffset < settings.EventSize)
                                        {
                                            byte[] textOffsetBytes = ((uint)textOffset).ToBytes();
                                            Array.Copy(textOffsetBytes, 0, eventBytes, 0, 4);
                                        }
                                    }

                                    _entryData.Events[eventIndex] = _dataHelper.GetEventFromBytes(eventIndex, eventBytes, true);
                                }
                            }
                        }
                        else if (pnl_World.Visible)
                        {
                            if (chk_WorldConditionals.Checked)
                            {
                                byte[] bytes = PsxIso.LoadFromPsxSaveState(reader, (uint)spinner_WorldConditionals_RamLocation.Value, (int)spinner_WorldConditionals_Size.Value);
                                _entryData.WorldConditionals = _dataHelper.LoadConditionalSetsFromByteArray(CommandType.WorldConditional, bytes);
                            }
                        }
                    }

                    DialogResult = DialogResult.OK;
                    Close();
                }
                catch (Exception ex)
                {
                    PatcherLib.MyMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);
                    btn_Load.Enabled = true;
                }
            }
        }
Example #24
0
        public static void PatchPsxSaveState(EntryData entryData, string filepath, DataHelper dataHelper)
        {
            SettingsData settings = Settings.PSX;

            Dictionary <uint, byte[]> ramPatches = new Dictionary <uint, byte[]>();
            bool isBattleLoaded         = false;
            bool isWorldLoaded          = false;
            bool saveBattleConditionals = false;
            bool saveWorldConditionals  = false;
            bool saveEvent = false;

            byte[] worldConditionalsBytes  = null;
            int    battleConditionalsIndex = 0;
            int    eventID = 0;

            int worldConditionalsRamLocation = 0;

            if (!string.IsNullOrEmpty(filepath))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(filepath, FileMode.Open)))
                {
                    Stream stream = reader.BaseStream;

                    if (PsxIso.IsSectorInPsxSaveState(stream, PsxIso.Sectors.BATTLE_BIN))
                    {
                        isBattleLoaded = true;

                        saveBattleConditionals = (PsxIso.LoadFromPsxSaveState(reader, (uint)settings.BattleConditionalBlockOffsetsRAMLocation, 4).ToIntLE() != 0);
                        saveEvent = (PsxIso.LoadFromPsxSaveState(reader, (uint)settings.EventRAMLocation, 1).ToIntLE() != 0);

                        eventID = PsxIso.LoadFromPsxSaveState(reader, (uint)settings.EventIDRAMLocation, 2).ToIntLE();
                        if (!((eventID >= 0) && (eventID < entryData.Events.Count)))
                        {
                            saveEvent = false;
                        }

                        if (PsxIso.IsSectorInPsxSaveState(stream, (PsxIso.Sectors)settings.ScenariosSector))
                        {
                            battleConditionalsIndex = PsxIso.LoadFromPsxSaveState(reader, (uint)(settings.ScenariosRAMLocation + (eventID * 24) + 22), 2).ToIntLE();
                        }
                        else
                        {
                            saveBattleConditionals = false;
                        }
                    }
                    else if (PsxIso.IsSectorInPsxSaveState(stream, PsxIso.Sectors.WORLD_WLDCORE_BIN))
                    {
                        isWorldLoaded = true;

                        worldConditionalsRamLocation = settings.WorldConditionalsRepoint
                            ? PsxIso.LoadFromPsxSaveState(reader, (uint)settings.WorldConditionalsWorkingPointerRAMLocation, 3).ToIntLE()
                            : settings.WorldConditionalsCalcRAMLocation;

                        worldConditionalsBytes = dataHelper.ConditionalSetsToByteArray(CommandType.WorldConditional, entryData.WorldConditionals);
                        if (worldConditionalsBytes.Length > settings.WorldConditionalsSize)
                        {
                            saveWorldConditionals = false;
                        }
                    }
                }

                using (BinaryReader reader = new BinaryReader(File.Open(filepath, FileMode.Open)))
                {
                    if (isBattleLoaded)
                    {
                        if (saveBattleConditionals)
                        {
                            int setIndex = battleConditionalsIndex;
                            if (setIndex >= 0)
                            {
                                uint blockRamOffset   = (uint)settings.BattleConditionalBlockOffsetsRAMLocation;
                                uint commandRamOffset = (uint)settings.BattleConditionalsRAMLocation;

                                List <byte[]> byteArrays = dataHelper.ConditionalSetToActiveByteArrays(CommandType.BattleConditional, entryData.BattleConditionals[setIndex]);
                                ramPatches.Add(blockRamOffset, byteArrays[0]);
                                ramPatches.Add(commandRamOffset, byteArrays[1]);

                                if (settings.BattleConditionalsApplyLimitPatch)
                                {
                                    int numBlocks = entryData.BattleConditionals[setIndex].ConditionalBlocks.Count;
                                    if (numBlocks > 10)
                                    {
                                        ramPatches.Add((uint)settings.BattleConditionalsLimitPatchRAMLocation, settings.BattleConditionalsLimitPatchBytes);
                                    }
                                }
                            }
                        }

                        if (saveEvent)
                        {
                            int eventIndex = eventID;
                            if (eventIndex >= 0)
                            {
                                uint   eventRamOffset = (uint)settings.EventRAMLocation;
                                byte[] eventBytes     = dataHelper.EventToByteArray(entryData.Events[eventIndex], true);
                                ramPatches.Add(eventRamOffset, eventBytes);

                                uint eventRamOffsetKSeg0 = eventRamOffset | PsxIso.KSeg0Mask;
                                uint textOffset          = eventBytes.SubLength(0, 4).ToUInt32();
                                if (textOffset != DataHelper.BlankTextOffsetValue)
                                {
                                    ramPatches.Add((uint)settings.TextOffsetRAMLocation, (eventRamOffsetKSeg0 + textOffset).ToBytes().ToArray());
                                }
                            }
                        }
                    }
                    else if (isWorldLoaded)
                    {
                        if (saveWorldConditionals)
                        {
                            uint ramOffset = (uint)worldConditionalsRamLocation;
                            ramPatches.Add(ramOffset, worldConditionalsBytes);
                            uint ramOffsetKSeg0 = ramOffset | PsxIso.KSeg0Mask;
                            if ((settings.WorldConditionalsRepoint) && (ramOffsetKSeg0 != PsxIso.LoadFromPsxSaveState(reader, (uint)settings.WorldConditionalsPointerRAMLocation, 4).ToUInt32()))
                            {
                                ramPatches.Add((uint)settings.WorldConditionalsPointerRAMLocation, ramOffsetKSeg0.ToBytes().ToArray());
                                ramPatches.Add((uint)settings.WorldConditionalsWorkingPointerRAMLocation, ramOffsetKSeg0.ToBytes().ToArray());
                            }
                        }
                    }

                    PsxIso.PatchPsxSaveState(reader, ramPatches);
                }
            }
        }