Example #1
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 #2
0
        private void ShowConflicts(AsmPatch patch)
        {
            List <PatchRangeConflict> conflictList = conflictPatchData.ConflictMap[patch];

            foreach (PatchRangeConflict conflict in conflictList)
            {
                PsxIso.Sectors sector = (PsxIso.Sectors)(conflict.ConflictRange.Sector);
                //string strSector = Enum.GetName(typeof(PsxIso.Sectors), sector);
                string strSector = PatcherLib.Iso.PsxIso.GetSectorName(sector);

                // Patch #, Sector, Location, Conflict Location
                ListViewItem item = new ListViewItem();
                item.Text = conflict.ConflictPatchNumber.ToString();
                item.SubItems.Add(strSector);
                item.SubItems.Add(conflict.Range.StartOffset.ToString("X"));
                item.SubItems.Add(conflict.ConflictRange.StartOffset.ToString("X"));
                item.BackColor = conflict.IsInFreeSpace ? backColor_Conflict_FreeSpace : backColor_Conflict_NonFreeSpace;
                lv_Conflicts.Items.Add(item);
            }
        }
        private static List <SpecificLocation> FillSpecificAttributeData(XmlAttribute attrSpecific, PsxIso.Sectors defaultSector)
        {
            string strSpecificAttr = (attrSpecific != null) ? attrSpecific.InnerText : "";

            string[] strSpecifics             = ASMStringHelper.RemoveSpaces(strSpecificAttr).Split(',');
            List <SpecificLocation> specifics = new List <SpecificLocation>();

            PsxIso.Sectors lastSector = defaultSector;
            if (!string.IsNullOrEmpty(strSpecificAttr))
            {
                foreach (string strSpecific in strSpecifics)
                {
                    string[] strSpecificData = strSpecific.Split(':');

                    string strSector         = "";
                    string strSpecificOffset = strSpecificData[0];
                    if (strSpecificData.Length > 1)
                    {
                        strSector         = strSpecificData[0];
                        strSpecificOffset = strSpecificData[1];
                    }

                    PsxIso.Sectors specificSector;
                    if (string.IsNullOrEmpty(strSector))
                    {
                        specificSector = lastSector;
                    }
                    else
                    {
                        int  sectorNum       = 0;
                        bool isSectorNumeric = int.TryParse(strSector, out sectorNum);
                        specificSector = isSectorNumeric ? (PsxIso.Sectors)sectorNum : (PsxIso.Sectors)Enum.Parse(typeof(PsxIso.Sectors), strSector);
                    }

                    lastSector = specificSector;
                    specifics.Add(new SpecificLocation(specificSector, strSpecificOffset));
                }
            }

            return(specifics);
        }
Example #4
0
        public static IList <AsmPatch> GetPatches(XmlNode rootNode)
        {
            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;
                GetPatch(node, out name, out description, out staticPatches);
                List <KeyValuePair <string, PatchedByteArray> > variables = new List <KeyValuePair <string, PatchedByteArray> >();
                foreach (XmlNode varNode in node.SelectNodes("Variable"))
                {
                    string         varName     = varNode.Attributes["name"].InnerText;
                    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           def         = 0;
                    if (defaultAttr != null)
                    {
                        def = Byte.Parse(defaultAttr.InnerText, System.Globalization.NumberStyles.HexNumber);
                    }

                    variables.Add(new KeyValuePair <string, PatchedByteArray>(varName, new PatchedByteArray(varSec, varOffset, new byte[1] {
                        def
                    })));
                }
                result.Add(new AsmPatch(name, description, staticPatches, variables));
            }
            return(result.AsReadOnly());
        }
