/
CorsixTH.lua
142 lines (127 loc) · 4.33 KB
/
CorsixTH.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
---- CorsixTH bootstrap code -------------------------------------------------
-- This is not a configuration file and should not be edited. See config.txt
-- for CorsixTH configuration options.
-- Basic sanity check that the file hasn't been invoked by the standard Lua
-- interpreter (as then various packages would be missing and SDLmain would not
-- have run).
if (package and package.preload and package.preload.TH) == nil then
error "This file must be invoked by the CorsixTH executable"
end
-- Parse script parameters:
local run_debugger = false
for _, arg in ipairs({...}) do
if arg:match("^%-%-connect%-lua%-dbgp") then
run_debugger = true
end
end
-- Redefine dofile such that it adds the direction name and file extension, and
-- won't redo a file which it has previously done.
local pathsep = package.config:sub(1, 1)
local base_dir = debug.getinfo(1, "S").source:sub(2, -13)
local code_dir = base_dir .. "Lua" .. pathsep
package.cpath = base_dir .. '?.so;' .. package.cpath
for _, arg in ipairs{...} do
local dir = arg:match"^%-%-lua%-dir=(.*)$"
if dir then
code_dir = dir .. pathsep
end
end
package.path = code_dir .. "?.lua;" .. code_dir .. "?/init.lua;" .. package.path
local done_files = {}
local persist = require("persist")
local save_results
if table.pack then
-- Lua 5.2
save_results = function(t, k, ...)
t[k] = table.pack(...)
return ...
end
else
-- Lua 5.1
save_results = function(t, k, ...)
t[k] = {n = select('#', ...), ...}
return ...
end
end
_G['corsixth'] = {}
--! Loads and runs a lua file.
-- Similar to the built in require function with three important differences:
-- * This function searches for --[[persistance: comments and maps the
-- following function into the persistence table.
-- * This function only searches in the Lua code directory
-- * This function is only able to load lua source files (not C modules or
-- compiled lua.
--!param name (string)
-- The name of the lua source file to run. Use dots to separate directories,
-- and do not include the .lua file extension.
--!return The return value of whatever source file is opened.
corsixth.require = function(name)
name = name:gsub("%.", pathsep)
if done_files[name] then
local results = done_files[name]
return unpack(results, 1, results.n)
end
done_files[name] = true
return save_results(done_files, name, persist.dofile(code_dir .. name .. ".lua"))
end
-- Load standard library extensions
corsixth.require("utility")
-- If requested run a Lua DBGp Debugger Client:
if run_debugger then
dofile("run_debugger")()
end
-- Check Lua version
if _VERSION ~= "Lua 5.1" then
if _VERSION == "Lua 5.2" or _VERSION == "Lua 5.3" then
-- Compatibility: Keep the global unpack function
unpack = table.unpack
-- Compatibility: Provide a replacement for deprecated ipairs()
-- NB: It might be wiser to migrate away from ipairs entirely, but the
-- following works as an immediate band-aid
local rawget, error, type = rawget, error, type
if not pcall(ipairs, {}) then
local function next_int(t, i)
i = i + 1
local v = rawget(t, i)
if v ~= nil then
return i, v
end
end
function ipairs(t)
if type(t) ~= "table" then
error("table expected, got " .. type(t))
end
return next_int, t, 0
end
end
else
error "Please recompile CorsixTH and link against Lua version 5.1, 5.2 or 5.3"
end
end
--
-- A DBGp debugger can debug this file if you start a CorsixTH DBGp client & connect
-- it to a running server, using this CorsixTH startup arg: -debugger
-- Enable strict mode
corsixth.require("strict")
require = destrict(require)
dofile = destrict(dofile)
-- Load the class system (required for App)
corsixth.require("class")
-- Load the main App class
corsixth.require("app")
-- Create an instance of the App class and transfer control to it
strict_declare_global "TheApp"
TheApp = App()
TheApp:setCommandLine(
"--bitmap-dir=" ..base_dir.. "Bitmap",
"--config-file=" .. select(1, corsixth.require("config_finder")),
"--hotkeys-file=" .. select(3, corsixth.require("config_finder")),
-- If a command line option is given twice, the later one is used, hence
-- if the user gave one of the above, that will be used instead.
...
)
assert(TheApp:init())
return TheApp:run()
--[[!file
! Application bootstrap code
]]