-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathconfig.cpp
More file actions
299 lines (260 loc) · 10.3 KB
/
Copy pathconfig.cpp
File metadata and controls
299 lines (260 loc) · 10.3 KB
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
/*
* Copyright (C) 2013-2014 LINK/2012 <dma_2012@hotmail.com>
* Licensed under the MIT License, see LICENSE at top level directory.
*
*/
#include <stdinc.hpp>
#include <numeric>
#include "loader.hpp"
using namespace modloader;
/*
* Loader::ReadBasicConfig
* Read the basic configuration file @filename
*/
void Loader::ReadBasicConfig()
{
linb::ini data;
Log("Loading basic config file %s", basicConfig.c_str());
if(data.load_file(modloaderPath + basicConfig))
{
// Read basic stuff from [Config] section
for(auto& pair : data["Config"])
{
if(!compare(pair.first, "EnableMenu", false))
this->bEnableMenu = to_bool(pair.second);
else if(!compare(pair.first, "EnablePlugins", false))
this->bEnablePlugins = to_bool(pair.second);
else if(!compare(pair.first, "EnableLog", false))
this->bEnableLog = to_bool(pair.second);
else if(!compare(pair.first, "ImmediateFlushLog", false))
this->bImmediateFlush = to_bool(pair.second);
else if(!compare(pair.first, "MaxLogSize", false))
this->maxBytesInLog = std::strtoul(pair.second.data(), 0, 0);
else if(!compare(pair.first, "RefreshKey", false))
this->vkRefresh = std::stoi(pair.second.data(), 0, 0);
else if(!compare(pair.first, "AutoRefresh", false))
this->bAutoRefresh = to_bool(pair.second);
}
}
else
Log("Failed to load basic config file");
}
/*
* Loader::SaveConfig
* Saves the basic config file with the current settings applied on this loader
*/
void Loader::SaveBasicConfig()
{
linb::ini ini;
auto& config = ini["Config"];
config["EnableMenu"] = modloader::to_string(bEnableMenu);
config["EnablePlugins"] = modloader::to_string(bEnablePlugins);
config["EnableLog"] = modloader::to_string(bEnableLog);
config["ImmediateFlushLog"] = modloader::to_string(bImmediateFlush);
config["MaxLogSize"] = std::to_string(maxBytesInLog);
config["RefreshKey"] = std::to_string(vkRefresh);
config["AutoRefresh"] = modloader::to_string(bAutoRefresh);
// Log only about failure since we'll be saving every time a entry on the menu changes
if(!ini.write_file(modloaderPath + basicConfig))
Log("Failed to save basic config file");
}
/*
* Loader::ParseCommandLine
* Parse command line arguments
*/
void Loader::ParseCommandLine()
{
char buf[512];
wchar_t **argv; int argc;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
Log("\nParsing command line");
if(!argv) // CommandLineToArgvW failed!!
{
Log("Failed to parse command line. CommandLineToArgvW() failed.");
return;
}
// Converts wchar_t buffer into char* ASCII buffer
auto toASCII = [](const wchar_t* wstr, char* abuf, size_t asize)
{
return !!WideCharToMultiByte(CP_ACP, 0, wstr, -1, abuf, asize, NULL, NULL);
};
Profile mods_cmd = this->mods.MakeProfile("$CmdLine");
std::string modprof;
bool has_nomods_cmd = false;
bool has_mod_cmd = false;
bool has_modprof_cmd = false;
for(int i = 0; i < argc; ++i)
{
if(argv[i][0] == '-')
{
wchar_t *arg = (i+1 < argc? argv[i+1] : nullptr);
wchar_t *argname = &argv[i][1];
if(!_wcsicmp(argname, L"nomods"))
{
has_nomods_cmd = true;
Log("Command line ignore received (-nomods)");
}
else if(!_wcsicmp(argname, L"mod"))
{
if(arg == nullptr)
{
Log("Warning: Failed to read command line because -mod command line is incomplete.");
break;
}
else if(toASCII(arg, buf, sizeof(buf)))
{
std::string modname = buf;
int priority = default_cmd_priority;
auto comps = modloader::split(std::string(buf), '='); // check if specifying priority
if(comps.size() == 2)
{
modname = comps[0];
priority = std::strtol(comps[1].c_str(), 0, 0);
}
mods_cmd.Include(modname);
mods_cmd.SetPriority(modname, priority);
Log("Command line mod received: \"%s\" with priority %d", modname.c_str(), priority);
has_mod_cmd = true;
}
}
else if(!_wcsicmp(argname, L"modprof"))
{
// Is argument after mod argument valid?
if(arg == nullptr)
{
Log("Warning: Failed to read command line because -modprof command line is incomplete.");
break;
}
else if(toASCII(arg, buf, sizeof(buf)))
{
modprof = buf;
has_modprof_cmd = true;
Log("Command line mod profile received: \"%s\"", modprof.c_str());
}
}
}
}
// Process commands.......
if(has_nomods_cmd)
{
Profile nomods = this->mods.MakeProfile("$CmdLine");
nomods.SetIgnoreAll(true);
this->mods.SetAnonymousProfile(nomods, true);
}
else if(has_modprof_cmd)
{
this->modprof_cmd = std::move(modprof);
}
else if(has_mod_cmd)
{
mods_cmd.SetExcludeAll(true);
this->mods.SetAnonymousProfile(mods_cmd, true);
}
LocalFree(argv);
}
/*
* Loader::UpdateOldConfig
* Updates any old ini config into any new format
*/
void Loader::UpdateOldConfig()
{
// Order matters
this->UpdateOldConfig_0115_021();
this->UpdateOldConfig_023_024();
}
/*
* Loader::UpdateOldConfig_0115_021
* Updates the old ini config (from 0.1.15) into the new ini config format (as from 0.2.1)
*/
void Loader::UpdateOldConfig_0115_021()
{
linb::ini old, newer;
struct keydata { const char *section, *key; };
// Loader independent config, as loaded by this->LoadBasicConfig(), as of 0.2.0 those configs are part of a different ini
// So just translate them to the new ini file
auto UpdateBasicConfig = [&]()
{
for(auto& pair : old["CONFIG"])
{
if(!compare(pair.first, "ENABLE_PLUGINS", false))
this->bEnablePlugins = to_bool(pair.second);
else if(!compare(pair.first, "LOG_ENABLE", false))
this->bEnableLog = to_bool(pair.second);
else if(!compare(pair.first, "LOG_IMMEDIATE_FLUSH", false))
this->bImmediateFlush = to_bool(pair.second);
}
this->SaveBasicConfig();
};
// Takes the key from the old ini and puts in the new ini with new formating
auto UpdateKey = [&](const keydata& oldkey, const keydata& newkey)
{
if(old[oldkey.section].count(oldkey.key))
newer[newkey.section].emplace(newkey.key, old[oldkey.section][oldkey.key]);
};
// Takes a section from the old ini and puts in the new ini with new formating
auto UpdateSection = [&](const keydata& oldsec, const keydata& newsec)
{
if(old.count(oldsec.section))
newer[newsec.section] = old[oldsec.section];
};
if(old.load_file(modloaderPath + folderConfigFilename))
{
if(old.count("CONFIG") && old.count("PRIORITY"))
{
this->Log("Found 0.1.15 modloader.ini, updating it to 0.2.1 format.");
// Updates key-values to newer version
UpdateBasicConfig();
UpdateSection(keydata { "PRIORITY" }, keydata { "Priority" });
UpdateSection(keydata { "EXCLUDE_FILES" }, keydata { "IgnoreFiles" });
UpdateSection(keydata { "INCLUDE_MODS" }, keydata { "IncludeMods" });
UpdateKey(keydata { "CONFIG", "IGNORE_ALL" }, keydata { "Config", "IgnoreAllFiles" });
UpdateKey(keydata { "CONFIG", "EXCLUDE_ALL" }, keydata { "Config", "ExcludeAllMods" });
// Reverse the priority since as from 0.2.1 it's '100 is greater, 1 is lower'
for(auto& kv : newer["Priority"])
{
auto pr = std::stoi(kv.second);
auto limit = this->mods.GetPriorityLimit();
kv.second = std::to_string(pr > 0 && pr <= limit? (limit + 1) - pr : pr);
}
newer.write_file(modloaderPath + folderConfigFilename);
}
}
}
/*
* Loader::UpdateOldConfig_023_024
* Updates the old ini config (from 0.2.3) into the new ini config format (as from 0.2.4)
*/
void Loader::UpdateOldConfig_023_024()
{
modloader_ini old, newer;
struct keydata { const char *section, *key; };
// Takes the key from the old ini and puts in the new ini with new formating
auto UpdateKey = [&](const keydata& oldkey, const keydata& newkey)
{
if(old[oldkey.section].count(oldkey.key))
newer[newkey.section].emplace(newkey.key, old[oldkey.section][oldkey.key]);
};
// Takes a section from the old ini and puts in the new ini with new formating
auto UpdateSection = [&](const keydata& oldsec, const keydata& newsec)
{
if(old.count(oldsec.section))
newer[newsec.section] = old[oldsec.section];
};
if(old.load_file(modloaderPath + folderConfigFilename))
{
if(old.count("Config") && old.count("Priority"))
{
this->Log("Found 0.2.3 modloader.ini, updating it to 0.2.4 format.");
// Updates key-values to newer version
UpdateSection(keydata { "Config" }, keydata { "Profiles.Default.Config" });
UpdateSection(keydata { "Priority" }, keydata { "Profiles.Default.Priority" });
UpdateSection(keydata { "IgnoreFiles" }, keydata { "Profiles.Default.IgnoreFiles" });
UpdateSection(keydata { "IncludeMods" }, keydata { "Profiles.Default.IncludeMods" });
UpdateSection(keydata { "IgnoreMods" }, keydata { "Profiles.Default.IgnoreMods" });
// Add new key values
newer.set("Folder.Config", "Profile", "Default");
newer["Profiles.Default.ExclusiveMods"];
newer.write_file(modloaderPath + folderConfigFilename);
}
}
}