-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration_primitives.hpp
279 lines (245 loc) · 9.81 KB
/
configuration_primitives.hpp
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
#ifndef configuration_primitives_hpp
#define configuration_primitives_hpp
namespace dfterm {
class SlotProfile;
class User;
class UserGroup;
};
#include "types.hpp"
#include <unicode/unistr.h>
#include "id.hpp"
#include "hash.hpp"
#include <set>
namespace dfterm
{
class User
{
private:
UnicodeString name;
data1D password_hash_sha512;
data1D password_salt;
bool active;
bool admin;
ID id;
public:
User() { active = true; admin = false; };
User(UnicodeString us) { name = us; active = true; admin = false; };
User(const std::string &us) { name = UnicodeString::fromUTF8(us); };
data1D getPasswordHash() const { return password_hash_sha512; };
void setPasswordHash(data1D hash) { password_hash_sha512 = hash; };
data1D getPasswordSalt() const { return password_salt; };
void setPasswordSalt(data1D salt) { password_salt = salt; };
ID getID() const { return id; };
const ID& getIDRef() const { return id; };
void setID(const ID& id) { this->id = id; };
/* This one hashes the password and then calls setPasswordHash. */
/* UnicodeString will be first converted to UTF-8 */
/* If you use salt, set it before calling this function. */
void setPassword(const UnicodeString &password)
{
setPassword(TO_UTF8(password));
}
void setPassword(const data1D &password)
{
setPasswordHash(hash_data(password + password_salt));
}
/* Returns true if password is correct. */
bool verifyPassword(const UnicodeString &password)
{
return verifyPassword(TO_UTF8(password));
}
bool verifyPassword(const data1D &password)
{
return (hash_data(password + password_salt) == password_hash_sha512);
}
UnicodeString getName() const { return name; };
std::string getNameUTF8() const
{
return TO_UTF8(name);
}
void setName(const UnicodeString &us) { name = us; };
void setNameUTF8(const std::string &us)
{
setName(UnicodeString::fromUTF8(us));
}
bool isActive() const { return active; };
void kill() { active = false; };
bool isAdmin() const { return admin; };
void setAdmin(bool admin_status) { admin = admin_status; };
};
class UserGroup
{
private:
bool has_nobody;
bool has_anybody;
bool has_launcher;
std::set<ID> has_user;
public:
UserGroup()
{
has_nobody = true;
has_anybody = false;
has_launcher = false;
}
data1D serialize() const;
static UserGroup unSerialize(data1D data);
bool hasAnySpecificUser() const { return ((!has_user.empty()) || has_anybody); };
bool hasNobody() const { return has_nobody; };
bool hasAnybody() const { return has_anybody; };
bool hasLauncher() const { return has_launcher; };
bool hasUser(const ID &user_id) const
{
if (has_nobody) return false;
if (has_anybody) return true;
return has_user.find(user_id) != has_user.end();
};
bool toggleAnybody()
{
if (!has_anybody)
setAnybody();
else
setNobody();
return hasAnybody();
}
bool toggleLauncher()
{
if (has_launcher) unsetLauncher();
else setLauncher();
return hasLauncher();
}
bool toggleUser(const ID &user_id)
{
std::set<ID>::iterator i1 = has_user.find(user_id);
if (i1 != has_user.end())
{
has_user.erase(i1);
if (has_user.empty() && !has_anybody && !has_nobody) setNobody();
return false;
}
else
{
has_user.insert(user_id);
if (has_nobody) has_nobody = false;
return true;
}
}
void setNobody()
{
has_nobody = true;
has_anybody = false;
has_launcher = false;
has_user.clear();
}
void setAnybody()
{
has_nobody = false;
has_anybody = true;
has_launcher = true;
has_user.clear();
}
void setLauncher()
{
has_launcher = true;
has_nobody = false;
}
void unsetLauncher()
{
if (has_anybody)
has_launcher = true;
else
{
has_launcher = false;
if (has_user.empty())
has_nobody = true;
}
}
void setUser(const ID &user_id)
{
has_user.insert(user_id);
}
void unsetUser(const ID &user_id)
{
has_user.erase(user_id);
}
};
class SlotProfile
{
private:
UnicodeString name; /* name of the slot profile */
trankesbel::ui32 w, h; /* width and height of the game inside slot */
UnicodeString path; /* path to game executable */
UnicodeString working_path; /* working directory for the game. */
trankesbel::ui32 slot_type; /* slot type (should be type SlotType, is ui32 for header dependency reasons) */
UserGroup allowed_watchers; /* who may watch */
UserGroup allowed_launchers; /* who may launch */
UserGroup allowed_players; /* who may play */
UserGroup allowed_closers; /* who may force close the game */
UserGroup forbidden_watchers; /* who may not watch */
UserGroup forbidden_launchers; /* who may not launch */
UserGroup forbidden_players; /* who may not play */
UserGroup forbidden_closers; /* who may not force close the game */
trankesbel::ui32 max_slots; /* maximum number of slots to create */
ID id; /* Identify the slot profile with this. */
public:
SlotProfile()
{
w = 80; h = 25;
slot_type = 0;
max_slots = 1;
allowed_watchers.setAnybody();
allowed_launchers.setAnybody();
allowed_players.setAnybody();
allowed_closers.setAnybody();
forbidden_players.setNobody();
forbidden_launchers.setNobody();
forbidden_watchers.setNobody();
forbidden_closers.setNobody();
}
/* All these functions are just simple getter/setter pairs */
void setName(const UnicodeString &name) { this->name = name; };
void setNameUTF8(const std::string &name) { this->name = UnicodeString::fromUTF8(name); };
UnicodeString getName() const { return name; };
std::string getNameUTF8() const { return TO_UTF8(name); };
ID getID() const { return id; };
const ID& getIDRef() const { return id; };
void setID(const ID &id) { this->id = id; };
void setWidth(trankesbel::ui32 w) { this->w = w; };
trankesbel::ui32 getWidth() const { return w; };
void setHeight(trankesbel::ui32 h) { this->h = h; };
trankesbel::ui32 getHeight() const { return h; };
void setSize(trankesbel::ui32 w, trankesbel::ui32 h)
{ setWidth(w); setHeight(h); };
void getSize(trankesbel::ui32* w, trankesbel::ui32* h)
{ (*w) = getWidth(); (*h) = getHeight(); };
void setExecutable(const UnicodeString &executable) { path = executable; };
void setExecutableUTF8(const std::string &executable) { path = UnicodeString::fromUTF8(executable); };
UnicodeString getExecutable() const { return path; };
std::string getExecutableUTF8() const { return TO_UTF8(path); };
void setWorkingPath(const UnicodeString &executable_path) { working_path = executable_path; };
void setWorkingPathUTF8(const std::string &executable_path) { working_path = UnicodeString::fromUTF8(executable_path); };
UnicodeString getWorkingPath() const { return working_path; };
std::string getWorkingPathUTF8() const { return TO_UTF8(working_path); };
/* Use like this: setSlotType((ui32) t); where t is of SlotType enum. */
void setSlotType(trankesbel::ui32 t) { slot_type = t; };
trankesbel::ui32 getSlotType() const { return slot_type; };
void setMaxSlots(trankesbel::ui32 slots) { max_slots = slots; };
trankesbel::ui32 getMaxSlots() const { return max_slots; };
UserGroup getAllowedWatchers() const { return allowed_watchers; };
UserGroup getAllowedLaunchers() const { return allowed_launchers; };
UserGroup getAllowedPlayers() const { return allowed_players; };
UserGroup getAllowedClosers() const { return allowed_closers; };
UserGroup getForbiddenWatchers() const { return forbidden_watchers; };
UserGroup getForbiddenLaunchers() const { return forbidden_launchers; };
UserGroup getForbiddenPlayers() const { return forbidden_players; };
UserGroup getForbiddenClosers() const { return forbidden_closers; };
void setAllowedWatchers(UserGroup gr) { allowed_watchers = gr; };
void setAllowedLaunchers(UserGroup gr) { allowed_launchers = gr; };
void setAllowedPlayers(UserGroup gr) { allowed_players = gr; };
void setAllowedClosers(UserGroup gr) { allowed_closers = gr; };
void setForbiddenWatchers(UserGroup gr) { forbidden_watchers = gr; };
void setForbiddenLaunchers(UserGroup gr) { forbidden_launchers = gr; };
void setForbiddenPlayers(UserGroup gr) { forbidden_players = gr; };
void setForbiddenClosers(UserGroup gr) { forbidden_closers = gr; };
};
};
#endif