diff --git a/src/filesystem/filesystem.c b/src/filesystem/filesystem.c new file mode 100644 index 00000000..f9a2718e --- /dev/null +++ b/src/filesystem/filesystem.c @@ -0,0 +1,36 @@ +#include "filesystem.h" +#include "../util.h" +#include +#ifdef APPLE +#include +#endif + +void lovrFilesystemInit(const char* arg0) { + if (!PHYSFS_init(arg0)) { + error("Could not initialize filesystem: %s", PHYSFS_getLastError()); + } +} + +void lovrFilesystemDestroy() { + PHYSFS_deinit(); +} + +int lovrFilesystemExists(const char* path) { + return PHYSFS_exists(path); +} + +const char* lovrFilesystemGetExecutablePath() { +#ifdef APPLE + char path[1024]; + uint32_t size = sizeof(path); + if (_NSGetExecutablePath(path, &size) == 0) { + return path; + } +#endif + + return NULL; +} + +int lovrFilesystemSetSource(const char* source) { + return PHYSFS_mount(source, NULL, 0); +} diff --git a/src/filesystem/filesystem.h b/src/filesystem/filesystem.h new file mode 100644 index 00000000..4e67c317 --- /dev/null +++ b/src/filesystem/filesystem.h @@ -0,0 +1,5 @@ +void lovrFilesystemInit(const char* arg0); +void lovrFilesystemDestroy(); +int lovrFilesystemExists(const char* path); +const char* lovrFilesystemGetExecutablePath(); +int lovrFilesystemSetSource(const char* source); diff --git a/src/lovr.c b/src/lovr.c index 9499a1a0..fbc40898 100644 --- a/src/lovr.c +++ b/src/lovr.c @@ -3,11 +3,28 @@ #include #include "lovr/event.h" +#include "lovr/filesystem.h" #include "lovr/graphics.h" #include "lovr/headset.h" #include "lovr/timer.h" -void lovrInit(lua_State* L) { +void lovrInit(lua_State* L, int argc, char** argv) { + + // arg global + lua_newtable(L); + if (argc > 0) { + lua_pushstring(L, argv[0]); + lua_rawseti(L, -2, -2); + } + + lua_pushstring(L, "lovr"); + lua_rawseti(L, -2, -1); + for (int i = 1; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i); + } + + lua_setglobal(L, "arg"); // lovr = {} lua_newtable(L); @@ -15,12 +32,13 @@ void lovrInit(lua_State* L) { // Preload modules luaPreloadModule(L, "lovr.event", l_lovrEventInit); + luaPreloadModule(L, "lovr.filesystem", l_lovrFilesystemInit); luaPreloadModule(L, "lovr.graphics", l_lovrGraphicsInit); luaPreloadModule(L, "lovr.headset", l_lovrHeadsetInit); luaPreloadModule(L, "lovr.timer", l_lovrTimerInit); // Bootstrap - char buffer[1024]; + char buffer[2048]; snprintf(buffer, sizeof(buffer), "%s", "local conf = { " " modules = { " @@ -31,6 +49,12 @@ void lovrInit(lua_State* L) { " } " "} " + "lovr.filesystem = require('lovr.filesystem') " + "lovr.filesystem.init(arg[-2]) " + "if not lovr.filesystem.setSource(lovr.filesystem.getExecutablePath()) then " + " lovr.filesystem.setSource(arg[1] or '.') " + "end " + "local success, err = pcall(require, 'conf') " "if lovr.conf then " " success, err = pcall(lovr.conf, conf) " @@ -76,18 +100,10 @@ void lovrDestroy(int exitCode) { exit(exitCode); } -void lovrRun(lua_State* L, char* root) { - - // Construct path to main.lua based on command line argument - char path[512]; - if (root) { - snprintf(path, sizeof(path), "%s/main.lua", root); - } else { - snprintf(path, sizeof(path), "main.lua"); - } +void lovrRun(lua_State* L) { // Run "main.lua" which will override/define pieces of lovr - if (fileExists(path) && luaL_dofile(L, path)) { + if (fileExists("main.lua") && luaL_dofile(L, "main.lua")) { lovrOnLuaError(L); exit(EXIT_FAILURE); } diff --git a/src/lovr.h b/src/lovr.h index 68a45c94..cda2e8cc 100644 --- a/src/lovr.h +++ b/src/lovr.h @@ -3,9 +3,9 @@ #include #include "glfw.h" -void lovrInit(lua_State* L); +void lovrInit(lua_State* L, int argc, char** argv); void lovrDestroy(int exitCode); -void lovrRun(lua_State* L, char* mainPath); +void lovrRun(lua_State* L); int lovrOnLuaError(lua_State* L); void lovrOnGlfwError(int code, const char* description); void lovrOnClose(GLFWwindow* window); diff --git a/src/lovr/filesystem.c b/src/lovr/filesystem.c new file mode 100644 index 00000000..8ae7ea48 --- /dev/null +++ b/src/lovr/filesystem.c @@ -0,0 +1,52 @@ +#include "filesystem.h" +#include "../filesystem/filesystem.h" + +const luaL_Reg lovrFilesystem[] = { + { "exists", l_lovrFilesystemExists }, + { "getExecutablePath", l_lovrFilesystemGetExecutablePath }, + { "init", l_lovrFilesystemInitialize }, + { "setSource", l_lovrFilesystemSetSource }, + { NULL, NULL } +}; + +int l_lovrFilesystemInit(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrFilesystem); + return 1; +} + +int l_lovrFilesystemInitialize(lua_State* L) { + const char* arg0 = luaL_checkstring(L, 1); + lovrFilesystemInit(arg0); + return 0; +} + +int l_lovrFilesystemExists(lua_State* L) { + const char* path = luaL_checkstring(L, 1); + lua_pushboolean(L, lovrFilesystemExists(path)); + return 1; +} + +int l_lovrFilesystemGetExecutablePath(lua_State* L) { + const char* exePath = lovrFilesystemGetExecutablePath(); + + if (exePath) { + lua_pushstring(L, exePath); + } else { + lua_pushnil(L); + } + + return 1; +} + +int l_lovrFilesystemSetSource(lua_State* L) { + const char* source = lua_tostring(L, 1); + + if (source) { + lua_pushboolean(L, lovrFilesystemSetSource(source)); + } else { + lua_pushboolean(L, 0); + } + + return 1; +} diff --git a/src/lovr/filesystem.h b/src/lovr/filesystem.h new file mode 100644 index 00000000..17bf76ef --- /dev/null +++ b/src/lovr/filesystem.h @@ -0,0 +1,10 @@ +#include +#include +#include + +extern const luaL_Reg lovrFilesystem[]; +int l_lovrFilesystemInit(lua_State* L); +int l_lovrFilesystemInitialize(lua_State* L); +int l_lovrFilesystemExists(lua_State* L); +int l_lovrFilesystemGetExecutablePath(lua_State* L); +int l_lovrFilesystemSetSource(lua_State* L); diff --git a/src/main.c b/src/main.c index f2704c29..81d21aff 100644 --- a/src/main.c +++ b/src/main.c @@ -6,8 +6,8 @@ int main(int argc, char** argv) { luaL_openlibs(L); initGlfw(lovrOnGlfwError, lovrOnClose, L); - lovrInit(L); - lovrRun(L, argc > 1 ? argv[1] : NULL); + lovrInit(L, argc, argv); + lovrRun(L); return 0; }