forked from Critical-Impact/CriticalCommonLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
291 lines (251 loc) · 12.4 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using CriticalCommonLib.Helpers;
using Dalamud.Game.Text.SeStringHandling;
using FFXIVClientStructs.FFXIV.Client.Graphics;
using FFXIVClientStructs.FFXIV.Client.System.String;
using ImGuiNET;
using Lumina.Excel;
using Lumina.Excel.GeneratedSheets;
namespace CriticalCommonLib
{
public static class Utils
{
public static Vector4 ConvertUiColorToColor(UIColor uiColor)
{
var temp = BitConverter.GetBytes(uiColor.UIForeground);
return new Vector4((float) temp[3] / 255,
(float) temp[2] / 255,
(float) temp[1] / 255,
(float) temp[0] / 255);
}
public static string GenerateRandomId()
{
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[8];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var finalString = new String(stringChars);
return finalString;
}
public static unsafe SeString ReadSeString(Utf8String xivString) {
var len = (int) (xivString.BufUsed > int.MaxValue ? int.MaxValue : xivString.BufUsed);
var bytes = new byte[len];
Marshal.Copy(new IntPtr(xivString.StringPtr), bytes, 0, len);
return SeString.Parse(bytes);
}
public static ByteColor ColorFromHex(string hexString, int alpha)
{
if (hexString.IndexOf('#') != -1)
hexString = hexString.Replace("#", "");
var r = int.Parse(hexString.Substring(0, 2), NumberStyles.AllowHexSpecifier);
var g = int.Parse(hexString.Substring(2, 2), NumberStyles.AllowHexSpecifier);
var b = int.Parse(hexString.Substring(4, 2), NumberStyles.AllowHexSpecifier);
return new ByteColor() {R = (byte) r, B = (byte) b, G = (byte) g, A = (byte) alpha};
}
public static ByteColor ColorFromVector4(Vector4 hexString)
{
return new () {R = (byte) (hexString.X * 0xFF), B = (byte) (hexString.Z * 0xFF), G = (byte) (hexString.Y * 0xFF), A = (byte) (hexString.W * 0xFF)};
}
private static ulong _beginModule;
private static ulong _endModule;
public static void ClickToCopyText(string text, string? textCopy = null) {
textCopy ??= text;
ImGui.Text($"{text}");
if (ImGui.IsItemHovered()) {
ImGui.SetMouseCursor(ImGuiMouseCursor.Hand);
if (textCopy != text) ImGui.SetTooltip(textCopy);
}
if (ImGui.IsItemClicked()) ImGui.SetClipboardText($"{textCopy}");
}
public static unsafe void PrintOutObject(object obj, ulong addr, List<string> path, bool autoExpand = false, string? headerText = null) {
if (obj is Utf8String utf8String) {
var text = string.Empty;
Exception? err = null;
try {
var s = utf8String.BufUsed > int.MaxValue ? int.MaxValue : (int) utf8String.BufUsed;
if (s > 1) {
text = Encoding.UTF8.GetString(utf8String.StringPtr, s - 1);
}
} catch (Exception ex) {
err = ex;
}
if (err != null) {
ImGui.TextDisabled(err.Message);
ImGui.SameLine();
} else {
ImGui.Text($"\"{text}\"");
ImGui.SameLine();
}
}
var pushedColor = 0;
var openedNode = false;
try {
if (_endModule == 0 && _beginModule == 0) {
try {
_beginModule = (ulong) Process.GetCurrentProcess().MainModule!.BaseAddress.ToInt64();
_endModule = (_beginModule + (ulong)Process.GetCurrentProcess().MainModule!.ModuleMemorySize);
} catch {
_endModule = 1;
}
}
ImGui.PushStyleColor(ImGuiCol.Text, 0xFF00FFFF);
pushedColor++;
if (autoExpand) {
ImGui.SetNextItemOpen(true, ImGuiCond.Appearing);
}
headerText ??= $"{obj}";
if (ImGui.TreeNode($"{headerText}##print-obj-{addr:X}-{string.Join("-", path)}")) {
openedNode = true;
ImGui.PopStyleColor();
pushedColor--;
foreach (var f in obj.GetType().GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance)) {
var fixedBuffer = (FixedBufferAttribute?) f.GetCustomAttribute(typeof(FixedBufferAttribute));
if (fixedBuffer != null) {
ImGui.Text($"fixed");
ImGui.SameLine();
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{fixedBuffer.ElementType.Name}[0x{fixedBuffer.Length:X}]");
} else {
if (f.FieldType.IsArray) {
var arr = (Array) f.GetValue(obj)!;
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{f.FieldType.GetElementType()?.Name ?? f.FieldType.Name}[{arr.Length}]");
} else {
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{f.FieldType.Name}");
}
}
ImGui.SameLine();
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.4f, 1), $"{f.Name}: ");
ImGui.SameLine();
PrintOutValue(addr, new List<string>(path) { f.Name }, f.FieldType, f.GetValue(obj)!, f);
}
foreach (var p in obj.GetType().GetProperties()) {
if (p.PropertyType.IsGenericType) {
var gTypeName = string.Join(',', p.PropertyType.GetGenericArguments().Select(gt => gt.Name));
var baseName = p.PropertyType.Name.Split('`')[0];
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{baseName}<{gTypeName}>");
} else {
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{p.PropertyType.Name}");
}
ImGui.SameLine();
ImGui.TextColored(new Vector4(0.2f, 0.6f, 0.4f, 1), $"{p.Name}: ");
ImGui.SameLine();
PrintOutValue(addr, new List<string>(path) { p.Name }, p.PropertyType, p.GetValue(obj)!, p);
}
openedNode = false;
ImGui.TreePop();
} else {
ImGui.PopStyleColor();
pushedColor--;
}
} catch (Exception ex) {
ImGui.Text($"{{{ ex }}}");
}
if (openedNode) ImGui.TreePop();
if (pushedColor > 0) ImGui.PopStyleColor(pushedColor);
}
private static unsafe void PrintOutValue(ulong addr, List<string> path, Type type, object value, MemberInfo member) {
try {
var valueParser = member.GetCustomAttribute(typeof(ValueParser));
if (valueParser is ValueParser vp) {
vp.ImGuiPrint(type, value, member, addr);
return;
}
if (type.IsPointer) {
var val = (Pointer) value;
var unboxed = Pointer.Unbox(val);
if (unboxed != null) {
var unboxedAddr = (ulong) unboxed;
ClickToCopyText($"{(ulong) unboxed:X}");
if (_beginModule > 0 && unboxedAddr >= _beginModule && unboxedAddr <= _endModule) {
ImGui.SameLine();
ImGui.PushStyleColor(ImGuiCol.Text, 0xffcbc0ff);
ClickToCopyText($"ffxiv_dx11.exe+{(unboxedAddr - _beginModule):X}");
ImGui.PopStyleColor();
}
try {
var eType = type.GetElementType();
var ptrObj = Marshal.PtrToStructure(new IntPtr(unboxed), eType!);
ImGui.SameLine();
PrintOutObject(ptrObj!, (ulong) unboxed, new List<string>(path));
} catch {
// Ignored
}
} else {
ImGui.Text("null");
}
} else {
if (type.IsArray) {
var arr = (Array) value;
if (ImGui.TreeNode($"Values##{member.Name}-{addr}-{string.Join("-", path)}")) {
for (var i = 0; i < arr.Length; i++) {
ImGui.Text($"[{i}]");
ImGui.SameLine();
PrintOutValue(addr, new List<string>(path) { $"_arrValue_{i}" }, type.GetElementType()!, arr.GetValue(i)!, member);
}
ImGui.TreePop();
}
} else if (!type.IsPrimitive) {
switch (value) {
case ILazyRow ilr:
var p = ilr.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
if (p != null) {
var getter = p.GetGetMethod();
if (getter != null) {
var rowValue = getter.Invoke(ilr, new object?[] { });
PrintOutObject(rowValue!, addr, new List<string>(path));
break;
}
}
PrintOutObject(value, addr, new List<string>(path));
break;
case Lumina.Text.SeString seString:
ImGui.Text($"{seString.RawString}");
break;
default:
PrintOutObject(value, addr, new List<string>(path));
break;
}
} else {
if (value is IntPtr p) {
var pAddr = (ulong)p.ToInt64();
ClickToCopyText($"{p:X}");
if (_beginModule > 0 && pAddr >= _beginModule && pAddr <= _endModule) {
ImGui.SameLine();
ImGui.PushStyleColor(ImGuiCol.Text, 0xffcbc0ff);
ClickToCopyText($"ffxiv_dx11.exe+{(pAddr - _beginModule):X}");
ImGui.PopStyleColor();
}
} else {
ImGui.Text($"{value}");
}
}
}
} catch (Exception ex) {
ImGui.Text($"{{{ex}}}");
}
}
public static Vector3 WorldToMap(Vector3 pos, ushort sizeFactor, short offsetX, short offsetY) {
var scale = sizeFactor / 100f;
var x = (10 - ((pos.X + offsetX) * scale + 1024f) * -0.2f / scale) / 10f;
var y = (10 - ((pos.Z + offsetY) * scale + 1024f) * -0.2f / scale) / 10f;
x = MathF.Round(x, 1, MidpointRounding.ToZero);
y = MathF.Round(y, 1, MidpointRounding.ToZero);
return new Vector3(x, y, pos.Z);
}
public static string ToTitleCase(string npcNameSingular)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(npcNameSingular.ToLower());
}
}
}