public static List <PatchedByteArray> GetISOPatches(EntryData entryData, Context context, DataHelper dataHelper = null) { dataHelper = dataHelper ?? new DataHelper(context); List <PatchedByteArray> patches = new List <PatchedByteArray>(); SettingsData settings = Settings.GetSettings(context); Enum battleSector = settings.BattleConditionalsSector; int battleOffset = settings.BattleConditionalsOffset; 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)); } Enum worldSector = settings.WorldConditionalsSector; int worldOffset = settings.WorldConditionalsOffset; byte[] worldBytes = dataHelper.ConditionalSetsToByteArray(CommandType.WorldConditional, entryData.WorldConditionals); patches.Add(new PatchedByteArray(worldSector, worldOffset, worldBytes)); if (settings.WorldConditionalsRepoint) { byte[] patchBytes = (((uint)(ISOHelper.GetRamOffsetUnsigned(worldSector, context, true) + worldOffset))).ToBytes(); patches.Add(new PatchedByteArray(settings.WorldConditionalsPointerSector, settings.WorldConditionalsPointerOffset, patchBytes)); } Enum eventSector = settings.EventsSector; int eventOffset = settings.EventsOffset; byte[] eventBytes = dataHelper.EventsToByteArray(entryData.Events); patches.Add(new PatchedByteArray(eventSector, eventOffset, eventBytes)); return(patches); }
private void menuItem_CheckSize_Click(object sender, EventArgs e) { SettingsData settings = Settings.GetSettings(_context); if (menuItem_View.Enabled) { if (tabControl.SelectedTab == tabPage_BattleConditionals) { battleConditionalSetsEditor.SaveBlock(); byte[] bytes = _dataHelper.ConditionalSetsToByteArray(CommandType.BattleConditional, _entryData.BattleConditionals); ConditionalSet selectedConditionalSet = battleConditionalSetsEditor.CopyConditionalSet(); StringBuilder sb = new StringBuilder(); sb.AppendFormat("All Battle Conditionals Size: {0} / {1} bytes{2}", bytes.Length, settings.BattleConditionalsSize, Environment.NewLine); if (selectedConditionalSet != null) { int maxCommands = _dataHelper.BattleConditionalSetMaxCommands; sb.AppendFormat("{0} {1}: {2} / {3} commands{4}", selectedConditionalSet.Index.ToString("X2"), selectedConditionalSet.Name, selectedConditionalSet.GetNumCommands(), maxCommands, Environment.NewLine); bool hasInvalidSets = false; int highestCommandTotal = 0; int highestCommandIndex = 0; for (int index = 0; index < _entryData.BattleConditionals.Count; index++) { ConditionalSet conditionalSet = _entryData.BattleConditionals[index]; int numSetCommands = conditionalSet.GetNumCommands(); if (highestCommandTotal < numSetCommands) { highestCommandTotal = numSetCommands; highestCommandIndex = index; } if (numSetCommands > maxCommands) { hasInvalidSets = true; sb.AppendFormat("{0} {1}: {2} / {3} commands{4}", conditionalSet.Index.ToString("X2"), conditionalSet.Name, numSetCommands, maxCommands, Environment.NewLine); } } if (!hasInvalidSets) { ConditionalSet conditionalSet = _entryData.BattleConditionals[highestCommandIndex]; sb.AppendFormat("Largest set: {0} {1}: {2} / {3} commands{4}", conditionalSet.Index.ToString("X2"), conditionalSet.Name, highestCommandTotal, maxCommands, Environment.NewLine); } } PatcherLib.MyMessageBox.Show(this, sb.ToString(), "Size", MessageBoxButtons.OK); } else if (tabControl.SelectedTab == tabPage_WorldConditionals) { worldConditionalSetsEditor.SaveBlock(); byte[] bytes = _dataHelper.ConditionalSetsToByteArray(CommandType.WorldConditional, _entryData.WorldConditionals); PatcherLib.MyMessageBox.Show(this, string.Format("All World Conditionals Size: {0} / {1} bytes", bytes.Length, settings.WorldConditionalsSize), "Size", MessageBoxButtons.OK); } else if (tabControl.SelectedTab == tabPage_Events) { eventsEditor.SavePage(); Event selectedEvent = eventsEditor.CopyEvent(); byte[] bytes = _dataHelper.EventToByteArray(selectedEvent, false); int maxEventBytes = _dataHelper.EventSize; StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0} {1}: {2} / {3} bytes{4}", selectedEvent.Index.ToString("X4"), selectedEvent.Name, bytes.Length, settings.EventSize, Environment.NewLine); bool hasInvalidEvents = false; int highestByteTotal = 0; int highestByteIndex = 0; for (int index = 0; index < _entryData.Events.Count; index++) { Event ev = _entryData.Events[index]; ev.TextSection.DecodeText(); byte[] eventBytes = _dataHelper.EventToByteArray(ev, false); int numEventBytes = eventBytes.Length; if (highestByteTotal < numEventBytes) { highestByteTotal = numEventBytes; highestByteIndex = index; } if (numEventBytes > maxEventBytes) { hasInvalidEvents = true; sb.AppendFormat("{0} {1}: {2} / {3} bytes{4}", ev.Index.ToString("X4"), ev.Name, numEventBytes, maxEventBytes, Environment.NewLine); } } if (!hasInvalidEvents) { Event ev = _entryData.Events[highestByteIndex]; sb.AppendFormat("Largest event: {0} {1}: {2} / {3} bytes{4}", ev.Index.ToString("X4"), ev.Name, highestByteTotal, maxEventBytes, Environment.NewLine); } PatcherLib.MyMessageBox.Show(this, sb.ToString(), "Size", MessageBoxButtons.OK); } } }
private void PopulateWorldTab() { SettingsData settings = Settings.GetSettings(_context); worldConditionalSetsEditor.Populate(_entryData.WorldConditionals, _entryDataDefault.WorldConditionals, _commandDataMap[CommandType.WorldConditional], settings.IgnoreParameterTypes); }
private void PopulateBattleTab() { SettingsData settings = Settings.GetSettings(_context); battleConditionalSetsEditor.Populate(_entryData.BattleConditionals, _entryDataDefault.BattleConditionals, _commandDataMap[CommandType.BattleConditional], settings.IgnoreParameterTypes, _dataHelper.BattleConditionalSetMaxBlocks); }
public static SettingsData LoadSettings(XmlNode settingsNode, Context context) { SettingsData data = new SettingsData(); data.Context = context; PropertyInfo[] properties = typeof(SettingsData).GetProperties(); foreach (PropertyInfo property in properties) { Type type = property.PropertyType; string xpath = "add[@key='" + property.Name + "']"; XmlNode node = settingsNode.SelectSingleNode(xpath); if (node != null) { string strValue = node.Attributes["value"].InnerText; object value = 0; bool isSet = true; if (type == typeof(string)) { if (property.Name.ToLower().Trim().StartsWith("filepath")) { value = data.GetFilepath(strValue); } else { value = strValue; } } else if (type == typeof(int)) { value = Utilities.ParseInt(strValue); } else if (type == typeof(bool)) { value = Utilities.ParseBool(strValue); } else if (type == typeof(byte[])) { value = Utilities.GetBytesFromHexString(strValue.Replace("0x", "")); } else if (type == typeof(Enum)) { value = ISOHelper.GetSector(strValue, context); } else { isSet = false; } if (isSet) { property.SetValue(data, value, null); } } } data.TotalEventSize = data.EventSize * data.NumEvents; data.WorldConditionalsPointerRAMLocation = ISOHelper.GetRamOffset(data.WorldConditionalsPointerSector, context) + data.WorldConditionalsPointerOffset; data.BattleConditionalsLimitPatchRAMLocation = ISOHelper.GetRamOffset(data.BattleConditionalsLimitPatchSector, context) + data.BattleConditionalsLimitPatchOffset; data.ScenariosRAMLocation = ISOHelper.GetRamOffset(data.ScenariosSector, context) + data.ScenariosOffset; data.WorldConditionalsCalcRAMLocation = ISOHelper.GetRamOffset(data.WorldConditionalsSector, context) + data.WorldConditionalsOffset; return(data); }
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); } } }