Browse Source

Add optional load mode parameter to love.filesystem.load.

Miku AuahDark 2 years ago
parent
commit
c35ceeb223

+ 1 - 0
changes.txt

@@ -9,6 +9,7 @@ Released: N/A
 * Added love.filesystem.mountFullPath and love.filesystem.unmountFullPath, including opt-in mount-for-write support.
 * Added love.filesystem.mountCommonPath, unmountCommonPath, and getFullCommonPath.
 * Added 'readonly' field to love.filesystem.getInfo's returned table.
+* Added an optional load mode parameter to love.filesystem.load whetever to only allow binary chunks, text chunks, or both.
 * Added SoundData:copyFrom.
 * Added SoundData:slice.
 * Added Joystick:setPlayerIndex and Joystick:getPlayerIndex.

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

@@ -221,5 +221,13 @@ STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::MountPermissions, Filesystem::MOUN
 }
 STRINGMAP_CLASS_END(Filesystem, Filesystem::MountPermissions, Filesystem::MOUNT_PERMISSIONS_MAX_ENUM, mountPermissions)
 
+STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::LoadMode, Filesystem::LOADMODE_MAX_ENUM, loadMode)
+{
+	{ "b",  Filesystem::LOADMODE_BINARY},
+	{ "t",  Filesystem::LOADMODE_TEXT  },
+	{ "bt", Filesystem::LOADMODE_ANY   }
+}
+STRINGMAP_CLASS_END(Filesystem, Filesystem::LoadMode, Filesystem::LOADMODE_MAX_ENUM, loadMode)
+
 } // filesystem
 } // love

+ 9 - 0
src/modules/filesystem/Filesystem.h

@@ -89,6 +89,14 @@ public:
 		MOUNT_PERMISSIONS_MAX_ENUM
 	};
 
+	enum LoadMode
+	{
+		LOADMODE_BINARY,
+		LOADMODE_TEXT,
+		LOADMODE_ANY,
+		LOADMODE_MAX_ENUM
+	};
+
 	struct Info
 	{
 		// Numbers will be -1 if they cannot be determined.
@@ -307,6 +315,7 @@ public:
 	STRINGMAP_CLASS_DECLARE(FileType);
 	STRINGMAP_CLASS_DECLARE(CommonPath);
 	STRINGMAP_CLASS_DECLARE(MountPermissions);
+	STRINGMAP_CLASS_DECLARE(LoadMode);
 
 private:
 

+ 28 - 1
src/modules/filesystem/wrap_Filesystem.cpp

@@ -711,6 +711,16 @@ int w_load(lua_State *L)
 {
 	std::string filename = std::string(luaL_checkstring(L, 1));
 
+	Filesystem::LoadMode loadMode = Filesystem::LOADMODE_ANY;
+
+	if (!lua_isnoneornil(L, 2))
+	{
+		const char *mode = luaL_checkstring(L, 2);
+
+		if (!Filesystem::getConstant(mode, loadMode))
+			return luax_enumerror(L, "load mode", Filesystem::getConstants(loadMode), mode);
+	}
+
 	Data *data = nullptr;
 	try
 	{
@@ -721,7 +731,24 @@ int w_load(lua_State *L)
 		return luax_ioError(L, "%s", e.what());
 	}
 
-	int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
+	int status;
+
+#if (LUA_VERSION_NUM > 501) || defined(LUA_JITLIBNAME)
+	// LuaJIT support this Lua 5.2 function.
+	const char *mode;
+	Filesystem::getConstant(loadMode, mode);
+
+	status = luaL_loadbufferx(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str(), mode);
+#else
+	if (loadMode == Filesystem::LOADMODE_ANY)
+		status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
+	else
+	{
+		// Unsupported
+		data->release();
+		return luaL_error(L, "only \"bt\" is supported on this Lua interpreter\n");
+	}
+#endif
 
 	data->release();