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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
#pragma once
#include <QString>
#include <QStringList>
#include <QList>
#include <QMap>
#include <exception>
using CRSMConfigException = std::logic_error;
namespace CRSMConfigValueType {
enum Type {
None,
String,
Integer,
Boolean,
List,
Map
};
}
class CRSMConfigValueBase {
public:
virtual void setValue(const QString&) { }
virtual QString value() { return ""; }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::None; }
template<typename Type>
static Type getValue(const QString& value);
template<typename Type>
static QString getStringValue(Type val);
};
template<typename Type>
class CRSMConfigValue : public CRSMConfigValueBase {
public:
// CRSMConfigValue(Type&) { }
virtual void setValue(const QString&) { throw CRSMConfigException("Cannot assign"); }
virtual QString value() { throw CRSMConfigException("Cannot get the value"); }
};
using String = QString;
using Integer = int;
using Boolean = bool;
#define List(Type) QList<Type>
#define Map(KeyType, ValueType) QMap<KeyType, ValueType>
template<>
class CRSMConfigValue<String> : public CRSMConfigValueBase {
String& config;
public:
CRSMConfigValue(String& config) : config(config) { }
virtual void setValue(const QString& value) { config = value; }
virtual QString value() { return config; }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::String; }
};
template<>
class CRSMConfigValue<Integer> : public CRSMConfigValueBase {
Integer& config;
public:
CRSMConfigValue(Integer& config) : config(config) { }
virtual void setValue(const QString& value)
{
bool ok = true;
Integer val = value.toInt(&ok, 0);
if(!ok) throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as an Integer");
config = val;
}
virtual QString value() { return QString::number(config); }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Integer; }
};
template<>
class CRSMConfigValue<Boolean> : public CRSMConfigValueBase {
Boolean& config;
public:
CRSMConfigValue(Boolean& config) : config(config) { }
virtual void setValue(const QString& value)
{
if(value == "true")
{
config = true;
}
else if(value == "false")
{
config = false;
}
else
{
throw CRSMConfigException("\"" + value.toStdString() + "\" could not be parsed as a Boolean");
}
}
virtual QString value() { return config ? "true" : "false"; }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Boolean; }
};
class CRSMConfigValueList : public CRSMConfigValueBase {
public:
virtual void append(const QString& entry) = 0;
virtual QStringList getValues() = 0;
};
class CRSMConfigValueMap : public CRSMConfigValueBase {
public:
virtual void setKeyValue(const QString& key, const QString& value) = 0;
virtual QMap<QString, QString> getValues() = 0;
};
template<typename Type>
class CRSMConfigValue<QList<Type>> : public CRSMConfigValueList {
using ListType = QList<Type>;
ListType& config;
public:
CRSMConfigValue(ListType& config) : config(config) {}
virtual ListType& list() { return config; }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::List; }
virtual void append(const QString &entry)
{
Type val = CRSMConfigValueBase::getValue<Type>(entry);
if(!config.contains(val))
{
config.append(val);
}
}
virtual QStringList getValues()
{
QStringList ret;
foreach(Type val, config)
{
ret.append(CRSMConfigValueBase::getStringValue(val));
}
return ret;
}
};
template<typename KeyType, typename ValueType>
class CRSMConfigValue<QMap<KeyType, ValueType>> : public CRSMConfigValueMap {
using MapType = QMap<KeyType, ValueType>;
MapType& config;
public:
CRSMConfigValue(MapType& config) : config(config) {}
virtual MapType& map() { return config; }
virtual CRSMConfigValueType::Type type() { return CRSMConfigValueType::Type::Map; }
virtual void setKeyValue(const QString& key, const QString& value)
{
config[CRSMConfigValueBase::getValue<KeyType>(key)] = CRSMConfigValueBase::getValue<ValueType>(value);
}
virtual QMap<QString, QString> getValues()
{
QMap<QString, QString> ret;
foreach(KeyType key, config.keys())
{
ret[CRSMConfigValueBase::getStringValue(key)] = CRSMConfigValueBase::getStringValue(config[key]);
}
return ret;
}
};
template<typename Type>
CRSMConfigValue<Type>* mkConfigValue(Type& Value)
{
return new CRSMConfigValue<Type>(Value);
}
template<typename Type>
Type CRSMConfigValueBase::getValue(const QString& value)
{
Type ret;
CRSMConfigValue<Type> val(ret);
val.setValue(value);
return ret;
}
template<typename Type>
QString CRSMConfigValueBase::getStringValue(Type value)
{
CRSMConfigValue<Type> val(value);
return val.value();
}
class CRSMConfig
{
public:
struct {
Integer ManagementPort = 9372;
String ListFolder = "ScenarioLists";
} CRSM;
struct {
struct {
String Arguments = "/fullscreen /lobby:300 /nosignup Objects.c4d";
String Config = "config";
String Executable = "clonk-server";
List(String) IgnoreFolders = {"Network", "Records.c4f", "Savegames.c4f"};
Integer EmptyTimer = 60;
} Server;
struct {
struct {
Integer Count = 5;
Integer Time = 3;
} AntiFlood;
Map(Integer, String) Moderators;
Integer RegainAdminTime = 120;
} Chat;
} Clonk;
struct {
Boolean Auto = true;
Boolean RandomizeAuto = true;
Integer MaxWishesPerScen = 2;
Integer MaxWishesPerUser = 2;
Integer UserListLength = 5;
Map(String, String) Alias;
} Hosting;
struct {
Boolean Use = false;
String Server = "irc.euirc.net";
String Nick = "CRSM";
String Password = "";
String RealName = "Dedicated Clonk server powered by CRSM";
String Channel = "#crsm";
Boolean UseIngameChat = false;
String IngameChannel = "#crsm-ingame";
String QuitMessage = "Dedicated Clonk server powered by CRSM";
Integer ReconnectDelay = 10;
String ScenListMessage = "A list of available scenarios is available ingame or in a lobby.";
Map(String, String) Moderators;
} IRC;
struct {
struct {
String ReattachId = "";
} ProcessManager;
struct {
struct {
String Directory;
String ServerNick;
String ServerPCName;
} Clonk;
} Volatile;
struct {
Map(String, String) AliasWishes;
} Hosting;
} Auto;
struct {
Boolean ServerUses = false;
Boolean PromptEnabled = false;
Integer RereadLimit = 50;
} Readline;
explicit CRSMConfig();
void setConfigValue(const QString& name, const QString& value);
void addConfigListEntry(const QString& name, const QString& value);
void setConfigMapValue(const QString& name, const QString& key, const QString& value);
QString read(const QString& fileName);
bool write(const QString& fileName);
void clear();
protected:
void addConfigValue(QString name, CRSMConfigValueBase *value);
CRSMConfigValueBase& getConfigValue(const QString &name);
private:
#define ConfigValue(Value) {#Value, mkConfigValue(Value)}
QMap<QString, CRSMConfigValueBase*> configValues {
ConfigValue(CRSM.ManagementPort),
ConfigValue(CRSM.ListFolder),
ConfigValue(Clonk.Server.Arguments),
ConfigValue(Clonk.Server.Config),
ConfigValue(Clonk.Server.Executable),
ConfigValue(Clonk.Server.EmptyTimer),
ConfigValue(Clonk.Server.IgnoreFolders),
ConfigValue(Clonk.Chat.AntiFlood.Count),
ConfigValue(Clonk.Chat.AntiFlood.Time),
ConfigValue(Clonk.Chat.Moderators),
ConfigValue(Clonk.Chat.RegainAdminTime),
ConfigValue(Hosting.Auto),
ConfigValue(Hosting.RandomizeAuto),
ConfigValue(Hosting.MaxWishesPerScen),
ConfigValue(Hosting.MaxWishesPerUser),
ConfigValue(Hosting.UserListLength),
ConfigValue(Hosting.Alias),
ConfigValue(IRC.Use),
ConfigValue(IRC.Server),
ConfigValue(IRC.Nick),
ConfigValue(IRC.Password),
ConfigValue(IRC.RealName),
ConfigValue(IRC.Channel),
ConfigValue(IRC.UseIngameChat),
ConfigValue(IRC.IngameChannel),
ConfigValue(IRC.QuitMessage),
ConfigValue(IRC.ReconnectDelay),
ConfigValue(IRC.ScenListMessage),
ConfigValue(IRC.Moderators),
ConfigValue(Auto.ProcessManager.ReattachId),
ConfigValue(Auto.Hosting.AliasWishes),
ConfigValue(Readline.ServerUses),
ConfigValue(Readline.PromptEnabled),
ConfigValue(Readline.RereadLimit)
};
#undef ConfigValue
};
#undef String
#undef Integer
#undef Boolean
#undef List
#undef Map
|