Пример #1
0
        private void UpdateReferenceVariableValue(VariableType variable)
        {
            if (variable.isReference)
            {
                byte[] referenceBytes = VariableMap[variable.reference.name].byteArray;
                uint   value          = AsmPatch.GetUnsignedByteArrayValue_LittleEndian(referenceBytes);

                switch (variable.reference.operatorSymbol)
                {
                case "+":
                    value += variable.reference.operand;
                    break;

                case "-":
                    value -= variable.reference.operand;
                    break;

                case "*":
                    value *= variable.reference.operand;
                    break;

                case "/":
                    value /= variable.reference.operand;
                    break;
                }

                UpdateVariable(variable, value);
            }
        }
Пример #2
0
        void clb_Patches_SelectedIndexChanged(object sender, EventArgs e)
        {
            AsmPatch p = clb_Patches.SelectedItem as AsmPatch;

            if (p != null)
            {
                textBox1.Text = p.Description;

                int index = clb_Patches.SelectedIndex;
                txt_Messages.Text = (index >= 0) ? patchMessages[index] : "";

                if (p.Variables.Count > 0)
                {
                    ignoreChanges = true;
                    variableComboBox.Items.Clear();
                    p.Variables.ForEach(varType => variableComboBox.Items.Add(varType.content.Key));
                    variableComboBox.SelectedIndex = 0;

                    //variableSpinner.Value = p.Variables[0].content.Value.GetBytes()[0];
                    Byte[] byteArray = p.Variables[0].content.Value.GetBytes();
                    variableSpinner.Maximum = (decimal)Math.Pow(256, p.Variables[0].bytes) - 1;
                    variableSpinner.Value   = p.GetUnsignedByteArrayValue_LittleEndian(byteArray);

                    variableSpinner.Visible  = true;
                    ignoreChanges            = false;
                    variableComboBox.Visible = true;
                }
                else
                {
                    variableSpinner.Visible  = false;
                    ignoreChanges            = true;
                    variableComboBox.Visible = false;
                }
            }
        }
Пример #3
0
        private void clb_Patches_SelectedIndexChanged(object sender, EventArgs e)
        {
            AsmPatch p = clb_Patches.SelectedItem as AsmPatch;

            if (p != null)
            {
                textBox1.Text = p.Description;

                int index = clb_Patches.SelectedIndex;
                txt_Messages.Text = p.ErrorText;

                //if (p.Variables.Count > 0)
                if (p.CountNonReferenceVariables() > 0)
                {
                    ignoreChanges = true;
                    variableComboBox.Items.Clear();

                    bool         foundFirst = false;
                    VariableType firstNonReferenceVariable = p.Variables[0];
                    foreach (VariableType variable in p.Variables)
                    {
                        if (!variable.isReference)
                        {
                            variableComboBox.Items.Add(variable.name);
                            if (!foundFirst)
                            {
                                firstNonReferenceVariable = variable;
                                foundFirst = true;
                            }
                        }
                    }
                    variableComboBox.SelectedIndex = 0;

                    byte[] byteArray = firstNonReferenceVariable.byteArray;
                    variableSpinner.Maximum = (decimal)Math.Pow(256, firstNonReferenceVariable.numBytes) - 1;
                    variableSpinner.Value   = AsmPatch.GetUnsignedByteArrayValue_LittleEndian(byteArray);

                    variableSpinner.Visible  = true;
                    ignoreChanges            = false;
                    variableComboBox.Visible = true;
                }
                else
                {
                    variableSpinner.Visible  = false;
                    ignoreChanges            = true;
                    variableComboBox.Visible = false;
                }
            }
        }
Пример #4
0
        void variableComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!ignoreChanges)
            {
                AsmPatch patch     = (clb_Patches.SelectedItem as AsmPatch);
                Byte[]   byteArray = patch.Variables[variableComboBox.SelectedIndex].content.Value.GetBytes();

                // Setting Maximum can trigger the variableSpinner_ValueChanged event, but we don't want to change the variable value here, so set ignoreChanges = true before setting Maximum.
                ignoreChanges           = true;
                variableSpinner.Maximum = (decimal)Math.Pow(256, patch.Variables[variableComboBox.SelectedIndex].bytes) - 1;
                ignoreChanges           = false;

                variableSpinner.Value = patch.GetUnsignedByteArrayValue_LittleEndian(byteArray);
            }
        }
Пример #5
0
        private void variableComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!ignoreChanges)
            {
                AsmPatch     patch            = (clb_Patches.SelectedItem as AsmPatch);
                VariableType selectedVariable = patch.VariableMap[(string)variableComboBox.SelectedItem];

                byte[] byteArray = selectedVariable.byteArray;

                // Setting Maximum can trigger the variableSpinner_ValueChanged event, but we don't want to change the variable value here, so set ignoreChanges = true before setting Maximum.
                ignoreChanges           = true;
                variableSpinner.Maximum = (decimal)Math.Pow(256, selectedVariable.numBytes) - 1;
                ignoreChanges           = false;

                variableSpinner.Value = AsmPatch.GetUnsignedByteArrayValue_LittleEndian(byteArray);
            }
        }
Пример #6
0
        public void Update(ASMEncoding.ASMEncodingUtility asmUtility)
        {
            UpdateReferenceVariableValues();

            List <PatchedByteArray> allPatches = GetAllPatches();

            foreach (PatchedByteArray patchedByteArray in allPatches)
            {
                if (patchedByteArray.IsAsm)
                {
                    string encodeContent = patchedByteArray.AsmText;
                    //string strPrefix = "";
                    //IList<VariableType> variables = Variables;

                    System.Text.StringBuilder sbPrefix = new System.Text.StringBuilder();
                    foreach (PatchedByteArray currentPatchedByteArray in allPatches)
                    {
                        if (!string.IsNullOrEmpty(currentPatchedByteArray.Label))
                        {
                            sbPrefix.AppendFormat(".label @{0}, {1}{2}", currentPatchedByteArray.Label, currentPatchedByteArray.RamOffset, Environment.NewLine);
                        }
                    }
                    foreach (VariableType variable in Variables)
                    {
                        sbPrefix.AppendFormat(".eqv %{0}, {1}{2}", ASMEncoding.Helpers.ASMStringHelper.RemoveSpaces(variable.name).Replace(",", ""),
                                              AsmPatch.GetUnsignedByteArrayValue_LittleEndian(variable.byteArray), Environment.NewLine);
                    }

                    encodeContent = sbPrefix.ToString() + patchedByteArray.AsmText;
                    //patchedByteArray.SetBytes(asmUtility.EncodeASM(encodeContent, (uint)patchedByteArray.RamOffset).EncodedBytes);

                    byte[] bytes = asmUtility.EncodeASM(encodeContent, (uint)patchedByteArray.RamOffset).EncodedBytes;

                    if ((!patchedByteArray.IsMoveSimple) && (blockMoveList.Count > 0))
                    {
                        bytes = asmUtility.UpdateBlockReferences(bytes, (uint)patchedByteArray.RamOffset, true, blockMoveList);
                    }

                    patchedByteArray.SetBytes(bytes);
                }
            }
        }
Пример #7
0
        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()));
        }