-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainForm.cs
255 lines (203 loc) · 7.27 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace CarWarpHelper {
public partial class MainForm : Form {
private static readonly int[] UPDATE_OPTIONS = new int[] {
1, 5, 10, 17, 50, 100, 500, 1000
};
private int updateMS = UPDATE_OPTIONS[2];
private readonly Timer UpdateTimer;
private readonly List<NumericUpDown> allPosInputs;
private readonly StationManager stationMgr;
private readonly BindingForm binds;
private Vect3F? savedPos = null;
public MainForm() {
InitializeComponent();
EnsureHooked();
// This is at the very back so that I can keep using the visual editor,
// but needs to be at the very front at runtime for actual use
Controls.SetChildIndex(notHookedLabel, 0);
allPosInputs = new List<NumericUpDown>() { xInput, yInput, zInput };
foreach (NumericUpDown input in allPosInputs) {
input.Minimum = decimal.MinValue;
input.Maximum = decimal.MaxValue;
}
stationMgr = new StationManager();
mapLabel.Text = stationMgr.World;
stationLabel.Text = stationMgr.Station;
binds = new BindingForm(this);
ContextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem dropDown = new ToolStripMenuItem("Polling Rate (ms)");
foreach (int option in UPDATE_OPTIONS) {
ToolStripMenuItem sub = new ToolStripMenuItem(option.ToString()) {
CheckOnClick = true,
Checked = option == updateMS
};
sub.Click += new EventHandler(ChangeUpdateRate);
dropDown.DropDownItems.Add(sub);
}
ContextMenuStrip.Items.Add(dropDown);
ToolStripMenuItem keybinds = new ToolStripMenuItem("Configure Keybinds");
keybinds.Click += KeybindMenuItem_Click;
ContextMenuStrip.Items.Add(keybinds);
UpdateTimer = new Timer() {
Interval = updateMS
};
UpdateTimer.Tick += new EventHandler(Update);
UpdateTimer.Start();
}
private bool EnsureHooked() {
if (!GameHook.IsHooked) {
if (GameHook.TryHook()) {
notHookedLabel.Visible = false;
} else {
notHookedLabel.Visible = true;
return false;
}
}
return true;
}
private void ChangeUpdateRate(object myObject, EventArgs myEventArgs) {
ToolStripMenuItem dropDown = (ToolStripMenuItem) ContextMenuStrip.Items[0];
for (int i = 0; i < dropDown.DropDownItems.Count; i++) {
ToolStripMenuItem item = (ToolStripMenuItem) dropDown.DropDownItems[i];
if (item == myObject) {
updateMS = UPDATE_OPTIONS[i];
} else {
item.Checked = false;
}
}
UpdateTimer.Stop();
UpdateTimer.Interval = updateMS;
UpdateTimer.Start();
}
private void Update(object myObject, EventArgs myEventArgs) {
if (!EnsureHooked()) {
return;
}
allPosInputs.ForEach(input => input.ValueChanged -= Position_ValueChanged);
try {
Vect3F pos = GameHook.Position;
if (xLock.Checked) {
pos.X = Convert.ToSingle(xInput.Value);
} else {
xInput.Value = Convert.ToDecimal(GameHook.Position.X);
}
if (yLock.Checked) {
pos.Y = Convert.ToSingle(yInput.Value);
} else {
yInput.Value = Convert.ToDecimal(GameHook.Position.Y);
}
if (zLock.Checked) {
pos.Z = Convert.ToSingle(zInput.Value);
} else {
zInput.Value = Convert.ToDecimal(GameHook.Position.Z);
}
if (xLock.Checked || yLock.Checked || zLock.Checked) {
GameHook.Position = pos;
}
allPosInputs.ForEach(input => input.Enabled = true);
// Should be caused by invalid pointers - most likely you're on the main menu
} catch (Win32Exception) {
allPosInputs.ForEach(input => input.Enabled = false);
}
allPosInputs.ForEach(input => input.ValueChanged += Position_ValueChanged);
}
private void KeybindMenuItem_Click(object sender, EventArgs e) {
if (binds.Visible) {
binds.BringToFront();
} else {
binds.Show();
}
}
private void Position_ValueChanged(object sender, EventArgs e) {
if (!EnsureHooked()) {
return;
}
try {
Vect3F pos = GameHook.Position;
pos.X = Convert.ToSingle(xInput.Value);
pos.Y = Convert.ToSingle(yInput.Value);
pos.Z = Convert.ToSingle(zInput.Value);
GameHook.Position = pos;
} catch (Win32Exception) { }
}
// This just prevents the error noise when you press enter
private void Position_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
e.SuppressKeyPress = true;
}
}
internal void SavePosButtton_Click(object sender, EventArgs e) {
if (!EnsureHooked()) {
return;
}
try {
savedPos = GameHook.Position;
loadPosButton.Enabled = true;
} catch (Win32Exception) { }
}
internal void LoadPosButton_Click(object sender, EventArgs e) {
if (!EnsureHooked()) {
return;
}
if (savedPos.HasValue) {
try {
GameHook.Position = savedPos.Value;
allPosInputs.ForEach(input => input.ValueChanged -= Position_ValueChanged);
xInput.Value = Convert.ToDecimal(savedPos.Value.X);
yInput.Value = Convert.ToDecimal(savedPos.Value.Y);
zInput.Value = Convert.ToDecimal(savedPos.Value.Z);
allPosInputs.ForEach(input => input.ValueChanged += Position_ValueChanged);
} catch (Win32Exception) { }
}
}
internal void NextMapButton_Click(object sender, EventArgs e) {
stationMgr.NextMap();
mapLabel.Text = stationMgr.World;
stationLabel.Text = stationMgr.Station;
if (autoTeleportBox.Checked) {
TeleportButton_Click(this, null);
}
}
internal void PrevMapButton_Click(object sender, EventArgs e) {
stationMgr.PrevMap();
mapLabel.Text = stationMgr.World;
stationLabel.Text = stationMgr.Station;
if (autoTeleportBox.Checked) {
TeleportButton_Click(this, null);
}
}
internal void NextStationButton_Click(object sender, EventArgs e) {
stationMgr.NextStation();
mapLabel.Text = stationMgr.World;
stationLabel.Text = stationMgr.Station;
if (autoTeleportBox.Checked) {
TeleportButton_Click(this, null);
}
}
internal void PrevStationButton_Click(object sender, EventArgs e) {
stationMgr.PrevStation();
mapLabel.Text = stationMgr.World;
stationLabel.Text = stationMgr.Station;
if (autoTeleportBox.Checked) {
TeleportButton_Click(this, null);
}
}
internal void TeleportButton_Click(object sender, EventArgs e) {
if (!EnsureHooked()) {
return;
}
try {
GameHook.Position = stationMgr.Coords;
allPosInputs.ForEach(input => input.ValueChanged -= Position_ValueChanged);
xInput.Value = Convert.ToDecimal(stationMgr.Coords.X);
yInput.Value = Convert.ToDecimal(stationMgr.Coords.Y);
zInput.Value = Convert.ToDecimal(stationMgr.Coords.Z);
allPosInputs.ForEach(input => input.ValueChanged += Position_ValueChanged);
} catch (Win32Exception) { }
}
}
}