Example #5
0
        private static void GetPatch(XmlNode node, out string name, out string description, out IList <PatchedByteArray> staticPatches)
        {
            name = node.Attributes["name"].InnerText;
            XmlNode descriptionNode = node.SelectSingleNode("Description");

            description = name;
            if (descriptionNode != null)
            {
                description = descriptionNode.InnerText;
            }

            XmlNodeList             currentLocs = node.SelectNodes("Location");
            List <PatchedByteArray> patches     = new List <PatchedByteArray>(currentLocs.Count);

            foreach (XmlNode location in currentLocs)
            {
                UInt32         offset = UInt32.Parse(location.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber);
                PsxIso.Sectors sector = (PsxIso.Sectors)Enum.Parse(typeof(PsxIso.Sectors), location.Attributes["file"].InnerText);
                byte[]         bytes  = GetBytes(location.InnerText);
                patches.Add(new PatchedByteArray(sector, offset, bytes));
            }

            staticPatches = patches.AsReadOnly();
        }
 public LazyLoadedPatchedByteArray(PsxIso.Sectors sector, long offset)
     : this((int)sector, offset)
 {
     SectorEnum = sector;
 }
 public InputFilePatch(PsxIso.Sectors sector, uint offset, uint expectedLength)
     : base(sector, offset)
 {
     ExpectedLength = expectedLength;
 }
 public STRPatchedByteArray(PsxIso.Sectors sector, string filename)
     : base(sector, 0)
 {
     this.Filename = filename;
 }
 public PatchedByteArray(PsxIso.Sectors sector, long offset, byte[] bytes)
     : this((int)sector, offset, bytes)
 {
     SectorEnum = sector;
 }
