-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoptimization.h
More file actions
218 lines (181 loc) · 8.23 KB
/
optimization.h
File metadata and controls
218 lines (181 loc) · 8.23 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
#pragma once
#include <windows.h>
#include <string>
#include <vector>
#include <chrono>
#include <mutex>
#include <memory>
#include <atomic>
#include "logger.h"
#include "patch_settings.h"
// Forward declaration
struct PatchMetadata;
// Base class for optimization patches
class OptimizationPatch {
public:
struct SampleWindow {
volatile LONG calls{0};
std::chrono::steady_clock::time_point start;
};
static constexpr auto SETTING_CHANGE_DEBOUNCE = std::chrono::seconds(2);
protected:
std::mutex statsMutex;
std::mutex patchMutex;
SampleWindow currentWindow;
static constexpr auto SAMPLE_INTERVAL = std::chrono::seconds(5);
void* originalFunc;
std::string patchName;
std::atomic<bool> isEnabled{false};
double lastSampleRate = 0.0;
PatchMetadata* metadata = nullptr;
std::string lastError;
// Settings storage
std::vector<std::unique_ptr<PatchSetting>> settings;
// Debounced reinstall state to mitigate people crashing themselves :)
std::chrono::steady_clock::time_point lastSettingChange;
bool pendingReinstall = false;
void MaybeSampleMinimal(LONG currentCalls);
// Helper to set error and return false (reduces boilerplate)
bool Fail(const std::string& msg) {
lastError = msg;
LOG_ERROR("[" + patchName + "] " + msg);
return false;
}
// Settings registration helpers
void RegisterFloatSetting(
float* ptr, const std::string& name, SettingUIType uiType, float defaultVal, float minVal, float maxVal, const std::string& desc = "", const std::vector<std::pair<std::string, float>>& presets = {}) {
auto setting = std::make_unique<FloatSetting>(ptr, name, defaultVal, minVal, maxVal, desc, presets, uiType);
setting->SetChangedCallback([this]() { NotifySettingChanged(); });
settings.push_back(std::move(setting));
}
void RegisterIntSetting(
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) {
auto setting = std::make_unique<IntSetting>(ptr, name, defaultVal, minVal, maxVal, desc, presets, uiType);
setting->SetChangedCallback([this]() { NotifySettingChanged(); });
settings.push_back(std::move(setting));
}
void RegisterBoolSetting(bool* ptr, const std::string& name, bool defaultVal, const std::string& desc = "") {
auto setting = std::make_unique<BoolSetting>(ptr, name, defaultVal, desc);
setting->SetChangedCallback([this]() { NotifySettingChanged(); });
settings.push_back(std::move(setting));
}
void RegisterEnumSetting(int* ptr, const std::string& name, int defaultVal, const std::string& desc, const std::vector<std::string>& choices) {
auto setting = std::make_unique<EnumSetting>(ptr, name, defaultVal, desc, choices);
setting->SetChangedCallback([this]() { NotifySettingChanged(); });
settings.push_back(std::move(setting));
}
// Bind a setting to a memory address for auto-reapplication
void BindSettingToAddress(const std::string& settingName, void* memoryAddress) {
for (auto& setting : settings) {
if (setting->GetName() == settingName) {
// Use dynamic_cast to get the specific type and bind
if (auto* floatSetting = dynamic_cast<FloatSetting*>(setting.get())) {
floatSetting->BindToAddress(memoryAddress);
} else if (auto* intSetting = dynamic_cast<IntSetting*>(setting.get())) {
intSetting->BindToAddress(memoryAddress);
} else if (auto* boolSetting = dynamic_cast<BoolSetting*>(setting.get())) {
boolSetting->BindToAddress(memoryAddress);
} else if (auto* enumSetting = dynamic_cast<EnumSetting*>(setting.get())) {
enumSetting->BindToAddress(memoryAddress);
}
return;
}
}
}
public:
OptimizationPatch(const std::string& name, void* original) : patchName(name), originalFunc(original), lastError("") {
currentWindow.start = std::chrono::steady_clock::now();
currentWindow.calls = 0;
}
virtual ~OptimizationPatch() = default;
virtual bool Install() = 0;
virtual bool Uninstall() = 0;
// Override for patches that need periodic updates (e.g., deferred installation)
// Called from the main message loop
virtual void Update() {
// Handle debounced reinstall when settings change
if (pendingReinstall && isEnabled.load()) {
auto now = std::chrono::steady_clock::now();
if (now - lastSettingChange >= SETTING_CHANGE_DEBOUNCE) {
pendingReinstall = false;
LOG_DEBUG("[" + patchName + "] Reinstalling after setting change");
Uninstall();
Install();
}
}
}
// Called by settings when their value changes in the UI, debounces reinstall to avoid rapid reinstalls while user is typing which would be bad
void NotifySettingChanged() {
lastSettingChange = std::chrono::steady_clock::now();
pendingReinstall = true;
}
const std::string& GetName() const { return patchName; }
bool IsEnabled() const { return isEnabled.load(); }
double GetLastSampleRate() const { return lastSampleRate; }
const std::string& GetLastError() const { return lastError; }
bool PendingReinstall() const { return pendingReinstall; }
std::chrono::steady_clock::time_point GetLastSettingChange() const { return lastSettingChange; }
// Metadata management
void SetMetadata(const PatchMetadata& meta);
const PatchMetadata* GetMetadata() const { return metadata; }
// Version compatibility check
bool IsCompatibleWithCurrentVersion() const;
// Override this for custom UI in ImGui (called when patch is enabled)
// By default, auto-renders all registered settings
virtual void RenderCustomUI() {
#ifdef IMGUI_VERSION
if (!settings.empty()) {
ImGui::Text("Settings:");
ImGui::Separator();
for (auto& setting : settings) { setting->RenderUI(); }
}
#endif
}
static constexpr size_t GetCurrentWindowOffset() { return offsetof(OptimizationPatch, currentWindow); }
static constexpr size_t GetCallsOffset() { return offsetof(SampleWindow, calls); }
// TOML serialization
virtual void SaveToToml(toml::table& patchTable) const {
patchTable.insert("enabled", isEnabled.load());
for (const auto& setting : settings) { setting->SaveToToml(patchTable); }
}
virtual bool LoadFromToml(const toml::table& patchTable) {
// Load settings first (before Enabled, so patches install with correct values)
for (auto& setting : settings) { setting->LoadFromToml(patchTable); }
// Then handle Enabled state
auto enabled = patchTable["enabled"].value<bool>();
if (enabled.has_value()) {
bool currentlyEnabled = isEnabled.load();
if (enabled.value() && !currentlyEnabled) {
return Install();
} else if (!enabled.value() && currentlyEnabled) {
return Uninstall();
}
}
return true;
}
};
// CPU feature detection
struct CPUFeatures {
bool hasSSE41{false};
bool hasFMA{false};
bool hasAVX2{false};
CPUFeatures();
static const CPUFeatures& Get();
};
// Manager for all game patches (keeps the name for backward compatibility, remove later)
class OptimizationManager {
std::vector<std::unique_ptr<OptimizationPatch>> patches;
bool m_hasUnsavedChanges = false;
public:
static OptimizationManager& Get();
void RegisterPatch(std::unique_ptr<OptimizationPatch> patch);
const std::vector<std::unique_ptr<OptimizationPatch>>& GetPatches() const;
bool EnablePatch(const std::string& name);
bool DisablePatch(const std::string& name);
// TOML serialization
void SaveToToml(toml::table& root);
void LoadFromToml(const toml::table& root);
// Unsaved changes tracking
bool HasUnsavedChanges() const { return m_hasUnsavedChanges; }
void SetUnsavedChanges(bool unsaved) { m_hasUnsavedChanges = unsaved; }
};