-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.lua
437 lines (387 loc) · 9.94 KB
/
gen.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
json=require('json')
function makestack()
local t = {}
t._et = {}
function t:push(v)
table.insert(self._et, v)
return self
end
function t:last()
return self._et[#self._et]
end
function t:pop()
if #self._et ~= 0 then
local v=self._et[#self._et]
table.remove(self._et)
return v
end
end
return t
end
-- http://lua-users.org/lists/lua-l/2010-06/msg00313.html
setfenv = setfenv or function(f, t)
f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func)
local name
local up = 0
repeat
up = up + 1
name = debug.getupvalue(f, up)
until name == '_ENV' or name == nil
if name then
debug.upvaluejoin(f, up, function() return name end, 1) -- use unique upvalue
debug.setupvalue(f, up, t)
end
end
local modules={}
function newrequire(requiredname)
local required=requiredname
if required:match('/') then
if not required:match('%.lua$') then
required=required .. '.lua'
end
else
required=required:gsub('%.','/') .. '.lua'
end
print('requiring',modroots['core'] .. '/lualib/' .. required)
if required:match('^__.-__/') then
local modn,fpath=required:match('^__(.-)__/(.*)')
modname:push(modn)
filepath:push(fpath)
else
if filepath:last() then
local filedir=filepath:last():match("(.-)([^/]-[^%.]+)$")
if io.open(modroots[modname:last()] .. '/' .. filedir .. '/' .. required, 'r') then
modname:push(modname:last())
filepath:push(filedir .. '/' .. required)
elseif io.open(modroots[modname:last()] .. '/' .. required, 'r') then
modname:push(modname:last())
filepath:push(required)
elseif io.open(modroots['core'] .. '/lualib/' .. required, 'r') then
modname:push('core')
filepath:push('lualib/' .. required)
else
print(modname:last(),filepath:last())
error('no module named ' .. required)
end
--elseif modname:last() and io.open(modroots[mod]name:last() .. '/' .. required, 'r') then
-- modname:push(modname:last())
-- filepath:push(required)
elseif io.open(modroots['core'] .. '/lualib/' .. required, 'r') then
modname:push('core')
filepath:push('lualib/' .. required)
else
print(modname:last(),filepath:last())
error('no module named ' .. required)
end
end
local result
local path=modroots[modname:last()] .. '/' .. filepath:last()
local mkey=path .. '@@' .. modname:last()
if false and modules[mkey] then
print('using cached',path)
result=modules[mkey]
else
print('requiring',path)
local f,err=loadfile(path)
if not f then print(err) end
setfenv(f,newenv)
result=f()
modules[mkey]=result
end
modname:pop()
filepath:pop()
return result
end
local f=io.open('modroots.json','r')
modroots=json.decode(f:read('*a'))
f:close()
modname=makestack()
filepath=makestack()
function table_size(t)
local count=0
for _,_ in pairs(t) do
count=count+1
end
return count
end
newenv={
math=math,
string=string,
table=table,
debug=debug,
_VERSION=_VERSION,
assert=assert,
error=error,
ipairs=ipairs,
next=next,
pairs=pairs,
pcall=pcall,
select=select,
tonumber=tonumber,
tostring=tostring,
type=type,
unpack=unpack,
xpcall=xpcall,
setmetatable=setmetatable,
getmetatable=getmetatable,
rawset=rawset,
rawget=rawget,
rawequal=rawequal,
require=newrequire,
log=print,
serpent=require('serpent'),
table_size=table_size,
}
local fdata=require('fdata')
newenv.defines=fdata.defines
newenv._G=newenv
local modinfo={}
for mod,modroot in pairs(modroots) do
print('finding info at',modroot .. '/info.json')
local f=io.open(modroot .. '/info.json','r')
local data=f:read('*a')
f:close()
local info=json.decode(data)
modinfo[mod]=info
end
-- setup mods table
local mods={}
for mod,info in pairs(modinfo) do
mods[mod]=info.version
end
newenv.mods=mods
-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
function natsort(o)
local function conv(s)
local res, dot = "", ""
for n, m, c in tostring(s):gmatch"(0*(%d*))(.?)" do
if n == "" then
dot, c = "", dot..c
else
res = res..(dot == "" and ("%03d%s"):format(#m, m)
or "."..n)
dot, c = c:match"(%.?)(.*)"
end
res = res..c:gsub(".", "\0%0")
end
return res
end
table.sort(o,
function (a, b)
local ca, cb = conv(a), conv(b)
return ca < cb or ca == cb and a < b
end)
return o
end
-- https://lua-users.org/wiki/StringTrim
function trim(s)
return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
end
function getdepname(dep)
local parts={}
for s in trim(dep):gmatch("%S+") do
table.insert(parts,s)
end
if parts[1]=='?' or parts[1]=='(?)' then
return parts[2]
end
return parts[1]
end
-- sort mods
local deps={'core'} -- core always loads first
local sortedmods={'core'}
while true do
local newlayer={}
for mod,info in pairs(modinfo) do
if not sortedmods[mod] then
local valid=true
for _,dep in pairs(info.dependencies) do
local depname=getdepname(dep)
print('dependency',dep,'name',depname)
if mods[depname] then
if not sortedmods[depname] then
valid=false
break
end
end
end
if valid then
table.insert(newlayer,mod)
end
end
end
if #newlayer==0 then
break
end
newlayer=natsort(newlayer)
for _,mod in ipairs(newlayer) do
table.insert(deps,mod)
sortedmods[mod]=true
end
end
-- setup data table
newrequire('dataloader')
local dataraw=newenv.data.raw
for _,ptype in pairs(fdata.types) do
dataraw[ptype]={}
end
-- settings stage
for _,mod in ipairs(deps) do
local f=io.open(modroots[mod] .. '/settings.lua','r')
if f then
f:close()
newrequire('__' .. mod .. '__.settings')
end
end
for _,mod in ipairs(deps) do
local f=io.open(modroots[mod] .. '/settings-updates.lua','r')
if f then
f:close()
newrequire('__' .. mod .. '__.settings-updates')
end
end
for _,mod in ipairs(deps) do
local f=io.open(modroots[mod] .. '/settings-final-fixes.lua','r')
if f then
f:close()
newrequire('__' .. mod .. '__.settings-final-fixes')
end
end
function getsettingvalue(setting,value)
if setting.type=='bool-setting' then
if setting.hidden then
if setting.forced_value~=nil then
return setting.forced_value
end
end
if value~=nil then
return value
end
return setting.default_value
elseif setting.type=='int-setting' then
if value~=nil then
if math.mod(value,1)~=0 then
-- nothing
elseif setting.allowed_values then
if #setting.allowed_values==1 then
return setting.allowed_values[1]
end
for _,v in pairs(setting.allowed_values) do
if value==v then
return value
end
end
elseif setting.minimum_value or setting.maximum_value then
if (setting.minimum_value and value>=setting.minimum_value) and (setting.maximum_value and value<=setting.maximum_value) then
return value
end
else
return value
end
end
return setting.default_value
elseif setting.type=='double-setting' then
if value~=nil then
if setting.allowed_values then
if #setting.allowed_values==1 then
return setting.allowed_values[1]
end
for _,v in pairs(setting.allowed_values) do
if value==v then
return value
end
end
elseif setting.minimum_value or setting.maximum_value then
if (setting.minimum_value and value>=setting.minimum_value) and (setting.maximum_value and value<=setting.maximum_value) then
return value
end
else
return value
end
end
return setting.default_value
elseif setting.type=='string-setting' then
if value~=nil then
if setting.allowed_values then
if #setting.allowed_values==1 then
return setting.allowed_values[1]
end
for _,v in pairs(setting.allowed_values) do
if value==v then
return value
end
end
else
if value=='' and setting.allow_blank then
return value
end
if setting.auto_trim then
value=trim(value)
end
return value
end
end
return setting.default_value
elseif setting.type=='color-setting' then
if value~=nil then
return value
end
return setting.default_value
end
end
-- setup settings table
local f=io.open('settings.json','r')
local settingsdata
if f then
local data=f:read('*a')
f:close()
settingsdata=json.load(data)
else
settingsdata={}
end
local settings={}
settings.startup={}
for _,stype in pairs({'bool','int','double','string','color'}) do
-- print(dataraw)
-- for k,v in pairs(dataraw) do
-- print(k)
-- end
if dataraw[stype .. '-setting'] then
for settingname,setting in pairs(dataraw[stype .. '-setting']) do
if setting.setting_type=='startup' then
local value=getsettingvalue(setting,settingsdata[settingname])
settings.startup[settingname]={value=value}
end
end
end
end
print('settings:',json.encode(settings))
newenv.settings=settings
-- data stage
for _,mod in ipairs(deps) do
local f=io.open(modroots[mod] .. '/data.lua','r')
if f then
f:close()
newrequire('__' .. mod .. '__.data')
end
end
for _,mod in ipairs(deps) do
local f=io.open(modroots[mod] .. '/data-updates.lua','r')
if f then
f:close()
newrequire('__' .. mod .. '__.data-updates')
end
end
for _,mod in ipairs(deps) do
local f=io.open(modroots[mod] .. '/data-final-fixes.lua','r')
if f then
f:close()
newrequire('__' .. mod .. '__.data-final-fixes')
end
end
print('done')
-- dump to file
local f=io.open('data.json','w')
local data=json.encode(newenv.data.raw)
f:write(data)
f:close()