-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_settings.cpp
69 lines (60 loc) · 2.69 KB
/
global_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
#include "global_settings.h"
#include <fstream>
#include "utilities.h"
using json = nlohmann::json;
namespace fs = std::filesystem;
MasterSettings::MasterSettings(fs::path settingsPath) {
if (!fs::exists(settingsPath) || loadSettings(fetchJson(settingsPath))) {
std::stringstream settingsText;
writeSettings(settingsText);
writeStringStreamToPath(settingsText, settingsPath);
}
postLoad();
}
bool MasterSettings::loadSettings(json settingsJson) {
//TODO: Handle invalid types more gracefully.
bool missingSettings = false;
if (settingsJson.contains("baselinePatchStyleName")) {
baselinePatchStyleName = settingsJson["baselinePatchStyleName"];
} else {
missingSettings = true;
}
if (settingsJson.contains("overwriteFiles")) {
overwriteFiles = settingsJson["overwriteFiles"];
} else {
missingSettings = true;
}
if (settingsJson.contains("useInverseTestOps")) {
useInverseTestOps = settingsJson["useInverseTestOps"];
} else {
missingSettings = true;
}
if (settingsJson.contains("useOperationSets")) {
useOperationSets = settingsJson["useOperationSets"];
} else {
missingSettings = true;
}
return missingSettings;
}
void MasterSettings::writeSettings(std::stringstream & settingsText) {
settingsText << "//User added comments in this file are not preserved when it is updated.\n"
<< '{'
<< "\n //Loaded from \\patch_styles\\*.json"
<< "\n \"baselinePatchStyleName\" : \"" << baselinePatchStyleName << "\","
<< "\n //Skip prompts to delete files. Not suggested for normal use."
<< "\n \"overwriteFiles\" : " << (overwriteFiles ? "true" : "false") << ','
<< "\n //Inverse tests are used by Starbound to see if a value is present or not, regardless of its contents."
<< "\n \"useInverseTestOps\" : " << (useInverseTestOps ? "true" : "false") << ','
<< "\n //Operation sets are used by Starbound to allow more than one set of operations to be applied from a patch file."
<< "\n \"useOperationSets\" : " << (useOperationSets ? "true" : "false")
<< "\n}\n";
}
void MasterSettings::postLoad() {
const fs::path patchStylePath = fs::current_path() /= ("config/patch_styles/" + baselinePatchStyleName + ".json");
baselinePatchStyle = PatchStyleSettings(patchStylePath);
}
//Getters
std::string MasterSettings::getBaselinePatchStyleName() { return baselinePatchStyleName; }
const bool MasterSettings::getOverwriteFiles() { return overwriteFiles; }
const bool MasterSettings::getUseInverseTestOps() { return useInverseTestOps; }
const bool MasterSettings::getUseOperationSets() { return useOperationSets; }