forked from samjviana/souls_vision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
340 lines (288 loc) · 17.3 KB
/
Copy pathconfig.cpp
File metadata and controls
340 lines (288 loc) · 17.3 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
//
// Created by PC-SAMUEL on 23/11/2024.
//
#include "config.h"
#include "globals.h"
#include <regex>
#include <toml.hpp>
namespace souls_vision {
Components Config::components;
BarSettings Config::statBarSettings;
ImVec2 Config::bestEffectIconSize = ImVec2(39, 33);
ImVec2 Config::dmgTypeIconSize = ImVec2(30, 30);
ImVec2 Config::effectBarIconSize = ImVec2(56, 48);
int Config::bestEffects = 3;
int Config::statBarSpacing = 0;
float Config::fontSize = 18.0f;
float Config::opacity;
int Config::delay = 0;
bool Config::debug = false;
bool Config::dragOverlay = false;
bool Config::configUpdated = false;
bool Config::opacityUpdated = false;
bool Config::fontSizeUpdated = false;
bool Config::hideBlightMadness = false;
int Config::maxEffectBars = 7;
void Config::SaveConfig(const std::string& configFilePath) {
try {
toml::table configToml;
configToml.insert_or_assign("general", toml::table{
{"debug", debug},
{"dragOverlay", dragOverlay},
{"fontSize", fontSize},
{"opacity", opacity},
{"delay", delay},
});
configToml.insert_or_assign("appearance", toml::table{
{"bestEffects", bestEffects},
{"maxEffectBars", maxEffectBars},
{"bestEffectIconSize", bestEffectIconSize.x},
{"dmgTypeIconSize", dmgTypeIconSize.x},
{"statBarSpacing", statBarSpacing},
{"hideBlightMadness", hideBlightMadness}
});
toml::table statBar;
statBar.insert_or_assign("position", toml::table{
{"x", statBarSettings.position.x},
{"y", statBarSettings.position.y}
});
statBar.insert_or_assign("size", toml::table{
{"width", statBarSettings.size.x},
{"height", statBarSettings.size.y}
});
configToml.insert_or_assign("statBar", statBar);
toml::table componentsTable;
componentsTable.insert_or_assign("hp", toml::table{{"visible", components.hp.visible}, {"hideText", components.hp.hideText}});
componentsTable.insert_or_assign("fp", toml::table{{"visible", components.fp.visible}, {"hideText", components.fp.hideText}});
componentsTable.insert_or_assign("stamina", toml::table{{"visible", components.stamina.visible}, {"hideText", components.stamina.hideText}});
componentsTable.insert_or_assign("stagger", toml::table{{"visible", components.stagger.visible}, {"hideText", components.stagger.hideText}});
componentsTable.insert_or_assign("poison", toml::table{{"visible", components.poison.visible}, {"hideText", components.poison.hideText}});
componentsTable.insert_or_assign("scarletRot", toml::table{{"visible", components.scarletRot.visible}, {"hideText", components.scarletRot.hideText}});
componentsTable.insert_or_assign("hemorrhage", toml::table{{"visible", components.hemorrhage.visible}, {"hideText", components.hemorrhage.hideText}});
componentsTable.insert_or_assign("deathBlight", toml::table{{"visible", components.deathBlight.visible}, {"hideText", components.deathBlight.hideText}});
componentsTable.insert_or_assign("frostbite", toml::table{{"visible", components.frostbite.visible}, {"hideText", components.frostbite.hideText}});
componentsTable.insert_or_assign("sleep", toml::table{{"visible", components.sleep.visible}, {"hideText", components.sleep.hideText}});
componentsTable.insert_or_assign("madness", toml::table{{"visible", components.madness.visible}, {"hideText", components.madness.hideText}});
componentsTable.insert_or_assign("bestEffects", components.bestEffects);
componentsTable.insert_or_assign("immuneEffects", components.immuneEffects);
componentsTable.insert_or_assign("dmgTypes", components.dmgTypes);
componentsTable.insert_or_assign("neutralDmgTypes", components.neutralDmgTypes);
configToml.insert_or_assign("components", componentsTable);
std::ofstream configFile(configFilePath);
if (!configFile.is_open()) {
throw std::runtime_error("Failed to open " + configFilePath + " for writing.");
}
configFile << configToml;
configFile.close();
AddComments(configFilePath);
} catch (const std::exception& e) {
Logger::Error(std::string("Config::SaveConfig - Error: ") + e.what());
}
}
void Config::LoadConfig(const std::string& configFilePath) {
try {
toml::table configToml;
if (!std::filesystem::exists(configFilePath)) {
Logger::Info("Config file not found. Creating a new one...");
CreateConfig(configFilePath);
std::ifstream configFile(configFilePath);
if (!configFile.is_open()) {
throw std::runtime_error("Failed to open " + configFilePath + " after creation.");
}
configToml = toml::parse(configFile);
} else {
std::ifstream configFile(configFilePath);
if (!configFile.is_open()) {
throw std::runtime_error("Failed to open " + configFilePath);
}
configToml = toml::parse(configFile);
}
debug = configToml["general"]["debug"].value_or(false);
dragOverlay = configToml["general"]["dragOverlay"].value_or(false);
hideBlightMadness = configToml["general"]["hideBlightMadness"].value_or(false);
fontSize = configToml["general"]["fontSize"].value_or(18.0f);
opacity = configToml["general"]["opacity"].value_or(0.9f);
delay = configToml["general"]["delay"].value_or(0);
fontSizeUpdated = (fontSize != configToml["general"]["fontSize"].value_or(fontSize));
opacityUpdated = (opacity != configToml["general"]["opacity"].value_or(opacity));
bestEffects = configToml["appearance"]["bestEffects"].value_or(3);
bestEffectIconSize.x = configToml["appearance"]["bestEffectIconSize"].value_or(39);
bestEffectIconSize.y = bestEffectIconSize.x * 0.85f;
dmgTypeIconSize.x = configToml["appearance"]["dmgTypeIconSize"].value_or(30);
dmgTypeIconSize.y = dmgTypeIconSize.x;
statBarSpacing = configToml["appearance"]["statBarSpacing"].value_or(0);
maxEffectBars = configToml["appearance"]["maxEffectBars"].value_or(7);
auto statBarTable = configToml["statBar"].as_table();
if (statBarTable) {
auto positionTable = statBarTable->at("position").as_table();
if (positionTable) {
statBarSettings.position.x = positionTable->at("x").value_or(gGameWindowSize.width - 555 - 5);
statBarSettings.position.y = positionTable->at("y").value_or(10);
}
auto sizeTable = statBarTable->at("size").as_table();
if (sizeTable) {
statBarSettings.size.x = sizeTable->at("width").value_or(555);
statBarSettings.size.y = sizeTable->at("height").value_or(40);
}
}
float iconWidth = statBarSettings.size.y * 1.70f;
effectBarIconSize = ImVec2(iconWidth, iconWidth * 0.85f);
auto componentsTable = configToml["components"].as_table();
if (componentsTable) {
components.hp.visible = componentsTable->at("hp").as_table()->at("visible").value_or(true);
components.hp.hideText = componentsTable->at("hp").as_table()->at("hideText").value_or(false);
components.fp.visible = componentsTable->at("fp").as_table()->at("visible").value_or(true);
components.fp.hideText = componentsTable->at("fp").as_table()->at("hideText").value_or(false);
components.stamina.visible = componentsTable->at("stamina").as_table()->at("visible").value_or(true);
components.stamina.hideText = componentsTable->at("stamina").as_table()->at("hideText").value_or(false);
components.stagger.visible = componentsTable->at("stagger").as_table()->at("visible").value_or(true);
components.stagger.hideText = componentsTable->at("stagger").as_table()->at("hideText").value_or(false);
components.poison.visible = componentsTable->at("poison").as_table()->at("visible").value_or(true);
components.poison.hideText = componentsTable->at("poison").as_table()->at("hideText").value_or(false);
components.scarletRot.visible = componentsTable->at("scarletRot").as_table()->at("visible").value_or(true);
components.scarletRot.hideText = componentsTable->at("scarletRot").as_table()->at("hideText").value_or(false);
components.hemorrhage.visible = componentsTable->at("hemorrhage").as_table()->at("visible").value_or(true);
components.hemorrhage.hideText = componentsTable->at("hemorrhage").as_table()->at("hideText").value_or(false);
components.deathBlight.visible = componentsTable->at("deathBlight").as_table()->at("visible").value_or(true);
components.deathBlight.hideText = componentsTable->at("deathBlight").as_table()->at("hideText").value_or(false);
components.frostbite.visible = componentsTable->at("frostbite").as_table()->at("visible").value_or(true);
components.frostbite.hideText = componentsTable->at("frostbite").as_table()->at("hideText").value_or(false);
components.sleep.visible = componentsTable->at("sleep").as_table()->at("visible").value_or(true);
components.sleep.hideText = componentsTable->at("sleep").as_table()->at("hideText").value_or(false);
components.madness.visible = componentsTable->at("madness").as_table()->at("visible").value_or(true);
components.madness.hideText = componentsTable->at("madness").as_table()->at("hideText").value_or(false);
components.bestEffects = componentsTable->at("bestEffects").value_or(true);
components.immuneEffects = componentsTable->at("immuneEffects").value_or(true);
components.dmgTypes = componentsTable->at("dmgTypes").value_or(true);
components.neutralDmgTypes = componentsTable->at("neutralDmgTypes").value_or(false);
}
Logger::Info("Config loaded successfully.");
} catch (const std::exception& e) {
Logger::Error(std::string("Config::LoadConfig - Error: ") + e.what());
}
}
void Config::CreateConfig(const std::string &configFilePath) {
Size barSize = {600, 40};
try {
toml::table configToml;
configToml.insert_or_assign("general", toml::table{
{"debug", false},
{"dragOverlay", false},
{"hideBlightMadness", false},
{"fontSize", 18.0f},
{"opacity", 0.9f},
{"delay", 0}
});
configToml.insert_or_assign("appearance", toml::table{
{"bestEffects", 3},
{"bestEffectIconSize", 39},
{"dmgTypeIconSize", 30},
{"statBarSpacing", 0},
{"maxEffectBars", 7}
});
toml::table statBar;
statBar.insert_or_assign("position", toml::table{
{"x", gGameWindowSize.width - barSize.width - 5},
{"y", 10}
});
statBar.insert_or_assign("size", toml::table{
{"width", barSize.width},
{"height", barSize.height}
});
configToml.insert_or_assign("statBar", statBar);
toml::table componentsToml;
componentsToml.insert_or_assign("hp", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("fp", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("stamina", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("stagger", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("poison", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("scarletRot", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("hemorrhage", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("deathBlight", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("frostbite", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("sleep", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("madness", toml::table{{"visible", true}, {"hideText", false}});
componentsToml.insert_or_assign("bestEffects", true);
componentsToml.insert_or_assign("immuneEffects", true);
componentsToml.insert_or_assign("dmgTypes", true);
componentsToml.insert_or_assign("neutralDmgTypes", false);
configToml.insert_or_assign("components", componentsToml);
std::ofstream configFile(configFilePath);
if (!configFile.is_open()) {
throw std::runtime_error("Failed to create sv_config.toml");
}
configFile << configToml;
configFile.close();
AddComments(configFilePath);
} catch (const std::exception& e) {
Logger::Error(std::string("Config::CreateConfig - Error: ") + e.what());
}
}
void Config::AddComments(const std::string &configFilePath) {
try {
std::ifstream configFile(configFilePath);
if (!configFile.is_open()) {
throw std::runtime_error("Failed to open " + configFilePath);
}
std::ostringstream buffer;
buffer << configFile.rdbuf();
std::string content = buffer.str();
configFile.close();
std::map<std::string, std::string> comments = {
// general
{"debug", "Enables or disables debug mode. If set to `true`, a console window will open with the game, showing the same information as the one found in the `souls_vision.log`"},
{"dragOverlay", "If set to `true`, the overlay can be dragged around the screen by clicking and dragging it"},
{"fontSize", "Font size of the text displayed on the bars. Default is `18.0`"},
{"opacity", "Opacity of the overlay, from 0.0 (fully transparent) to 1.0 (fully opaque). This option requires the game to be restarted to take effect"},
{"delay", "Delay in milliseconds before initializing the overlay. Default is `0`"},
// appearance
{"bestEffectIconSize", "Size of the best effect icons. Default is `33`"},
{"bestEffects", "How many of the best effects to show on the overlay. The effects are sorted (left to right) by the lowest value necessary to trigger them. Default is `2`"},
{"dmgTypeIconSize", "Size of the damage type icons. Default is 30."},
{"hideBlightMadness", "If true, hides Death Blight and Madness bars for common enemies. Default is false."},
{"maxEffectBars", "Maximum number of effect bars to show. Default is 7."},
{"statBarSpacing", "Spacing between the bars. Default is 0."},
// statBar.position
{"x", "Horizontal position of the bar in pixels"},
{"y", "Vertical position of the bar in pixels"},
// statBar.size
{"width", "Width of the bar in pixels"},
{"height", "Height of the bar in pixels"},
// components
{"hp", "HP Bar configuration"},
{"fp", "FP Bar configuration"},
{"stamina", "Stamina Bar configuration"},
{"stagger", "Stagger Bar configuration"},
{"poison", "Poison Bar configuration"},
{"scarletRot", "Scarlet Rot Bar configuration"},
{"hemorrhage", "Hemorrhage Bar configuration"},
{"deathBlight", "Death Blight Bar configuration"},
{"frostbite", "Frostbite Bar configuration"},
{"sleep", "Sleep Bar configuration"},
{"madness", "Madness Bar configuration"},
{"bestEffects", "If set to `false`, the Best Effects against the enemy will be hidden. Default is `true`"},
{"immuneEffects", "If set to `false`, the effects that the enemy is immune to will be hidden. Default is `true`"},
{"dmgTypes", "If set to `false`, the Damage Type information will be hidden. Default is `true`"},
{"neutralDmgTypes", "If set to `true`, the Neutral Damage Type information will be shown. Default is `false`"},
// components.bar
{"visible", "If set to `false`, the bar will be hidden. Default is `true`"},
{"hideText", "If set to `true`, hides the text displayed on the bar (e.g. “90/219”). Default is `false`"}
};
std::regex configRegex(R"((\w+)\s*=\s*.+)");
std::string updatedContent = content;
for (const auto& [key, comment] : comments) {
std::regex keyRegex(key + R"(\s*=\s*.+)");
updatedContent = std::regex_replace(updatedContent, keyRegex, "$& # " + comment);
}
std::ofstream outFile(configFilePath);
if (!outFile.is_open()) {
throw std::runtime_error("Failed to write to " + configFilePath);
}
outFile << updatedContent;
outFile.close();
Logger::Info("Comments added to config file successfully.");
} catch (const std::exception& e) {
Logger::Error(std::string("Config::AddComments - Error: ") + e.what());
}
}
} // souls_vision