-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathEnumNode.cs
More file actions
183 lines (147 loc) · 4.67 KB
/
EnumNode.cs
File metadata and controls
183 lines (147 loc) · 4.67 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
using System;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Text;
using ReClassNET.Controls;
using ReClassNET.Extensions;
using ReClassNET.Memory;
using ReClassNET.Project;
using ReClassNET.UI;
namespace ReClassNET.Nodes
{
public class EnumNode : BaseNode
{
public override int MemorySize => (int)Enum.Size;
public EnumDescription Enum { get; private set; } = EnumDescription.Default;
public override void GetUserInterfaceInfo(out string name, out Image icon)
{
name = "Enum";
icon = Properties.Resources.B16x16_Button_Enum;
}
public void ChangeEnum(EnumDescription @enum)
{
Contract.Requires(@enum != null);
Enum = @enum;
GetParentContainer()?.ChildHasChanged(this);
}
private string GetTextRepresentation(MemoryBuffer memory)
{
return Enum.UseFlagsMode ? GetFlagsStringRepresentation(memory) : GetStringRepresentation(memory);
}
private long ReadSignedValueFromMemory(MemoryBuffer memory)
{
switch (Enum.Size)
{
case EnumDescription.UnderlyingTypeSize.OneByte:
return memory.ReadInt8(Offset);
case EnumDescription.UnderlyingTypeSize.TwoBytes:
return memory.ReadInt16(Offset);
case EnumDescription.UnderlyingTypeSize.FourBytes:
return memory.ReadInt32(Offset);
case EnumDescription.UnderlyingTypeSize.EightBytes:
return memory.ReadInt64(Offset);
default:
throw new ArgumentOutOfRangeException();
}
}
private string GetStringRepresentation(MemoryBuffer memory)
{
var value = ReadSignedValueFromMemory(memory);
var index = Enum.Values.FindIndex(kv => kv.Value == value);
if (index == -1)
{
return value.ToString();
}
return Enum.Values[index].Key;
}
private ulong ReadUnsignedValueFromMemory(MemoryBuffer memory)
{
switch (Enum.Size)
{
case EnumDescription.UnderlyingTypeSize.OneByte:
return memory.ReadUInt8(Offset);
case EnumDescription.UnderlyingTypeSize.TwoBytes:
return memory.ReadUInt16(Offset);
case EnumDescription.UnderlyingTypeSize.FourBytes:
return memory.ReadUInt32(Offset);
case EnumDescription.UnderlyingTypeSize.EightBytes:
return memory.ReadUInt64(Offset);
default:
throw new ArgumentOutOfRangeException();
}
}
private string GetFlagsStringRepresentation(MemoryBuffer memory)
{
var value = ReadUnsignedValueFromMemory(memory);
var result = value;
var values = Enum.Values;
var index = values.Count - 1;
var retval = new StringBuilder();
var firstTime = true;
var saveResult = result;
while (index >= 0)
{
var temp = (ulong)values[index].Value;
if (index == 0 && temp == 0)
{
break;
}
if ((result & temp) == temp)
{
result -= temp;
if (!firstTime)
{
retval.Prepend(" | ");
}
retval.Prepend(values[index].Key);
firstTime = false;
}
index--;
}
if (result != 0)
{
return value.ToString();
}
if (saveResult == 0)
{
if (values.Count > 0 && values[0].Value == 0)
{
return values[0].Key;
}
return "0";
}
return retval.ToString();
}
public override Size Draw(DrawContext context, int x, int y)
{
if (IsHidden && !IsWrapped)
{
return DrawHidden(context, x, y);
}
var origX = x;
AddSelection(context, x, y, context.Font.Height);
x = AddIconPadding(context, x);
x = AddIcon(context, x, y, context.IconProvider.Enum, HotSpot.NoneId, HotSpotType.None);
x = AddAddressOffset(context, x, y);
x = AddText(context, x, y, context.Settings.TypeColor, HotSpot.NoneId, "Enum") + context.Font.Width;
if (!IsWrapped)
{
x = AddText(context, x, y, context.Settings.NameColor, HotSpot.NameId, Name) + context.Font.Width;
}
x = AddText(context, x, y, context.Settings.ValueColor, HotSpot.NoneId, $"<{Enum.Name}>") + context.Font.Width;
x = AddIcon(context, x, y, context.IconProvider.Change, 4, HotSpotType.ChangeEnumType) + context.Font.Width;
x = AddText(context, x, y, context.Settings.TextColor, HotSpot.NoneId, "=") + context.Font.Width;
var text = GetTextRepresentation(context.Memory);
x = AddText(context, x, y, context.Settings.TextColor, HotSpot.NoneId, text) + context.Font.Width;
x = AddComment(context, x, y);
DrawInvalidMemoryIndicatorIcon(context, y);
AddContextDropDownIcon(context, y);
AddDeleteIcon(context, y);
return new Size(x - origX, context.Font.Height);
}
public override int CalculateDrawnHeight(DrawContext context)
{
return IsHidden && !IsWrapped ? HiddenHeight : context.Font.Height;
}
}
}