forked from Areteic/dsfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Settings.cpp
186 lines (186 loc) · 6.05 KB
/
Settings.cpp
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
#include "Settings.h"
#include <fstream>
#include <string>
#include "main.h"
#include "WindowManager.h"
Settings Settings::instance;
void Settings::load() {
std::ifstream sfile;
sfile.open(GetDirectoryFile("DSfix.ini"), std::ios::in);
char buffer[128];
while(!sfile.eof()) {
sfile.getline(buffer, 128);
if(buffer[0] == '#') continue;
if(sfile.gcount() <= 1) continue;
std::string bstring(buffer);
#define SETTING(_type, _var, _inistring, _defaultval) \
if(bstring.find(_inistring) == 0) { \
read(buffer + strlen(_inistring) + 1, _var); \
}
#include "Settings.def"
#undef SETTING
}
sfile.close();
if(getBackupInterval() < 600) {
BackupInterval = 600;
}
if(getPresentWidth() == 0) PresentWidth = getRenderWidth();
if(getPresentHeight() == 0) PresentHeight = getRenderHeight();
if(getOverrideLanguage().length() >= 2 && getOverrideLanguage().find("none") != 0) {
performLanguageOverride();
}
curFPSlimit = getFPSLimit();
}
void Settings::report() {
SDLOG(0, "= Settings read:\n");
#define SETTING(_type, _var, _inistring, _defaultval) \
log(_inistring, _var);
#include "Settings.def"
#undef SETTING
SDLOG(0, "_____________\n");
}
void Settings::init() {
if(!inited) {
if(getDisableCursor()) WindowManager::get().toggleCursorVisibility();
if(getCaptureCursor()) WindowManager::get().toggleCursorCapture();
if(getBorderlessFullscreen()) WindowManager::get().toggleBorderlessFullscreen();
WindowManager::get().resize(NULL, NULL);
inited = true;
}
}
void Settings::shutdown() {
if(inited) {
undoLanguageOverride();
inited = false;
}
}
unsigned Settings::getCurrentFPSLimit() {
return curFPSlimit;
}
void Settings::setCurrentFPSLimit(unsigned limit) {
curFPSlimit = limit;
}
void Settings::toggle30FPSLimit() {
if(curFPSlimit == 30) curFPSlimit = getFPSLimit();
else curFPSlimit = 30;
}
//reading
void Settings::read(char* source, bool& value) {
std::string ss(source);
if(ss.find("true")==0 || ss.find("1")==0 || ss.find("TRUE")==0 || ss.find("enable")==0) value = true;
else value = false;
}
void Settings::read(char* source, int& value) {
sscanf_s(source, "%d", &value);
}
void Settings::read(char* source, unsigned& value) {
sscanf_s(source, "%u", &value);
}
void Settings::read(char* source, float& value) {
sscanf_s(source, "%f", &value);
}
void Settings::read(char* source, std::string& value) {
value.assign(source);
}
//logging
void Settings::log(const char* name, bool value) {
SDLOG(0, " - %s : %s\n", name, value ? "true" : "false");
}
void Settings::log(const char* name, int value) {
SDLOG(0, " - %s : %d\n", name, value);
}
void Settings::log(const char* name, unsigned value) {
SDLOG(0, " - %s : %u\n", name, value);
}
void Settings::log(const char* name, float value) {
SDLOG(0, " - %s : %f\n", name, value);
}
void Settings::log(const char* name, const std::string& value) {
SDLOG(0, " - %s : %s\n", name, value.c_str());
}
//language override
void Settings::performLanguageOverride() {
HKEY key;
//Reading operations
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\International", 0, KEY_READ, &key) != ERROR_SUCCESS) {
SDLOG(0, "ERROR opening language registry key for reading\n");
return;
}
BYTE prevLang[16]; //previous locale registry key
DWORD prevLangSize;
//check if prev key already set -- if so, assume correct override and return
if(RegQueryValueEx(key, "PrevLocaleName", 0, 0, prevLang, &prevLangSize) == ERROR_SUCCESS) {
RegCloseKey(key);
return;
}
//read current locale
if(RegQueryValueEx(key, "LocaleName", 0, 0, prevLang, &prevLangSize) != ERROR_SUCCESS) {
RegCloseKey(key);
SDLOG(0, "ERROR reading from language registry key\n");
return;
}
//if locale already set: no override necessary
if(getOverrideLanguage().find((char*)prevLang) == 0) {
RegCloseKey(key);
SDLOG(0, "Language set to %s\n", prevLang);
return;
}
RegFlushKey(key);
RegCloseKey(key);
//Writing operations
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\International", 0, KEY_WRITE, &key) != ERROR_SUCCESS) {
SDLOG(0, "ERROR opening language registry key for writing\n");
return;
}
//store previous locale
if(RegSetValueEx(key, "PrevLocaleName", 0, REG_SZ, prevLang, prevLangSize) != ERROR_SUCCESS) {
RegCloseKey(key);
SDLOG(0, "ERROR setting previous language registry key\n");
return;
}
//override existing locale
if(RegSetValueEx(key, "LocaleName", 0, REG_SZ, (BYTE*)getOverrideLanguage().c_str(), getOverrideLanguage().length()+1) != ERROR_SUCCESS) {
RegCloseKey(key);
SDLOG(0, "ERROR setting language registry key\n");
return;
}
SDLOG(0, "Set Language key to %s, stored previous value %s\n", getOverrideLanguage().c_str(), prevLang);
RegFlushKey(key);
RegCloseKey(key);
}
void Settings::undoLanguageOverride() {
HKEY key;
//reading operations
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\International", 0, KEY_READ, &key) != ERROR_SUCCESS) {
SDLOG(0, "ERROR opening language registry key for reading (restore)\n");
return;
}
BYTE prevLang[16]; //previous locale registry key
DWORD prevLangSize;
//load previous locale
if(RegQueryValueEx(key, "PrevLocaleName", 0, 0, prevLang, &prevLangSize) != ERROR_SUCCESS) {
RegCloseKey(key);
SDLOG(0, "ERROR reading previous locale from language registry key (restore)\n");
return;
}
RegFlushKey(key);
RegCloseKey(key);
//Writing operations
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\International", 0, KEY_WRITE, &key) != ERROR_SUCCESS) {
SDLOG(0, "ERROR opening language registry key for restoring\n");
return;
}
//restore previous locale
if(RegSetValueEx(key, "LocaleName", 0, REG_SZ, prevLang, prevLangSize) != ERROR_SUCCESS) {
RegCloseKey(key);
SDLOG(0, "ERROR restoring language registry key\n");
return;
}
//remove PrevLocaleName value
if(RegDeleteValue(key, "PrevLocaleName") != ERROR_SUCCESS) {
SDLOG(0, "ERROR deleting PrevLocaleName registry key\n");
}
SDLOG(0, "Restored previous language value %s\n", prevLang);
RegFlushKey(key);
RegCloseKey(key);
}