Skip to content

Commit

Permalink
Add love.filesystem.openNativeFile (name may change in the future)
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Dec 29, 2024
1 parent fe23e19 commit aef47df
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/modules/filesystem/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

// LOVE
#include "Filesystem.h"
#include "NativeFile.h"
#include "common/utf8.h"

// Assume POSIX or Visual Studio.
Expand Down Expand Up @@ -67,6 +68,11 @@ bool Filesystem::isAndroidSaveExternal() const
return useExternal;
}

NativeFile *Filesystem::openNativeFile(const char *path, File::Mode mode) const
{
return new NativeFile(path, mode);
}

FileData *Filesystem::newFileData(const void *data, size_t size, const char *filename) const
{
FileData *fd = new FileData(size, std::string(filename));
Expand Down
6 changes: 5 additions & 1 deletion src/modules/filesystem/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace love
namespace filesystem
{

class NativeFile;

class Filesystem : public Module
{
public:
Expand Down Expand Up @@ -173,6 +175,8 @@ class Filesystem : public Module
**/
virtual File *openFile(const char *filename, File::Mode mode) const = 0;

NativeFile *openNativeFile(const char *path, File::Mode mode) const;

/**
* Creates a new FileData object. Data will be copied.
* @param data Pointer to the data.
Expand Down Expand Up @@ -328,7 +332,7 @@ class Filesystem : public Module
bool getRealPathType(const std::string &path, FileType &ftype) const;

// Should we save external or internal for Android
bool useExternal;
bool useExternal = false;

}; // Filesystem

Expand Down
25 changes: 25 additions & 0 deletions src/modules/filesystem/wrap_Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ int w_openFile(lua_State *L)
return 1;
}

int w_openNativeFile(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
const char *modestr = luaL_checkstring(L, 2);

File::Mode mode = File::MODE_CLOSED;
if (!File::getConstant(modestr, mode))
return luax_enumerror(L, "file open mode", File::getConstants(mode), modestr);

File *t = nullptr;
try
{
t = instance()->openNativeFile(path, mode);
}
catch (love::Exception &e)
{
return luax_ioError(L, "%s", e.what());
}

luax_pushtype(L, t);
t->release();
return 1;
}

int w_newFile(lua_State* L)
{
luax_markdeprecated(L, 1, "love.filesystem.newFile", API_FUNCTION, DEPRECATED_RENAMED, "love.filesystem.openFile");
Expand Down Expand Up @@ -1027,6 +1051,7 @@ static const luaL_Reg functions[] =
{ "unmountFullPath", w_unmountFullPath },
{ "unmountCommonPath", w_unmountCommonPath },
{ "openFile", w_openFile },
{ "openNativeFile", w_openNativeFile },
{ "getFullCommonPath", w_getFullCommonPath },
{ "getWorkingDirectory", w_getWorkingDirectory },
{ "getUserDirectory", w_getUserDirectory },
Expand Down

0 comments on commit aef47df

Please sign in to comment.