Example #10
0
 public PatchRange(PsxIso.Sectors sector, uint startOffset, uint endOffset) : this((int)sector, startOffset, endOffset)
 {
 }
        private static GetPatchResult GetPatch(XmlNode node, string xmlFileName, ASMEncodingUtility asmUtility, List <VariableType> variables)
        {
            KeyValuePair <string, string> nameDesc = GetPatchNameAndDescription(node);

            bool         hideInDefault     = false;
            XmlAttribute attrHideInDefault = node.Attributes["hideInDefault"];

            if (attrHideInDefault != null)
            {
                if (attrHideInDefault.InnerText.ToLower().Trim() == "true")
                {
                    hideInDefault = true;
                }
            }

            bool hasDefaultSector = false;

            PsxIso.Sectors defaultSector     = (PsxIso.Sectors) 0;
            XmlAttribute   attrDefaultFile   = node.Attributes["file"];
            XmlAttribute   attrDefaultSector = node.Attributes["sector"];

            if (attrDefaultFile != null)
            {
                defaultSector    = (PsxIso.Sectors)Enum.Parse(typeof(PsxIso.Sectors), attrDefaultFile.InnerText);
                hasDefaultSector = true;
            }
            else if (attrDefaultSector != null)
            {
                defaultSector    = (PsxIso.Sectors)Int32.Parse(attrDefaultSector.InnerText, System.Globalization.NumberStyles.HexNumber);
                hasDefaultSector = true;
            }

            XmlNodeList             currentLocs      = node.SelectNodes("Location");
            List <PatchedByteArray> patches          = new List <PatchedByteArray>(currentLocs.Count);
            StringBuilder           sbOuterErrorText = new StringBuilder();

            foreach (XmlNode location in currentLocs)
            {
                //UInt32 offset = UInt32.Parse( location.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber );
                XmlAttribute offsetAttribute        = location.Attributes["offset"];
                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"];
                XmlAttribute attrLabel    = location.Attributes["label"];
                XmlAttribute attrSpecific = location.Attributes["specific"];
                XmlAttribute attrMovable  = location.Attributes["movable"];

                string   strOffsetAttr      = (offsetAttribute != null) ? offsetAttribute.InnerText : "";
                string[] strOffsets         = strOffsetAttr.Replace(" ", "").Split(',');
                bool     ignoreOffsetMode   = false;
                bool     isSpecific         = false;
                bool     isSequentialOffset = false;

                List <SpecificLocation> specifics = FillSpecificAttributeData(attrSpecific, defaultSector);

                if (specifics.Count > 0)
                {
                    isSpecific = true;
                    List <string> newStrOffsets = new List <string>(specifics.Count);
                    foreach (SpecificLocation specific in specifics)
                    {
                        newStrOffsets.Add(specific.OffsetString);
                    }
                    strOffsets = newStrOffsets.ToArray();
                }
                else if ((string.IsNullOrEmpty(strOffsetAttr)) && (patches.Count > 0))
                {
                    // No offset defined -- offset is (last patch offset) + (last patch size)
                    PatchedByteArray lastPatchedByteArray = patches[patches.Count - 1];
                    long             offset    = lastPatchedByteArray.Offset + lastPatchedByteArray.GetBytes().Length;
                    string           strOffset = offset.ToString("X");
                    strOffsets = new string[1] {
                        strOffset
                    };
                    ignoreOffsetMode   = true;
                    isSequentialOffset = true;
                }

                PsxIso.Sectors sector = (PsxIso.Sectors) 0;
                if (isSpecific)
                {
                    sector = specifics[0].Sector;
                }
                else 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 if (hasDefaultSector)
                {
                    sector = defaultSector;
                }
                else if (patches.Count > 0)
                {
                    sector = (PsxIso.Sectors)(patches[patches.Count - 1].Sector);
                }
                else
                {
                    throw new Exception("Error in patch XML: Invalid file/sector!");
                }

                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 ((!ignoreOffsetMode) && (offsetModeAttribute != null))
                {
                    if (offsetModeAttribute.InnerText.ToLower().Trim() == "ram")
                    {
                        isRamOffset = true;
                    }
                }

                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)
                {
                    foreach (PatchedByteArray currentPatchedByteArray in patches)
                    {
                        StringBuilder sbLabels = new StringBuilder();
                        if (!string.IsNullOrEmpty(currentPatchedByteArray.Label))
                        {
                            sbLabels.Append(String.Format(".label @{0}, {1}{2}", currentPatchedByteArray.Label, currentPatchedByteArray.RamOffset, Environment.NewLine));
                        }
                        asmUtility.EncodeASM(sbLabels.ToString(), 0);
                    }
                    content = asmUtility.ReplaceLabelsInHex(content, true);
                }

                string label = "";
                if (attrLabel != null)
                {
                    label = attrLabel.InnerText.Replace(" ", "");
                }

                bool isMoveSimple = asmMode;
                if (attrMovable != null)
                {
                    bool.TryParse(attrMovable.InnerText, out isMoveSimple);
                }

                Nullable <PsxIso.FileToRamOffsets> ftrOffset = null;
                try
                {
                    ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + Enum.GetName(typeof(PsxIso.Sectors), sector));
                }
                catch (Exception)
                {
                    ftrOffset = null;
                }

                int offsetIndex = 0;
                foreach (string strOffset in strOffsets)
                {
                    UInt32 offset = UInt32.Parse(strOffset, System.Globalization.NumberStyles.HexNumber);

                    UInt32 ramOffset  = offset;
                    UInt32 fileOffset = offset;

                    if (ftrOffset.HasValue)
                    {
                        try
                        {
                            if (isRamOffset)
                            {
                                fileOffset -= (UInt32)ftrOffset;
                            }
                            else
                            {
                                ramOffset += (UInt32)ftrOffset;
                            }
                        }
                        catch (Exception) { }
                    }

                    ramOffset = ramOffset | 0x80000000;     // KSEG0

                    byte[] bytes;
                    string errorText = "";
                    if (asmMode)
                    {
                        string encodeContent = content;

                        StringBuilder sbPrefix = new StringBuilder();
                        foreach (PatchedByteArray currentPatchedByteArray in patches)
                        {
                            if (!string.IsNullOrEmpty(currentPatchedByteArray.Label))
                            {
                                sbPrefix.Append(String.Format(".label @{0}, {1}{2}", currentPatchedByteArray.Label, currentPatchedByteArray.RamOffset, Environment.NewLine));
                            }
                        }
                        foreach (VariableType variable in variables)
                        {
                            sbPrefix.Append(String.Format(".eqv %{0}, {1}{2}", ASMStringHelper.RemoveSpaces(variable.name).Replace(",", ""),
                                                          AsmPatch.GetUnsignedByteArrayValue_LittleEndian(variable.byteArray), Environment.NewLine));
                        }

                        encodeContent = sbPrefix.ToString() + content;

                        ASMEncoderResult result = asmUtility.EncodeASM(encodeContent, ramOffset);
                        bytes     = result.EncodedBytes;
                        errorText = result.ErrorText;
                    }
                    else
                    {
                        bytes = GetBytes(content);
                    }

                    bool isCheckedAsm = false;
                    if (!markedAsData)
                    {
                        ASMCheckResult checkResult = asmUtility.CheckASMFromBytes(bytes, ramOffset, true, false, new HashSet <ASMCheckCondition>()
                        {
                            ASMCheckCondition.LoadDelay,
                            ASMCheckCondition.UnalignedOffset,
                            ASMCheckCondition.MultCountdown,
                            ASMCheckCondition.StackPointerOffset4,
                            ASMCheckCondition.BranchInBranchDelaySlot
                        });

                        if (checkResult.IsASM)
                        {
                            isCheckedAsm = true;
                            if (!string.IsNullOrEmpty(checkResult.ErrorText))
                            {
                                errorText += checkResult.ErrorText;
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(errorText))
                    {
                        sbOuterErrorText.Append(errorText);
                    }

                    PatchedByteArray patchedByteArray = new PatchedByteArray(sector, fileOffset, bytes);
                    patchedByteArray.IsAsm              = asmMode;
                    patchedByteArray.MarkedAsData       = markedAsData;
                    patchedByteArray.IsCheckedAsm       = isCheckedAsm;
                    patchedByteArray.IsSequentialOffset = isSequentialOffset;
                    patchedByteArray.IsMoveSimple       = isMoveSimple;
                    patchedByteArray.AsmText            = asmMode ? content : "";
                    patchedByteArray.RamOffset          = ramOffset;
                    patchedByteArray.ErrorText          = errorText;
                    patchedByteArray.Label              = label;

                    patches.Add(patchedByteArray);

                    offsetIndex++;
                    if (offsetIndex < strOffsets.Length)
                    {
                        if (isSpecific)
                        {
                            sector = specifics[offsetIndex].Sector;

                            try
                            {
                                ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + Enum.GetName(typeof(PsxIso.Sectors), sector));
                            }
                            catch (Exception)
                            {
                                ftrOffset = null;
                            }
                        }
                    }
                }
            }

            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));
            }

            return(new GetPatchResult(nameDesc.Key, nameDesc.Value, patches.AsReadOnly(), hideInDefault, sbOuterErrorText.ToString()));
        }
        public static IList <AsmPatch> GetPatches(XmlNode rootNode, string xmlFilename, ASMEncodingUtility asmUtility)
        {
            bool         rootHideInDefault = false;
            XmlAttribute attrHideInDefault = rootNode.Attributes["hideInDefault"];

            if (attrHideInDefault != null)
            {
                rootHideInDefault = (attrHideInDefault.InnerText.ToLower().Trim() == "true");
            }

            string shortXmlFilename = xmlFilename.Substring(xmlFilename.LastIndexOf("\\") + 1);

            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;
                }

                bool           hasDefaultSector  = false;
                PsxIso.Sectors defaultSector     = (PsxIso.Sectors) 0;
                XmlAttribute   attrDefaultFile   = node.Attributes["file"];
                XmlAttribute   attrDefaultSector = node.Attributes["sector"];

                if (attrDefaultFile != null)
                {
                    defaultSector    = (PsxIso.Sectors)Enum.Parse(typeof(PsxIso.Sectors), attrDefaultFile.InnerText);
                    hasDefaultSector = true;
                }
                else if (attrDefaultSector != null)
                {
                    defaultSector    = (PsxIso.Sectors)Int32.Parse(attrDefaultSector.InnerText, System.Globalization.NumberStyles.HexNumber);
                    hasDefaultSector = true;
                }

                List <VariableType>     variables      = new List <VariableType>();
                List <PatchedByteArray> includePatches = new List <PatchedByteArray>();

                XmlNodeList includeNodes = node.SelectNodes("Include");
                foreach (XmlNode includeNode in includeNodes)
                {
                    XmlAttribute attrPatch = includeNode.Attributes["patch"];
                    if (attrPatch != null)
                    {
                        string patchName = attrPatch.InnerText.ToLower().Trim();
                        foreach (AsmPatch currentAsmPatch in result)
                        {
                            if (currentAsmPatch.Name.ToLower().Trim().Equals(patchName))
                            {
                                foreach (VariableType variable in currentAsmPatch.Variables)
                                {
                                    variables.Add(AsmPatch.CopyVariable(variable));
                                }
                                for (int index = 0; index < currentAsmPatch.NonVariableCount; index++)
                                {
                                    includePatches.Add(currentAsmPatch[index].Copy());
                                }
                            }
                        }
                    }
                }

                foreach (XmlNode varNode in node.SelectNodes("Variable"))
                {
                    XmlAttribute numBytesAttr = varNode.Attributes["bytes"];
                    string       strNumBytes  = (numBytesAttr == null) ? "1" : numBytesAttr.InnerText;
                    byte         numBytes     = (byte)(UInt32.Parse(strNumBytes) & 0xff);

                    string varName = varNode.Attributes["name"].InnerText;

                    XmlAttribute fileAttribute   = varNode.Attributes["file"];
                    XmlAttribute sectorAttribute = varNode.Attributes["sector"];
                    XmlAttribute attrSpecific    = varNode.Attributes["specific"];

                    //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 );
                    //string strOffsetAttr = varNode.Attributes["offset"].InnerText;
                    XmlAttribute offsetAttribute  = varNode.Attributes["offset"];
                    string       strOffsetAttr    = (offsetAttribute != null) ? offsetAttribute.InnerText : "";
                    string[]     strOffsets       = strOffsetAttr.Replace(" ", "").Split(',');
                    bool         ignoreOffsetMode = false;
                    bool         isSpecific       = false;

                    List <SpecificLocation> specifics = FillSpecificAttributeData(attrSpecific, defaultSector);

                    if (specifics.Count > 0)
                    {
                        isSpecific = true;
                        List <string> newStrOffsets = new List <string>(specifics.Count);
                        foreach (SpecificLocation specific in specifics)
                        {
                            newStrOffsets.Add(specific.OffsetString);
                        }
                        strOffsets = newStrOffsets.ToArray();
                    }
                    else if ((string.IsNullOrEmpty(strOffsetAttr)) && (variables.Count > 0) && (variables[variables.Count - 1].content.Count > 0))
                    {
                        // No offset defined -- offset is (last patch offset) + (last patch size)
                        int lastIndex = variables[variables.Count - 1].content.Count - 1;
                        PatchedByteArray lastPatchedByteArray = variables[variables.Count - 1].content[lastIndex];
                        long             offset    = lastPatchedByteArray.Offset + lastPatchedByteArray.GetBytes().Length;
                        string           strOffset = offset.ToString("X");
                        strOffsets = new string[1] {
                            strOffset
                        };
                        ignoreOffsetMode = true;
                    }

                    PsxIso.Sectors sector = (PsxIso.Sectors) 0;
                    if (isSpecific)
                    {
                        sector = specifics[0].Sector;
                    }
                    else 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 if (hasDefaultSector)
                    {
                        sector = defaultSector;
                    }
                    else if ((variables.Count > 0) && (variables[variables.Count - 1].content.Count > 0))
                    {
                        int lastIndex = variables[variables.Count - 1].content.Count - 1;
                        sector = (PsxIso.Sectors)(variables[variables.Count - 1].content[lastIndex].Sector);
                    }
                    else
                    {
                        throw new Exception("Error in patch XML: Invalid file/sector!");
                    }

                    XmlAttribute offsetModeAttribute = varNode.Attributes["offsetMode"];
                    bool         isRamOffset         = false;
                    if ((!ignoreOffsetMode) && (offsetModeAttribute != null))
                    {
                        if (offsetModeAttribute.InnerText.ToLower().Trim() == "ram")
                        {
                            isRamOffset = true;
                        }
                    }

                    Nullable <PsxIso.FileToRamOffsets> ftrOffset = null;
                    try
                    {
                        ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + Enum.GetName(typeof(PsxIso.Sectors), sector));
                        //ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + fileAttribute.InnerText);
                    }
                    catch (Exception)
                    {
                        ftrOffset = null;
                    }

                    XmlAttribute defaultAttr = varNode.Attributes["default"];
                    Byte[]       byteArray   = new Byte[numBytes];
                    UInt32       def         = 0;
                    if (defaultAttr != null)
                    {
                        def = UInt32.Parse(defaultAttr.InnerText, System.Globalization.NumberStyles.HexNumber);
                        for (int i = 0; i < numBytes; i++)
                        {
                            byteArray[i] = (Byte)((def >> (i * 8)) & 0xff);
                        }
                    }

                    List <PatchedByteArray> patchedByteArrayList = new List <PatchedByteArray>();
                    int offsetIndex = 0;

                    foreach (string strOffset in strOffsets)
                    {
                        UInt32 offset = UInt32.Parse(strOffset, System.Globalization.NumberStyles.HexNumber);
                        //UInt32 ramOffset = offset;
                        UInt32 fileOffset = offset;

                        if (ftrOffset.HasValue)
                        {
                            try
                            {
                                if (isRamOffset)
                                {
                                    fileOffset -= (UInt32)ftrOffset;
                                }
                                //else
                                //    ramOffset += (UInt32)ftrOffset;
                            }
                            catch (Exception) { }
                        }

                        //ramOffset = ramOffset | 0x80000000;     // KSEG0

                        patchedByteArrayList.Add(new PatchedByteArray(sector, fileOffset, byteArray));

                        offsetIndex++;
                        if (offsetIndex < strOffsets.Length)
                        {
                            if (isSpecific)
                            {
                                sector = specifics[offsetIndex].Sector;

                                try
                                {
                                    ftrOffset = (PsxIso.FileToRamOffsets)Enum.Parse(typeof(PsxIso.FileToRamOffsets), "OFFSET_" + Enum.GetName(typeof(PsxIso.Sectors), sector));
                                }
                                catch (Exception)
                                {
                                    ftrOffset = null;
                                }
                            }
                        }
                    }

                    bool   isReference             = false;
                    string referenceName           = "";
                    string referenceOperatorSymbol = "";
                    uint   referenceOperand        = 0;

                    XmlAttribute attrReference = varNode.Attributes["reference"];
                    XmlAttribute attrOperator  = varNode.Attributes["operator"];
                    XmlAttribute attrOperand   = varNode.Attributes["operand"];

                    if (attrReference != null)
                    {
                        isReference             = true;
                        referenceName           = attrReference.InnerText;
                        referenceOperatorSymbol = (attrOperator != null) ? attrOperator.InnerText : "";
                        if (attrOperand != null)
                        {
                            //UInt32.Parse(defaultAttr.InnerText, System.Globalization.NumberStyles.HexNumber);
                            uint.TryParse(attrOperand.InnerText, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.CurrentCulture, out referenceOperand);
                        }
                    }

                    VariableType vType = new VariableType();
                    vType.numBytes                 = numBytes;
                    vType.byteArray                = byteArray;
                    vType.name                     = varName;
                    vType.content                  = patchedByteArrayList;
                    vType.isReference              = isReference;
                    vType.reference                = new VariableReference();
                    vType.reference.name           = referenceName;
                    vType.reference.operatorSymbol = referenceOperatorSymbol;
                    vType.reference.operand        = referenceOperand;

                    variables.Add(vType);
                }

                GetPatchResult getPatchResult = GetPatch(node, xmlFilename, asmUtility, variables);

                List <PatchedByteArray> patches = new List <PatchedByteArray>(includePatches.Count + getPatchResult.StaticPatches.Count);
                patches.AddRange(includePatches);
                patches.AddRange(getPatchResult.StaticPatches);

                AsmPatch asmPatch = new AsmPatch(getPatchResult.Name, shortXmlFilename, getPatchResult.Description, patches,
                                                 (getPatchResult.HideInDefault | rootHideInDefault), variables);

                asmPatch.ErrorText = getPatchResult.ErrorText;
                result.Add(asmPatch);
            }

            patchNodes = rootNode.SelectNodes("ImportFilePatch");
            foreach (XmlNode node in patchNodes)
            {
                KeyValuePair <string, string> nameDesc = GetPatchNameAndDescription(node);

                string name        = nameDesc.Key;
                string description = nameDesc.Value;

                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, shortXmlFilename, description, new InputFilePatch(sector, offset, expectedLength)));
            }

            return(result.AsReadOnly());
        }
 public SpecificLocation(PsxIso.Sectors sector, string offsetString)
 {
     Sector       = sector;
     OffsetString = offsetString;
 }
Example #14
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 #15
0
        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;
        }
Example #16
0
        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());
        }