-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.lua
204 lines (169 loc) · 4.72 KB
/
main.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
require("hooks")
require("mime")
require("ltn12")
require("enet")
require("socket")
if arg[2] == "debug" then
require("lldebugger").start()
end
local pkg = require("aqua.pkg")
pkg.addc()
pkg.addc("3rd-deps/lib")
pkg.addc("bin/lib")
pkg.add()
pkg.add("3rd-deps/lua")
pkg.add("aqua")
pkg.add("ncdk")
pkg.add("chartbase")
pkg.add("libchart")
pkg.add("tree/share/lua/5.1")
pkg.export_lua()
pkg.export_love()
require("aqua.string")
local luacov_runner
if arg[2] == "test" then
local ok, err = pcall(require, "luacov.runner")
if ok then
luacov_runner = err
luacov_runner.init()
end
end
local deco = require("deco")
deco.read_file = love.filesystem.read
deco.blacklist = {
"3rd-deps",
"tree",
"aqua/byte.lua",
-- "sphere/views",
}
local reqprof = require("reqprof")
if love.filesystem.getInfo("reqprof", "file") then
deco.add(reqprof.ProfileDecorator())
print("enabled reqprof.ProfileDecorator")
end
local typecheck = require("typecheck")
if love.filesystem.getInfo("typecheck", "file") then
typecheck.strict = true
deco.add(require("typecheck.TypeDecorator")())
deco.add(require("typecheck.ClassDecorator")())
print("enabled typecheck.TypeDecorator")
print("enabled typecheck.ClassDecorator")
end
deco.replace_loader()
local ffi = require("ffi")
local source = love.filesystem.getSource()
local sourceBase = love.filesystem.getSourceBaseDirectory()
local root
if source:find("^.+%.love$") then
print("starting from .love file directly")
root = sourceBase
else
print("starting from current directory")
root = source
end
if jit.os == "Windows" then
local winapi = require("winapi")
winapi.putenv("PATH", ("%s;%s"):format(winapi.getenv("PATH"), root .. "/bin/win64"))
winapi.chdir(root)
pkg.addc("bin/win64")
elseif jit.os == "Linux" then
local ldlp = os.getenv("LD_LIBRARY_PATH")
if not ldlp or not ldlp:find("bin/linux64") then
ffi.cdef("int setenv(const char *name, const char *value, int overwrite);")
ffi.C.setenv("LD_LIBRARY_PATH", (ldlp or "") .. ":" .. root .. "/bin/linux64", true)
os.execute(("%q %q &"):format(arg[-2], arg[1]))
return os.exit()
end
ffi.cdef("int chdir(const char *path);")
ffi.C.chdir(root)
pkg.addc("bin/linux64")
elseif jit.os == "OSX" then
local ldlp = os.getenv("DYLD_FALLBACK_LIBRARY_PATH")
if not ldlp or not ldlp:find("bin/mac64") then
ffi.cdef("int setenv(const char *name, const char *value, int overwrite);")
ffi.C.setenv("DYLD_FALLBACK_LIBRARY_PATH", root .. "/bin/mac64", true)
--os.execute(("%q %q &"):format(arg[-2], arg[1]))
--return os.exit()
end
ffi.cdef("int chdir(const char *path);")
ffi.C.chdir(root)
pkg.addc("bin/mac64")
end
pkg.export_lua()
pkg.export_love()
love.errhand = require("errhand")
local physfs = require("physfs")
physfs.setWriteDir(root)
if root == sourceBase then
assert(physfs.mount(root, "/", true))
end
require("preload")
local modloader = require("modloader")(root)
modloader:loadMods()
local love_run = require("love_run")
love.load = function() end -- for compatibility with old conf.lua
local defaultLoop = love.loop or love_run()
function love.run()
return function()
return defaultLoop()
end
end
if arg[2] == "test" then
local Testing = require("testing.Testing")
local BaseTestingIO = require("testing.BaseTestingIO")
local tio = BaseTestingIO()
tio.blacklist = {
".git",
"3rd-deps",
"tree",
"userdata",
}
local testing = Testing(tio)
local file_pattern, method_pattern = arg[3], arg[4]
testing:test(file_pattern, method_pattern)
if luacov_runner then
debug.sethook(nil)
luacov_runner.save_stats()
require("luacov.reporter.lcov").report()
end
os.exit()
end
local delay = require("delay")
delay.set_timer(love.timer.getTime)
---@type sphere.GameController?
local game
local thread = require("thread")
thread.setInitFunc(function(packageLoader)
print("thread init")
require("preload")
if not packageLoader then
return
end
local PackageLoader = require("sphere.pkg.PackageLoader")
local PackageRequire = require("sphere.pkg.PackageRequire")
setmetatable(packageLoader, PackageLoader)
---@cast packageLoader sphere.PackageLoader
local packageRequire = PackageRequire()
local pkgs = packageLoader:getPackagesByType("require")
packageRequire:require(pkgs)
end, function()
return {game and game.packageManager.loader}
end)
thread.coro(function()
local UpdateController = require("sphere.update.UpdateController")
local updateController = UpdateController()
local needRestart = updateController:updateAsync()
if needRestart then
thread.unload()
thread.waitAsync()
return love.event.quit("restart")
end
thread.stopThreads()
local GameController = require("sphere.controllers.GameController")
game = GameController()
game:load()
local loop = require("loop")
loop:init()
defaultLoop = loop:run()
loop:add(game)
end)()