From b8ff64e9e35b1f7cd7467828d9a0f56612d21b85 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 22 Apr 2022 21:05:20 -0300 Subject: [PATCH] Add love.filesystem.openFile. Deprecate love.filesystem.newFile. --- src/modules/filesystem/Filesystem.h | 4 +- src/modules/filesystem/NativeFile.cpp | 18 +++-- src/modules/filesystem/physfs/File.cpp | 6 +- src/modules/filesystem/physfs/Filesystem.cpp | 2 +- src/modules/filesystem/physfs/Filesystem.h | 2 +- src/modules/filesystem/wrap_Filesystem.cpp | 81 ++++++++++++-------- 6 files changed, 69 insertions(+), 44 deletions(-) diff --git a/src/modules/filesystem/Filesystem.h b/src/modules/filesystem/Filesystem.h index 53ca5cdbb..daa735f40 100644 --- a/src/modules/filesystem/Filesystem.h +++ b/src/modules/filesystem/Filesystem.h @@ -165,9 +165,9 @@ public: virtual bool unmountFullPath(const char *fullpath) = 0; /** - * Creates a new file. + * Opens a new File object from the specified path, using the given mode. **/ - virtual File *newFile(const char *filename, File::Mode = File::MODE_CLOSED) const = 0; + virtual File *openFile(const char *filename, File::Mode mode) const = 0; /** * Creates a new FileData object. Data will be copied. diff --git a/src/modules/filesystem/NativeFile.cpp b/src/modules/filesystem/NativeFile.cpp index 0fd5d0f1d..58753cfcf 100644 --- a/src/modules/filesystem/NativeFile.cpp +++ b/src/modules/filesystem/NativeFile.cpp @@ -46,13 +46,8 @@ NativeFile::NativeFile(const std::string &filename, Mode mode) , bufferMode(BUFFER_NONE) , bufferSize(0) { - open(mode); -} - -NativeFile::~NativeFile() -{ - if (mode != MODE_CLOSED) - close(); + if (!open(mode)) + throw love::Exception("Could not open file at path %s", filename.c_str()); } NativeFile::NativeFile(const NativeFile &other) @@ -62,7 +57,14 @@ NativeFile::NativeFile(const NativeFile &other) , bufferMode(other.bufferMode) , bufferSize(other.bufferSize) { - open(other.mode); + if (!open(other.mode)) + throw love::Exception("Could not open file at path %s", filename.c_str()); +} + +NativeFile::~NativeFile() +{ + if (mode != MODE_CLOSED) + close(); } NativeFile *NativeFile::clone() diff --git a/src/modules/filesystem/physfs/File.cpp b/src/modules/filesystem/physfs/File.cpp index f56455fc5..181153d3e 100644 --- a/src/modules/filesystem/physfs/File.cpp +++ b/src/modules/filesystem/physfs/File.cpp @@ -48,7 +48,8 @@ File::File(const std::string &filename, Mode mode) , bufferMode(BUFFER_NONE) , bufferSize(0) { - open(mode); + if (!open(mode)) + throw love::Exception("Could not open file at path %s", filename.c_str()); } File::File(const File &other) @@ -58,7 +59,8 @@ File::File(const File &other) , bufferMode(other.bufferMode) , bufferSize(other.bufferSize) { - open(other.mode); + if (!open(other.mode)) + throw love::Exception("Could not open file at path %s", filename.c_str()); } File::~File() diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index 4a2752d5e..02b7d0f78 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -496,7 +496,7 @@ bool Filesystem::unmount(Data *data) return false; } -love::filesystem::File *Filesystem::newFile(const char *filename, File::Mode mode) const +love::filesystem::File *Filesystem::openFile(const char *filename, File::Mode mode) const { return new File(filename, mode); } diff --git a/src/modules/filesystem/physfs/Filesystem.h b/src/modules/filesystem/physfs/Filesystem.h index 9b31c5f9f..3641e5ded 100644 --- a/src/modules/filesystem/physfs/Filesystem.h +++ b/src/modules/filesystem/physfs/Filesystem.h @@ -71,7 +71,7 @@ public: bool unmount(CommonPath path) override; bool unmountFullPath(const char *fullpath) override; - love::filesystem::File *newFile(const char *filename, File::Mode mode = File::MODE_CLOSED) const override; + love::filesystem::File *openFile(const char *filename, File::Mode mode) const override; std::string getFullCommonPath(CommonPath path) override; const char *getWorkingDirectory() override; diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index fb239915d..7ca9a7c4f 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -227,34 +227,53 @@ int w_unmountCommonPath(lua_State *L) return 1; } -int w_newFile(lua_State *L) +int w_openFile(lua_State *L) { const char *filename = luaL_checkstring(L, 1); + const char *modestr = luaL_checkstring(L, 2); - const char *str = 0; File::Mode mode = File::MODE_CLOSED; + if (!File::getConstant(modestr, mode)) + return luax_enumerror(L, "file open mode", File::getConstants(mode), modestr); - if (lua_isstring(L, 2)) + File *t = nullptr; + try { - str = luaL_checkstring(L, 2); - if (!File::getConstant(str, mode)) - return luax_enumerror(L, "file open mode", File::getConstants(mode), str); + t = instance()->openFile(filename, mode); + } + catch (love::Exception &e) + { + return luax_ioError(L, "%s", e.what()); } - File *t = instance()->newFile(filename); + luax_pushtype(L, t); + t->release(); + return 1; +} - if (mode != File::MODE_CLOSED) +int w_newFile(lua_State* L) +{ + luax_markdeprecated(L, 1, "love.filesystem.newFile", API_FUNCTION, DEPRECATED_RENAMED, "love.filesystem.openFile"); + + const char* filename = luaL_checkstring(L, 1); + + File::Mode mode = File::MODE_CLOSED; + + if (!lua_isnoneornil(L, 2)) { - try - { - if (!t->open(mode)) - throw love::Exception("Could not open file."); - } - catch (love::Exception &e) - { - t->release(); - return luax_ioError(L, "%s", e.what()); - } + const char* modestr = luaL_checkstring(L, 2); + if (!File::getConstant(modestr, mode)) + return luax_enumerror(L, "file open mode", File::getConstants(mode), modestr); + } + + File* t = nullptr; + try + { + t = instance()->openFile(filename, mode); + } + catch (love::Exception& e) + { + return luax_ioError(L, "%s", e.what()); } luax_pushtype(L, t); @@ -268,7 +287,14 @@ File *luax_getfile(lua_State *L, int idx) if (lua_isstring(L, idx)) { const char *filename = luaL_checkstring(L, idx); - file = instance()->newFile(filename); + try + { + file = instance()->openFile(filename, File::MODE_CLOSED); + } + catch (love::Exception &e) + { + luax_ioError(L, "%s", e.what()); + } } else { @@ -663,16 +689,8 @@ int w_lines(lua_State *L) { if (lua_isstring(L, 1)) { - File *file = instance()->newFile(lua_tostring(L, 1)); - bool success = false; - - luax_catchexcept(L, [&](){ success = file->open(File::MODE_READ); }); - - if (!success) - { - file->release(); - return luaL_error(L, "Could not open file."); - } + File *file = nullptr; + luax_catchexcept(L, [&]() { file = instance()->openFile(lua_tostring(L, 1), File::MODE_READ); }); luax_pushtype(L, file); file->release(); @@ -965,7 +983,7 @@ static const luaL_Reg functions[] = { "unmount", w_unmount }, { "unmountFullPath", w_unmountFullPath }, { "unmountCommonPath", w_unmountCommonPath }, - { "newFile", w_newFile }, + { "openFile", w_openFile }, { "getFullCommonPath", w_getFullCommonPath }, { "getWorkingDirectory", w_getWorkingDirectory }, { "getUserDirectory", w_getUserDirectory }, @@ -991,6 +1009,9 @@ static const luaL_Reg functions[] = { "getCRequirePath", w_getCRequirePath }, { "setCRequirePath", w_setCRequirePath }, + // Deprecated + { "newFile", w_newFile }, + { 0, 0 } };