private void CheckPatches() { List <Color> bgColors = new List <Color>(); List <string> messages = new List <string>(); for (int index = 0; index < clb_Patches.Items.Count; index++) { Color color = clb_Patches.BackColor; StringBuilder sbMessage = new StringBuilder(); object objPatch = clb_Patches.Items[index]; if (objPatch != null) { AsmPatch asmPatch = (AsmPatch)objPatch; int byteArrayIndex = 0; foreach (PatchedByteArray patchedByteArray in asmPatch) { if (byteArrayIndex >= (asmPatch.Count - asmPatch.Variables.Count)) { break; } bool canLoadBytes = true; byte[] bytes = null; try { bytes = patchedByteArray.GetBytes(); } catch (Exception) { canLoadBytes = false; } if (canLoadBytes) { UInt32 ramOffset = 0; try { PsxIso.FileToRamOffsets ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + Enum.GetName(typeof(PsxIso.Sectors), patchedByteArray.Sector)); ramOffset = (UInt32)patchedByteArray.Offset + (UInt32)ftrOffset; } catch (Exception) { } ramOffset = ramOffset | 0x80000000; // KSEG0 bool markedAsData = asmPatch.isDataSectionList[byteArrayIndex]; if (!markedAsData) { ASMCheckResult result = asmUtility.CheckASMFromBytes(bytes, ramOffset, true, false, asmCheckConditions); if (result.IsASM) { if (!string.IsNullOrEmpty(result.ErrorText)) { color = Color.FromArgb(225, 125, 125); sbMessage.Append(result.ErrorText); } } } } byteArrayIndex++; } } bgColors.Add(color); messages.Add(sbMessage.ToString()); } clb_Patches.BackColors = bgColors.ToArray(); patchMessages = messages.ToArray(); }
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; }