Browse Source

Add love.filesystem.openNativeFile (name may change in the future)

Sasha Szpakowski 7 months ago
parent
commit
aef47df40c

+ 6 - 0
src/modules/filesystem/Filesystem.cpp

@@ -20,6 +20,7 @@
 
 // LOVE
 #include "Filesystem.h"
+#include "NativeFile.h"
 #include "common/utf8.h"
 
 // Assume POSIX or Visual Studio.
@@ -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));

+ 5 - 1
src/modules/filesystem/Filesystem.h

@@ -58,6 +58,8 @@ namespace love
 namespace filesystem
 {
 
+class NativeFile;
+
 class Filesystem : public Module
 {
 public:
@@ -173,6 +175,8 @@ public:
 	 **/
 	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.
@@ -328,7 +332,7 @@ private:
 	bool getRealPathType(const std::string &path, FileType &ftype) const;
 
 	// Should we save external or internal for Android
-	bool useExternal;
+	bool useExternal = false;
 
 }; // Filesystem
 

+ 25 - 0
src/modules/filesystem/wrap_Filesystem.cpp

@@ -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");
@@ -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 },