lovr/src/api/l_filesystem.c

507 lines
13 KiB
C
Raw Normal View History

2017-12-10 20:40:37 +00:00
#include "api.h"
2016-11-19 09:28:01 +00:00
#include "filesystem/filesystem.h"
#include "data/blob.h"
2019-11-23 21:23:48 +00:00
#include "core/fs.h"
#include "core/ref.h"
2019-01-18 16:04:20 +00:00
#include <stdlib.h>
#include <string.h>
#include "platform.h"
2016-11-02 03:27:15 +00:00
2019-11-25 01:27:17 +00:00
#ifdef _WIN32
const char lovrDirSep = '\\';
#else
const char lovrDirSep = '/';
#endif
2017-04-02 12:55:21 +00:00
// Returns a Blob, leaving stack unchanged. The Blob must be released when finished.
Blob* luax_readblob(lua_State* L, int index, const char* debug) {
if (lua_type(L, index) == LUA_TUSERDATA) {
2017-07-14 14:58:12 +00:00
Blob* blob = luax_checktype(L, index, Blob);
2018-02-26 08:59:03 +00:00
lovrRetain(blob);
2017-04-02 12:55:21 +00:00
return blob;
} else {
const char* path = luaL_checkstring(L, index);
size_t size;
void* data = lovrFilesystemRead(path, -1, &size);
2017-04-02 12:55:21 +00:00
if (!data) {
luaL_error(L, "Could not read %s from '%s'", debug, path);
}
return lovrBlobCreate(data, size, path);
}
}
2019-11-25 01:27:17 +00:00
static void pushDirectoryItem(void* context, const char* path) {
lua_State* L = context;
if (path[0] == '.' && (path[1] == '\0' || (path[1] == '.' && path[2] == '\0'))) {
return;
}
lua_pushstring(L, path);
lua_rawget(L, 2);
if (lua_isnil(L, -1)) {
2017-03-11 09:37:00 +00:00
2019-11-25 01:27:17 +00:00
// t[path] = true
lua_pushstring(L, path);
lua_pushboolean(L, true);
lua_rawset(L, 2);
2019-11-25 01:27:17 +00:00
// t[#t + 1] = path
int n = luax_len(L, 2);
lua_pushstring(L, path);
lua_rawseti(L, 2, n + 1);
}
lua_pop(L, 1);
2016-11-02 03:27:15 +00:00
}
2016-11-01 00:14:31 +00:00
static int luax_loadfile(lua_State* L, const char* path, const char* debug) {
2019-11-25 01:27:17 +00:00
size_t size;
void* buffer = lovrFilesystemRead(path, -1, &size);
int status = luaL_loadbuffer(L, buffer, size, debug);
free(buffer);
switch (status) {
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1));
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1));
default: return 1;
}
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemAppend(lua_State* L) {
2016-11-07 22:31:11 +00:00
size_t size;
const char* path = luaL_checkstring(L, 1);
const char* content = luaL_checklstring(L, 2, &size);
2019-09-27 04:58:06 +00:00
lua_pushnumber(L, lovrFilesystemWrite(path, content, size, true));
2016-11-07 22:31:11 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemCreateDirectory(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
2019-05-20 10:51:22 +00:00
lua_pushboolean(L, lovrFilesystemCreateDirectory(path));
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetAppdataDirectory(lua_State* L) {
char buffer[LOVR_PATH_MAX];
2017-03-11 09:37:00 +00:00
if (lovrFilesystemGetAppdataDirectory(buffer, sizeof(buffer))) {
2017-03-11 09:37:00 +00:00
lua_pushstring(L, buffer);
2019-05-20 10:51:22 +00:00
} else {
lua_pushnil(L);
2017-03-11 09:37:00 +00:00
}
return 1;
}
2019-08-21 23:30:20 +00:00
static int l_lovrFilesystemGetApplicationId(lua_State *L) {
char buffer[LOVR_PATH_MAX];
if (lovrFilesystemGetApplicationId(buffer, sizeof(buffer))) {
lua_pushstring(L, buffer);
} else {
lua_pushnil(L);
}
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetDirectoryItems(lua_State* L) {
2017-03-11 09:37:00 +00:00
const char* path = luaL_checkstring(L, 1);
2019-11-25 01:27:17 +00:00
lua_settop(L, 1);
2017-03-11 09:37:00 +00:00
lua_newtable(L);
lovrFilesystemGetDirectoryItems(path, pushDirectoryItem, L);
2019-11-25 01:27:17 +00:00
// Remove all string keys (used for deduplication)
lua_pushnil(L);
while (lua_next(L, 2)) {
lua_pop(L, 1);
if (lua_type(L, -1) == LUA_TSTRING) {
lua_pushvalue(L, -1);
lua_pushnil(L);
lua_rawset(L, 2);
}
}
lua_getglobal(L, "table");
lua_getfield(L, -1, "sort");
lua_pushvalue(L, 2);
lua_call(L, 1, 0);
lua_pop(L, 1);
2017-03-11 09:37:00 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetExecutablePath(lua_State* L) {
char buffer[LOVR_PATH_MAX];
2016-11-01 00:14:31 +00:00
2016-11-07 22:30:32 +00:00
if (lovrFilesystemGetExecutablePath(buffer, sizeof(buffer))) {
lua_pushstring(L, buffer);
2019-11-25 01:27:17 +00:00
} else {
lua_pushnil(L);
2016-11-07 22:30:32 +00:00
}
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetIdentity(lua_State* L) {
const char* identity = lovrFilesystemGetIdentity();
if (identity) {
lua_pushstring(L, identity);
} else {
lua_pushnil(L);
}
2016-11-07 22:30:32 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetLastModified(lua_State* L) {
2017-03-11 09:37:00 +00:00
const char* path = luaL_checkstring(L, 1);
2019-11-25 01:27:17 +00:00
uint64_t lastModified = lovrFilesystemGetLastModified(path);
2017-03-11 09:37:00 +00:00
2019-11-25 01:27:17 +00:00
if (lastModified == ~0ull) {
2017-03-11 09:37:00 +00:00
lua_pushnil(L);
} else {
lua_pushinteger(L, lastModified);
}
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetRealDirectory(lua_State* L) {
2016-11-07 22:30:32 +00:00
const char* path = luaL_checkstring(L, 1);
2016-11-08 21:45:12 +00:00
lua_pushstring(L, lovrFilesystemGetRealDirectory(path));
2016-11-07 22:30:32 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetRequirePath(lua_State* L) {
lua_pushstring(L, lovrFilesystemGetRequirePath());
lua_pushstring(L, lovrFilesystemGetCRequirePath());
2018-07-07 04:21:07 +00:00
return 2;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetSaveDirectory(lua_State* L) {
2017-03-11 09:37:00 +00:00
lua_pushstring(L, lovrFilesystemGetSaveDirectory());
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetSize(lua_State* L) {
2017-03-11 09:37:00 +00:00
const char* path = luaL_checkstring(L, 1);
2019-11-25 01:27:17 +00:00
uint64_t size = lovrFilesystemGetSize(path);
if (size == ~0ull) {
return luaL_error(L, "File does not exist");
}
lua_pushinteger(L, size);
2017-03-11 09:37:00 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetSource(lua_State* L) {
2017-04-02 12:55:21 +00:00
const char* source = lovrFilesystemGetSource();
if (source) {
lua_pushstring(L, source);
} else {
lua_pushnil(L);
}
2016-11-01 00:14:31 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetUserDirectory(lua_State* L) {
2019-11-25 01:27:17 +00:00
char buffer[LOVR_PATH_MAX];
if (lovrFilesystemGetUserDirectory(buffer, sizeof(buffer))) {
lua_pushstring(L, buffer);
} else {
lua_pushnil(L);
}
2016-11-07 22:30:32 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemGetWorkingDirectory(lua_State* L) {
char buffer[LOVR_PATH_MAX];
if (lovrFilesystemGetWorkingDirectory(buffer, sizeof(buffer))) {
lua_pushstring(L, buffer);
2019-05-20 10:51:22 +00:00
} else {
lua_pushnil(L);
}
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemIsDirectory(lua_State* L) {
2016-11-01 01:35:00 +00:00
const char* path = luaL_checkstring(L, 1);
lua_pushboolean(L, lovrFilesystemIsDirectory(path));
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemIsFile(lua_State* L) {
2016-11-01 01:35:00 +00:00
const char* path = luaL_checkstring(L, 1);
lua_pushboolean(L, lovrFilesystemIsFile(path));
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemIsFused(lua_State* L) {
2017-03-11 09:37:00 +00:00
lua_pushboolean(L, lovrFilesystemIsFused());
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemLoad(lua_State* L) {
2017-03-02 04:08:13 +00:00
const char* path = luaL_checkstring(L, 1);
lua_pushfstring(L, "@%s", path);
const char* debug = lua_tostring(L, -1);
return luax_loadfile(L, path, debug);
2017-03-02 04:08:13 +00:00
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemMount(lua_State* L) {
2017-03-11 09:37:00 +00:00
const char* path = luaL_checkstring(L, 1);
const char* mountpoint = luaL_optstring(L, 2, NULL);
2017-10-31 08:14:09 +00:00
bool append = lua_isnoneornil(L, 3) ? 0 : lua_toboolean(L, 3);
const char* root = luaL_optstring(L, 4, NULL);
2019-05-20 10:51:22 +00:00
lua_pushboolean(L, lovrFilesystemMount(path, mountpoint, append, root));
2017-03-11 09:37:00 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemNewBlob(lua_State* L) {
2017-04-01 23:50:10 +00:00
size_t size;
const char* path = luaL_checkstring(L, 1);
uint8_t* data = lovrFilesystemRead(path, -1, &size);
lovrAssert(data, "Could not load file '%s'", path);
Blob* blob = lovrBlobCreate(data, size, path);
luax_pushtype(L, Blob, blob);
lovrRelease(Blob, blob);
2017-04-01 23:50:10 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemRead(lua_State* L) {
2016-11-05 22:55:01 +00:00
const char* path = luaL_checkstring(L, 1);
lua_Integer luaSize = luaL_optinteger(L, 2, -1);
size_t size = MAX(luaSize, -1);
size_t bytesRead;
void* content = lovrFilesystemRead(path, size, &bytesRead);
if (!content) {
lua_pushnil(L);
return 1;
}
lua_pushlstring(L, content, bytesRead);
lua_pushinteger(L, bytesRead);
2016-11-08 06:23:13 +00:00
free(content);
return 2;
2016-11-05 22:55:01 +00:00
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemRemove(lua_State* L) {
2017-03-11 09:37:00 +00:00
const char* path = luaL_checkstring(L, 1);
2019-05-20 10:51:22 +00:00
lua_pushboolean(L, lovrFilesystemRemove(path));
2017-03-11 09:37:00 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemSetIdentity(lua_State* L) {
2019-11-25 01:27:17 +00:00
const char* identity = luaL_checkstring(L, 1);
lovrFilesystemSetIdentity(identity);
2016-11-07 22:30:32 +00:00
return 0;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemSetRequirePath(lua_State* L) {
if (lua_type(L, 1) == LUA_TSTRING) lovrFilesystemSetRequirePath(lua_tostring(L, 1));
if (lua_type(L, 2) == LUA_TSTRING) lovrFilesystemSetCRequirePath(lua_tostring(L, 2));
return 0;
}
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemUnmount(lua_State* L) {
2017-03-11 09:37:00 +00:00
const char* path = luaL_checkstring(L, 1);
2019-05-20 10:51:22 +00:00
lua_pushboolean(L, lovrFilesystemUnmount(path));
2016-11-01 00:14:31 +00:00
return 1;
}
2016-11-07 22:31:02 +00:00
2018-09-27 01:27:38 +00:00
static int l_lovrFilesystemWrite(lua_State* L) {
2016-11-07 22:31:02 +00:00
size_t size;
const char* path = luaL_checkstring(L, 1);
const char* content = luaL_checklstring(L, 2, &size);
2019-09-27 04:58:06 +00:00
lua_pushnumber(L, lovrFilesystemWrite(path, content, size, false));
2016-11-07 22:31:02 +00:00
return 1;
}
2017-03-11 11:08:07 +00:00
2018-09-27 01:27:38 +00:00
static const luaL_Reg lovrFilesystem[] = {
2017-03-11 11:08:07 +00:00
{ "append", l_lovrFilesystemAppend },
{ "createDirectory", l_lovrFilesystemCreateDirectory },
2017-03-11 11:08:07 +00:00
{ "getAppdataDirectory", l_lovrFilesystemGetAppdataDirectory },
2019-08-21 23:30:20 +00:00
{ "getApplicationId", l_lovrFilesystemGetApplicationId },
2017-03-11 11:08:07 +00:00
{ "getDirectoryItems", l_lovrFilesystemGetDirectoryItems },
{ "getExecutablePath", l_lovrFilesystemGetExecutablePath },
{ "getIdentity", l_lovrFilesystemGetIdentity },
{ "getLastModified", l_lovrFilesystemGetLastModified },
{ "getRealDirectory", l_lovrFilesystemGetRealDirectory },
{ "getRequirePath", l_lovrFilesystemGetRequirePath },
2017-03-11 11:08:07 +00:00
{ "getSaveDirectory", l_lovrFilesystemGetSaveDirectory },
{ "getSize", l_lovrFilesystemGetSize },
{ "getSource", l_lovrFilesystemGetSource },
{ "getUserDirectory", l_lovrFilesystemGetUserDirectory },
{ "getWorkingDirectory", l_lovrFilesystemGetWorkingDirectory },
2017-03-11 11:08:07 +00:00
{ "isDirectory", l_lovrFilesystemIsDirectory },
{ "isFile", l_lovrFilesystemIsFile },
{ "isFused", l_lovrFilesystemIsFused },
{ "load", l_lovrFilesystemLoad },
{ "mount", l_lovrFilesystemMount },
2017-04-01 23:50:10 +00:00
{ "newBlob", l_lovrFilesystemNewBlob },
2017-03-11 11:08:07 +00:00
{ "read", l_lovrFilesystemRead },
{ "remove", l_lovrFilesystemRemove },
{ "setRequirePath", l_lovrFilesystemSetRequirePath },
2017-03-11 11:08:07 +00:00
{ "setIdentity", l_lovrFilesystemSetIdentity },
{ "unmount", l_lovrFilesystemUnmount },
2017-03-11 11:08:07 +00:00
{ "write", l_lovrFilesystemWrite },
{ NULL, NULL }
};
2018-09-27 01:27:38 +00:00
static int luaLoader(lua_State* L) {
const char* module = lua_tostring(L, 1);
const char* p = lovrFilesystemGetRequirePath();
char buffer[1024] = { '@' };
char* debug = &buffer[0];
char* filename = &buffer[1];
char* f = filename;
size_t n = sizeof(buffer) - 1;
// Loop over the require path, character by character, and:
// - Replace question marks with the module that's being required, converting '.' to '/'.
// - If there's a semicolon/eof, treat it as the end of a potential filename and try to load it.
// The filename buffer has an '@' before it so it can also be used as the debug label for the Lua
// chunk without additional memory allocation.
while (1) {
if (*p == ';' || *p == '\0') {
*f = '\0';
if (lovrFilesystemIsFile(filename)) {
return luax_loadfile(L, filename, debug);
}
if (*p == '\0') {
break;
} else {
p++;
f = filename;
n = sizeof(buffer) - 1;
}
} else if (*p == '?') {
for (const char* m = module; n && *m; n--, m++) {
*f++ = *m == '.' ? '/' : *m;
}
p++;
} else {
*f++ = *p++;
n--;
}
lovrAssert(n > 0, "Tried to require a filename that was too long (%s)", module);
}
return 0;
}
static int libLoader(lua_State* L) {
#ifdef _WIN32
const char* extension = ".dll";
#else
const char* extension = ".so";
#endif
const char* module = lua_tostring(L, 1);
const char* hyphen = strchr(module, '-');
const char* symbol = hyphen ? hyphen + 1 : module;
const char* p = lovrFilesystemGetCRequirePath();
char filename[1024];
char* f = filename;
size_t n = sizeof(filename);
lua_getglobal(L, "package");
while (1) {
if (*p == ';' || *p == '\0') {
*f = '\0';
if (lovrFilesystemIsFile(filename)) {
lua_getfield(L, -1, "loadlib");
// Synthesize the absolute path to the library on disk (outside of physfs)
luaL_Buffer buffer;
luaL_buffinit(L, &buffer);
luaL_addstring(&buffer, lovrFilesystemGetRealDirectory(filename));
luaL_addchar(&buffer, lovrDirSep);
luaL_addstring(&buffer, filename);
luaL_pushresult(&buffer);
// Synthesize the symbol to load: luaopen_ followed by the module name with dots converted
// to underscores, starting after the first hyphen (if there is one).
luaL_buffinit(L, &buffer);
luaL_addstring(&buffer, "luaopen_");
for (const char* s = symbol; *s; s++) {
luaL_addchar(&buffer, *s == '.' ? '_' : *s);
}
luaL_pushresult(&buffer);
// Finally call package.loadlib with the library path and symbol name
lua_call(L, 2, 1);
return 1;
}
if (*p == '\0') {
break;
} else {
p++;
f = filename;
n = sizeof(filename);
}
} else if (*p == '?') {
for (const char* m = module; n && *m; n--, m++) {
*f++ = *m == '.' ? lovrDirSep : *m;
}
p++;
if (*p == '?') {
for (const char* e = extension; n && *e; n--, e++) {
*f++ = *e;
}
p++;
}
} else {
*f++ = *p++;
n--;
}
lovrAssert(n > 0, "Tried to require a filename that was too long (%s)", module);
}
return 0;
}
int luaopen_lovr_filesystem(lua_State* L) {
2018-09-27 01:27:38 +00:00
lua_getglobal(L, "arg");
if (lua_istable(L, -1)) {
lua_getfield(L, -1, "exe");
const char* argExe = lua_tostring(L, -1);
lua_rawgeti(L, -2, 0);
const char* argGame = lua_tostring(L, -1);
lua_getfield(L, -3, "root");
const char* argRoot = luaL_optstring(L, -1, NULL);
if (lovrFilesystemInit(argExe, argGame, argRoot)) {
luax_atexit(L, lovrFilesystemDestroy);
}
lua_pop(L, 4);
} else {
lua_pop(L, 1);
if (lovrFilesystemInit(NULL, NULL, NULL)) {
luax_atexit(L, lovrFilesystemDestroy);
}
}
2018-09-27 01:27:38 +00:00
lua_newtable(L);
luaL_register(L, NULL, lovrFilesystem);
luax_registerloader(L, luaLoader, 2);
luax_registerloader(L, libLoader, 3);
2018-09-27 01:27:38 +00:00
return 1;
}