-
Notifications
You must be signed in to change notification settings - Fork 0
/
f_civ.cpp
346 lines (298 loc) · 10.1 KB
/
f_civ.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
#include <f_civ.h>
#include <w_main.h>
#include <QtCore>
#include <QtNetwork>
#include <QtGui>
#include <QtWidgets>
#include <QDebug>
#include <QtXml/QtXml>
using namespace std;
// Set the mod to start by default
bool setConfigParam(QString param, QString newValue)
{
// Get config paths
QDir config_path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/My Games/Beyond the sword/";
QString ini_orig = config_path.filePath("CivilizationIV.ini");
QString ini_temp = config_path.filePath("CivlizationIV.ini.temp");
QString ini_backup = config_path.filePath("CivilizationIV.bak");
// Make a backup
QFile::remove(ini_backup);
QFile::copy(ini_orig, ini_backup);
// Set value
QFile file_in(ini_orig);
QFile file_out(ini_temp);
file_in.open(QIODevice::ReadOnly | QIODevice::Text);
file_out.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream in_enc(&file_in);
QTextStream out_enc(&file_out);
while(!in_enc.atEnd())
{
QString line = in_enc.readLine();
if(line.contains(param + " ="))
{
line = param + " = " + newValue;
}
out_enc << line << "\n";
}
file_in.close();
file_out.close();
QFile::remove(ini_orig);
QFile::rename(ini_temp,ini_orig);
//qDebug() << "Parameter set to " << newValue;
return 0;
}
QString readConfigParam(QString param)
{
// Get config paths
QDir config_path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/My Games/Beyond the sword/";
QString ini_orig = config_path.filePath("CivilizationIV.ini");
// Read value
QFile file_in(ini_orig);
QString value;
file_in.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in_enc(&file_in);
while(!in_enc.atEnd())
{
QString line = in_enc.readLine();
if(line.contains(param + " ="))
{
line.replace(param + " = ","");
value = line;
}
}
file_in.close();
return value;
}
QString readCheckerParam(QString param)
{
QSettings settings("checker/checker_config.ini", QSettings::IniFormat);
if(!settings.contains(param)) {
return "error";
}
QString value = settings.value(param).toString();
return value;
}
bool setCheckerParam(QString param, QString newValue)
{
if(!QFile::exists("checker/checker_config.ini")) {
QFile ch_conf;
ch_conf.open(QIODevice::WriteOnly);
}
QSettings settings("checker/checker_config.ini", QSettings::IniFormat);
//qDebug() << settings.status();
settings.setValue(param, newValue);
//qDebug() << "Checker parameter set to" << settings.value(param);
return 0;
}
int readColorsCounter()
{
// Open the file
QDomDocument read;
QFile file("Assets/Modules/Interface Colors/MLF_CIV4ModularLoadingControls.xml");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Error opening Assets/Modules/Interface Colors/MLF_CIV4ModularLoadingControls.xml";
return 0;
}
read.setContent(&file);
// Go to color level
QDomElement value_el = read.firstChildElement("Civ4ModularLoadControls").firstChildElement("ConfigurationInfos").firstChildElement("ConfigurationInfo").firstChildElement("Modules").firstChildElement("Module").toElement();
// Loop
int counter = 0;
for(;; value_el=value_el.nextSiblingElement() ) {
QString bLoad = value_el.firstChildElement("bLoad").text();
counter++;
if (bLoad == "1") {
//qDebug() << "Counter is " << counter;
file.close();
return counter;
}
if (counter == 7)
{
file.close();
return 0;
}
}
file.close();
return 99;
}
bool setColors(QString color)
{
// Open the file
QDomDocument read;
QFile file("Assets/Modules/Interface Colors/MLF_CIV4ModularLoadingControls.xml");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Error opening Assets/Modules/Interface Colors/MLF_CIV4ModularLoadingControls.xml";
return 0;
}
read.setContent(&file);
file.close();
// Go to color level
QDomElement color_element = read.firstChildElement("Civ4ModularLoadControls").firstChildElement("ConfigurationInfos").firstChildElement("ConfigurationInfo").firstChildElement("Modules").firstChildElement("Module").toElement();
// Reset all values
for(color_element ; !color_element.isNull(); color_element=color_element.nextSiblingElement() ) {
color_element.firstChildElement("bLoad").firstChild().setNodeValue("0");
}
if(color != "default"){
color_element = read.firstChildElement("Civ4ModularLoadControls").firstChildElement("ConfigurationInfos").firstChildElement("ConfigurationInfo").firstChildElement("Modules").firstChildElement("Module").toElement();
for(color_element ; !color_element.isNull(); color_element=color_element.nextSiblingElement() ) {
QString txtValue = color_element.firstChildElement("Directory").firstChild().nodeValue();
if (txtValue == color) {
color_element.firstChildElement("bLoad").firstChild().setNodeValue("1");
}
}
}
// Save content back to the file
if (!file.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
return 0;
}
file.write(read.toByteArray());
file.close();
return 0;
}
void launchGame(){
QString bt_exe = readCheckerParam("Main/ExecutablePath");
QUrl u = QUrl::fromLocalFile(bt_exe);
QDesktopServices::openUrl(QUrl(u));
}
/*QString check_addon_mcp()
{
QFile addon_MCP_file("Assets/Addon_MCP.ini");
QFile addon_MCP_file2("Assets/MCP0.FPK");
if(addon_MCP_file2.exists() == true)
{
if(addon_MCP_file.exists() == true)
{
qDebug() << "Civ Mega Pack detected";
QSettings addon_MCP_settings("Assets/Addon_MCP.ini", QSettings::IniFormat);
QString MCP_version = addon_MCP_settings.value("Main/Version").toString();
setCheckerParam("ADDON_MEGACIVPACK/FilesVersion",MCP_version);
return MCP_version;
}
else
{
setCheckerParam("ADDON_MEGACIVPACK/FilesVersion","Unknown");
return "Unknown";
}
}
else
{
qDebug() << "Civ Mega Pack not detected";
setCheckerParam("ADDON_MEGACIVPACK/FilesVersion","Not installed");
return "Not installed";
}
}
QString check_addon_more_music()
{
QFile addon_audio_file("Assets/Addon_audio.ini");
QDir addon_audio_new("Assets/Sounds/Addon_audio");
QDir addon_audio_old("Assets/Sounds/NEW");
if (addon_audio_new.exists() == true || addon_audio_old.exists() == true)
{
qDebug() << "More music detected";
if(addon_audio_file.exists() == true)
{
QSettings addon_audio_settings("Assets/Addon_audio.ini", QSettings::IniFormat);
QString audio_version = addon_audio_settings.value("Main/Version").toString();
setCheckerParam("ADDON_MOREMUSIC/Version",audio_version);
return audio_version;
}
else
{
setCheckerParam("ADDON_MOREMUSIC/Version","Unknown");
return "Unknown";
}
}
else
{
qDebug() << "More music not detected";
setCheckerParam("ADDON_MOREMUSIC/Version","Not installed");
return "Not installed";
}
}
QString check_addon_more_handicaps()
{
QFile addon_handicaps_file("Assets/Addon_handicap.ini");
if(addon_handicaps_file.exists() == true)
{
qDebug() << "More handicaps detected";
QSettings addon_handicaps_settings("Assets/Addon_handicap.ini", QSettings::IniFormat);
QString handicaps_version = addon_handicaps_settings.value("Main/Version").toString();
setCheckerParam("ADDON_MOREHANDICAPS/Version",handicaps_version);
return handicaps_version;
}
else
{
qDebug() << "More handicaps not detected";
setCheckerParam("ADDON_MOREHANDICAPS/Version","Not installed");
return "Not installed";
}
}*/
bool readOptionFormations()
{
// Open the file
QDomDocument read;
QFile file("Assets/Modules/Formations/MLF_CIV4ModularLoadingControls.xml");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Error opening file";
return 0;
}
read.setContent(&file);
file.close();
// Go to color level
QDomElement value_el = read.firstChildElement("Civ4ModularLoadControls").firstChildElement("ConfigurationInfos").firstChildElement("ConfigurationInfo").firstChildElement("Modules").firstChildElement("Module").toElement();
QString bLoad = value_el.firstChildElement("bLoad").firstChild().nodeValue();
if (bLoad.toInt() == 1) {
return true;
}
return false;
}
bool setOptionFormations(bool value)
{
// Open the file
QDomDocument read;
QFile file("Assets/Modules/Formations/MLF_CIV4ModularLoadingControls.xml");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Error opening file";
return 0;
}
read.setContent(&file);
file.close();
// Go to color level
QDomElement value_el = read.firstChildElement("Civ4ModularLoadControls").firstChildElement("ConfigurationInfos").firstChildElement("ConfigurationInfo").firstChildElement("Modules").firstChildElement("Module").toElement();
// Set values
if (value)
{
value_el.firstChildElement("bLoad").firstChild().setNodeValue("1");
}
else
{
value_el.firstChildElement("bLoad").firstChild().setNodeValue("0");
}
// Save content back to the file
file.open(QIODevice::Truncate | QIODevice::WriteOnly);
file.write(read.toByteArray());
file.close();
return 0;
}
bool clearCache()
{
// Getting the cache path
QString cacheDir;
QString finalDir = "\\My Games\\Beyond the Sword\\cache\\";
char* Appdata = getenv("LOCALAPPDATA");
cacheDir = Appdata + finalDir;
qDebug() << "Cache found at " << cacheDir;
QDir cache(cacheDir);
cache.removeRecursively();
return 0;
}
bool clearGameOptions()
{
setConfigParam("GameOptions","");
qDebug() << "Cleared parameters";
return 0;
}