/
config.cpp
176 lines (149 loc) · 4.64 KB
/
config.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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2009-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "de/Config"
#include "de/App"
#include "de/Archive"
#include "de/Log"
#include "de/Folder"
#include "de/ArrayValue"
#include "de/NumberValue"
#include "de/Reader"
#include "de/Writer"
#include "de/Version"
namespace de {
struct Config::Instance
{
/// Configuration file name.
Path configPath;
/// Path where the configuration is written (inside persist.pack).
Path writtenConfigPath;
/// The configuration namespace.
Process config;
Instance(Path const &path) : configPath(path)
{
writtenConfigPath = Path("modules/Config");
}
};
Config::Config(Path const &path) : d(new Instance(path))
{}
Config::~Config()
{
LOG_AS("~Config");
try
{
write();
}
catch(Error const &err)
{
LOG_ERROR("") << err.asText();
}
delete d;
}
void Config::read()
{
LOG_AS("Config::read");
// Current version.
Version verInfo;
QScopedPointer<ArrayValue> version(new ArrayValue());
version->add(new NumberValue(verInfo.major));
version->add(new NumberValue(verInfo.minor));
version->add(new NumberValue(verInfo.patch));
version->add(new NumberValue(verInfo.build));
File &scriptFile = App::rootFolder().locate<File>(d->configPath);
bool shouldRunScript = false;
try
{
// If we already have a saved copy of the config, read it.
Archive const &persist = App::persistentData();
Reader(persist.entryBlock(d->writtenConfigPath)) >> names();
LOG_DEBUG("Found serialized Config:\n") << names();
// If the saved config is from a different version, rerun the script.
Value const &oldVersion = names()["__version__"].value();
if(oldVersion.compare(*version))
{
// Version mismatch: store the old version in a separate variable.
d->config.globals().add(new Variable("__oldversion__", oldVersion.duplicate(),
Variable::AllowArray | Variable::ReadOnly));
shouldRunScript = true;
}
else
{
// Versions match.
LOG_MSG("") << d->writtenConfigPath << " matches version " << version->asText();
}
// Also check the timestamp of written config vs. they config script.
// If script is newer, it should be rerun.
if(scriptFile.status().modifiedAt > persist.entryStatus(d->writtenConfigPath).modifiedAt)
{
LOG_MSG("%s is newer than %s, rerunning the script.")
<< d->configPath << d->writtenConfigPath;
shouldRunScript = true;
}
}
catch(Archive::NotFoundError const &)
{
// It is missing if the config hasn't been written yet.
shouldRunScript = true;
}
catch(Error const &error)
{
LOG_WARNING(error.what());
// Something is wrong, maybe rerunning will fix it.
shouldRunScript = true;
}
// The version of libdeng2 is automatically included in the namespace.
d->config.globals().add(new Variable("__version__", version.take(),
Variable::AllowArray | Variable::ReadOnly));
if(shouldRunScript)
{
// Read the main configuration.
Script script(scriptFile);
d->config.run(script);
d->config.execute();
}
}
void Config::write()
{
Writer(App::persistentData().entryBlock(d->writtenConfigPath)) << names();
}
Record &Config::names()
{
return d->config.globals();
}
Value &Config::get(String const &name)
{
return d->config.globals()[name].value();
}
dint Config::geti(String const &name)
{
return dint(get(name).asNumber());
}
duint Config::getui(String const &name)
{
return duint(get(name).asNumber());
}
ddouble Config::getd(String const &name)
{
return get(name).asNumber();
}
String Config::gets(String const &name)
{
return get(name).asText();
}
} // namespace de