Ejemplo n.º 1
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;
                }
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }