Changes for physfs 3; rm lovr.filesystem.exists;

This commit is contained in:
bjorn 2017-10-22 01:37:06 -07:00
parent dfc7d6bfab
commit 9b3723145b
3 changed files with 35 additions and 30 deletions

View File

@ -23,11 +23,12 @@ Blob* luax_readblob(lua_State* L, int index, const char* debug) {
}
}
static void pushDirectoryItem(void* userdata, const char* path, const char* filename) {
static int pushDirectoryItem(void* userdata, const char* path, const char* filename) {
lua_State* L = userdata;
int n = lua_objlen(L, -1);
lua_pushstring(L, filename);
lua_rawseti(L, -2, n + 1);
return 1;
}
// Loader to help Lua's require understand PhysFS.
@ -122,12 +123,6 @@ int l_lovrFilesystemCreateDirectory(lua_State* L) {
return 1;
}
int l_lovrFilesystemExists(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
lua_pushboolean(L, lovrFilesystemExists(path));
return 1;
}
int l_lovrFilesystemGetAppdataDirectory(lua_State* L) {
char buffer[1024];
@ -195,7 +190,11 @@ int l_lovrFilesystemGetSaveDirectory(lua_State* L) {
int l_lovrFilesystemGetSize(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
lua_pushinteger(L, lovrFilesystemGetSize(path));
size_t size = lovrFilesystemGetSize(path);
if ((int) size == -1) {
return luaL_error(L, "File does not exist");
}
lua_pushinteger(L, size);
return 1;
}
@ -322,7 +321,6 @@ int l_lovrFilesystemWrite(lua_State* L) {
const luaL_Reg lovrFilesystem[] = {
{ "append", l_lovrFilesystemAppend },
{ "createDirectory", l_lovrFilesystemCreateDirectory },
{ "exists", l_lovrFilesystemExists },
{ "getAppdataDirectory", l_lovrFilesystemGetAppdataDirectory },
{ "getDirectoryItems", l_lovrFilesystemGetDirectoryItems },
{ "getExecutablePath", l_lovrFilesystemGetExecutablePath },

View File

@ -22,7 +22,7 @@ static FilesystemState state;
void lovrFilesystemInit(const char* arg0, const char* arg1) {
if (!PHYSFS_init(arg0)) {
lovrThrow("Could not initialize filesystem: %s", PHYSFS_getLastError());
lovrThrow("Could not initialize filesystem: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}
state.source = malloc(LOVR_PATH_MAX * sizeof(char));
@ -60,10 +60,6 @@ int lovrFilesystemCreateDirectory(const char* path) {
return !PHYSFS_mkdir(path);
}
int lovrFilesystemExists(const char* path) {
return PHYSFS_exists(path);
}
int lovrFilesystemGetAppdataDirectory(char* dest, unsigned int size) {
#ifdef __APPLE__
const char* home;
@ -97,7 +93,7 @@ int lovrFilesystemGetAppdataDirectory(char* dest, unsigned int size) {
}
void lovrFilesystemGetDirectoryItems(const char* path, getDirectoryItemsCallback callback, void* userdata) {
PHYSFS_enumerateFilesCallback(path, callback, userdata);
PHYSFS_enumerate(path, callback, userdata);
}
int lovrFilesystemGetExecutablePath(char* dest, unsigned int size) {
@ -126,7 +122,8 @@ const char* lovrFilesystemGetIdentity() {
}
long lovrFilesystemGetLastModified(const char* path) {
return PHYSFS_getLastModTime(path);
PHYSFS_Stat stat;
return PHYSFS_stat(path, &stat) ? stat.modtime : -1;
}
const char* lovrFilesystemGetRealDirectory(const char* path) {
@ -138,12 +135,8 @@ const char* lovrFilesystemGetSaveDirectory() {
}
size_t lovrFilesystemGetSize(const char* path) {
File* file = lovrFileCreate(path);
lovrFileOpen(file, OPEN_READ);
size_t size = lovrFileGetSize(file);
lovrFileClose(file);
lovrRelease(&file->ref);
return size;
PHYSFS_Stat stat;
return PHYSFS_stat(path, &stat) ? stat.filesize : -1;
}
const char* lovrFilesystemGetSource() {
@ -151,15 +144,29 @@ const char* lovrFilesystemGetSource() {
}
const char* lovrFilesystemGetUserDirectory() {
return PHYSFS_getUserDir();
#if defined(__APPLE__) || defined(__linux__)
const char* home;
if ((home = getenv("HOME")) == NULL) {
home = getpwuid(getuid())->pw_dir;
}
return home;
#elif _WIN32
return getenv("USERPROFILE");
#elif EMSCRIPTEN
return "/home/web_user";
#else
#error "This platform is missing an implementation for lovrFilesystemGetUserDirectory"
#endif
}
int lovrFilesystemIsDirectory(const char* path) {
return PHYSFS_isDirectory(path);
PHYSFS_Stat stat;
return PHYSFS_stat(path, &stat) ? stat.filetype == PHYSFS_FILETYPE_DIRECTORY : 0;
}
int lovrFilesystemIsFile(const char* path) {
return lovrFilesystemExists(path) && !lovrFilesystemIsDirectory(path);
PHYSFS_Stat stat;
return PHYSFS_stat(path, &stat) ? stat.filetype == PHYSFS_FILETYPE_REGULAR : 0;
}
int lovrFilesystemIsFused() {
@ -217,7 +224,7 @@ int lovrFilesystemSetIdentity(const char* identity) {
// Unmount old write directory
if (state.savePathFull && state.savePathRelative) {
PHYSFS_removeFromSearchPath(state.savePathRelative);
PHYSFS_unmount(state.savePathRelative);
} else {
state.savePathRelative = malloc(LOVR_PATH_MAX);
state.savePathFull = malloc(LOVR_PATH_MAX);
@ -234,7 +241,8 @@ int lovrFilesystemSetIdentity(const char* identity) {
strncpy(state.savePathFull, fullPathBuffer, LOVR_PATH_MAX);
PHYSFS_mkdir(state.savePathRelative);
if (!PHYSFS_setWriteDir(state.savePathFull)) {
lovrThrow("Could not set write directory: %s (%s)", PHYSFS_getLastError(), state.savePathRelative);
const char* error = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
lovrThrow("Could not set write directory: %s (%s)", error, state.savePathRelative);
}
PHYSFS_mount(state.savePathFull, NULL, 0);
@ -243,7 +251,7 @@ int lovrFilesystemSetIdentity(const char* identity) {
}
int lovrFilesystemUnmount(const char* path) {
return !PHYSFS_removeFromSearchPath(path);
return !PHYSFS_unmount(path);
}
size_t lovrFilesystemWrite(const char* path, const char* content, size_t size, int append) {

View File

@ -4,7 +4,7 @@
#define LOVR_PATH_MAX 1024
typedef void getDirectoryItemsCallback(void* userdata, const char* dir, const char* file);
typedef int getDirectoryItemsCallback(void* userdata, const char* dir, const char* file);
typedef struct {
char* source;
@ -17,7 +17,6 @@ typedef struct {
void lovrFilesystemInit(const char* arg0, const char* arg1);
void lovrFilesystemDestroy();
int lovrFilesystemCreateDirectory(const char* path);
int lovrFilesystemExists(const char* path);
int lovrFilesystemGetAppdataDirectory(char* dest, unsigned int size);
void lovrFilesystemGetDirectoryItems(const char* path, getDirectoryItemsCallback callback, void* userdata);
int lovrFilesystemGetExecutablePath(char* dest, unsigned int size);