forked from Detegr/openRBRVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.hpp
153 lines (137 loc) · 4.29 KB
/
Config.hpp
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
#pragma once
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "Util.hpp"
#include "glm/vec3.hpp"
static float floatOrDefault(const std::string& value, float def)
{
try {
return std::stof(value);
} catch (const std::exception&) {
return def;
}
}
static int intOrDefault(const std::string& value, int def)
{
try {
return std::stoi(value);
} catch (const std::exception&) {
return def;
}
}
// Simplified ini file parser for plugin configuration
struct Config {
enum HorizonLock : uint8_t {
LOCK_NONE = 0x0,
LOCK_ROLL = 0x1,
LOCK_PITCH = 0x2,
};
float menuSize;
float overlaySize;
glm::vec3 overlayTranslation;
float superSampling;
HorizonLock lockToHorizon;
bool drawCompanionWindow;
bool drawLoadingScreen;
bool debug;
auto operator<=>(const Config&) const = default;
std::string ToString() const
{
return std::format(
"superSampling = {:.2f}\n"
"menuSize = {:.2f}\n"
"overlaySize = {:.2f}\n"
"overlayTranslateX = {:.2f}\n"
"overlayTranslateY = {:.2f}\n"
"overlayTranslateZ = {:.2f}\n"
"lockToHorizon = {}\n"
"drawDesktopWindow = {}\n"
"drawLoadingScreen = {}\n"
"debug = {}",
superSampling,
menuSize,
overlaySize,
overlayTranslation.x,
overlayTranslation.y,
overlayTranslation.z,
(int)lockToHorizon,
drawCompanionWindow,
drawLoadingScreen,
debug);
}
bool Write(const std::filesystem::path& path) const
{
std::ofstream f(path);
if (!f.good()) {
return false;
}
f << ToString();
f.close();
return f.good();
}
static Config fromFile(const std::filesystem::path& path)
{
auto cfg = Config {
.menuSize = 1.0,
.overlaySize = 1.0,
.overlayTranslation = glm::vec3 { 0.0f, 0.0f, 0.0f },
.superSampling = 1.0,
.lockToHorizon = LOCK_NONE,
.drawCompanionWindow = true,
.drawLoadingScreen = true,
.debug = false,
};
if (!std::filesystem::exists(path)) {
cfg.Write(path);
return cfg;
}
std::ifstream f(path);
if (!f.good()) {
Dbg("Could not open openRBRVR.ini");
return cfg;
}
std::string line;
while (std::getline(f, line)) {
if (line.empty()) {
continue;
}
if (line.empty() || line.front() == ';') {
// Skip empty lines and comments
continue;
}
auto end = std::remove_if(line.begin(), line.end(), isspace);
auto s = std::string(line.begin(), end);
std::stringstream ss { s };
std::string key, value;
std::getline(ss, key, '=');
std::getline(ss, value);
if (key == "menuSize") {
cfg.menuSize = floatOrDefault(value, 1.0);
} else if (key == "overlaySize") {
cfg.overlaySize = floatOrDefault(value, 1.0);
} else if (key == "overlayTranslateX") {
cfg.overlayTranslation.x = floatOrDefault(value, 1.0);
} else if (key == "overlayTranslateY") {
cfg.overlayTranslation.y = floatOrDefault(value, 1.0);
} else if (key == "overlayTranslateZ") {
cfg.overlayTranslation.z = floatOrDefault(value, 1.0);
} else if (key == "superSampling") {
cfg.superSampling = floatOrDefault(value, 1.0);
} else if (key == "lockToHorizon") {
cfg.lockToHorizon = static_cast<HorizonLock>(intOrDefault(value, 0));
} else if (key == "drawDesktopWindow") {
cfg.drawCompanionWindow = (value == "true");
} else if (key == "drawLoadingScreen") {
cfg.drawLoadingScreen = (value == "true");
} else if (key == "debug") {
cfg.debug = (value == "true");
}
}
return cfg;
}
};