boot.lua sets source instead of filesystem module;

This commit is contained in:
bjorn 2023-11-01 15:19:21 -07:00
parent 64950bc152
commit 9d55a5b222
4 changed files with 23 additions and 51 deletions

View File

@ -52,6 +52,22 @@ local conf = {
function lovr.boot()
lovr.filesystem = require('lovr.filesystem')
local bundle, root = lovr.filesystem.getBundlePath()
if lovr.filesystem.mount(bundle, nil, true, root) then
lovr.filesystem.setSource(bundle)
elseif arg[0] then
local source = arg[0]
if arg[0]:match('%.lua$') then
source = arg[0]:match('[/\\]') and arg[0]:gsub('[/\\][^/\\]+$', '') or '.'
end
if lovr.filesystem.mount(source) then
lovr.filesystem.setSource(source)
end
end
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 require('nogame') end

View File

@ -592,18 +592,7 @@ int luaopen_lovr_filesystem(lua_State* L) {
luax_registerloader(L, luaLoader, 2);
luax_registerloader(L, libLoader, 3);
luax_registerloader(L, libLoaderAllInOne, 4);
const char* archive = NULL;
lua_getglobal(L, "arg");
if (lua_istable(L, -1)) {
lua_rawgeti(L, -1, 0);
archive = lua_tostring(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);
lovrFilesystemInit(archive);
lovrFilesystemInit();
luax_atexit(L, lovrFilesystemDestroy);
return 1;
}

View File

@ -81,7 +81,6 @@ static struct {
char source[1024];
char requirePath[1024];
char identity[64];
bool fused;
} state;
// Rejects any path component that would escape the virtual filesystem (./, ../, :, and \)
@ -138,7 +137,7 @@ static bool sanitize(const char* path, char* buffer, size_t* length) {
return true;
}
bool lovrFilesystemInit(const char* archive) {
bool lovrFilesystemInit(void) {
if (atomic_fetch_add(&state.ref, 1)) return false;
lovrFilesystemSetRequirePath("?.lua;?/init.lua");
@ -165,40 +164,6 @@ bool lovrFilesystemInit(const char* archive) {
}
#endif
// Try to mount a bundled archive
const char* root = NULL;
if (os_get_bundle_path(state.source, LOVR_PATH_MAX, &root) && lovrFilesystemMount(state.source, NULL, true, root)) {
state.fused = true;
return true;
}
// If that didn't work, try mounting an archive passed in from the command line
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;
}
}
// Otherwise, there is no source
state.source[0] = '\0';
return true;
}
@ -222,11 +187,13 @@ void lovrFilesystemSetSource(const char* source) {
}
const char* lovrFilesystemGetSource(void) {
return state.source;
return state.source[0] ? state.source : NULL;
}
bool lovrFilesystemIsFused(void) {
return state.fused;
char path[LOVR_PATH_MAX];
const char* root;
return lovrFilesystemGetBundlePath(path, sizeof(path), &root) && !strcmp(state.source, path);
}
// Archives

View File

@ -9,7 +9,7 @@
typedef struct Archive Archive;
typedef struct File File;
bool lovrFilesystemInit(const char* archive);
bool lovrFilesystemInit(void);
void lovrFilesystemDestroy(void);
void lovrFilesystemSetSource(const char* source);
const char* lovrFilesystemGetSource(void);