-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_system.nut
More file actions
307 lines (273 loc) · 5.85 KB
/
config_system.nut
File metadata and controls
307 lines (273 loc) · 5.85 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
299
300
301
302
303
304
305
306
307
local _ctx = this;
class Property
{
_name = null;
_type = null;
_getter = null;
_setter = null;
constructor(name)
{
_name = name;
}
function GetName()
{
return _name;
}
function GetType()
{
return _type;
}
function SetType(type)
{
_type = type;
return this;
}
function GetGetter()
{
return _getter;
}
function SetGetter(getter)
{
_getter = getter;
return this;
}
function GetSetter()
{
return _setter;
}
function SetSetter(setter)
{
_setter = setter;
return this;
}
function RawGetValue(scope)
{
return scope[_name];
}
function RawSetValue(val, scope)
{
scope[_name] = val;
}
function GetValue(scope)
{
if (_getter == null)
{
return RawGetValue(scope);
}
return _getter(this, scope);
}
function SetValue(val, scope, asDefault = false)
{
val = ConvertType(val);
if (_setter == null)
{
RawSetValue(val, scope);
return;
}
_setter(val, this, scope);
if (!asDefault)
{
scope._changedProperties[_name] <- true;
}
}
//may throw a exception
function ConvertType(val)
{
local valType = typeof val;
if (_type == "string")
{
return val.tostring();
}
if (_type == "bool")
{
if (valType == "string")
{
if (val == "true")
{
return true;
}
if (val == "false")
{
return false;
}
try
{
local num = val.tointeger();
if (num == 0)
{
return false;
}
else
{
return true;
}
}
catch (exception)
{
}
}
if (val)
{
return true;
}
else
{
return false;
}
}
if (_type == "integer")
{
return val.tointeger();
}
if (_type == "float")
{
return val.tofloat();
}
throw format("Unsupported type: %s", _type);
}
}
//table(string name -> Property propertyInstance)
_properties <- {};
_configScope <- null;
_setterExpEnv <- {
range = function(min = null, max = null) {
return NewRangePropertySetter(min, max);
}.bindenv(_ctx)
};
/**
* infoTable:
* {
* string name;
* string type; (optional)
* function getter(Property property, table scope); (optional)
* function setter(any val, Property property, table scope); (optional)
* }
*/
function DefineProperty(infoTable)
{
local name = infoTable.name;
local property = Property(name);
if ("type" in infoTable)
{
property.SetType(infoTable.type);
}
if ("getter" in infoTable)
{
property.SetGetter(infoTable.getter);
}
if ("setter" in infoTable)
{
local setter = infoTable.setter;
local setterType = typeof setter;
if (setterType == "string")
{
try
{
local func = compilestring("return " + setter).bindenv(_setterExpEnv);
setter = func();
}
catch (exception)
{
setter = null;
}
}
if (setter != null && typeof setter == "function")
{
property.SetSetter(setter);
}
}
_properties[name] <- property;
_configScope[name] <- null;
}
function LoadConfigFromTable(table, asDefault = false)
{
if (asDefault)
{
_configScope._changedProperties.clear();
}
foreach (name, val in table)
{
if (name in _properties)
{
SetCfgProp(name, val, asDefault);
}
}
}
function SaveConfigToTable(table)
{
local changedProps = _configScope._changedProperties;
foreach (name, val in changedProps)
{
table[name] <- _configScope[name];
}
}
function SaveConfigToFile(filePath)
{
local table = {};
SaveConfigToTable(table);
::KtScript.TableToFile(table, filePath);
}
function GetCfgProp(name)
{
if (!(name in _properties))
{
throw format("The config \"%s\" doesn't exist!", name);
}
local property = _properties[name];
return property.GetValue(_configScope);
}
function SetCfgProp(name, val, asDefault = false)
{
if (!(name in _properties))
{
throw format("The property \"%s\" doesn't exist!", name);
}
local property = _properties[name];
property.SetValue(val, _configScope, asDefault);
}
function ContainsCfgProp(name)
{
return name in _properties;
}
function Init(configScope)
{
_configScope = configScope;
//string propName -> true
_configScope._changedProperties <- {};
}
function GetPublicMemberNames()
{
return [
"DefineProperty",
"LoadConfigFromTable",
"SaveConfigToFile",
"GetCfgProp",
"SetCfgProp",
"ContainsCfgProp"
]
}
function NewRangePropertySetter(min = null, max = null)
{
return function(val, property, scope) {
if (min != null)
{
if (val < min)
{
val = min;
}
}
if (max != null)
{
if (val > max)
{
val = max;
}
}
property.RawSetValue(val, scope);
}
}
function PrintError(str)
{
printl(str);
//Say(null, str, false);
}