-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitScript.lua
304 lines (276 loc) · 7.77 KB
/
initScript.lua
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
------------------------------------------------------------------
-- 1) cond function
------------------------------------------------------------------
function cond(cond, trueVal, falseVal)
local res = {result = '', skip = false} -- Initialize result table with defaults
if cond == nil then
error('cond: condition cannot be nil')
return res
end
if cond and trueVal == nil then
error('cond: trueVal cannot be nil')
return res
end
if not cond and falseVal == nil then
res.skip = true -- Skip ONLY if falseVal is not provided
end
if type(trueVal) == 'function' then
trueVal = trueVal()
end
if type(falseVal) == 'function' then
falseVal = falseVal()
end
if cond then
if type(trueVal) == 'table' then
res.result = trueVal.result
res.skip = trueVal.skip
else
res.result = trueVal
res.skip = false
end
else
if not res.skip then
if type(falseVal) == 'table' then
res.result = falseVal.result
res.skip = falseVal.skip
else
res.result = falseVal
end
end
end
resultTable = res
return res
end
------------------------------------------------------------------
-- 2) set function
------------------------------------------------------------------
function set(strOrCalc)
local res = {result = '', skip = false}
if strOrCalc == nil then
error('set: cannot be nil')
return
end
if type(strOrCalc) == 'string' then
res.result = strOrCalc
elseif type(strOrCalc) == 'number' then
res.result = tostring(strOrCalc)
else
error('set: Expected string or number')
return
end
resultTable = res
return res
end
------------------------------------------------------------------
-- 3) fmtN function
------------------------------------------------------------------
function fmtN(num, maxDecimals, fixedDecimals)
if num == nil then
error('fmtN: num cannot be nil')
return
elseif type(num) ~= 'number' then
error('fmtN: Invalid type for num. Expected a number')
return
end
if maxDecimals == nil then
error('fmtN: maxDecimals cannot be nil')
return
elseif type(maxDecimals) ~= 'number' then
error('fmtN: Invalid type for maxDecimals. Expected a number')
return
end
if fixedDecimals == nil then
error('fmtN: fixedDecimals cannot be nil')
return
elseif type(fixedDecimals) ~= 'boolean' then
error('fmtN: Invalid type for fixedDecimals. Expected a boolean')
return
end
local multiplier = 10 ^ maxDecimals
local rounded = math.floor(num * multiplier + 0.5) / multiplier
local output = ''
if fixedDecimals then
output = string.format('%.' .. maxDecimals .. 'f', rounded)
else
local intPart, fracPart = math.modf(rounded)
if fracPart == 0 then
output = tostring(intPart)
else
output = tostring(rounded)
end
end
return output
end
------------------------------------------------------------------
-- 4) vars function (and init alias)
------------------------------------------------------------------
function vars(args)
for name, value in pairs(args) do
-- Set the global variable only if it does not already exist
if _G[name] == nil then
if type(name) ~= 'string' then
error('vars: Variable name must be a string')
end
if not string.match(name, '^[A-Za-z_][A-Za-z0-9_]*$') then
error('vars: Invalid variable name')
end
if value == nil then
error('vars: Value missing')
end
-- If REGEX is true and value is a string, escape backslashes
if type(value) == 'string' and REGEX then
value = value:gsub('\\\\', '\\\\\\\\')
end
_G[name] = value
end
end
-- Forward or initialize resultTable
local res = {result = '', skip = true}
if resultTable == nil then
resultTable = res
else
if resultTable.result == nil then
resultTable.result = res.result
end
if resultTable.skip == nil then
resultTable.skip = res.skip
end
end
return resultTable
end
init = vars -- 'init' alias for compatibility
------------------------------------------------------------------
-- 5) safeLoadFileSandbox and lkp
------------------------------------------------------------------
hashTables = {}
function safeLoadFileSandbox(path)
-- Minimal environment
local safeEnv = {
pairs = pairs,
ipairs = ipairs,
type = type,
tonumber = tonumber,
tostring = tostring,
table = table,
math = math,
string = string,
}
setmetatable(safeEnv, {
__index = function(_, k)
-- Block access to critical globals
if k == '_G' or k == 'os' or k == 'io' or k == 'dofile' or k == 'require' then
return nil
end
return _G[k]
end
})
local chunk, err = loadfile(path, 't', safeEnv)
if not chunk then
return false, err
end
return pcall(chunk)
end
function lkp(key, hpath, inner)
local res = { result = '', skip = false }
if type(key) == 'number' then
key = tostring(key)
end
if key == nil then
error('lkp: key passed to file is nil in ' .. tostring(hpath))
end
if hpath == nil or hpath == '' then
error('lkp: file path is invalid or empty')
end
if inner == nil then
inner = false
end
if hashTables[hpath] == nil then
local success, dataEntries = safeLoadFileSandbox(hpath)
if not success then
error('lkp: failed to safely load file at ' .. tostring(hpath) .. ': ' .. tostring(dataEntries))
end
if type(dataEntries) ~= 'table' then
error('lkp: invalid format in file at ' .. tostring(hpath))
end
local tbl = {}
for _, entry in ipairs(dataEntries) do
local keys = entry[1]
local value = entry[2]
if value == nil then
goto continue
end
if type(keys) == 'table' then
for _, k in ipairs(keys) do
if type(k) == 'number' then
k = tostring(k)
end
tbl[k] = value
end
elseif type(keys) == 'string' or type(keys) == 'number' then
if type(keys) == 'number' then
keys = tostring(keys)
end
tbl[keys] = value
else
goto continue
end
::continue::
end
hashTables[hpath] = tbl
end
local val = hashTables[hpath][key]
if val == nil then
if inner then
res.result = nil
else
res.result = key
end
else
res.result = val
end
resultTable = res
return res
end
------------------------------------------------------------------
-- 6) lvars function
------------------------------------------------------------------
function lvars(filePath)
local res = {result = '', skip = true}
if filePath == nil or filePath == '' then
error('lvars: file path is invalid or empty')
return res
end
local success, dataTable = safeLoadFileSandbox(filePath)
if not success then
error('lvars: failed to safely load file at ' .. tostring(filePath) .. ': ' .. tostring(dataTable))
end
if type(dataTable) ~= 'table' then
error('lvars: invalid data format in file at ' .. tostring(filePath))
end
for name, value in pairs(dataTable) do
if type(name) ~= 'string' then
error('lvars: Variable name must be a string. Found invalid key \"' .. tostring(name) .. '\"')
end
if not string.match(name, '^[A-Za-z_][A-Za-z0-9_]*$') then
error('lvars: Invalid variable name \"' .. tostring(name) .. '\"')
end
if value == nil then
error('lvars: Value missing for variable \"' .. tostring(name) .. '\"')
end
if REGEX and type(value) == 'string' then
value = value:gsub('\\\\', '\\\\\\\\')
end
_G[name] = value
end
if resultTable == nil then
resultTable = res
else
if resultTable.result == nil then
resultTable.result = res.result
end
if resultTable.skip == nil then
resultTable.skip = res.skip
end
end
return resultTable
end