Exemple #1
0
        private void txtModifier_TextChanged(object sender, EventArgs e)
        {
            if (lstPatches.SelectedNode != null && lstPatches.SelectedNode.Tag != null && lstPatches.SelectedNode.Tag is DiffPatch && cmbModifiers.SelectedIndex >= 0)
            {
                DiffInput input = ((DiffPatch)lstPatches.SelectedNode.Tag).Inputs[cmbModifiers.SelectedIndex];
                if (input.Type == ChangeType.Color)
                {
                    return;
                }

                bool ok = DiffInput.CheckInput(txtModifier.Text, input);

                if (ok)
                {
                    input.Value = txtModifier.Text;
                }

                if (!ok)
                {
                    picModifier.Image = imgListModifier.Images["red.png"];
                }
                else
                {
                    picModifier.Image = imgListModifier.Images["green.png"];
                }
            }
        }
Exemple #2
0
        private void picModifier_Click(object sender, EventArgs e)
        {
            if (lstPatches.SelectedNode != null && lstPatches.SelectedNode.Tag != null && lstPatches.SelectedNode.Tag is DiffPatch && cmbModifiers.SelectedIndex >= 0)
            {
                DiffInput input = ((DiffPatch)lstPatches.SelectedNode.Tag).Inputs[cmbModifiers.SelectedIndex];
                if (input.Type != ChangeType.Color)
                {
                    return;
                }

                var ret     = 0;
                var colPick = new ColorDialog();
                ret = (int)colPick.ShowDialog(this);

                if (ret == (int)System.Windows.Forms.DialogResult.OK)
                {
                    input.Value = String.Format("{0:X2}{1:X2}{2:X2}", colPick.Color.R, colPick.Color.G, colPick.Color.B);
                    ShowModifier(cmbModifiers.SelectedIndex);
                }
            }
        }
Exemple #3
0
        public void LoadFromXML(XmlNode patch)
        {
            XmlNode tmpNode = null;

            //DiffPatch p = new DiffPatch();
            this.ID   = int.Parse(patch.Attributes["id"].InnerText);
            this.Name = patch.Attributes["name"].InnerText;
            this.Type = patch.Attributes["type"].InnerText;

            tmpNode = patch.ParentNode;
            if (tmpNode != null && tmpNode.Name == "patchgroup")
            {
                this.GroupID = int.Parse(tmpNode.Attributes["id"].InnerText);
            }

            if (patch.Attributes["recommended"] != null)
            {
                this.Recommended = true;
            }

            tmpNode = patch.SelectSingleNode("desc");
            if (tmpNode != null)
            {
                this.Desc = tmpNode.InnerText;
            }

            foreach (XmlNode i in patch.SelectNodes("input"))
            {
                var input = new DiffInput();
                input.LoadFromXML(i);
                this.Inputs.Add(input);
            }

            /*                        var input = new DiffInput();
             *          input.LoadFromXML(change);
             *          this.Inputs.Add(input);*/

            tmpNode = patch.SelectSingleNode("changes");
            if (tmpNode != null)
            {
                foreach (XmlNode change in tmpNode.ChildNodes)
                {
                    DiffChange c = new DiffChange();

                    if (change.Name == "byte")
                    {
                        c.Type = ChangeType.Byte;
                    }
                    else if (change.Name == "word")
                    {
                        c.Type = ChangeType.Word;
                    }
                    else if (change.Name == "dword")
                    {
                        c.Type = ChangeType.Dword;
                    }
                    else if (change.Name == "string")
                    {
                        c.Type = ChangeType.String;
                    }
                    else
                    {
                        c.Type = ChangeType.None;
                    }

                    if (change.Attributes["new"].InnerText.StartsWith("$"))
                    {
                        c.New_ = change.Attributes["new"].InnerText;
                    }
                    c.Offset = uint.Parse(change.Attributes["offset"].InnerText, System.Globalization.NumberStyles.HexNumber);
                    if (c.Type == ChangeType.String)
                    {
                        if (c.New_ == null)
                        {
                            c.New_ = change.Attributes["new"].InnerText;
                        }
                        c.Old = change.Attributes["old"].InnerText;
                    }
                    else if (c.Type == ChangeType.Byte)
                    {
                        if (c.New_ == null)
                        {
                            c.New_ = byte.Parse(change.Attributes["new"].InnerText, System.Globalization.NumberStyles.HexNumber);
                        }
                        c.Old = byte.Parse(change.Attributes["old"].InnerText, System.Globalization.NumberStyles.HexNumber);
                    }
                    else if (c.Type == ChangeType.Word)
                    {
                        if (c.New_ == null)
                        {
                            c.New_ = ushort.Parse(change.Attributes["new"].InnerText, System.Globalization.NumberStyles.HexNumber);
                        }
                        c.Old = ushort.Parse(change.Attributes["old"].InnerText, System.Globalization.NumberStyles.HexNumber);
                    }
                    else if (c.Type == ChangeType.Dword)
                    {
                        if (c.New_ == null)
                        {
                            c.New_ = uint.Parse(change.Attributes["new"].InnerText, System.Globalization.NumberStyles.HexNumber);
                        }
                        c.Old = uint.Parse(change.Attributes["old"].InnerText, System.Globalization.NumberStyles.HexNumber);
                    }

                    this.Changes.Add(c);
                }
            }
        }
