mirror of https://github.com/bjornbytes/lovr.git
Rough package loader;
This commit is contained in:
parent
8869539f97
commit
c0e7837ac1
|
@ -39,6 +39,25 @@ int lovrFilesystemIsFile(const char* path) {
|
|||
return lovrFilesystemExists(path) && !lovrFilesystemIsDirectory(path);
|
||||
}
|
||||
|
||||
int lovrFilesystemSetSource(const char* source) {
|
||||
return PHYSFS_mount(source, NULL, 0);
|
||||
char* lovrFilesystemRead(const char* path, int* size) {
|
||||
PHYSFS_file* handle = PHYSFS_openRead(path);
|
||||
|
||||
if (!handle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int length = PHYSFS_fileLength(handle);
|
||||
|
||||
if (length < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* data = malloc(length * sizeof(char));
|
||||
*size = PHYSFS_read(handle, data, sizeof(char), length);
|
||||
return data;
|
||||
}
|
||||
|
||||
int lovrFilesystemSetSource(const char* source) {
|
||||
int res = PHYSFS_mount(source, NULL, 0);
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -4,4 +4,5 @@ int lovrFilesystemExists(const char* path);
|
|||
const char* lovrFilesystemGetExecutablePath();
|
||||
int lovrFilesystemIsDirectory(const char* path);
|
||||
int lovrFilesystemIsFile(const char* path);
|
||||
char* lovrFilesystemRead(const char* path, int* size);
|
||||
int lovrFilesystemSetSource(const char* source);
|
||||
|
|
10
src/lovr.c
10
src/lovr.c
|
@ -84,7 +84,9 @@ void lovrInit(lua_State* L, int argc, char** argv) {
|
|||
" lovr.graphics.present() "
|
||||
" lovr.timer.sleep(.001) "
|
||||
" end "
|
||||
"end"
|
||||
"end "
|
||||
|
||||
"pcall(require, 'main')"
|
||||
);
|
||||
|
||||
if (luaL_dostring(L, buffer)) {
|
||||
|
@ -102,12 +104,6 @@ void lovrDestroy(int exitCode) {
|
|||
|
||||
void lovrRun(lua_State* L) {
|
||||
|
||||
// Run "main.lua" which will override/define pieces of lovr
|
||||
if (fileExists("main.lua") && luaL_dofile(L, "main.lua")) {
|
||||
lovrOnLuaError(L);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// lovr.run()
|
||||
lua_getglobal(L, "lovr");
|
||||
lua_getfield(L, -1, "run");
|
||||
|
|
|
@ -1,5 +1,51 @@
|
|||
#include "filesystem.h"
|
||||
#include "../filesystem/filesystem.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Loader to help Lua's require understand PhysFS.
|
||||
static int filesystemLoader(lua_State* L) {
|
||||
const char* module = luaL_checkstring(L, -1);
|
||||
char* dot;
|
||||
|
||||
while ((dot = strchr(module, '.')) != NULL) {
|
||||
*dot = '/';
|
||||
}
|
||||
|
||||
const char* requirePath[] = {
|
||||
"?.lua",
|
||||
"?/init.lua"
|
||||
};
|
||||
|
||||
for (int i = 0; i < sizeof(requirePath) / sizeof(char*); i++) {
|
||||
char filename[256];
|
||||
char* sub = strchr(requirePath[i], '?');
|
||||
|
||||
memset(filename, 0, 256);
|
||||
|
||||
if (sub) {
|
||||
int index = (int) (sub - requirePath[i]);
|
||||
strncat(filename, requirePath[i], index);
|
||||
strncat(filename, module, strlen(module));
|
||||
strncat(filename, requirePath[i] + index + 1, strlen(requirePath[i]) - index);
|
||||
|
||||
if (lovrFilesystemIsFile(filename)) {
|
||||
int size;
|
||||
char* data = lovrFilesystemRead(filename, &size);
|
||||
|
||||
if (data) {
|
||||
if (!luaL_loadbuffer(L, data, size, filename)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrFilesystem[] = {
|
||||
{ "init", l_lovrFilesystemInitialize },
|
||||
|
@ -14,6 +60,17 @@ const luaL_Reg lovrFilesystem[] = {
|
|||
int l_lovrFilesystemInit(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrFilesystem);
|
||||
|
||||
// Add custom package loader
|
||||
lua_getglobal(L, "package");
|
||||
lua_getfield(L, -1, "loaders");
|
||||
if (lua_istable(L, -1)) {
|
||||
lua_pushcfunction(L, filesystemLoader);
|
||||
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
|
||||
}
|
||||
|
||||
lua_pop(L, 2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue