예제 #1
0
        public Patch ReadPatch(string name)
        {
            Patch patch = new Patch
            {
                instructions = new Stack <PatchInstruction>()
            };

            try
            {
                using (StreamReader file = new StreamReader(name))
                {
                    string ln;
                    if ((ln = file.ReadLine()) != null)
                    {
                        patch.fileName = ln.Substring(ln.LastIndexOf(">") + 1);
                        while ((ln = file.ReadLine()) != null)
                        {
                            PatchInstruction instruction = new PatchInstruction();
                            string[]         parts       = ln.Split(':');
                            instruction.offset = System.Convert.ToInt32(parts[0], 16);
                            instruction.oldHex = parts[1].Substring(0, 2);
                            instruction.newHex = parts[1].Substring(4, 2);
                            patch.instructions.Push(instruction);
                        }
                    }

                    file.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return(patch);
        }
예제 #2
0
        private void ApplyPatch(Patch patch)
        {
            Stack <string> errors    = new Stack <string>();
            int            maxOffset = richTBMain.Text.Length / 3;

            while (patch.instructions.Count > 0)
            {
                var instruc = patch.instructions.Pop();
                int offset  = instruc.offset * 3;
                if (maxOffset < offset)
                {
                    errors.Push("Offset " + instruc.offset + " greater than size of file");
                }
                else if (instruc.oldHex != richTBMain.Text.Substring(offset, 2) && instruc.oldHex != "**")
                {
                    errors.Push("Byte " + instruc.oldHex + " not the same as " + richTBMain.Text.Substring(offset, 2));
                }
                else
                {
                    PatchInstruction toAdd = new PatchInstruction
                    {
                        offset = instruc.offset,
                        oldHex = richTBMain.Text.Substring(offset, 2),
                        newHex = instruc.newHex
                    };
                    history.Add(toAdd);

                    ChangeText(instruc.newHex, Color.Red, offset, ref richTBMain);

                    HexChanged(offset);
                }
            }

            if (errors.Count > 0)
            {
                int    counter  = 1;
                string toCreate = "Errors:";
                while (errors.Count > 0)
                {
                    toCreate += Environment.NewLine + counter.ToString() + ". " + errors.Pop();
                    counter++;
                }
                MessageBox.Show(toCreate, "Patch failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("Patch successfully applied");
            }
        }
예제 #3
0
        private void breakFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var rand = new Random();

            for (int i = 0; i < 20; i++)
            {
                int pos = rand.Next(richTBMain.Text.Length / 9) * 3;
                PatchInstruction toAdd = new PatchInstruction
                {
                    offset = pos / 3,
                    oldHex = richTBMain.Text.Substring(pos, 2),
                    newHex = "90"
                };
                ChangeText("90", Color.Red, pos, ref richTBMain);
                history.Add(toAdd);
                HexChanged(pos);
            }
            MessageBox.Show("File probably could not be opened now, otherwise use break again\n(do not forget to save file to apply changes)");
        }
예제 #4
0
        private void richTBAscii_KeyPress(object sender, KeyPressEventArgs e)
        {
            char currentChar = e.KeyChar;

            if (!(currentChar >= 0 && currentChar <= 31))
            {
                Color            highlightColor = Color.Red;
                int              pos            = richTBAscii.SelectionStart;
                int              line           = richTBAscii.GetLineFromCharIndex(pos);
                PatchInstruction toAdd          = new PatchInstruction
                {
                    offset = (pos - richTBAscii.GetLineFromCharIndex(pos))
                };
                if (pos >= richTBAscii.Text.Length)
                {
                    bool newline = ((pos - line) % 16 == 0);

                    ChangeText(currentChar.ToString(), highlightColor, pos, ref richTBAscii, newline);
                    AsciiAdded();
                    toAdd.oldHex = "";
                    toAdd.newHex = richTBMain.Text.Substring((pos - richTBAscii.GetLineFromCharIndex(pos)) * 3, 2);
                    history.Add(toAdd);
                    e.Handled = true;
                    if (newline)
                    {
                        richTBAscii.SelectionStart = pos + 2;
                    }
                    else
                    {
                        richTBAscii.SelectionStart = pos + 1;
                    }
                }
                else
                {
                    if ((pos - line) % 16 == 0 && pos != 0)
                    {
                        ChangeText(currentChar.ToString(), highlightColor, pos + 1, ref richTBAscii, true);
                        toAdd.oldHex = richTBMain.Text.Substring((pos + 1 - richTBAscii.GetLineFromCharIndex(pos)) * 3, 2);
                        AsciiChanged(pos + 1);
                        toAdd.offset = (pos + 1 - richTBAscii.GetLineFromCharIndex(pos));
                        toAdd.newHex = richTBMain.Text.Substring((pos + 1 - richTBAscii.GetLineFromCharIndex(pos)) * 3, 2);
                        history.Add(toAdd);
                        e.Handled = true;
                        richTBAscii.SelectionStart = pos + 2;
                    }
                    else
                    {
                        ChangeText(currentChar.ToString(), highlightColor, pos, ref richTBAscii);
                        toAdd.oldHex = richTBMain.Text.Substring((pos - richTBAscii.GetLineFromCharIndex(pos)) * 3, 2);
                        AsciiChanged(pos);

                        toAdd.newHex = richTBMain.Text.Substring((pos - richTBAscii.GetLineFromCharIndex(pos)) * 3, 2);
                        history.Add(toAdd);
                        e.Handled = true;
                        richTBAscii.SelectionStart = pos + 1;
                    }
                }
            }
            else
            {
                e.Handled = true;
            }
        }
예제 #5
0
        private void richTBMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            char currentChar = e.KeyChar;

            currentChar = char.ToUpper(currentChar);

            if ((currentChar >= 65 && currentChar <= 70) || char.IsDigit(currentChar))
            {
                Color highlightColor = Color.Red;
                int   pos            = richTBMain.SelectionStart;

                if (pos >= richTBMain.Text.Length)
                {
                    bool newline = (isTextAtBorder(pos));

                    ChangeText(currentChar.ToString(), highlightColor, pos, ref richTBMain, newline);
                    PatchInstruction toadd = new PatchInstruction
                    {
                        oldHex = "",
                        newHex = richTBMain.Text.Substring(richTBMain.Text.Length - 2),
                        offset = (richTBMain.Text.Length - 2) / 3
                    };
                    history.Add(toadd);
                    HexAdded(newline);

                    e.Handled = true;
                    richTBMain.SelectionStart = pos + 2;
                }
                else
                {
                    PatchInstruction toadd = new PatchInstruction();
                    int blockStart;
                    if (pos == richTBMain.Text.Length - 1)
                    {
                        blockStart = pos - 1;
                    }
                    else
                    {
                        blockStart = GetBlockStart(pos);
                    }
                    toadd.oldHex = richTBMain.Text.Substring(blockStart, 2);
                    ChangeText(currentChar.ToString(), highlightColor, pos, ref richTBMain);

                    toadd.newHex = richTBMain.Text.Substring(blockStart, 2);
                    toadd.offset = blockStart / 3;
                    history.Add(toadd);

                    HexChanged(pos);
                    e.Handled = true;
                    if (pos >= richTBMain.Text.Length - 1) //REDO
                    {
                        richTBMain.SelectionStart = pos + 1;
                    }
                    else if (richTBMain.Text[pos + 1] == ' ' || richTBMain.Text[pos + 1] == '\n')
                    {
                        richTBMain.SelectionStart = pos + 2;
                    }
                    else
                    {
                        richTBMain.SelectionStart = pos + 1;
                    }
                }
            }
            else
            {
                e.Handled = true;
            }
        }