Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
226 lines (189 loc) · 5.41 KB
/
Copy pathmain.lua
File metadata and controls
226 lines (189 loc) · 5.41 KB
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
if arg[2] == "cli" then
table.remove(arg, 1)
table.remove(arg, 1)
dofile("sea/app/cli.lua")
return
end
require("mime")
require("ltn12")
require("enet")
require("socket")
if arg[2] == "debug" then
require("lldebugger").start()
end
require("pkg_config")
require("pprint").export()
require("coext").export()
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)
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)
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)
end
love.errorhandler = require("errhand")
love.filesystem.unmountCommonPath("appsavedir")
love.filesystem.unmountFullPath(root)
assert(love.filesystem.mountFullPath(root, "/", "readwrite", false))
require("log_print").install("userdata/log.txt", true)
pcall(require, "luamidi")
setmetatable(_G, {__newindex = function(a, b, c)
print("__newindex", a, b, c, debug.traceback())
rawset(a, b, c)
end})
local current_loop = love.run()
function love.run()
return function()
return current_loop()
end
end
if arg[2] == "test" then
local stbl = require("stbl")
local utf8validate = require("utf8validate")
local typecheck = require("typecheck")
function love.errorhandler(msg)
if type(msg) ~= "string" then
if type(msg) ~= "table" then
msg = tostring(msg)
else
local _ = false
_, msg = pcall(stbl.encode, msg)
end
end
local message = utf8validate(msg)
local trace = debug.traceback()
message = typecheck.fix_traceback(message)
trace = typecheck.fix_traceback(trace)
print(message .. "\n" .. trace)
os.exit()
return function() end
end
local Testing = require("testing.Testing")
local BaseTestingIO = require("testing.BaseTestingIO")
local tio = BaseTestingIO()
tio.blacklist = {
".git",
"3rd-deps",
"tree",
"userdata",
"storages",
"build/deps/",
}
local testing = Testing(tio)
testing.file_pattern = "_lovetest%.lua$"
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)
-- The default user interface on a fake game: `./ui-preview`.
if arg[2] == "ui" then
local preview = require("rizu.api.fake.Preview")(require("ui.UserInterface"))
preview:load()
local loop = require("rizu.loop.Loop")
loop:init()
current_loop = loop:run()
loop:add(preview)
return
end
---@type sphere.GameController?
local game
local thread = require("thread")
thread.setInitFunc(function(packageLoader)
print("thread init")
if not packageLoader then
return
end
local PackageLoader = require("rizu.pkg.PackageLoader")
local PackageRequire = require("rizu.pkg.PackageRequire")
setmetatable(packageLoader, PackageLoader)
---@cast packageLoader rizu.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("rizu.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("rizu.loop.Loop")
loop:init()
current_loop = loop:run()
loop:add(game)
end)()