-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpatch_settings.h
More file actions
371 lines (313 loc) · 13.6 KB
/
patch_settings.h
File metadata and controls
371 lines (313 loc) · 13.6 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
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <functional>
#include <Windows.h>
#include "imgui.h"
#include <toml++/toml.hpp>
// UI type for settings
enum class SettingUIType {
InputBox, // Text input box for exact values
Slider, // Slider for dragging values
Drag // DragFloat widget (compact inline control)
};
// Callback type for notifying parent patch of setting changes
using SettingChangedCallback = std::function<void()>;
// Base class for all patch settings
class PatchSetting {
public:
virtual ~PatchSetting() = default;
virtual void RenderUI() = 0;
virtual std::string GetName() const = 0;
virtual void ResetToDefault() = 0;
// TOML serialization
virtual void SaveToToml(toml::table& settingsTable) const = 0;
virtual bool LoadFromToml(const toml::table& settingsTable) = 0;
// Set callback for when setting value changes in UI
void SetChangedCallback(SettingChangedCallback callback) { onChangedCallback = callback; }
protected:
SettingChangedCallback onChangedCallback;
// Call this when value changes in UI
void NotifyChanged() {
if (onChangedCallback) { onChangedCallback(); }
}
// Helper to write to protected memory
void WriteToMemory(void* address, const void* data, size_t size) {
if (!address) return;
DWORD oldProtect;
if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &oldProtect)) {
memcpy(address, data, size);
VirtualProtect(address, size, oldProtect, &oldProtect);
}
}
};
// Float setting with configurable UI type and optional presets
class FloatSetting : public PatchSetting {
private:
float* valuePtr;
std::string name;
float defaultValue;
float minValue;
float maxValue;
std::string description;
std::vector<std::pair<std::string, float>> presets;
SettingUIType uiType;
void* boundAddress = nullptr;
public:
FloatSetting(float* ptr, const std::string& name, float defaultVal, float minVal, float maxVal, const std::string& desc = "", const std::vector<std::pair<std::string, float>>& presets = {},
SettingUIType uiType = SettingUIType::InputBox)
: valuePtr(ptr), name(name), defaultValue(defaultVal), minValue(minVal), maxValue(maxVal), description(desc), presets(presets), uiType(uiType) {
*valuePtr = defaultValue;
}
void BindToAddress(void* address) { boundAddress = address; }
void RenderUI() override {
#ifdef IMGUI_VERSION
if (!description.empty()) { ImGui::Text("%s", description.c_str()); }
bool changed = false;
// Render presets if available
if (!presets.empty()) {
ImGui::Text("Presets:");
float rowHeight = ImGui::GetFrameHeightWithSpacing() + ImGui::GetStyle().ScrollbarSize;
ImGui::BeginChild(("##presets_" + name).c_str(), ImVec2(0, rowHeight), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar);
for (const auto& [label, value] : presets) {
if (ImGui::Button(label.c_str())) {
*valuePtr = value;
changed = true;
}
ImGui::SameLine();
}
ImGui::EndChild();
ImGui::Separator();
ImGui::Spacing();
}
// Render input widget based on UI type
// Use ##name as widget ID so the technical name doesn't appear as a label (the user-friendly description is already shown above)
std::string widgetId = "##" + name;
ImGui::Text("Custom Value:");
ImGui::SetNextItemWidth(-FLT_MIN);
switch (uiType) {
case SettingUIType::Slider:
if (ImGui::SliderFloat(widgetId.c_str(), valuePtr, minValue, maxValue, "%.5f")) { changed = true; }
break;
case SettingUIType::Drag:
if (ImGui::DragFloat(widgetId.c_str(), valuePtr, (maxValue - minValue) / 100.0f, minValue, maxValue, "%.5f")) { changed = true; }
break;
case SettingUIType::InputBox:
default:
if (ImGui::InputFloat(widgetId.c_str(), valuePtr, 0.0f, 0.0f, "%.5f")) {
// Clamp to bounds
if (*valuePtr < minValue) *valuePtr = minValue;
if (*valuePtr > maxValue) *valuePtr = maxValue;
changed = true;
}
ImGui::TextDisabled("Range: %g - %g", minValue, maxValue);
break;
}
if (changed) {
// Auto-reapply to memory if bound
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(float)); }
// Notify parent patch of change for debounced reinstall
NotifyChanged();
}
#endif
}
void SaveToToml(toml::table& settingsTable) const override { settingsTable.insert(name, static_cast<double>(*valuePtr)); }
bool LoadFromToml(const toml::table& settingsTable) override {
auto val = settingsTable[name].value<double>();
if (val.has_value()) {
float newValue = static_cast<float>(val.value());
if (newValue < minValue) newValue = minValue;
if (newValue > maxValue) newValue = maxValue;
*valuePtr = newValue;
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(float)); }
return true;
}
return false;
}
std::string GetName() const override { return name; }
void ResetToDefault() override { *valuePtr = defaultValue; }
};
// Int setting with slider and optional presets
class IntSetting : public PatchSetting {
private:
int* valuePtr;
std::string name;
int defaultValue;
int minValue;
int maxValue;
std::string description;
std::vector<std::pair<std::string, int>> presets;
SettingUIType uiType;
void* boundAddress = nullptr;
public:
IntSetting(
int* ptr, const std::string& name, int defaultVal, int minVal, int maxVal, const std::string& desc = "", const std::vector<std::pair<std::string, int>>& presets = {}, SettingUIType uiType = SettingUIType::Slider)
: valuePtr(ptr), name(name), defaultValue(defaultVal), minValue(minVal), maxValue(maxVal), description(desc), presets(presets), uiType(uiType) {
*valuePtr = defaultValue;
}
void BindToAddress(void* address) { boundAddress = address; }
void RenderUI() override {
#ifdef IMGUI_VERSION
if (!description.empty()) { ImGui::Text("%s", description.c_str()); }
bool changed = false;
// Render presets if available
if (!presets.empty()) {
ImGui::Text("Presets:");
float rowHeight = ImGui::GetFrameHeightWithSpacing() + ImGui::GetStyle().ScrollbarSize;
ImGui::BeginChild(("##presets_" + name).c_str(), ImVec2(0, rowHeight), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar);
for (const auto& [label, value] : presets) {
if (ImGui::Button(label.c_str())) {
*valuePtr = value;
changed = true;
}
ImGui::SameLine();
}
ImGui::EndChild();
ImGui::Separator();
}
// Render input based on UI type
std::string widgetId = "##" + name;
ImGui::Text("Custom Value:");
ImGui::SetNextItemWidth(-FLT_MIN);
switch (uiType) {
case SettingUIType::Slider:
if (ImGui::SliderInt(widgetId.c_str(), valuePtr, minValue, maxValue)) { changed = true; }
break;
case SettingUIType::Drag:
if (ImGui::DragInt(widgetId.c_str(), valuePtr, (maxValue - minValue) / 100.0f, minValue, maxValue)) { changed = true; }
break;
case SettingUIType::InputBox:
default:
if (ImGui::InputInt(widgetId.c_str(), valuePtr)) {
// Clamp to bounds
if (*valuePtr < minValue) *valuePtr = minValue;
if (*valuePtr > maxValue) *valuePtr = maxValue;
changed = true;
}
ImGui::TextDisabled("Range: %d - %d", minValue, maxValue);
break;
}
if (changed) {
// Auto-reapply to memory if bound
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(int)); }
// Notify parent patch of change for debounced reinstall
NotifyChanged();
}
#endif
}
void SaveToToml(toml::table& settingsTable) const override { settingsTable.insert(name, static_cast<int64_t>(*valuePtr)); }
bool LoadFromToml(const toml::table& settingsTable) override {
auto val = settingsTable[name].value<int64_t>();
if (val.has_value()) {
int newValue = static_cast<int>(val.value());
if (newValue < minValue) newValue = minValue;
if (newValue > maxValue) newValue = maxValue;
*valuePtr = newValue;
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(int)); }
return true;
}
return false;
}
std::string GetName() const override { return name; }
void ResetToDefault() override { *valuePtr = defaultValue; }
};
// Bool setting with checkbox
class BoolSetting : public PatchSetting {
private:
bool* valuePtr;
std::string name;
bool defaultValue;
std::string description;
void* boundAddress = nullptr;
public:
BoolSetting(bool* ptr, const std::string& name, bool defaultVal, const std::string& desc = "") : valuePtr(ptr), name(name), defaultValue(defaultVal), description(desc) { *valuePtr = defaultValue; }
void BindToAddress(void* address) { boundAddress = address; }
void RenderUI() override {
#ifdef IMGUI_VERSION
bool changed = false;
if (ImGui::Checkbox(name.c_str(), valuePtr)) { changed = true; }
if (!description.empty()) {
ImGui::SameLine();
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered()) { ImGui::SetTooltip("%s", description.c_str()); }
}
if (changed) {
// Auto-reapply to memory if bound
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(bool)); }
// Notify parent patch of change for debounced reinstall
NotifyChanged();
}
#endif
}
void SaveToToml(toml::table& settingsTable) const override { settingsTable.insert(name, *valuePtr); }
bool LoadFromToml(const toml::table& settingsTable) override {
auto val = settingsTable[name].value<bool>();
if (val.has_value()) {
*valuePtr = val.value();
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(bool)); }
return true;
}
return false;
}
std::string GetName() const override { return name; }
void ResetToDefault() override { *valuePtr = defaultValue; }
};
// Enum/Choice setting with dropdown
class EnumSetting : public PatchSetting {
private:
int* valuePtr;
std::string name;
int defaultValue;
std::string description;
std::vector<std::string> choices;
void* boundAddress = nullptr;
public:
EnumSetting(int* ptr, const std::string& name, int defaultVal, const std::string& desc, const std::vector<std::string>& choices)
: valuePtr(ptr), name(name), defaultValue(defaultVal), description(desc), choices(choices) {
*valuePtr = defaultValue;
}
void BindToAddress(void* address) { boundAddress = address; }
void RenderUI() override {
#ifdef IMGUI_VERSION
if (!description.empty()) { ImGui::Text("%s", description.c_str()); }
bool changed = false;
// Create combo box
const char* currentChoice = (*valuePtr >= 0 && *valuePtr < static_cast<int>(choices.size())) ? choices[*valuePtr].c_str() : "Invalid";
std::string widgetId = "##" + name;
ImGui::SetNextItemWidth(-FLT_MIN);
if (ImGui::BeginCombo(widgetId.c_str(), currentChoice)) {
for (size_t i = 0; i < choices.size(); i++) {
bool isSelected = (*valuePtr == static_cast<int>(i));
if (ImGui::Selectable(choices[i].c_str(), isSelected)) {
*valuePtr = static_cast<int>(i);
changed = true;
}
if (isSelected) { ImGui::SetItemDefaultFocus(); }
}
ImGui::EndCombo();
}
if (changed) {
// Auto-reapply to memory if bound
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(int)); }
// Notify parent patch of change for debounced reinstall
NotifyChanged();
}
#endif
}
void SaveToToml(toml::table& settingsTable) const override { settingsTable.insert(name, static_cast<int64_t>(*valuePtr)); }
bool LoadFromToml(const toml::table& settingsTable) override {
auto val = settingsTable[name].value<int64_t>();
if (val.has_value()) {
int newValue = static_cast<int>(val.value());
if (newValue >= 0 && newValue < static_cast<int>(choices.size())) {
*valuePtr = newValue;
if (boundAddress) { WriteToMemory(boundAddress, valuePtr, sizeof(int)); }
return true;
}
}
return false;
}
std::string GetName() const override { return name; }
void ResetToDefault() override { *valuePtr = defaultValue; }
};