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