-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathMemoryPreviewPopUp.cs
More file actions
230 lines (181 loc) · 5.28 KB
/
MemoryPreviewPopUp.cs
File metadata and controls
230 lines (181 loc) · 5.28 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ReClassNET.Extensions;
using ReClassNET.Memory;
using ReClassNET.Nodes;
using ReClassNET.UI;
namespace ReClassNET.Controls
{
[ToolboxItem(false)]
public class MemoryPreviewPopUp : ToolStripDropDown
{
private const int ToolTipWidth = 1000 + ToolTipPadding;
private const int ToolTipPadding = 4;
/// <summary>Panel for the memory preview.</summary>
private class MemoryPreviewPanel : Panel
{
private const int MinNodeCount = 10;
public DrawContext DrawContext { get; }
private readonly List<BaseHexNode> nodes;
public MemoryPreviewPanel(FontEx font)
{
Contract.Requires(font != null);
DoubleBuffered = true;
nodes = new List<BaseHexNode>();
DrawContext = new DrawContext
{
Font = font,
IconProvider = new IconProvider(), // TODO use single instance
Memory = new MemoryBuffer(),
HotSpots = new List<HotSpot>()
};
SetNodeCount(MinNodeCount);
CalculateSize();
}
/// <summary>Sets the absolute number of nodes and resizes the underlaying memory buffer.</summary>
/// <param name="count">Number of nodes.</param>
private void SetNodeCount(int count)
{
BaseHexNode CreateNode(int index)
{
return new
#if RECLASSNET64
Hex64Node
#else
Hex32Node
#endif
{
Offset = index * IntPtr.Size
};
}
if (nodes.Count < count)
{
nodes.AddRange(Enumerable.Range(nodes.Count, count - nodes.Count).Select(CreateNode));
}
else if (nodes.Count > count && count >= MinNodeCount)
{
nodes.RemoveRange(count, nodes.Count - count);
}
DrawContext.Memory.Size = nodes.Select(n => n.MemorySize).Sum();
}
/// <summary>Changes the number of nodes with the provided delta.</summary>
/// <param name="delta">The change delta.</param>
public void ChangeNodeCount(int delta)
{
SetNodeCount(nodes.Count + delta);
CalculateSize();
}
/// <summary>Resets the settings of the panel to the defaults.</summary>
public void Reset()
{
SetNodeCount(MinNodeCount);
CalculateSize();
}
/// <summary>Calculates the size of the panel.</summary>
private void CalculateSize()
{
var size = new Size(
ToolTipWidth,
nodes.Sum(n => n.CalculateDrawnHeight(DrawContext)) + ToolTipPadding
);
DrawContext.ClientArea = new Rectangle(ToolTipPadding / 2, ToolTipPadding / 2, size.Width - ToolTipPadding, size.Height - ToolTipPadding);
Size = MinimumSize = MaximumSize = size;
}
protected override void OnPaint(PaintEventArgs e)
{
DrawContext.HotSpots.Clear();
// Some settings are not usefull for the preview.
DrawContext.Settings = Program.Settings.Clone();
DrawContext.Settings.ShowNodeAddress = false;
DrawContext.Graphics = e.Graphics;
using (var brush = new SolidBrush(DrawContext.Settings.BackgroundColor))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
}
using (var pen = new Pen(DrawContext.Settings.BackgroundColor.Invert(), 1))
{
e.Graphics.DrawRectangle(pen, new Rectangle(Bounds.X, Bounds.Y, Bounds.Width - 1, Bounds.Height - 1));
}
int x = -24;
int y = 2;
foreach (var node in nodes)
{
y += node.Draw(DrawContext, x, y).Height;
}
}
}
private readonly MemoryPreviewPanel panel;
private IntPtr memoryAddress;
protected override CreateParams CreateParams
{
get
{
const int WS_EX_NOACTIVATE = 0x08000000;
var cp = base.CreateParams;
cp.ExStyle |= WS_EX_NOACTIVATE;
return cp;
}
}
public MemoryPreviewPopUp(FontEx font)
{
Contract.Requires(font != null);
AutoSize = false;
AutoClose = false;
DoubleBuffered = true;
ResizeRedraw = true;
TabStop = false;
panel = new MemoryPreviewPanel(font)
{
Location = Point.Empty
};
var host = new ToolStripControlHost(panel);
Padding = Margin = host.Padding = host.Margin = Padding.Empty;
MinimumSize = panel.MinimumSize;
panel.MinimumSize = panel.Size;
MaximumSize = panel.MaximumSize;
panel.MaximumSize = panel.Size;
Size = panel.Size;
panel.SizeChanged += (s, e) => Size = MinimumSize = MaximumSize = panel.Size;
Items.Add(host);
}
protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
{
panel.Reset();
base.OnClosed(e);
}
internal void HandleMouseWheelEvent(MouseEventArgs e)
{
if (e.Delta != 0)
{
panel.ChangeNodeCount(e.Delta < 0 ? 1 : -1);
UpdateMemory();
Invalidate();
if (e is HandledMouseEventArgs he)
{
he.Handled = true;
}
}
}
/// <summary>Initializes the memory buffer.</summary>
/// <param name="process">The process to use.</param>
/// <param name="address">The address to read from.</param>
public void InitializeMemory(RemoteProcess process, IntPtr address)
{
Contract.Requires(process != null);
memoryAddress = address;
panel.DrawContext.Process = process;
panel.DrawContext.Memory.UpdateFrom(process, address);
}
/// <summary>Updates the memory buffer to get current data.</summary>
public void UpdateMemory()
{
panel.DrawContext.Memory.UpdateFrom(panel.DrawContext.Process, memoryAddress);
panel.Invalidate();
}
}
}