-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.lua
executable file
·242 lines (228 loc) · 5.37 KB
/
client.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
#!/usr/bin/env luajit
local OUTPUT, REFNAME = ...
local REFNAME_VERSION = REFNAME and REFNAME:match("^refs/tags/(.*)$")
local MAIN_MODULE = "tptmp.client"
local ENV_DEFAULTS = {
-- * Defaults for amalgamation mode.
sim = { XRES = 612, YRES = 384, CELL = 4, PMAPBITS = 0, signs = {} },
elem = {
DEFAULT_PT_FIGH = 0,
DEFAULT_PT_LIGH = 0,
DEFAULT_PT_SPRK = 0,
DEFAULT_PT_STKM = 0,
DEFAULT_PT_STKM2 = 0,
DEFAULT_PT_TESC = 0,
DEFAULT_UI_PROPERTY = 0,
DEFAULT_UI_SAMPLE = 0,
DEFAULT_UI_SIGN = 0,
DEFAULT_UI_WIND = 0,
TPTMP_PT_UNKNOWN = 0,
},
tpt = { version = { upstreamBuild = 356 } },
http = {},
ui = setmetatable({}, { __index = function(tbl, key)
if key:find("^SDL_") then
return 0
end
if key:find("^SDLK_") then
return 0
end
end }),
socket = { tcp = function() end },
}
if OUTPUT then
for key, value in pairs(ENV_DEFAULTS) do
rawset(getfenv(1), key, value)
end
end
local env__ = setmetatable({}, { __index = function(_, key)
error("__index on env: " .. tostring(key), 2)
end, __newindex = function(_, key)
error("__newindex on env: " .. tostring(key), 2)
end })
for key, value in pairs(_G) do
rawset(env__, key, value)
end
local _ENV = env__
if rawget(_G, "setfenv") then
setfenv(1, env__)
end
math.randomseed(os.time())
local script_path = debug.getinfo(1).source
do
assert(script_path:sub(1, 1) == "@", "something is fishy")
script_path = script_path:sub(2)
local slash_at
for ix = #script_path, 1, -1 do
if script_path:sub(ix, ix):find("[\\/]") then
slash_at = ix
break
end
end
if slash_at then
script_path = script_path:sub(1, slash_at - 1)
else
script_path = "."
end
end
local unpack = rawget(_G, "unpack") or table.unpack
local function packn(...)
return { [ 0 ] = select("#", ...), ... }
end
local function unpackn(tbl, from, to)
return unpack(tbl, from or 1, to or tbl[0])
end
local function xpcall_wrap(func, handler)
return function(...)
local iargs = packn(...)
local oargs
xpcall(function()
oargs = packn(func(unpackn(iargs)))
end, function(err)
if handler then
handler(err)
end
print(debug.traceback(err, 2))
return err
end)
if oargs then
return unpackn(oargs)
end
end
end
local chunks = {}
local loaded = {}
local function require(modname)
if not loaded[modname] then
local try = {
modname:gsub("%.", "/") .. ".lua",
modname:gsub("%.", "/") .. "/init.lua",
}
local mod
for i = 1, #try do
local relative = try[i]
local handle = io.open(script_path .. "/" .. relative, "r")
if handle then
local content = handle:read("*a")
handle:close()
local func, err
if rawget(_G, "setfenv") then
func, err = loadstring(content, "=" .. relative)
else
func, err = load(content, "=" .. relative, "bt", env__)
end
if not func then
error(err, 0)
end
if rawget(_G, "setfenv") then
setfenv(func, env__)
end
local ok = true
local err_outer
xpcall_wrap(function()
mod = func()
end, function(err)
ok = false
err_outer = err
end)()
if not ok then
error(err_outer, 0)
end
chunks[modname] = content
break
end
end
if not mod then
error("module " .. modname .. " not found", 2)
end
loaded[modname] = mod
end
return loaded[modname]
end
rawset(env__, "require", require)
rawset(env__, "xpcall_wrap", xpcall_wrap)
local main_module = require(MAIN_MODULE)
if main_module.loadtime_error then
error(main_module.loadtime_error)
end
if OUTPUT then
local handle = assert(io.open(OUTPUT, "w"))
handle:write([[
local env__ = setmetatable({}, { __index = function(_, key)
error("__index on env: " .. tostring(key), 2)
end, __newindex = function(_, key)
error("__newindex on env: " .. tostring(key), 2)
end })
for key, value in pairs(_G) do
rawset(env__, key, value)
end
local _ENV = env__
if rawget(_G, "setfenv") then
setfenv(1, env__)
end
math.randomseed(os.time())
local require_preload__ = {}
local require_loaded__ = {}
local function require(modname)
local mod = require_loaded__[modname]
if not mod then
mod = assert(assert(require_preload__[modname], "missing module " .. modname)())
require_loaded__[modname] = mod
end
return mod
end
rawset(env__, "require", require)
local unpack = rawget(_G, "unpack") or table.unpack
local function packn(...)
return { [ 0 ] = select("#", ...), ... }
end
local function unpackn(tbl, from, to)
return unpack(tbl, from or 1, to or tbl[0])
end
local function xpcall_wrap(func, handler)
return function(...)
local iargs = packn(...)
local oargs
xpcall(function()
oargs = packn(func(unpackn(iargs)))
end, function(err)
if handler then
handler(err)
end
print(debug.traceback(err, 2))
return err
end)
if oargs then
return unpackn(oargs)
end
end
end
rawset(env__, "xpcall_wrap", xpcall_wrap)
]])
local chunk_keys = {}
for key in pairs(chunks) do
table.insert(chunk_keys, key)
end
table.sort(chunk_keys)
for i = 1, #chunk_keys do
local chunk = chunks[chunk_keys[i]]:gsub("\n", "\n\t")
if REFNAME_VERSION then
chunk = chunk:gsub([[local versionstr = "v2%.[^"]+"]], ([[local versionstr = "%s"]]):format(REFNAME_VERSION))
end
handle:write(([[
require_preload__["%s"] = function()
%s
end
]]):format(chunk_keys[i], chunk))
end
handle:write(([[
xpcall_wrap(function()
require("%s").run()
end)()
]]):format(MAIN_MODULE))
handle:close()
else
xpcall_wrap(function()
main_module.run()
end)()
end