From 4483145d5386c6c08713261e4d9e28bcaca0aee9 Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 22 Apr 2022 22:47:49 -0700 Subject: [PATCH] Allow passing a file to lovr; The file will be treated as main.lua. Its directory will be treated as the source. --- etc/boot.lua | 5 +++-- src/main.c | 6 ++++-- src/modules/filesystem/filesystem.c | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/etc/boot.lua b/etc/boot.lua index 689d2a25..dd43fae2 100644 --- a/etc/boot.lua +++ b/etc/boot.lua @@ -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 diff --git a/src/main.c b/src/main.c index a738b839..738e1b7c 100644 --- a/src/main.c +++ b/src/main.c @@ -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] []\n" + "usage: lovr [options] []\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" + " can be a Lua file, a folder, or a zip archive\n" ); exit(0); } diff --git a/src/modules/filesystem/filesystem.c b/src/modules/filesystem/filesystem.c index 0327f05e..7a2a5d77 100644 --- a/src/modules/filesystem/filesystem.c +++ b/src/modules/filesystem/filesystem.c @@ -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; }