Start lovr.filesystem;

This commit is contained in:
bjorn 2016-10-31 17:14:31 -07:00
parent 116701036a
commit e988f7ae41
7 changed files with 135 additions and 16 deletions

View File

@ -0,0 +1,36 @@
#include "filesystem.h"
#include "../util.h"
#include <physfs.h>
#ifdef APPLE
#include <mach-o/dyld.h>
#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);
}

View File

@ -0,0 +1,5 @@
void lovrFilesystemInit(const char* arg0);
void lovrFilesystemDestroy();
int lovrFilesystemExists(const char* path);
const char* lovrFilesystemGetExecutablePath();
int lovrFilesystemSetSource(const char* source);

View File

@ -3,11 +3,28 @@
#include <stdlib.h>
#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);
}

View File

@ -3,9 +3,9 @@
#include <lualib.h>
#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);

52
src/lovr/filesystem.c Normal file
View File

@ -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;
}

10
src/lovr/filesystem.h Normal file
View File

@ -0,0 +1,10 @@
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
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);

View File

@ -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;
}