forked from 2xlbu4u/NativeWorkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertiesControl.cs
435 lines (366 loc) · 13.7 KB
/
PropertiesControl.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GTA;
public class PropertiesControl
{
private DataGridView _propertyControlGrid;
private int MAX_PROP_ROWS = 25;
private ComboBox _nativeGroupDDL;
private ComboBox _nativeNameDDL;
private Button _addToPropBtn;
private Button _addOnTickBtn;
private Button _addImmediateButton;
private TextBox _ontickText;
private TextBox _immediateText;
private TextBox _outputText;
private CheckBox _enablePropUpdate;
private CheckBox _enableBoolColors;
private Dictionary<int, PropertyBoundItem> _propMapData = new Dictionary<int, PropertyBoundItem>();
private NativeWorkbenchForm _parent;
public PropertiesControl(NativeWorkbenchForm parent,
DataGridView propertyControlGrid, ComboBox nativeGroupDDL, ComboBox nativeNameDDL,
Button addToPropBtn, Button addOnTickBtn, Button addImmediateButton, TextBox ontickText, TextBox immediateText, TextBox ouputText,
CheckBox enablePropUpdate, CheckBox enableBoolColors)
{
_parent = parent;
_propertyControlGrid = propertyControlGrid;
_propertyControlGrid.Click += _propertyControlGrid_Click;
_propertyControlGrid.LostFocus += _propertyControlGrid_LostFocus;
NativeManager.Init();
_nativeNameDDL = nativeNameDDL;
_nativeGroupDDL = nativeGroupDDL;
ItemGroup[] values = (ItemGroup[])Enum.GetValues(typeof(ItemGroup));
var nativeValues = values.OrderBy(v => v.ToString()).ToList();
nativeValues.Insert(0, ItemGroup.Select);
_nativeGroupDDL.DataSource = nativeValues;
_nativeNameDDL.DisplayMember = "NativeNameReturn";
_nativeNameDDL.ValueMember = "NativeHash";
_nativeNameDDL.SelectedValueChanged += _nativeNameDDL_SelectedValueChanged;
_nativeGroupDDL.SelectedValueChanged += nativeGroupDDL_SelectedValueChanged;
_nativeGroupDDL.GotFocus += _nativeGroupDDL_GotFocus;
_nativeNameDDL.GotFocus += _nativeNameDDL_GotFocus;
_ontickText = ontickText;
_outputText = ouputText;
_immediateText = immediateText;
_enablePropUpdate = enablePropUpdate;
_enablePropUpdate.CheckStateChanged += _enablePropUpdate_CheckStateChanged;
_enableBoolColors = enableBoolColors;
_enableBoolColors.CheckStateChanged += _enableBoolColors_CheckStateChanged;
_addToPropBtn = addToPropBtn;
_addOnTickBtn = addOnTickBtn;
_addImmediateButton = addImmediateButton;
_addToPropBtn.Click += _addBtnClick;
_addOnTickBtn.Click += _addBtnClick;
_addImmediateButton.Click += _addBtnClick;
_propertyControlGrid.MouseDown += _propertyControlGrid_MouseDown;
// _propertyControlGrid
initGrid();
}
void _nativeNameDDL_GotFocus(object sender, EventArgs e)
{
_propertyControlGrid.ClearSelection();
}
void _nativeGroupDDL_GotFocus(object sender, EventArgs e)
{
_propertyControlGrid.ClearSelection();
}
void _propertyControlGrid_LostFocus(object sender, EventArgs e)
{
}
void _propertyControlGrid_Click(object sender, EventArgs e)
{
_addOnTickBtn.Enabled = true;
_addImmediateButton.Enabled = true;
}
void _enablePropUpdate_CheckStateChanged(object sender, EventArgs e)
{
if (!_enablePropUpdate.Checked)
{
// Unchecked means must be recompiled
foreach (var key in _propMapData.Keys)
{
PropertyBoundItem propertyBoundItem = _propMapData[key];
// Submit for compile/run
propertyBoundItem.CompileHandle = null;
}
}
}
void _enableBoolColors_CheckStateChanged(object sender, EventArgs e)
{
if (!_enableBoolColors.Checked)
{
for (var i = 0; i < MAX_PROP_ROWS; i++)
_propertyControlGrid[1, i].Style.BackColor = Color.White;
}
}
private void initGrid()
{
_propertyControlGrid.RowTemplate.Height = 20;
_propertyControlGrid.Columns[0].Width = 250;
for (var i = 0; i < MAX_PROP_ROWS; i++)
{
_propertyControlGrid.Rows.Add("", "");
}
_propertyControlGrid.ClearSelection();
}
public List<string> GetPropsForSave()
{
var propData = new List<string>();
foreach (var dict in _propMapData)
{
var codeBehind = dict.Value.CodeBehind.TrimEnd(new []{'\n','\r'});
propData.Add(string.Format("{0}#{1}#{2}", dict.Key, dict.Value.Label, codeBehind));
propData.Add(dict.Key + "#end");
}
return propData;
}
public void OnLoadWork(List<string> propDatalines)
{
StringBuilder sb = new StringBuilder();
int rowId = -1;
string label = "";
for (var i = 0; i < propDatalines.Count; i++)
{
var line = propDatalines[i];
if (line == "")
continue;
var parts = line.Split("#".ToCharArray());
if (parts.Length < 3)
{
// not normal parse
if (parts.Length == 1)
{
// additional code
sb.AppendLine(parts[0]);
}
else if (parts[1] == "end")
{
// End of section
OnAddProp(label, sb.ToString(), rowId);
sb.Clear();
}
}
else
{
// normal parse
rowId = int.Parse(parts[0]);
label = parts[1];
sb.AppendLine(parts[2]);
}
}
}
#region Native
private void nativeGroupDDL_SelectedValueChanged(object sender, EventArgs e)
{
ItemGroup itemGroup = (ItemGroup)_nativeGroupDDL.SelectedItem;
var descriptors = NativeManager.GetDescriptorsByItemGroup(itemGroup);
var namedDescriptors = new List<NativeDescriptor>();
var unamedDescriptors = new List<NativeDescriptor>();
foreach (var descriptor in descriptors)
{
if (descriptor.NativeName.StartsWith("_0x"))
unamedDescriptors.Add(descriptor);
else
namedDescriptors.Add(descriptor);
}
var namedDescriptorsSorted = namedDescriptors.OrderBy(v => v.NativeName).ToList();
var unamedDescriptorsSorted = unamedDescriptors.OrderBy(v => v.NativeName).ToList();
namedDescriptorsSorted.AddRange(unamedDescriptorsSorted);
_nativeNameDDL.DataSource = namedDescriptorsSorted;
}
// Native Name selected
void _nativeNameDDL_SelectedValueChanged(object sender, EventArgs e)
{
NativeDescriptor nd = (NativeDescriptor)_nativeNameDDL.SelectedItem;
_addToPropBtn.Enabled = true;
_addOnTickBtn.Enabled = true;
_addImmediateButton.Enabled = true;
Debug.WriteLine(nd.ToLongName());
}
private void _addBtnClick(object sender, EventArgs e)
{
var theButton = (Button)sender;
NativeDescriptor nd = (NativeDescriptor)_nativeNameDDL.SelectedItem;
var textToAppend = "";
if (_propertyControlGrid.SelectedRows.Count > 0)
textToAppend = _propertyControlGrid.SelectedRows[0].Cells[1].Value.ToString();
else
{
textToAppend = nd.ToCodeFormat();
}
if (theButton.Name == "_addOnTickBtn")
{
_ontickText.AppendText(textToAppend);
_ontickText.AppendText("");
_ontickText.Select();
}
else if (theButton.Name == "_addImmediateBtn")
{
_immediateText.AppendText(textToAppend);
_immediateText.AppendText("");
_immediateText.Select();
}
else if (theButton.Name == "_addToPropBtn")
{
OnAddProp(nd.NativeName, nd.ToCodeFormat());
}
}
#endregion
#region Property Add
void _propertyControlGrid_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
var hti = _propertyControlGrid.HitTest(e.X, e.Y);
_propertyControlGrid.ClearSelection();
_propertyControlGrid.Rows[hti.RowIndex].Selected = true;
_enablePropUpdate.Checked = false;
EditProperty editPropertyPopup = new EditProperty();
if (_propMapData.ContainsKey(hti.RowIndex) && _propMapData[hti.RowIndex] != null)
{
var propBoundItem = _propMapData[hti.RowIndex];
editPropertyPopup.DisplayLabel = propBoundItem.Label;
editPropertyPopup.CodeBehind = propBoundItem.CodeBehind;
editPropertyPopup.Refresh();
}
_parent.TopMost = false;
editPropertyPopup.StartPosition = FormStartPosition.CenterParent;
DialogResult dialogresult = editPropertyPopup.ShowDialog(_parent);
if (dialogresult == DialogResult.OK)
{
OnAddProp(editPropertyPopup.DisplayLabel, editPropertyPopup.CodeBehind);
}
_parent.TopMost = true;
editPropertyPopup.Dispose();
}
}
public void OnAddProp(string label, string codeBehind, int rowIndex = -1)
{
if (rowIndex == -1 && _propertyControlGrid.SelectedCells.Count == 0)
return;
var rowIndexVal = rowIndex >= 0
? rowIndex
: _propertyControlGrid.SelectedCells[0].RowIndex;
var theRow = _propertyControlGrid.Rows[rowIndexVal];
theRow.Cells[0].Value = label;
theRow.Cells[1].Value = "";
var newBoundItem = new PropertyBoundItem(label, codeBehind);
if (!_propMapData.ContainsKey(theRow.Index))
_propMapData.Add(theRow.Index, newBoundItem);
else
{
_propMapData[theRow.Index] = newBoundItem;
}
if (label == "" && codeBehind == "")
_propMapData.Remove(theRow.Index);
}
private void SetProp(int row, object value, string name = null)
{
if (name != null)
_propertyControlGrid[0, row].Value = name;
if (value != null)
_propertyControlGrid[1, row].Value = value.ToString();
if (_enableBoolColors.Checked)
{
var backColor = Color.White;
if (value.ToString() == "True")
backColor = Color.LightGreen;
else if (value.ToString() == "False")
backColor = Color.LightCoral;
_propertyControlGrid[1, row].Style.BackColor = backColor;
}
}
#endregion Region
public void OnYieldTick()
{
updatePropertiesGrid();
}
private void updatePropertiesGrid()
{
if (_propMapData.Count == 0)
return;
// StringBuilder sb = new StringBuilder();
foreach (var key in _propMapData.Keys)
{
PropertyBoundItem propertyBoundItem = _propMapData[key];
// Submit for compile/run
if (propertyBoundItem.CompileHandle == null)
{
string errorText;
propertyBoundItem.CompileHandle = CSCompile.DoCompile(propertyBoundItem.CodeBehind, out errorText);
if (errorText != null)
{
SetProp(key, "Compile Fail - See Output for Details");
_outputText.Text = errorText;
propertyBoundItem.CompileHandle = null;
continue;
}
else
{
_outputText.Text = "Compiled Successfully";
}
}
try
{
propertyBoundItem.RunResults = CSCompile.DoRun(propertyBoundItem.CompileHandle);
SetProp(key, propertyBoundItem.RunResults);
}
catch (Exception ex)
{
SetProp(key, "Code Execution Fail on Row " + key + " - See Output for Details");
_outputText.Text = ex.ToString();
}
}
_propertyControlGrid.ClearSelection();
_propertyControlGrid.Refresh();
}
//private void loadCustomHash()
//{
// string[] lines;
// if (!File.Exists(CUSTOM_HASH_FILE))
// {
// File.WriteAllText(CUSTOM_HASH_FILE, "");
// }
// lines = File.ReadAllLines(CUSTOM_HASH_FILE);
// foreach (var line in lines)
// {
// var keyValSplit = line.Split("=".ToCharArray());
// string strVal = keyValSplit[0];
// int intKey = Convert.ToInt32(keyValSplit[1]);
// if (!_customHashLookup.ContainsKey(intKey))
// {
// _customHashLookup.Add(intKey, strVal);
// }
// }
//}
//private Entity getLastTargettedEntity()
//{
// var thisEntity = Game.Player.GetTargetedEntity();
// if (thisEntity != null)
// {
// _lastTargettedEntity = thisEntity;
// }
// return _lastTargettedEntity;
//}
//private string getCustomHashName(int hashID)
//{
// if (_customHashLookup.ContainsKey(hashID))
// return _customHashLookup[hashID];
// return hashID.ToString();
//}
//private void saveCustomHashName(int hashID, string hashName)
//{
// if (!_customHashLookup.ContainsKey(hashID))
// {
// _customHashLookup.Add(hashID, hashName);
// var newEntry = string.Format("{0}={1}", hashName, hashID);
// File.AppendAllLines(CUSTOM_HASH_FILE, new[] { newEntry });
// }
//}
}