-
Notifications
You must be signed in to change notification settings - Fork 3
/
Settings.cpp
354 lines (297 loc) · 16.4 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** @file PluginMain.cpp
* Controls the Plugin settings
*
**/
// Copyright (C) 2022 - Leonardo Silva
// The License.txt file describes the conditions under which this software may be distributed.
#include "pch.h"
//#include <algorithm>
//#include <cctype>
//#include <string>
//#include <tchar.h>
//#include <sstream>
//#include <iomanip>
#include "Settings.h"
#define PATHSPLITREGEX TEXT(R"(\"?([^;"]+)\"?;?)")
#define FILTERSREGEX TEXT(R"(([^,]+))")
jpcre2::select<TCHAR>::Regex dirRegex(PATHSPLITREGEX, 0, jpcre2::FIND_ALL | jpcre2::JIT_COMPILE);
jpcre2::select<TCHAR>::Regex filtersRegex(FILTERSREGEX, 0, jpcre2::FIND_ALL | jpcre2::JIT_COMPILE);
using namespace NWScriptPlugin;
void Settings::Load()
{
// Read or CREATE ini config.
std::ignore = iniFilePath->read(*iniFile);
// Try to read if INI file was valid/existent prior to this load. If not, flush any unclear results, keep default values.
_bValidINI = GetBoolean(TEXT("Plugin Startup"), TEXT("_bValidINI"));
if (!_bValidINI)
{
iniFile->clear();
return;
}
// Load all settings variables from INI here
enableAutoIndentation = GetBoolean(TEXT("Plugin Functions"), TEXT("enableAutoIndentation"));
autoIndentationWarningAccepted = GetBoolean(TEXT("Plugin Functions"), TEXT("autoIndentationWarningAccepted"));
installedEngineKnownObjects = GetBoolean(TEXT("Plugin Functions"), TEXT("installedEngineKnownObjects"));
notepadRestartMode = static_cast<RestartMode>(GetNumber<int>(TEXT("Notepad Restart"), TEXT("notepadRestartMode")));
notepadRestartFunction = static_cast<RestartFunctionHook>(GetNumber<int>(TEXT("Notepad Restart"), TEXT("notepadRestartFunction")));
// Compiler settings
compilerSettingsCreated = GetBoolean(TEXT("Compiler Settings"), TEXT("compilerSettingsCreated"));
neverwinterInstallChoice = GetNumber<int>(TEXT("Compiler Settings"), TEXT("neverwinterInstallChoice"));
neverwinterOneInstallDir = properDirNameW(GetString(TEXT("Compiler Settings"), TEXT("neverwinterOneInstallDir")));
neverwinterTwoInstallDir = properDirNameW(GetString(TEXT("Compiler Settings"), TEXT("neverwinterTwoInstallDir")));
ignoreInstallPaths = GetBoolean(TEXT("Compiler Settings"), TEXT("ignoreInstallPaths"));
additionalIncludeDirs = GetString(TEXT("Compiler Settings"), TEXT("additionalIncludeDirs"));
compilerEngine = GetNumber<int>(TEXT("Compiler Settings"), TEXT("compilerEngine"));
compilerFlags = GetNumber<int>(TEXT("Compiler Settings"), TEXT("compilerFlags"));
optimizeScript = GetBoolean(TEXT("Compiler Settings"), TEXT("optimizeScript"));
useNonBiowareExtenstions = GetBoolean(TEXT("Compiler Settings"), TEXT("useNonBiowareExtenstions"));
generateSymbols = GetBoolean(TEXT("Compiler Settings"), TEXT("generateSymbols"));
compileVersion = GetNumber<int>(TEXT("Compiler Settings"), TEXT("compileVersion"));
useScriptPathToCompile = GetBoolean(TEXT("Compiler Settings"), TEXT("useScriptPathToCompile"));
outputCompileDir = properDirNameW(GetString(TEXT("Compiler Settings"), TEXT("outputCompileDir")));
// Batch Process
startingBatchFolder = properDirNameW(GetString(TEXT("Batch Processing"), TEXT("startingBatchFolder")));
fileFiltersCompile = GetString(TEXT("Batch Processing"), TEXT("fileFiltersCompile"));
fileFiltersDisasm = GetString(TEXT("Batch Processing"), TEXT("fileFiltersDisasm"));
batchCompileMode = GetNumber<int>(TEXT("Batch Processing"), TEXT("batchCompileMode"));
recurseSubFolders = GetBoolean(TEXT("Batch Processing"), TEXT("recurseSubFolders"));
continueCompileOnFail = GetBoolean(TEXT("Batch Processing"), TEXT("continueCompileOnFail"));
useScriptPathToBatchCompile = GetBoolean(TEXT("Batch Processing"), TEXT("useScriptPathToBatchCompile"));
batchOutputCompileDir = properDirNameW(GetString(TEXT("Batch Processing"), TEXT("batchOutputCompileDir")));
// User's Preferences
autoDisplayDisassembled = GetBoolean(TEXT("User's Preferences"), TEXT("autoDisplayDisassembled"));
autoDisplayDebugSymbols = GetBoolean(TEXT("User's Preferences"), TEXT("autoDisplayDebugSymbols"));
autoInstallDarkTheme = GetBoolean(TEXT("User's Preferences"), TEXT("autoInstallDarkTheme"));
legacyDarkModeUse = GetBoolean(TEXT("User's Preferences"), TEXT("legacyDarkModeUse"));
lastOpenedDir = properDirNameW(GetString(TEXT("User's Preferences"), TEXT("lastOpenedDir")));
// Dark Theme auto-install support
darkThemePreviouslyInstalled = GetBoolean(TEXT("Auto-Install"), TEXT("darkThemePreviouslyInstalled"));
darkThemeInstallAttempt = GetBoolean(TEXT("Auto-Install"), TEXT("darkThemeInstallAttempt"));
notepadVersion = GetString(TEXT("Auto-Install"), TEXT("notepadVersion"));
// Plugin statistics
compileAttempts = GetNumber<int>(TEXT("User Statistics"), TEXT("compileAttempts"));
compileSuccesses = GetNumber<int>(TEXT("User Statistics"), TEXT("compileSuccesses"));
compileFails = GetNumber<int>(TEXT("User Statistics"), TEXT("compileFails"));
disassembledFiles = GetNumber<int>(TEXT("User Statistics"), TEXT("disassembledFiles"));
engineStructs = GetNumber<int>(TEXT("User Statistics"), TEXT("engineStructs"));
engineFunctionCount = GetNumber<int>(TEXT("User Statistics"), TEXT("engineFunctionCount"));
engineConstants = GetNumber<int>(TEXT("User Statistics"), TEXT("engineConstants"));
userFunctionCount = GetNumber<int>(TEXT("User Statistics"), TEXT("userFunctionCount"));
userConstants = GetNumber<int>(TEXT("User Statistics"), TEXT("userConstants"));
// Compiler window settings
compilerWindowSelectedTab = GetNumber<int>(TEXT("Compiler Window"), TEXT("compilerWindowSelectedTab"));
compilerWindowShowErrors = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowShowErrors"));
compilerWindowShowWarnings = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowShowWarnings"));
compilerWindowShowInfos = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowShowInfos"));
compilerWindowConsoleWordWrap = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleWordWrap"));
compilerWindowConsoleShowErrors = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleShowErrors"));
compilerWindowConsoleShowWarnings = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleShowWarnings"));
compilerWindowConsoleShowInfos = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleShowInfos"));
compilerWindowVisible = GetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowVisible"));
// Sanity checks: avoid loading missing or corrupted data for compiled settings. Mark configurations invalid if inconsistency detected.
if (!isValidDirectoryS(neverwinterOneInstallDir) && !isValidDirectoryS(neverwinterTwoInstallDir) && !ignoreInstallPaths)
{
neverwinterOneInstallDir = TEXT("");
neverwinterTwoInstallDir = TEXT("");
compilerSettingsCreated = false;
}
if (!isValidDirectoryS(neverwinterOneInstallDir) && neverwinterInstallChoice == 0 && !ignoreInstallPaths)
{
neverwinterOneInstallDir = TEXT("");
compilerSettingsCreated = false;
}
if (!isValidDirectoryS(neverwinterTwoInstallDir) && neverwinterInstallChoice == 1 && !ignoreInstallPaths)
{
neverwinterTwoInstallDir = TEXT("");
compilerSettingsCreated = false;
}
generic_string validPaths;
for (generic_string s : getIncludeDirsV())
{
if (isValidDirectoryS(s))
validPaths.append(s).append(TEXT(";"));
else
compilerSettingsCreated = false;
}
additionalIncludeDirs = validPaths;
if (compileVersion != 174 && compileVersion != 169)
{
compileVersion = 174;
compilerSettingsCreated = false;
}
if (!isValidDirectoryS(outputCompileDir) && !useScriptPathToCompile)
{
outputCompileDir = TEXT("");
compilerSettingsCreated = false;
}
if (!isValidDirectoryS(lastOpenedDir))
lastOpenedDir = TEXT("");
// We aren't checking batch operations settings here,
// since the user will have to run the Dialog first to run a batch...
}
void Settings::Save()
{
// Set this file as valid
SetBoolean(TEXT("Plugin Startup"), TEXT("_bValidINI"), true);
// Set all settings variables to INI here
SetBoolean(TEXT("Plugin Functions"), TEXT("enableAutoIndentation"), enableAutoIndentation);
SetBoolean(TEXT("Plugin Functions"), TEXT("autoIndentationWarningAccepted"), autoIndentationWarningAccepted);
SetBoolean(TEXT("Plugin Functions"), TEXT("installedEngineKnownObjects"), installedEngineKnownObjects);
SetNumber<int>(TEXT("Notepad Restart"), TEXT("notepadRestartMode"), static_cast<int>(notepadRestartMode));
SetNumber<int>(TEXT("Notepad Restart"), TEXT("notepadRestartFunction"), static_cast<int>(notepadRestartFunction));
// Compiler settings
SetBoolean(TEXT("Compiler Settings"), TEXT("compilerSettingsCreated"), compilerSettingsCreated);
SetNumber<int>(TEXT("Compiler Settings"), TEXT("neverwinterInstallChoice"), neverwinterInstallChoice);
SetString(TEXT("Compiler Settings"), TEXT("neverwinterOneInstallDir"), neverwinterOneInstallDir);
SetString(TEXT("Compiler Settings"), TEXT("neverwinterTwoInstallDir"), neverwinterTwoInstallDir);
SetBoolean(TEXT("Compiler Settings"), TEXT("ignoreInstallPaths"), ignoreInstallPaths);
SetString(TEXT("Compiler Settings"), TEXT("additionalIncludeDirs"), additionalIncludeDirs);
SetNumber<int>(TEXT("Compiler Settings"), TEXT("compilerEngine "), compilerEngine);
SetNumber<int>(TEXT("Compiler Settings"), TEXT("compilerFlags"), compilerFlags);
SetBoolean(TEXT("Compiler Settings"), TEXT("optimizeScript"), optimizeScript);
SetBoolean(TEXT("Compiler Settings"), TEXT("useNonBiowareExtenstions"), useNonBiowareExtenstions);
SetBoolean(TEXT("Compiler Settings"), TEXT("generateSymbols"), generateSymbols);
SetNumber<int>(TEXT("Compiler Settings"), TEXT("compileVersion"), compileVersion);
SetBoolean(TEXT("Compiler Settings"), TEXT("useScriptPathToCompile"), useScriptPathToCompile);
SetString(TEXT("Compiler Settings"), TEXT("outputCompileDir"), outputCompileDir);
// Batch Process
SetString(TEXT("Batch Processing"), TEXT("startingBatchFolder"), startingBatchFolder);
SetString(TEXT("Batch Processing"), TEXT("fileFiltersCompile"), fileFiltersCompile);
SetString(TEXT("Batch Processing"), TEXT("fileFiltersDisasm"), fileFiltersDisasm);
SetNumber<int>(TEXT("Batch Processing"), TEXT("batchCompileMode"), batchCompileMode);
SetBoolean(TEXT("Batch Processing"), TEXT("recurseSubFolders"), recurseSubFolders);
SetBoolean(TEXT("Batch Processing"), TEXT("continueCompileOnFail"), continueCompileOnFail);
SetBoolean(TEXT("Batch Processing"), TEXT("useScriptPathToBatchCompile"), useScriptPathToBatchCompile);
SetString(TEXT("Batch Processing"), TEXT("batchOutputCompileDir"), batchOutputCompileDir);
// User's Preferences
SetBoolean(TEXT("User's Preferences"), TEXT("autoDisplayDisassembled"), autoDisplayDisassembled);
SetBoolean(TEXT("User's Preferences"), TEXT("autoDisplayDebugSymbols"), autoDisplayDebugSymbols);
SetBoolean(TEXT("User's Preferences"), TEXT("autoInstallDarkTheme"), autoInstallDarkTheme);
SetBoolean(TEXT("User's Preferences"), TEXT("legacyDarkModeUse"), legacyDarkModeUse);
SetString(TEXT("User's Preferences"), TEXT("lastOpenedDir"), lastOpenedDir);
// Dark Theme auto-install support
SetBoolean(TEXT("Auto-Install"), TEXT("darkThemePreviouslyInstalled"), darkThemePreviouslyInstalled);
SetBoolean(TEXT("Auto-Install"), TEXT("darkThemeInstallAttempt"), darkThemeInstallAttempt);
SetString(TEXT("Auto-Install"), TEXT("notepadVersion"), notepadVersion.wstring());
// Plugin statistics
SetNumber<int>(TEXT("User Statistics"), TEXT("compileAttempts"), compileAttempts);
SetNumber<int>(TEXT("User Statistics"), TEXT("compileSuccesses"), compileSuccesses);
SetNumber<int>(TEXT("User Statistics"), TEXT("compileFails"), compileFails);
SetNumber<int>(TEXT("User Statistics"), TEXT("disassembledFiles"), disassembledFiles);
SetNumber<int>(TEXT("User Statistics"), TEXT("engineStructs"), engineStructs);
SetNumber<int>(TEXT("User Statistics"), TEXT("engineFunctionCount"), engineFunctionCount);
SetNumber<int>(TEXT("User Statistics"), TEXT("engineConstants"), engineConstants);
SetNumber<int>(TEXT("User Statistics"), TEXT("userFunctionCount"), userFunctionCount);
SetNumber<int>(TEXT("User Statistics"), TEXT("userConstants"), userConstants);
// Compiler window settings
SetNumber<int>(TEXT("Compiler Window"), TEXT("compilerWindowSelectedTab"), compilerWindowSelectedTab);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowShowErrors"), compilerWindowShowErrors);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowShowWarnings"), compilerWindowShowWarnings);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowShowInfos"), compilerWindowShowInfos);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleWordWrap"), compilerWindowConsoleWordWrap);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleShowErrors"), compilerWindowConsoleShowErrors);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleShowWarnings"), compilerWindowConsoleShowWarnings);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowConsoleShowInfos"), compilerWindowConsoleShowInfos);
SetBoolean(TEXT("Compiler Window"), TEXT("compilerWindowVisible"), compilerWindowVisible);
if (!iniFilePath->write(*iniFile))
MessageBox(NULL,
(TEXT("Could not save the .ini file: ") + iniFilePathName).c_str(),
TEXT("NWScript-Npp shutdown"), MB_ICONERROR | MB_OK);
}
std::vector<generic_string> Settings::getIncludeDirsV() {
return string2VectorRegex(additionalIncludeDirs, dirRegex);
}
std::vector<generic_string> Settings::getFileFiltersCompileV() {
return string2VectorRegex(fileFiltersCompile, filtersRegex);
}
std::vector<generic_string> Settings::getFileFiltersDisasmV() {
return string2VectorRegex(fileFiltersDisasm, filtersRegex);
}
void Settings::setIncludeDirs(const std::vector<generic_string>& newDirs)
{
additionalIncludeDirs = TEXT("");
for (const generic_string& m : newDirs)
additionalIncludeDirs.append(m);
}
void Settings::setFileFiltersCompile(const std::vector<generic_string>& newFilters)
{
fileFiltersCompile = TEXT("");
for (const generic_string& m : newFilters)
fileFiltersCompile.append(m);
}
void Settings::setFileFiltersDisasm(const std::vector<generic_string>& newFilters)
{
fileFiltersDisasm = TEXT("");
for (const generic_string& m : newFilters)
fileFiltersDisasm.append(m);
}
std::vector<generic_string> Settings::string2VectorRegex(const generic_string& target, const jpcre2::select<TCHAR>::Regex& separator)
{
std::vector<generic_string> results;
jpcre2::select<TCHAR>::VecNum matchr;
jpcre2::select<TCHAR>::RegexMatch matcher(&separator);
matcher.setNumberedSubstringVector(&matchr).setSubject(target).setModifier("g").match();
for (std::vector<generic_string> m : matchr)
{
if (separator.getPattern() == PATHSPLITREGEX)
results.push_back(properDirNameW(m[1]));
else
results.push_back(m[1]);
}
return results;
}
#pragma region
// Try to get a number from INI. Returns 0 if not a number or don't exist
template <typename T>
T Settings::GetNumber(const generic_string& section, const generic_string& key)
{
generic_stringstream rValue = {};
rValue << (*iniFile)[section.c_str()][key.c_str()];
if (rValue.str().empty())
return 0;
T xNumber = 0;
rValue << std::fixed;
rValue << std::setprecision(std::numeric_limits<T>::digits10);
rValue >> xNumber;
return xNumber;
}
bool Settings::GetBoolean(const generic_string& section, const generic_string& key)
{
generic_string rValue = (*iniFile)[section.c_str()][key.c_str()];
// False on empty or inexistent section
if (rValue.empty())
return false;
// Try to get from TRUE or FALSE types of values
std::transform(rValue.begin(), rValue.end(), rValue.begin(), ::towlower);
if (_tcscmp(rValue.c_str(), TEXT("false")) == 0)
return false;
if (_tcscmp(rValue.c_str(), TEXT("true")) == 0)
return true;
// Return from a number
return static_cast<bool>(GetNumber<int>(section, key));
}
inline generic_string Settings::GetString(const generic_string& section, const generic_string& key)
{
return (*iniFile)[section.c_str()][key.c_str()];
}
template <typename T>
void Settings::SetNumber(const generic_string& section, const generic_string& key, T value)
{
generic_stringstream sInput;
sInput << std::fixed;
sInput << std::setprecision(std::numeric_limits<T>::digits10);
sInput << value;
(*iniFile)[section.c_str()][key.c_str()] = sInput.str().c_str();
}
void Settings::SetBoolean(const generic_string& section, const generic_string& key, bool value)
{
if (value)
(*iniFile)[section.c_str()][key.c_str()] = TEXT("true");
else
(*iniFile)[section.c_str()][key.c_str()] = TEXT("false");
}
inline void Settings::SetString(const generic_string& section, const generic_string& key, const generic_string& value)
{
(*iniFile)[section.c_str()][key.c_str()] = value.c_str();
}
#pragma endregion Internal INI processing