-
Notifications
You must be signed in to change notification settings - Fork 23
/
StartupConfig.cpp
169 lines (144 loc) · 7.51 KB
/
StartupConfig.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
#include "StartupConfig.h"
#include "ExperimentConfig.h"
#include "FPSciAnyTableReader.h"
ConfigFiles::ConfigFiles(const Any& any) {
FPSciAnyTableReader reader(any);
reader.get("name", name, "Must provide a name for every entry in the experimentList!");
reader.getIfPresent("experimentConfigFilename", experimentConfigFilename);
checkValidAnyFilename("experimentConfigFilename", experimentConfigFilename);
reader.getIfPresent("userConfigFilename", userConfigFilename);
checkValidAnyFilename("userConfigFilename", userConfigFilename);
reader.getIfPresent("userStatusFilename", userStatusFilename);
checkValidAnyFilename("userStatusFilename", userStatusFilename);
reader.getIfPresent("keymapConfigFilename", keymapConfigFilename);
checkValidAnyFilename("keymapConfigFilename", keymapConfigFilename);
reader.getIfPresent("systemConfigFilename", systemConfigFilename);
checkValidAnyFilename("systemConfigFilename", systemConfigFilename);
reader.getIfPresent("resultsDirPath", resultsDirPath);
resultsDirPath = formatDirPath(resultsDirPath);
}
ConfigFiles ConfigFiles::defaults() {
return ConfigFiles("default", "experimentconfig.Any", "userconfig.Any", "userstatus.Any", "keymap.Any", "systemconfig.Any", "./results");
}
Any ConfigFiles::toAny(const bool forceAll) const {
Any a(Any::TABLE);
a["name"] = name;
a["experimentConfigFilename"] = experimentConfigFilename;
a["userConfigFilename"] = userConfigFilename;
a["userStatusFilename"] = userStatusFilename;
a["keymapConfigFilename"] = keymapConfigFilename;
a["systemConfigFilename"] = systemConfigFilename;
a["resultsDirPath"] = resultsDirPath;
return a;
}
void ConfigFiles::populateEmptyFieldsWithDefaults(const ConfigFiles& def) {
if (experimentConfigFilename.empty()) { experimentConfigFilename = def.experimentConfigFilename; }
if (userConfigFilename.empty()) { userConfigFilename = def.userConfigFilename; }
if (userStatusFilename.empty()) { userStatusFilename = def.userStatusFilename; }
if (keymapConfigFilename.empty()) { keymapConfigFilename = def.keymapConfigFilename; }
if (systemConfigFilename.empty()) { systemConfigFilename = def.systemConfigFilename; }
if (resultsDirPath.empty()) { resultsDirPath = def.resultsDirPath; }
}
void ConfigFiles::checkValidAnyFilename(const String& errorName, const String& path) {
if (path.empty()) return; // Allow empty paths (will be replaced)
alwaysAssertM(toLower(path.substr(path.length() - 4)) == ".any", "Config filenames specified in the startup config must end with \".any\"!, check the " + errorName + "!\n");
}
String ConfigFiles::formatDirPath(const String& path) {
String fpath = path;
if (!path.empty() && path.substr(path.length() - 1) != "/") { fpath = path + "/"; }
return fpath;
}
StartupConfig StartupConfig::load(const String& filename) {
StartupConfig config;
if (!FileSystem::exists(filename)) {
config.toAny(true).save(filename, config.jsonAnyOutput); // If the file doesn't exist create it
}
return Any::fromFile(filename); // Load back from the Any file
}
StartupConfig::StartupConfig(const Any& any) {
bool foundDefault = false;
int settingsVersion = 1;
FPSciAnyTableReader reader(any);
reader.getIfPresent("settingsVersion", settingsVersion);
Array<String> sampleFilenames;
ConfigFiles sampleExperiment;
switch (settingsVersion) {
case 1:
reader.getIfPresent("developerMode", developerMode);
reader.getIfPresent("waypointEditorMode", waypointEditorMode);
reader.getIfPresent("fullscreen", fullscreen);
reader.getIfPresent("windowSize", windowSize);
reader.getIfPresent("jsonAnyOutput", jsonAnyOutput);
foundDefault = reader.getIfPresent("defaultExperiment", defaultExperiment);
if (!foundDefault) {
logPrintf("Warning: no `defaultExperiment` found in `startupConfig.Any`, you may be using an old experiment config.\n");
String experimentConfigFilename, userConfigFilename, userStatusFilename, keymapConfigFilename, systemConfigFilename, resultsDirPath;
reader.getIfPresent("experimentConfigFilename", experimentConfigFilename);
reader.getIfPresent("userConfigFilename", userConfigFilename);
reader.getIfPresent("userStatusFilename", userStatusFilename);
reader.getIfPresent("keymapConfigFilename", keymapConfigFilename);
reader.getIfPresent("systemConfigFilename", systemConfigFilename);
reader.getIfPresent("resultsDirPath", resultsDirPath);
logPrintf("One possible fix is to use the following in `startupConfig.Any`:\n");
logPrintf(" defaultExperiment = {\n");
logPrintf(" name = \"default\";\n");
logPrintf(" experimentConfigFilename = \"%s\";\n", experimentConfigFilename);
logPrintf(" userConfigFilename = \"%s\";\n", userConfigFilename);
logPrintf(" userStatusFilename = \"%s\";\n", userStatusFilename);
logPrintf(" keymapConfigFilename = \"%s\";\n", keymapConfigFilename);
logPrintf(" systemConfigFilename = \"%s\";\n", systemConfigFilename);
logPrintf(" resultsDirPath = \"%s\";\n", resultsDirPath);
logPrintf(" };\n");
}
reader.getIfPresent("experimentList", experimentList);
for (ConfigFiles& files : experimentList) { files.populateEmptyFieldsWithDefaults(defaultExperiment); }
if (experimentList.length() == 0) { experimentList.append(defaultExperiment); }
logPrintf("Searching 'samples' directory for sample experiments.\n");
FileSystem::getFiles("samples/*.Experiment.Any", sampleFilenames);
sampleExperiment.populateEmptyFieldsWithDefaults(defaultExperiment);
sampleExperiment.userConfigFilename = format("samples/sample.User.Any");
for (String& sampleFile : sampleFilenames) {
String name = sampleFile.substr(0, sampleFile.find_first_of('.'));
sampleExperiment.experimentConfigFilename = format("samples/%s", sampleFile);
sampleExperiment.name = format("[Sample]%s", name);
sampleExperiment.userStatusFilename = format("samples/%s.Status.Any", name);
if (!FileSystem::exists(sampleExperiment.userStatusFilename)) {
logPrintf("Skipping Sample '%s' because user status '%s' doesn't exist\n", name, sampleExperiment.userStatusFilename);
continue;
}
experimentList.append(sampleExperiment);
logPrintf("Sample '%s' added to experiment list\n", sampleExperiment.name);
}
logPrintf("\n");
reader.getIfPresent("audioEnable", audioEnable);
break;
default:
debugPrintf("Settings version '%d' not recognized in StartupConfig.\n", settingsVersion);
break;
}
}
Any StartupConfig::toAny(const bool forceAll) const {
StartupConfig def; // Create a dummy default config for value testing
Any a(Any::TABLE);
if (forceAll || def.developerMode != developerMode) a["developerMode"] = developerMode;
if (forceAll || def.waypointEditorMode != waypointEditorMode) a["waypointEditorMode"] = waypointEditorMode;
if (forceAll || def.fullscreen != fullscreen) a["fullscreen"] = fullscreen;
if (forceAll || def.audioEnable != audioEnable) a["audioEnable"] = audioEnable;
if (forceAll || def.jsonAnyOutput != jsonAnyOutput) a["jsonAnyOutput"] = jsonAnyOutput;
a["defaultExperiment"] = defaultExperiment;
a["experimentList"] = experimentList;
return a;
}
bool StartupConfig::validateExperiments() const {
// Validate experiment configs
bool valid = true;
for (auto& configs : experimentList) {
ExperimentConfig experimentConfig = ExperimentConfig::load(configs.experimentConfigFilename, jsonAnyOutput);
logPrintf("Validating experiment '%s'\n", configs.name);
if (!experimentConfig.validate(false)) {
logPrintf(" Error: experiment '%s' is not valid! (See '%s')\n", configs.name, configs.experimentConfigFilename);
valid = false;
}
}
return valid;
}