-
Notifications
You must be signed in to change notification settings - Fork 0
/
loft.boot.lua
492 lines (422 loc) · 14.8 KB
/
loft.boot.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
-- modified: loft require name, modules to load, + config changes
--[[
Copyright (c) 2006-2024 LOVE Development Team
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
--]]
-- Make sure love exists.
local love = require("loft")
-- Essential code boot/init.
require("loft.arg")
require("loft.callbacks")
local function uridecode(s)
return s:gsub("%%%x%x", function(str)
return string.char(tonumber(str:sub(2), 16))
end)
end
local no_game_code = false
local invalid_game_path = nil
local main_file = "main.lua"
-- This can't be overridden.
function love.boot(...)
local arg2 = { ... }
-- This is absolutely needed.
require("loft.filesystem")
love.rawGameArguments = arg2
local arg0 = love.arg.getLow(love.rawGameArguments)
love.filesystem.init(arg0)
local exepath = love.filesystem.getExecutablePath()
if #exepath == 0 then
-- This shouldn't happen, but just in case we'll fall back to arg0.
exepath = arg0
end
no_game_code = false
invalid_game_path = nil
-- Is this one of those fancy "fused" games?
-- can I haz game too?
local can_has_game = pcall(love.filesystem.setSource, exepath)
-- It's a fused game, don't parse --game argument
if can_has_game then
love.arg.options.game.set = true
end
-- Parse options now that we know which options we're looking for.
love.arg.parseOptions(love.rawGameArguments)
-- parseGameArguments can only be called after parseOptions.
love.parsedGameArguments = love.arg.parseGameArguments(love.rawGameArguments)
local o = love.arg.options
local is_fused_game = can_has_game or love.arg.options.fused.set
love.filesystem.setFused(is_fused_game)
love.setDeprecationOutput(not love.filesystem.isFused())
main_file = "main.lua"
local custom_main_file = false
local identity = ""
if not can_has_game and o.game.set and o.game.arg[1] then
local nouri = o.game.arg[1]
if nouri:sub(1, 7) == "file://" then
nouri = uridecode(nouri:sub(8))
end
local full_source = love.path.getFull(nouri)
local source_leaf = love.path.leaf(full_source)
if source_leaf:match("%.lua$") then
main_file = source_leaf
custom_main_file = true
full_source = love.path.getFull(full_source:sub(1, -(#source_leaf + 1)))
end
can_has_game = pcall(love.filesystem.setSource, full_source)
if not can_has_game then
invalid_game_path = full_source
end
-- Use the name of the source .love as the identity for now.
identity = love.path.leaf(full_source)
else
-- Use the name of the exe as the identity for now.
identity = love.path.leaf(exepath)
end
-- Try to use the archive containing main.lua as the identity name. It
-- might not be available, in which case the fallbacks above are used.
local realdir = love.filesystem.getRealDirectory(main_file)
if realdir then
identity = love.path.leaf(realdir)
end
identity = identity:gsub("^([%.]+)", "") -- strip leading "."'s
identity = identity:gsub("%.([^%.]+)$", "") -- strip extension
identity = identity:gsub("%.", "_") -- replace remaining "."'s with "_"
identity = #identity > 0 and identity or "lovegame"
-- When conf.lua is initially loaded, the main source should be checked
-- before the save directory (the identity should be appended.)
pcall(love.filesystem.setIdentity, identity, true)
if
can_has_game
and not (love.filesystem.getInfo(main_file) or (not custom_main_file and love.filesystem.getInfo("conf.lua")))
then
no_game_code = true
end
if not can_has_game then
-- when editing this message, change it at love.cpp too
-- print([[LOVE is an *awesome* framework you can use to make 2D games in Lua
-- https://love2d.org
-- usage:
-- loft --version prints LOVE version and quits
-- loft --help prints this message and quits
-- loft path/to/gamedir runs the game from the given directory which contains a main.lua file
-- loft path/to/packagedgame.love runs the packaged game from the provided .love file
-- loft path/to/file.lua runs the game from the given .lua file
-- ]]);
local nogame = require("loft.nogame")
nogame()
end
end
function love.init()
-- Create default configuration settings.
-- NOTE: Adding a new module to the modules list
-- will NOT make it load, see below.
local c = {
title = "Untitled",
version = love._version,
window = {
width = 800,
height = 600,
x = nil,
y = nil,
minwidth = 800,
minheight = 600,
fullscreen = true,
fullscreentype = "exclusive",
display = 1,
vsync = 1,
msaa = 0,
borderless = true,
resizable = false,
centered = true,
usedpiscale = true,
},
modules = {
data = true,
event = true,
keyboard = true,
mouse = true,
timer = true,
joystick = true,
touch = true,
image = true,
graphics = true,
audio = true,
math = true,
physics = true,
sensor = true,
sound = true,
system = true,
font = true,
thread = true,
window = true,
video = true,
},
audio = {
mixwithsystem = true, -- Only relevant for Android / iOS.
mic = false, -- Only relevant for Android.
},
console = false, -- Only relevant for windows.
identity = false,
appendidentity = false,
externalstorage = false, -- Only relevant for Android.
accelerometerjoystick = nil, -- Only relevant for Android / iOS, deprecated.
gammacorrect = false,
highdpi = false,
renderers = nil,
excluderenderers = nil,
}
-- Console hack, part 1.
local openedconsole = false
if love.arg.options.console.set and love._openConsole then
love._openConsole()
openedconsole = true
end
-- If config file exists, load it and allow it to update config table.
local confok, conferr
if (not love.conf) and love.filesystem and love.filesystem.getInfo("conf.lua") then
confok, conferr = pcall(require, "conf")
end
-- Yes, conf.lua might not exist, but there are other ways of making
-- love.conf appear, so we should check for it anyway.
if love.conf then
confok, conferr = pcall(love.conf, c)
-- If love.conf errors, we'll trigger the error after loading modules so
-- the error message can be displayed in the window.
end
-- Console hack, part 2.
if c.console and love._openConsole and not openedconsole then
love._openConsole()
end
-- Hack for disabling accelerometer-as-joystick on Android / iOS.
if love._setAccelerometerAsJoystick then
love._setAccelerometerAsJoystick(c.accelerometerjoystick)
end
if love._setGammaCorrect then
love._setGammaCorrect(c.gammacorrect)
end
if love._setRenderers then
local renderers = love._getDefaultRenderers()
if type(c.renderers) == "table" then
renderers = {}
for i, v in ipairs(c.renderers) do
renderers[i] = v
end
end
if love.arg.options.renderers.set then
local renderersstr = love.arg.options.renderers.arg[1]
renderers = {}
for r in renderersstr:gmatch("[^,]+") do
table.insert(renderers, r)
end
end
local excluderenderers = c.excluderenderers
if love.arg.options.excluderenderers.set then
local excludestr = love.arg.options.excluderenderers.arg[1]
excluderenderers = {}
for r in excludestr:gmatch("[^,]+") do
table.insert(excluderenderers, r)
end
end
if type(excluderenderers) == "table" then
for i, v in ipairs(excluderenderers) do
for j = #renderers, 1, -1 do
if renderers[j] == v then
table.remove(renderers, j)
break
end
end
end
end
love._setRenderers(renderers)
end
if love._setHighDPIAllowed then
love._setHighDPIAllowed(c.highdpi)
end
if love._setAudioMixWithSystem then
if c.audio and c.audio.mixwithsystem ~= nil then
love._setAudioMixWithSystem(c.audio.mixwithsystem)
end
end
if love._requestRecordingPermission then
love._requestRecordingPermission(c.audio and c.audio.mic)
end
-- Gets desired modules.
-- if c.modules.data then require("loft.data") end
-- if c.modules.timer then require("loft.timer") end
-- if c.modules.event then require("loft.event") end
-- if c.modules.system then require("loft.system") end
-- if c.modules.window then require("loft.window") end
-- if c.modules.graphics then require("loft.graphics") end
for k, v in ipairs({
"data",
-- "thread",
"timer",
"event",
"input",
-- "keyboard",
-- "joystick",
"mouse",
-- "touch",
-- "sound",
"system",
-- "sensor",
-- "audio",
"image",
-- "video",
-- "font",
"window",
"graphics",
-- "math",
-- "physics",
}) do
if c.modules[v] then
require("loft." .. v)
end
end
if love.event then
love.createhandlers()
end
-- Check the version
c.version = tostring(c.version)
if not love.isVersionCompatible(c.version) then
local major, minor, revision = c.version:match("^(%d+)%.(%d+)%.(%d+)$")
if
(not major or not minor or not revision) or (major ~= love._version_major and minor ~= love._version_minor)
then
local msg = (
"This game indicates it was made for version '%s' of LOVE.\n"
.. "It may not be compatible with the running version (%s)."
):format(c.version, love._version)
print(msg)
if love.window then
love.window.showMessageBox("Compatibility Warning", msg, "warning")
end
end
end
if not confok and conferr then
error(conferr)
end
-- Setup window here.
if c.window and c.modules.window then
love.window.setTitle(c.window.title or c.title)
assert(
love.window.setMode(c.window.width, c.window.height, {
fullscreen = c.window.fullscreen,
fullscreentype = c.window.fullscreentype,
vsync = c.window.vsync,
msaa = c.window.msaa,
stencil = c.window.stencil,
depth = c.window.depth,
resizable = c.window.resizable,
minwidth = c.window.minwidth,
minheight = c.window.minheight,
borderless = c.window.borderless,
centered = c.window.centered,
displayindex = c.window.displayindex,
display = c.window.display, -- deprecated
highdpi = c.window.highdpi, -- deprecated
usedpiscale = c.window.usedpiscale,
x = c.window.x,
y = c.window.y,
}),
"Could not set window mode"
)
if c.window.icon then
assert(love.image, "If an icon is set in love.conf, love.image must be loaded!")
love.window.setIcon(love.image.newImageData(c.window.icon))
end
end
-- The first couple event pumps on some systems (e.g. macOS) can take a
-- while. We'd rather hit that slowdown here than in event processing
-- within the first frames.
if love.event then
for i = 1, 2 do
love.event.pump()
end
end
-- Our first timestep, because window creation can take some time
if love.timer then
love.timer.step()
end
if love.filesystem then
love.filesystem._setAndroidSaveExternal(c.externalstorage)
love.filesystem.setIdentity(c.identity or love.filesystem.getIdentity(), c.appendidentity)
if love.filesystem.getInfo(main_file) then
require(main_file:gsub("%.lua$", ""))
end
end
if no_game_code then
local opts = love.arg.options
local gamepath = opts.game.set and opts.game.arg and opts.game.arg[1] or ""
local gamestr = gamepath == "" and "" or " at " .. gamepath
error(
"No code to run"
.. gamestr
.. "\nYour game might be packaged incorrectly.\nMake sure "
.. main_file
.. " is at the top level of the zip or folder."
)
elseif invalid_game_path then
error("Cannot load game at path '" .. invalid_game_path .. "'.\nMake sure a folder exists at the specified path.")
end
end
local print, debug, tostring = print, debug, tostring
local function error_printer(msg, layer)
print((debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", "")))
end
-----------------------------------------------------------
-- The root of all calls.
-----------------------------------------------------------
return function(...)
local arg2 = { ... }
local func
local inerror = false
local function deferErrhand(...)
local errhand = love.errorhandler or love.errhand
local handler = (not inerror and errhand) or error_printer
inerror = true
func = handler(...)
end
local function earlyinit()
-- If love.boot fails, return 1 and finish immediately
local result = xpcall(function()
return love.boot((unpack or table.unpack)(arg2))
end, error_printer)
if not result then
return 1
end
-- If love.init or love.run fails, don't return a value,
-- as we want the error handler to take over
result = xpcall(love.init, deferErrhand)
if not result then
return
end
-- NOTE: We can't assign to func directly, as we'd
-- overwrite the result of deferErrhand with nil on error
local main
result, main = xpcall(love.run, deferErrhand)
if result then
func = main
end
end
func = earlyinit
while func do
local _, retval, restartvalue = xpcall(func, deferErrhand)
if retval then
return retval, restartvalue
end
coroutine.yield()
end
return 1
end