public MainForm() { InitializeComponent(); asmUtility = new ASMEncodingUtility(ASMEncodingMode.PSX); versionLabel.Text = string.Format( "v0.{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision.ToString() ); ReloadFiles(); patchButton.Click += new EventHandler( patchButton_Click ); reloadButton.Click += new EventHandler( reloadButton_Click ); clb_Patches.ItemCheck += new ItemCheckEventHandler( clb_Patches_ItemCheck ); patchButton.Enabled = false; clb_Patches.SelectedIndexChanged += new EventHandler( clb_Patches_SelectedIndexChanged ); variableSpinner.ValueChanged += new EventHandler( variableSpinner_ValueChanged ); variableComboBox.SelectedIndexChanged += new EventHandler(variableComboBox_SelectedIndexChanged); }
// Invoked when the form is initialized. public void Process() { _asmUtility = new ASMEncodingUtility(); _asmUtility.EncodingMode = ASMEncodingMode.PSX; // Set starting form state chk_LittleEndian.Checked = true; Timer timer = new Timer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = 10; timer.Enabled = true; timer.Start(); Icon = SystemIcons.Information; cb_Mode.Items.Add("Base"); cb_Mode.Items.Add("PSX"); cb_Mode.Items.Add("PSP"); cb_Mode.SelectedIndex = ASMEncodingMode.PSX; }
public void InitASMUtility() { _asmUtility = new ASMEncodingUtility(); _asmUtility.EncodingMode = ASMEncodingMode.PSX; }
public static IList<AsmPatch> GetPatches( XmlNode rootNode, string xmlFilename, ASMEncodingUtility asmUtility ) { XmlNodeList patchNodes = rootNode.SelectNodes( "Patch" ); List<AsmPatch> result = new List<AsmPatch>( patchNodes.Count ); foreach ( XmlNode node in patchNodes ) { XmlAttribute ignoreNode = node.Attributes["ignore"]; if ( ignoreNode != null && Boolean.Parse( ignoreNode.InnerText ) ) continue; string name; string description; IList<PatchedByteArray> staticPatches; List<bool> isDataSectionList; GetPatch(node, xmlFilename, asmUtility, out name, out description, out staticPatches, out isDataSectionList); List<VariableType> variables = new List<VariableType>(); foreach ( XmlNode varNode in node.SelectNodes( "Variable" ) ) { XmlAttribute xmlAttr = varNode.Attributes["bytes"]; string strBytes = (xmlAttr == null) ? "1" : xmlAttr.InnerText; char bytes = (char)(UInt32.Parse(strBytes) & 0xff); string varName = varNode.Attributes["name"].InnerText; XmlAttribute fileAttribute = varNode.Attributes["file"]; XmlAttribute sectorAttribute = varNode.Attributes["sector"]; PsxIso.Sectors varSec = (PsxIso.Sectors)0; if (fileAttribute != null) { varSec = (PsxIso.Sectors)Enum.Parse(typeof(PsxIso.Sectors), fileAttribute.InnerText); } else if (sectorAttribute != null) { varSec = (PsxIso.Sectors)Int32.Parse(sectorAttribute.InnerText, System.Globalization.NumberStyles.HexNumber); } else { throw new Exception(); } //PsxIso.Sectors varSec = (PsxIso.Sectors)Enum.Parse( typeof( PsxIso.Sectors ), varNode.Attributes["file"].InnerText ); UInt32 varOffset = UInt32.Parse( varNode.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber ); XmlAttribute defaultAttr = varNode.Attributes["default"]; Byte[] byteArray = new Byte[bytes]; UInt32 def = 0; if ( defaultAttr != null ) { def = UInt32.Parse( defaultAttr.InnerText, System.Globalization.NumberStyles.HexNumber ); for (int i=0; i < bytes; i++) { byteArray[i] = (Byte)((def >> (i * 8)) & 0xff); } } KeyValuePair<string, PatchedByteArray> kvp = new KeyValuePair<string, PatchedByteArray>( varName, new PatchedByteArray( varSec, varOffset, byteArray ) ); VariableType vType = new VariableType(); vType.content = kvp; vType.bytes = bytes; variables.Add( vType ); } result.Add( new AsmPatch( name, description, staticPatches, isDataSectionList, variables ) ); } patchNodes = rootNode.SelectNodes( "ImportFilePatch" ); foreach ( XmlNode node in patchNodes ) { string name; string description; GetPatchNameAndDescription(node, out name, out description); XmlNodeList fileNodes = node.SelectNodes( "ImportFile" ); if ( fileNodes.Count != 1 ) continue; XmlNode theRealNode = fileNodes[0]; PsxIso.Sectors sector = (PsxIso.Sectors)Enum.Parse( typeof( PsxIso.Sectors ), theRealNode.Attributes["file"].InnerText ); UInt32 offset = UInt32.Parse( theRealNode.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber ); UInt32 expectedLength = UInt32.Parse( theRealNode.Attributes["expectedLength"].InnerText, System.Globalization.NumberStyles.HexNumber ); result.Add( new FileAsmPatch( name, description, new InputFilePatch( sector, offset, expectedLength ) ) ); } return result.AsReadOnly(); }
private static void GetPatch( XmlNode node, string xmlFileName, ASMEncodingUtility asmUtility, out string name, out string description, out IList<PatchedByteArray> staticPatches, out List<bool> outDataSectionList) { GetPatchNameAndDescription( node, out name, out description ); XmlNodeList currentLocs = node.SelectNodes( "Location" ); List<PatchedByteArray> patches = new List<PatchedByteArray>( currentLocs.Count ); List<bool> isDataSectionList = new List<bool>( currentLocs.Count ); foreach ( XmlNode location in currentLocs ) { UInt32 offset = UInt32.Parse( location.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber ); XmlAttribute fileAttribute = location.Attributes["file"]; XmlAttribute sectorAttribute = location.Attributes["sector"]; XmlAttribute modeAttribute = location.Attributes["mode"]; XmlAttribute offsetModeAttribute = location.Attributes["offsetMode"]; XmlAttribute inputFileAttribute = location.Attributes["inputFile"]; XmlAttribute replaceLabelsAttribute = location.Attributes["replaceLabels"]; PsxIso.Sectors sector = (PsxIso.Sectors)0; if (fileAttribute != null) { sector = (PsxIso.Sectors)Enum.Parse( typeof( PsxIso.Sectors ), fileAttribute.InnerText ); } else if (sectorAttribute != null) { sector = (PsxIso.Sectors)Int32.Parse( sectorAttribute.InnerText, System.Globalization.NumberStyles.HexNumber ); } else { throw new Exception(); } bool asmMode = false; bool markedAsData = false; if (modeAttribute != null) { string modeAttributeText = modeAttribute.InnerText.ToLower().Trim(); if (modeAttributeText == "asm") { asmMode = true; } else if (modeAttributeText == "data") { markedAsData = true; } } bool isRamOffset = false; if (offsetModeAttribute != null) { if (offsetModeAttribute.InnerText.ToLower().Trim() == "ram") isRamOffset = true; } UInt32 ramOffset = offset; UInt32 fileOffset = offset; try { PsxIso.FileToRamOffsets ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + fileAttribute.InnerText); if (isRamOffset) fileOffset -= (UInt32)ftrOffset; else ramOffset += (UInt32)ftrOffset; } catch (Exception) { } ramOffset = ramOffset | 0x80000000; // KSEG0 string content = location.InnerText; if (inputFileAttribute != null) { using (StreamReader streamReader = new StreamReader(inputFileAttribute.InnerText, Encoding.UTF8)) { content = streamReader.ReadToEnd(); } } bool replaceLabels = false; if (replaceLabelsAttribute != null) { if (replaceLabelsAttribute.InnerText.ToLower().Trim() == "true") replaceLabels = true; } if (replaceLabels) { content = asmUtility.ReplaceLabelsInHex(content, true); } byte[] bytes; if (asmMode) { bytes = asmUtility.EncodeASM(content, ramOffset).EncodedBytes; } else { bytes = GetBytes( content ); } patches.Add( new PatchedByteArray( sector, fileOffset, bytes ) ); isDataSectionList.Add(markedAsData); } currentLocs = node.SelectNodes("STRLocation"); foreach (XmlNode location in currentLocs) { XmlAttribute fileAttribute = location.Attributes["file"]; XmlAttribute sectorAttribute = location.Attributes["sector"]; PsxIso.Sectors sector = (PsxIso.Sectors)0; if (fileAttribute != null) { sector = (PsxIso.Sectors)Enum.Parse( typeof( PsxIso.Sectors ), fileAttribute.InnerText ); } else if (sectorAttribute != null) { sector = (PsxIso.Sectors)Int32.Parse( sectorAttribute.InnerText, System.Globalization.NumberStyles.HexNumber ); } else { throw new Exception(); } string filename = location.Attributes["input"].InnerText; filename = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( xmlFileName ), filename ); patches.Add(new STRPatchedByteArray(sector, filename)); } staticPatches = patches.AsReadOnly(); outDataSectionList = isDataSectionList; }
public static bool TryGetPatches( string xmlString, string xmlFilename, ASMEncodingUtility asmUtility, out IList<AsmPatch> patches ) { try { XmlDocument doc = new XmlDocument(); doc.LoadXml( xmlString ); patches = GetPatches( doc.SelectSingleNode( "/Patches" ), xmlFilename, asmUtility ); return true; } catch ( Exception ex ) { patches = null; return false; } }
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())); }
public static PatchResult PatchFile(string filename, List <AsmPatch> asmPatches, ASMEncoding.ASMEncodingUtility asmUtility) { string modFilepath = filename.ToLower().Trim(); bool isISO = false; string[] validExtensions = { ".bin", ".iso", ".img" }; foreach (string extension in validExtensions) { if (modFilepath.EndsWith(extension.ToLower().Trim())) { isISO = true; break; } } if (isISO) { return(PatchISO(filename, asmPatches, asmUtility)); } else if ((asmUtility.EncodingMode == ASMEncodingMode.PSX) && (modFilepath.EndsWith(".psv"))) { return(PatchPSV(filename, asmPatches, asmUtility)); } else { return(new PatchResult(false, "Unsupported file!")); } }
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())); }
public PatchList(string[] files, ASMEncodingUtility asmUtility) { FilePatches = new PatchFile[files.Length]; LoadedCorrectly = new bool[files.Length]; IList<AsmPatch> tryPatches; int i = 0; foreach (string file in files) { if (PatchXmlReader.TryGetPatches(File.ReadAllText(file, Encoding.UTF8), file, asmUtility, out tryPatches)) { AllPatches.AddRange(tryPatches); FilePatches[i] = new PatchFile(tryPatches.Count); FilePatches[i].filename = file; FilePatches[i].Patches.AddRange(tryPatches); LoadedCorrectly[i] = true; } else { LoadedCorrectly[i] = false; //MessageBox.Show(file.Substring(file.LastIndexOf("\\")) + " Did not load correctly"); } i++; } AllCheckStates = new CheckState[AllPatches.Count]; for (int j = 0; j < AllCheckStates.Length; j++) { AllCheckStates[j] = new CheckState(); AllCheckStates[j] = CheckState.Unchecked; } }