Exemple #4
0
        public static bool CheckInput(string value, DiffInput input)
        {
            bool ok = true;

            if (input.Type == ChangeType.String)
            {
                if (input.Min != int.MaxValue && value.Length < input.Min)
                {
                    ok = false;
                }

                if (input.Max != int.MaxValue && value.Length > input.Max)
                {
                    ok = false;
                }
            }
            else if (input.Type == ChangeType.Byte)
            {
                byte val = 0;
                if (!byte.TryParse(value, out val))
                {
                    ok = false;
                }
                else if (input.Min != int.MaxValue && val < input.Min)
                {
                    ok = false;
                }
                else if (input.Max != int.MaxValue && val > input.Max)
                {
                    ok = false;
                }
            }
            else if (input.Type == ChangeType.Word)
            {
                UInt16 val = 0;
                if (!UInt16.TryParse(value, out val))
                {
                    ok = false;
                }
                else if (input.Min != int.MaxValue && val < input.Min)
                {
                    ok = false;
                }
                else if (input.Max != int.MaxValue && val > input.Max)
                {
                    ok = false;
                }
            }
            else if (input.Type == ChangeType.Byte)
            {
                UInt32 val = 0;
                if (!UInt32.TryParse(value, out val))
                {
                    ok = false;
                }
                else if (input.Min != int.MaxValue && val < input.Min)
                {
                    ok = false;
                }
                else if (input.Max != int.MaxValue && val > input.Max)
                {
                    ok = false;
                }
            }

            return(ok);
        }
Exemple #5
0
        private int ApplyPatch(DiffPatch patch, ref byte[] buf)
        {
            int changed = 0;

            if (!patch.Apply)
            {
                return(-1);
            }

            foreach (DiffInput i in patch.Inputs)
            {
                if (!DiffInput.CheckInput(i.Value, i))
                {
                    return(-2);
                }
            }

            foreach (DiffChange c in patch.Changes)
            {
                switch (c.Type)
                {
                case ChangeType.Byte:
                {
                    byte old = buf[c.Offset];

                    if (old == (byte)c.Old)
                    {
                        buf[c.Offset] = (byte)c.GetNewValue(patch);
                        changed++;
                    }
                    else
                    {
                        MessageBox.Show(String.Format("Data mismatch at 0x{0:X} (0x{1:X} != 0x{2:X})!", c.Offset, old, (byte)c.Old));
                    }

                    break;
                }

                case ChangeType.Word:
                {
                    UInt16 old = BitConverter.ToUInt16(buf, (int)c.Offset);

                    if (old == (UInt16)c.Old)
                    {
                        UInt16 val = (UInt16)c.GetNewValue(patch);
                        buf[c.Offset]     = (byte)val;
                        buf[c.Offset + 1] = (byte)(val >> 8);
                        changed          += 2;
                    }
                    else
                    {
                        MessageBox.Show(String.Format("Data mismatch at 0x{0:X} (0x{1:X} != 0x{2:X})!", c.Offset, old, (ushort)c.Old));
                    }

                    break;
                }

                case ChangeType.Dword:
                {
                    UInt32 old = BitConverter.ToUInt32(buf, (int)c.Offset);

                    if (old == (UInt32)c.Old)
                    {
                        WriteDword(ref buf, (UInt32)c.GetNewValue(patch), c.Offset);
                        changed += 4;
                    }
                    else
                    {
                        MessageBox.Show(String.Format("Data mismatch at 0x{0:X} (0x{1:X} != 0x{2:X})!", c.Offset, old, (uint)c.Old));
                    }
                    break;
                }

                case ChangeType.String:    // used only for displayable string
                {
                    //currently not checking for old string - if client crashes your screwed
                    byte[] val = Encoding.ASCII.GetBytes((String)c.GetNewValue(patch) + "\x00");

                    int i = 0;
                    foreach (byte b in val)
                    {
                        buf[c.Offset + i++] = b;
                    }

                    changed += i;
                    break;
                }
                }
            }

            //MessageBox.Show("Applied patch '" + patch.Name + "' (" + changed + " bytes)");

            return(changed);
        }