forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseNode.cs
More file actions
444 lines (365 loc) · 14.2 KB
/
BaseNode.cs
File metadata and controls
444 lines (365 loc) · 14.2 KB
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
436
437
438
439
440
441
442
443
444
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Drawing;
using ReClassNET.Extensions;
using ReClassNET.Memory;
using ReClassNET.UI;
using ReClassNET.Util;
namespace ReClassNET.Nodes
{
public delegate void NodeEventHandler(BaseNode sender);
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
[ContractClass(typeof(BaseNodeContract))]
public abstract class BaseNode
{
private string DebuggerDisplay => $"Type: {GetType().Name} Name: {Name} Offset: 0x{Offset.ToString("X")}";
internal static readonly List<INodeInfoReader> NodeInfoReader = new List<INodeInfoReader>();
protected static readonly int TextPadding = Icons.Dimensions;
protected static readonly int HiddenHeight = 1;
private static int nodeIndex = 0;
private string name = string.Empty;
private string comment = string.Empty;
/// <summary>Gets or sets the offset of the node.</summary>
public IntPtr Offset { get; set; }
/// <summary>Gets or sets the name of the node. If a new name was set the property changed event gets fired.</summary>
public virtual string Name { get => name; set { if (value != null && name != value) { name = value; NameChanged?.Invoke(this); } } }
/// <summary>Gets or sets the comment of the node.</summary>
public string Comment { get => comment; set { if (value != null && comment != value) { comment = value; CommentChanged?.Invoke(this); } } }
/// <summary>Gets or sets the parent node.</summary>
public BaseContainerNode ParentNode { get; internal set; }
/// <summary>Gets or sets a value indicating whether this object is hidden.</summary>
public bool IsHidden { get; protected set; }
/// <summary>Gets or sets a value indicating whether this object is selected.</summary>
public bool IsSelected { get; set; }
/// <summary>Size of the node in bytes.</summary>
public abstract int MemorySize { get; }
public event NodeEventHandler NameChanged;
public event NodeEventHandler CommentChanged;
protected readonly GrowingList<bool> levelsOpen = new GrowingList<bool>(false);
[ContractInvariantMethod]
private void ObjectInvariants()
{
Contract.Invariant(name != null);
Contract.Invariant(comment != null);
Contract.Invariant(Offset.ToInt32() >= 0);
Contract.Invariant(levelsOpen != null);
}
/// <summary>Constructor which sets a unique <see cref="Name"/>.</summary>
protected BaseNode()
{
Contract.Ensures(name != null);
Contract.Ensures(comment != null);
Name = $"N{nodeIndex++:X08}";
Comment = string.Empty;
levelsOpen[0] = true;
}
/// <summary>Clears the selection of the node.</summary>
public virtual void ClearSelection()
{
IsSelected = false;
}
/// <summary>Initializes this object from the given node. It copies the name and the comment.</summary>
/// <param name="node">The node to copy from.</param>
public virtual void CopyFromNode(BaseNode node)
{
Contract.Requires(node != null);
Name = node.Name;
Comment = node.Comment;
}
/// <summary>Called when the node was created. Does not get called after loading a project.</summary>
public virtual void Intialize()
{
}
public virtual bool UseMemoryPreviewToolTip(HotSpot spot, MemoryBuffer memory, out IntPtr address)
{
Contract.Requires(spot != null);
Contract.Requires(memory != null);
address = IntPtr.Zero;
return false;
}
/// <summary>Gets informations about this node to show in a tool tip.</summary>
/// <param name="spot">The spot.</param>
/// <param name="memory">The process memory.</param>
/// <returns>The information to show in a tool tip or null if no information should be shown.</returns>
public virtual string GetToolTipText(HotSpot spot, MemoryBuffer memory)
{
Contract.Requires(spot != null);
Contract.Requires(memory != null);
return null;
}
/// <summary>Draws the node.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <returns>The pixel size the node occupies.</returns>
public abstract Size Draw(ViewInfo view, int x, int y);
/// <summary>
/// Calculates the height of the node if drawn.
/// This method is used to determine if a node outside the visible area should be drawn.
/// The returned height must be equal to the height which is returned by the <see cref="Draw(ViewInfo, int, int)"/> method.
/// </summary>
/// <param name="view">The view information.</param>
/// <returns>The calculated height.</returns>
public abstract int CalculateDrawnHeight(ViewInfo view);
/// <summary>Updates the node from the given <paramref name="spot"/>. Sets the <see cref="Name"/> and <see cref="Comment"/> of the node.</summary>
/// <param name="spot">The spot.</param>
public virtual void Update(HotSpot spot)
{
Contract.Requires(spot != null);
if (spot.Id == HotSpot.NameId)
{
Name = spot.Text;
}
else if (spot.Id == HotSpot.CommentId)
{
Comment = spot.Text;
}
}
/// <summary>Toggles the specified level.</summary>
/// <param name="level">The level to toggle.</param>
internal void ToggleLevelOpen(int level)
{
levelsOpen[level] = !levelsOpen[level];
}
/// <summary>Sets the specific level.</summary>
/// <param name="level">The level to set.</param>
/// <param name="open">True to open.</param>
internal void SetLevelOpen(int level, bool open)
{
levelsOpen[level] = open;
}
/// <summary>Adds a <see cref="HotSpot"/> the user can interact with.</summary>
/// <param name="view">The view information.</param>
/// <param name="spot">The spot.</param>
/// <param name="text">The text to edit.</param>
/// <param name="id">The id of the spot.</param>
/// <param name="type">The type of the spot.</param>
protected void AddHotSpot(ViewInfo view, Rectangle spot, string text, int id, HotSpotType type)
{
Contract.Requires(view != null);
Contract.Requires(view.Memory != null);
Contract.Requires(text != null);
if (spot.Top > view.ClientArea.Bottom || spot.Bottom < 0)
{
return;
}
view.HotSpots.Add(new HotSpot
{
Rect = spot,
Text = text,
Address = view.Address.Add(Offset),
Id = id,
Type = type,
Node = this,
Level = view.Level,
Memory = view.Memory
});
}
/// <summary>Draws the specific text and adds a <see cref="HotSpot"/> if <paramref name="hitId"/> is not <see cref="HotSpot.NoneId"/>.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="color">The color of the text.</param>
/// <param name="hitId">Id for the clickable area.</param>
/// <param name="text">The text to draw.</param>
/// <returns>The new x coordinate after drawing the text.</returns>
protected int AddText(ViewInfo view, int x, int y, Color color, int hitId, string text)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
Contract.Requires(view.Font != null);
Contract.Requires(text != null);
var width = Math.Max(text.Length, hitId != HotSpot.NoneId ? 1 : 0) * view.Font.Width;
if (y >= -view.Font.Height && y + view.Font.Height <= view.ClientArea.Bottom + view.Font.Height)
{
if (hitId != HotSpot.NoneId)
{
var rect = new Rectangle(x, y, width, view.Font.Height);
AddHotSpot(view, rect, text, hitId, HotSpotType.Edit);
}
view.Context.DrawStringEx(text, view.Font.Font, color, x, y);
/*using (var brush = new SolidBrush(color))
{
view.Context.DrawString(text, view.Font.Font, brush, x, y);
}*/
}
return x + width;
}
/// <summary>Draws the address and <see cref="Offset"/> of the node.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <returns>The new x coordinate after drawing the text.</returns>
protected int AddAddressOffset(ViewInfo view, int x, int y)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
Contract.Requires(view.Font != null);
if (view.Settings.ShowNodeOffset)
{
x = AddText(view, x, y, view.Settings.OffsetColor, HotSpot.NoneId, $"{Offset.ToInt32():X04}") + view.Font.Width;
}
if (view.Settings.ShowNodeAddress)
{
x = AddText(view, x, y, view.Settings.AddressColor, HotSpot.AddressId, view.Address.Add(Offset).ToString(Constants.AddressHexFormat)) + view.Font.Width;
}
return x;
}
/// <summary>Draws a bar which indicates the selection status of the node. A <see cref="HotSpot"/> for this area gets added too.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="height">The height of the bar.</param>
protected void AddSelection(ViewInfo view, int x, int y, int height)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
if (y > view.ClientArea.Bottom || y + height < 0)
{
return;
}
if (IsSelected)
{
using (var brush = new SolidBrush(view.Settings.SelectedColor))
{
view.Context.FillRectangle(brush, 0, y, view.ClientArea.Right, height);
}
}
AddHotSpot(view, new Rectangle(0, y, view.ClientArea.Right - (IsSelected ? 16 : 0), height), string.Empty, -1, HotSpotType.Select);
}
/// <summary>Draws an icon and adds a <see cref="HotSpot"/> if <paramref name="id"/> is not <see cref="HotSpot.NoneId"/>.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="icon">The icon.</param>
/// <param name="id">The id of the spot.</param>
/// <param name="type">The type of the spot.</param>
/// <returns>The new x coordinate after drawing the icon.</returns>
protected int AddIcon(ViewInfo view, int x, int y, Image icon, int id, HotSpotType type)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
Contract.Requires(icon != null);
if (y > view.ClientArea.Bottom || y + Icons.Dimensions < 0)
{
return x + Icons.Dimensions;
}
view.Context.DrawImage(icon, x + 2, y, Icons.Dimensions, Icons.Dimensions);
if (id != -1)
{
AddHotSpot(view, new Rectangle(x, y, Icons.Dimensions, Icons.Dimensions), string.Empty, id, type);
}
return x + Icons.Dimensions;
}
/// <summary>Adds a togglable Open/Close icon.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <returns>The new x coordinate after drawing the icon.</returns>
protected int AddOpenClose(ViewInfo view, int x, int y)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
if (y > view.ClientArea.Bottom || y + Icons.Dimensions < 0)
{
return x + Icons.Dimensions;
}
return AddIcon(view, x, y, levelsOpen[view.Level] ? Icons.OpenCloseOpen : Icons.OpenCloseClosed, 0, HotSpotType.OpenClose);
}
/// <summary>Draws a type drop icon if the node is selected.</summary>
/// <param name="view">The view information.</param>
/// <param name="y">The y coordinate.</param>
protected void AddTypeDrop(ViewInfo view, int y)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
if (view.MultipleNodesSelected || (y > view.ClientArea.Bottom || y + Icons.Dimensions < 0))
{
return;
}
if (IsSelected)
{
AddIcon(view, 0, y, Icons.DropArrow, 0, HotSpotType.Drop);
}
}
/// <summary>Draws a delete icon if the node is selected.</summary>
/// <param name="view">The view information.</param>
/// <param name="y">The y coordinate.</param>
protected void AddDelete(ViewInfo view, int y)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
if (y > view.ClientArea.Bottom || y + Icons.Dimensions < 0)
{
return;
}
if (IsSelected)
{
AddIcon(view, view.ClientArea.Right - Icons.Dimensions, y, Icons.Delete, 0, HotSpotType.Delete);
}
}
/// <summary>Draws the <see cref="Comment"/>.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <returns>The new x coordinate after drawing the comment.</returns>
protected virtual int AddComment(ViewInfo view, int x, int y)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
Contract.Requires(view.Font != null);
x = AddText(view, x, y, view.Settings.CommentColor, HotSpot.NoneId, "//");
x = AddText(view, x, y, view.Settings.CommentColor, HotSpot.CommentId, Comment) + view.Font.Width;
return x;
}
/// <summary>Draws a vertical line to show the hidden state.</summary>
/// <param name="view">The view information.</param>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <returns>The size of the drawing.</returns>
protected Size DrawHidden(ViewInfo view, int x, int y)
{
Contract.Requires(view != null);
Contract.Requires(view.Context != null);
using (var brush = new SolidBrush(IsSelected ? view.Settings.SelectedColor : view.Settings.HiddenColor))
{
view.Context.FillRectangle(brush, 0, y, view.ClientArea.Right, 1);
}
return new Size(0, HiddenHeight);
}
/// <summary>Draws an error indicator if the used memory buffer is not valid.</summary>
/// <param name="view">The view information.</param>
/// <param name="y">The y coordinate.</param>
protected void DrawInvalidMemoryIndicator(ViewInfo view, int y)
{
if (!view.Memory.ContainsValidData)
{
view.Context.DrawImage(Properties.Resources.B16x16_Error, 2, y);
}
}
}
[ContractClassFor(typeof(BaseNode))]
internal abstract class BaseNodeContract : BaseNode
{
public override int MemorySize
{
get
{
Contract.Ensures(Contract.Result<int>() >= 0);
throw new NotImplementedException();
}
}
public override Size Draw(ViewInfo view, int x, int y)
{
Contract.Requires(view != null);
throw new NotImplementedException();
}
public override int CalculateDrawnHeight(ViewInfo view)
{
Contract.Requires(view != null);
throw new NotImplementedException();
}
}
}