-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathBaseNode.cs
More file actions
552 lines (455 loc) · 17.4 KB
/
BaseNode.cs
File metadata and controls
552 lines (455 loc) · 17.4 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Drawing;
using ReClassNET.Controls;
using ReClassNET.Extensions;
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:X}";
internal static readonly List<INodeInfoReader> NodeInfoReader = new List<INodeInfoReader>();
protected static readonly int HiddenHeight = 0;
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 int 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 BaseNode ParentNode { get; internal set; }
/// <summary>Gets a value indicating whether this node is wrapped into an other node.</summary>
public bool IsWrapped => ParentNode is BaseWrapperNode;
/// <summary>Gets or sets a value indicating whether this node is hidden.</summary>
public bool IsHidden { get; set; }
/// <summary>Gets or sets a value indicating whether this node 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 GrowingList<bool> LevelsOpen { get; } = new GrowingList<bool>(false);
[ContractInvariantMethod]
private void ObjectInvariants()
{
Contract.Invariant(name != null);
Contract.Invariant(comment != null);
Contract.Invariant(Offset >= 0);
Contract.Invariant(LevelsOpen != null);
}
/// <summary>
/// Creates an instance of the specific node type.
/// </summary>
/// <param name="nodeType">The <see cref="Type"/> of the node.</param>
/// <returns>An instance of the node type or null if the type is not a valid node type.</returns>
public static BaseNode CreateInstanceFromType(Type nodeType)
{
return CreateInstanceFromType(nodeType, true);
}
/// <summary>
/// Creates an instance of the specific node type.
/// </summary>
/// <param name="nodeType">The <see cref="Type"/> of the node.</param>
/// <param name="callInitialize">If true <see cref="Initialize"/> gets called for the new node.</param>
/// <returns>An instance of the node type or null if the type is not a valid node type.</returns>
public static BaseNode CreateInstanceFromType(Type nodeType, bool callInitialize)
{
var node = Activator.CreateInstance(nodeType) as BaseNode;
if (callInitialize)
{
node?.Initialize();
}
return node;
}
/// <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;
}
public abstract void GetUserInterfaceInfo(out string name, out Image icon);
public virtual bool UseMemoryPreviewToolTip(HotSpot spot, out IntPtr address)
{
Contract.Requires(spot != 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>
/// <returns>The information to show in a tool tip or null if no information should be shown.</returns>
public virtual string GetToolTipText(HotSpot spot)
{
Contract.Requires(spot != null);
return null;
}
/// <summary>Called when the node was created. Does not get called after loading a project.</summary>
public virtual void Initialize()
{
}
/// <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;
Offset = node.Offset;
}
/// <summary>
/// Gets the parent container of the node.
/// </summary>
/// <returns></returns>
public BaseContainerNode GetParentContainer()
{
var parentNode = ParentNode;
while (parentNode != null)
{
if (parentNode is BaseContainerNode containerNode)
{
return containerNode;
}
parentNode = parentNode.ParentNode;
}
if (this is BaseContainerNode containerNode2)
{
return containerNode2;
}
return null;
}
/// <summary>
/// Gets the parent class of the node.
/// </summary>
/// <returns></returns>
public ClassNode GetParentClass()
{
var parentNode = ParentNode;
while (parentNode != null)
{
if (parentNode is ClassNode classNode)
{
return classNode;
}
parentNode = parentNode.ParentNode;
}
return null;
}
/// <summary>
/// Gets the root wrapper node if this node is the inner node of a wrapper chain.
/// </summary>
/// <returns>The root <see cref="BaseWrapperNode"/> or null if this node is not wrapped or isn't itself a wrapper node.</returns>
public BaseWrapperNode GetRootWrapperNode()
{
BaseWrapperNode rootWrapperNode = null;
var parentNode = ParentNode;
while (parentNode is BaseWrapperNode wrapperNode)
{
rootWrapperNode = wrapperNode;
parentNode = parentNode.ParentNode;
}
// Test if this node is the root wrapper node.
if (rootWrapperNode == null)
{
if (this is BaseWrapperNode wrapperNode)
{
return wrapperNode;
}
}
return rootWrapperNode;
}
/// <summary>Clears the selection of the node.</summary>
public virtual void ClearSelection()
{
IsSelected = false;
}
/// <summary>Draws the node.</summary>
/// <param name="context">The drawing context.</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(DrawContext context, 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(DrawContext, int, int)"/> method.
/// </summary>
/// <param name="context">The drawing context.</param>
/// <returns>The calculated height.</returns>
public abstract int CalculateDrawnHeight(DrawContext context);
/// <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="context">The drawing context.</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(DrawContext context, Rectangle spot, string text, int id, HotSpotType type)
{
Contract.Requires(context != null);
Contract.Requires(context.Memory != null);
Contract.Requires(text != null);
if (spot.Top > context.ClientArea.Bottom || spot.Bottom < 0)
{
return;
}
context.HotSpots.Add(new HotSpot
{
Rect = spot,
Text = text,
Address = context.Address + Offset,
Id = id,
Type = type,
Node = this,
Level = context.Level,
Process = context.Process,
Memory = context.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="context">The drawing context.</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(DrawContext context, int x, int y, Color color, int hitId, string text)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
Contract.Requires(context.Font != null);
Contract.Requires(text != null);
var width = Math.Max(text.Length, hitId != HotSpot.NoneId ? 1 : 0) * context.Font.Width;
if (y >= -context.Font.Height && y + context.Font.Height <= context.ClientArea.Bottom + context.Font.Height)
{
if (hitId != HotSpot.NoneId)
{
var rect = new Rectangle(x, y, width, context.Font.Height);
AddHotSpot(context, rect, text, hitId, HotSpotType.Edit);
}
context.Graphics.DrawStringEx(text, context.Font.Font, color, x, y);
/*using (var brush = new SolidBrush(color))
{
context.Graphics.DrawString(text, context.Font.Font, brush, x, y);
}*/
}
return x + width;
}
/// <summary>Draws the address and <see cref="Offset"/> of the node.</summary>
/// <param name="context">The drawing context.</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(DrawContext context, int x, int y)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
Contract.Requires(context.Font != null);
if (context.Settings.ShowNodeOffset)
{
x = AddText(context, x, y, context.Settings.OffsetColor, HotSpot.NoneId, $"{Offset:X04}") + context.Font.Width;
}
if (context.Settings.ShowNodeAddress)
{
x = AddText(context, x, y, context.Settings.AddressColor, HotSpot.AddressId, (context.Address + Offset).ToString(Constants.AddressHexFormat)) + context.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="context">The drawing context.</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(DrawContext context, int x, int y, int height)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
if (y > context.ClientArea.Bottom || y + height < 0 || IsWrapped)
{
return;
}
if (IsSelected)
{
using var brush = new SolidBrush(context.Settings.SelectedColor);
context.Graphics.FillRectangle(brush, 0, y, context.ClientArea.Right, height);
}
AddHotSpot(context, new Rectangle(0, y, context.ClientArea.Right - (IsSelected ? 16 : 0), height), string.Empty, HotSpot.NoneId, HotSpotType.Select);
}
protected int AddIconPadding(DrawContext view, int x)
{
return x + view.IconProvider.Dimensions;
}
/// <summary>Draws an icon and adds a <see cref="HotSpot"/> if <paramref name="id"/> is not <see cref="HotSpot.NoneId"/>.</summary>
/// <param name="context">The drawing context.</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(DrawContext context, int x, int y, Image icon, int id, HotSpotType type)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
Contract.Requires(icon != null);
var size = context.IconProvider.Dimensions;
if (y > context.ClientArea.Bottom || y + size < 0)
{
return x + size;
}
context.Graphics.DrawImage(icon, x + 2, y, size, size);
if (id != HotSpot.NoneId)
{
AddHotSpot(context, new Rectangle(x, y, size, size), string.Empty, id, type);
}
return x + size;
}
/// <summary>Adds a togglable Open/Close icon.</summary>
/// <param name="context">The drawing context.</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 AddOpenCloseIcon(DrawContext context, int x, int y)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
if (y > context.ClientArea.Bottom || y + context.IconProvider.Dimensions < 0)
{
return x + context.IconProvider.Dimensions;
}
var icon = LevelsOpen[context.Level] ? context.IconProvider.OpenCloseOpen : context.IconProvider.OpenCloseClosed;
return AddIcon(context, x, y, icon, 0, HotSpotType.OpenClose);
}
/// <summary>Draws a context drop icon if the node is selected.</summary>
/// <param name="context">The drawing context.</param>
/// <param name="y">The y coordinate.</param>
protected void AddContextDropDownIcon(DrawContext context, int y)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
if (context.MultipleNodesSelected || y > context.ClientArea.Bottom || y + context.IconProvider.Dimensions < 0 || IsWrapped)
{
return;
}
if (IsSelected)
{
AddIcon(context, 0, y, context.IconProvider.DropArrow, 0, HotSpotType.Context);
}
}
/// <summary>Draws a delete icon if the node is selected.</summary>
/// <param name="context">The drawing context.</param>
/// <param name="y">The y coordinate.</param>
protected void AddDeleteIcon(DrawContext context, int y)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
if (y > context.ClientArea.Bottom || y + context.IconProvider.Dimensions < 0 || IsWrapped)
{
return;
}
if (IsSelected)
{
AddIcon(context, context.ClientArea.Right - context.IconProvider.Dimensions, y, context.IconProvider.Delete, 0, HotSpotType.Delete);
}
}
/// <summary>Draws the <see cref="Comment"/>.</summary>
/// <param name="context">The drawing context.</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(DrawContext context, int x, int y)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
Contract.Requires(context.Font != null);
x = AddText(context, x, y, context.Settings.CommentColor, HotSpot.NoneId, "//");
x = AddText(context, x, y, context.Settings.CommentColor, HotSpot.CommentId, Comment) + context.Font.Width;
return x;
}
/// <summary>Draws a vertical line to show the hidden state.</summary>
/// <param name="context">The drawing context.</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(DrawContext context, int x, int y)
{
Contract.Requires(context != null);
Contract.Requires(context.Graphics != null);
using (var brush = new SolidBrush(IsSelected ? context.Settings.SelectedColor : context.Settings.HiddenColor))
{
context.Graphics.FillRectangle(brush, 0, y, context.ClientArea.Right, 1);
}
return new Size(0, HiddenHeight);
}
/// <summary>Draws an error indicator if the used memory buffer is not valid.</summary>
/// <param name="context">The drawing context.</param>
/// <param name="y">The y coordinate.</param>
protected void DrawInvalidMemoryIndicatorIcon(DrawContext context, int y)
{
if (!context.Memory.ContainsValidData)
{
AddIcon(context, 0, y, Properties.Resources.B16x16_Error, HotSpot.NoneId, HotSpotType.None);
}
}
}
[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(DrawContext context, int x, int y)
{
Contract.Requires(context != null);
throw new NotImplementedException();
}
public override int CalculateDrawnHeight(DrawContext context)
{
Contract.Requires(context != null);
throw new NotImplementedException();
}
}
}