|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Diagnostics.Contracts; |
| 5 | +using System.Linq; |
| 6 | +using System.Windows.Forms; |
| 7 | +using ReClassNET.Memory; |
| 8 | +using ReClassNET.UI; |
| 9 | +using ReClassNET.Util; |
| 10 | + |
| 11 | +namespace ReClassNET.Forms |
| 12 | +{ |
| 13 | + public partial class NamedAddressesForm : IconForm |
| 14 | + { |
| 15 | + private readonly RemoteProcess process; |
| 16 | + |
| 17 | + public NamedAddressesForm(RemoteProcess process) |
| 18 | + { |
| 19 | + Contract.Requires(process != null); |
| 20 | + |
| 21 | + this.process = process; |
| 22 | + |
| 23 | + InitializeComponent(); |
| 24 | + |
| 25 | + DisplayNamedAddresses(); |
| 26 | + } |
| 27 | + |
| 28 | + protected override void OnLoad(EventArgs e) |
| 29 | + { |
| 30 | + base.OnLoad(e); |
| 31 | + |
| 32 | + GlobalWindowManager.AddWindow(this); |
| 33 | + } |
| 34 | + |
| 35 | + protected override void OnFormClosed(FormClosedEventArgs e) |
| 36 | + { |
| 37 | + base.OnFormClosed(e); |
| 38 | + |
| 39 | + GlobalWindowManager.RemoveWindow(this); |
| 40 | + } |
| 41 | + |
| 42 | + #region Event Handler |
| 43 | + |
| 44 | + private void InputTextBox_TextChanged(object sender, EventArgs e) |
| 45 | + { |
| 46 | + addAddressIconButton.Enabled = IsValidInput(); |
| 47 | + } |
| 48 | + |
| 49 | + private void namedAddressesListBox_SelectedIndexChanged(object sender, EventArgs e) |
| 50 | + { |
| 51 | + removeAddressIconButton.Enabled = namedAddressesListBox.SelectedIndex != -1; |
| 52 | + } |
| 53 | + |
| 54 | + private void addAddressIconButton_Click(object sender, EventArgs e) |
| 55 | + { |
| 56 | + if (!IsValidInput()) |
| 57 | + { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + var address = process.ParseAddress(addressTextBox.Text.Trim()); |
| 62 | + var name = nameTextBox.Text.Trim(); |
| 63 | + |
| 64 | + process.NamedAddresses[address] = name; |
| 65 | + |
| 66 | + addressTextBox.Text = nameTextBox.Text = null; |
| 67 | + |
| 68 | + DisplayNamedAddresses(); |
| 69 | + } |
| 70 | + |
| 71 | + private void removeAddressIconButton_Click(object sender, EventArgs e) |
| 72 | + { |
| 73 | + if (namedAddressesListBox.SelectedItem is BindingDisplayWrapper<KeyValuePair<IntPtr, string>> namedAddress) |
| 74 | + { |
| 75 | + process.NamedAddresses.Remove(namedAddress.Value.Key); |
| 76 | + |
| 77 | + DisplayNamedAddresses(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #endregion |
| 82 | + |
| 83 | + private void DisplayNamedAddresses() |
| 84 | + { |
| 85 | + namedAddressesListBox.DataSource = process.NamedAddresses |
| 86 | + .Select(kv => new BindingDisplayWrapper<KeyValuePair<IntPtr, string>>(kv, v => $"0x{v.Key.ToString(Constants.StringHexFormat)}: {v.Value}")) |
| 87 | + .ToList(); |
| 88 | + |
| 89 | + namedAddressesListBox_SelectedIndexChanged(null, null); |
| 90 | + } |
| 91 | + |
| 92 | + private bool IsValidInput() |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + var address = process.ParseAddress(addressTextBox.Text.Trim()); |
| 97 | + var name = nameTextBox.Text.Trim(); |
| 98 | + |
| 99 | + return !address.IsNull() && !string.IsNullOrEmpty(name); |
| 100 | + } |
| 101 | + catch |
| 102 | + { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments