Allow passing a file to lovr;

The file will be treated as main.lua.
Its directory will be treated as the source.
This commit is contained in:
bjorn 2022-04-22 22:47:49 -07:00 committed by Bjorn
parent 005530c9c0
commit 4483145d53
3 changed files with 23 additions and 4 deletions

View File

@ -143,7 +143,8 @@ function lovr.boot()
}
lovr.filesystem = require('lovr.filesystem')
local hasConf, hasMain = lovr.filesystem.isFile('conf.lua'), lovr.filesystem.isFile('main.lua')
local main = arg[0] and arg[0]:match('[^\\/]-%.lua$') or 'main.lua'
local hasConf, hasMain = lovr.filesystem.isFile('conf.lua'), lovr.filesystem.isFile(main)
if not lovr.filesystem.getSource() or not (hasConf or hasMain) then nogame() end
-- Shift args up in fused mode, instead of consuming one for the source path
@ -178,7 +179,7 @@ function lovr.boot()
lovr.handlers = setmetatable({}, { __index = lovr })
if not confOk then error(confError) end
if hasMain then require 'main' end
if hasMain then require(main:gsub('%.lua$', '')) end
return lovr.run()
end

View File

@ -35,10 +35,12 @@ int main(int argc, char** argv) {
if (argc > 1 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) {
os_open_console();
printf(
"usage: lovr [options] [<folder>]\n"
"usage: lovr [options] [<source>]\n\n"
"options:\n"
" -h, --help\t\tShow help and exit\n"
" -v, --version\t\tShow version and exit\n"
" --console\t\tAttach Windows console\n"
" --console\t\tAttach Windows console\n\n"
"<source> can be a Lua file, a folder, or a zip archive\n"
);
exit(0);
}

View File

@ -148,6 +148,22 @@ bool lovrFilesystemInit(const char* archive) {
if (archive) {
state.source[LOVR_PATH_MAX - 1] = '\0';
strncpy(state.source, archive, LOVR_PATH_MAX - 1);
// If the command line parameter is a file, use its containing folder as the source
size_t length = strlen(state.source);
if (length > 4 && !memcmp(state.source + length - 4, ".lua", 4)) {
char* slash = strrchr(state.source, '/');
if (slash) {
*slash = '\0';
} else if ((slash = strrchr(state.source, '\\')) != NULL) {
*slash = '\0';
} else {
state.source[0] = '.';
state.source[1] = '\0';
}
}
if (lovrFilesystemMount(state.source, NULL, true, NULL)) {
return true;
}