-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathoptimization.cpp
More file actions
204 lines (168 loc) · 6.67 KB
/
Copy pathoptimization.cpp
File metadata and controls
204 lines (168 loc) · 6.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "optimization.h"
#include "patch_system.h"
#include <intrin.h>
#include <format>
#include <unordered_map>
#include <detours/detours.h>
#include "cpu_optimization.h"
#include "utils.h"
#include "logger.h"
#include <toml++/toml.hpp>
// Metadata storage for patches (use unique_ptr to avoid pointer invalidation on reallocation)
static std::vector<std::unique_ptr<PatchMetadata>>& GetMetadataStorage() {
static std::vector<std::unique_ptr<PatchMetadata>> storage;
return storage;
}
void OptimizationPatch::SetMetadata(const PatchMetadata& meta) {
// Store in static storage to ensure lifetime
// Using unique_ptr so pointers remain valid even if vector grows
GetMetadataStorage().push_back(std::make_unique<PatchMetadata>(meta));
metadata = GetMetadataStorage().back().get();
}
bool OptimizationPatch::IsCompatibleWithCurrentVersion() const {
if (!metadata) {
return true; // No metadata = assume compatible
}
return IsVersionSupported(metadata->supportedVersions);
}
bool OptimizationPatch::IsEnabledByDefault() const {
return metadata->enabledByDefault;
}
void OptimizationPatch::MaybeSampleMinimal(LONG currentCalls) {
auto now = std::chrono::steady_clock::now();
if (now - currentWindow.start >= SAMPLE_INTERVAL) {
std::lock_guard<std::mutex> lock(statsMutex);
if (now - currentWindow.start >= SAMPLE_INTERVAL) {
lastSampleRate = currentCalls / std::chrono::duration<double>(SAMPLE_INTERVAL).count();
char buffer[256];
sprintf_s(buffer, "[%s] Calls/sec: %.0f\n", patchName.c_str(), lastSampleRate);
LOG_DEBUG(buffer);
InterlockedExchange(¤tWindow.calls, 0);
currentWindow.start = now;
}
}
}
CPUFeatures::CPUFeatures() {
int leaves[4] = {0};
__cpuid(leaves, 1);
hasSSE41 = (leaves[2] & (1 << 19)) != 0;
hasFMA = (leaves[2] & (1 << 12)) != 0;
// Check for AVX2 (CPUID leaf 7, EBX bit 5)
__cpuidex(leaves, 7, 0);
hasAVX2 = (leaves[1] & (1 << 5)) != 0;
}
const CPUFeatures& CPUFeatures::Get() {
static CPUFeatures instance;
return instance;
}
OptimizationManager& OptimizationManager::Get() {
static OptimizationManager instance;
// Register all patches when the manager is first created
static bool initialized = false;
static bool initializing = false;
if (!initialized && !initializing) {
initializing = true; // Prevent re-entry
LOG_INFO("[PatchSystem] Initializing patches...");
try {
// Use PatchRegistry to auto-register all patches
PatchRegistry::InstantiateAll(instance);
// Log all registered patches
LOG_INFO("[PatchSystem] Registered " + std::to_string(instance.patches.size()) + " patches:");
for (const auto& patch : instance.patches) { LOG_INFO(" - " + patch->GetName()); }
LOG_INFO("[PatchSystem] Patch registration complete");
} catch (const std::exception& e) { LOG_ERROR("[PatchSystem] Exception during patch initialization: " + std::string(e.what())); } catch (...) {
LOG_ERROR("[PatchSystem] Unknown exception during patch initialization");
}
initialized = true;
initializing = false;
} else if (initializing) {
LOG_ERROR("[PatchSystem] Recursive call detected during initialization!");
}
return instance;
}
const std::vector<std::unique_ptr<OptimizationPatch>>& OptimizationManager::GetPatches() const {
return patches;
}
bool OptimizationManager::EnablePatch(const std::string& name) {
for (auto& patch : patches) {
if (patch->GetName() == name) {
bool result = patch->Install();
if (result) { m_hasUnsavedChanges = true; }
return result;
}
}
return false;
}
bool OptimizationManager::DisablePatch(const std::string& name) {
for (auto& patch : patches) {
if (patch->GetName() == name) {
bool result = patch->Uninstall();
if (result) { m_hasUnsavedChanges = true; }
return result;
}
}
return false;
}
void OptimizationManager::SaveToToml(toml::table& root) {
toml::table patchesTable;
for (const auto& patch : patches) {
toml::table patchTable;
patch->SaveToToml(patchTable);
patchesTable.insert(patch->GetName(), std::move(patchTable));
}
if (!patchesTable.empty()) { root.insert("patches", std::move(patchesTable)); }
m_hasUnsavedChanges = false;
}
void OptimizationManager::LoadFromToml(const toml::table& root) {
auto patchesNode = root["patches"].as_table();
if (!patchesNode) {
LOG_DEBUG("[PatchSystem] No patches section found in TOML");
return;
}
for (auto& [patchName, patchNode] : *patchesNode) {
auto* patchTable = patchNode.as_table();
if (!patchTable) continue;
std::string name(patchName.str());
// Find the matching patch
OptimizationPatch* patch = nullptr;
for (auto& p : patches) {
if (p->GetName() == name) {
patch = p.get();
break;
}
}
if (!patch) {
LOG_WARNING("[PatchSystem] No matching patch found for TOML section: " + name);
continue;
}
patch->LoadFromToml(*patchTable);
}
}
// Register a new patch in the system
void OptimizationManager::RegisterPatch(std::unique_ptr<OptimizationPatch> patch) {
// Check if a patch with the same name already exists
std::string patchName = patch->GetName();
for (const auto& existingPatch : patches) {
if (existingPatch->GetName() == patchName) {
LOG_WARNING("[PatchSystem] Patch already registered: " + patchName);
return; // Skip this patch since it's already registered
}
}
// If we get here, this is a new patch
patches.push_back(std::move(patch));
LOG_DEBUG("[PatchSystem] Registered patch: " + patchName);
}
void OptimizationManager::OnSettingsRefired() {
for (const auto& patch : patches) {
if (patch && patch->IsEnabled()) {
try {
patch->OnSettingsRefired();
} catch (...) { LOG_ERROR("[PatchSystem] Exception during OnSettingsRefired for " + patch->GetName()); }
}
}
}
void OptimizationManager::EnsureEnabledByDefaultPatchesAreEnabled() {
for (const auto& patch : patches) {
if ((!patch->EnablementLoadedFromConfig() & !patch->IsEnabled()) && patch->IsEnabledByDefault()) { patch->Install(); }
}
}