diff --git a/src/modules/data/wrap_DataModule.cpp b/src/modules/data/wrap_DataModule.cpp index 9eb6f7956..e41b72690 100644 --- a/src/modules/data/wrap_DataModule.cpp +++ b/src/modules/data/wrap_DataModule.cpp @@ -321,7 +321,7 @@ int w_hash(lua_State *L) return 1; } -static int w_pack(lua_State *L) +int w_pack(lua_State *L) { ContainerType ctype = luax_checkcontainertype(L, 1); const char *fmt = luaL_checkstring(L, 2); diff --git a/src/modules/filesystem/wrap_File.cpp b/src/modules/filesystem/wrap_File.cpp index 0f3341bd1..f64a72e01 100644 --- a/src/modules/filesystem/wrap_File.cpp +++ b/src/modules/filesystem/wrap_File.cpp @@ -24,6 +24,8 @@ #include "common/Exception.h" #include "common/int.h" +#include "data/wrap_DataModule.h" + namespace love { namespace filesystem @@ -108,9 +110,18 @@ int w_File_isOpen(lua_State *L) int w_File_read(lua_State *L) { File *file = luax_checkfile(L, 1); - StrongRef d = nullptr; + StrongRef d = nullptr; - int64 size = (int64) luaL_optnumber(L, 2, (lua_Number) File::ALL); + love::data::ContainerType ctype = love::data::CONTAINER_STRING; + + int startidx = 2; + if (lua_type(L, 2) == LUA_TSTRING) + { + ctype = love::data::luax_checkcontainertype(L, 2); + startidx = 3; + } + + int64 size = (int64) luaL_optnumber(L, startidx, (lua_Number) File::ALL); try { @@ -121,9 +132,21 @@ int w_File_read(lua_State *L) return luax_ioError(L, "%s", e.what()); } - lua_pushlstring(L, (const char *) d->getData(), d->getSize()); - lua_pushnumber(L, d->getSize()); - return 2; + int nret = 0; + + if (ctype == love::data::CONTAINER_DATA) + { + luax_pushtype(L, d.get()); + nret = 1; + } + else + { + lua_pushlstring(L, (const char *) d->getData(), d->getSize()); + lua_pushinteger(L, d->getSize()); + nret = 2; + } + + return nret; } int w_File_write(lua_State *L) diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index 5463dff64..113e2d6e5 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -25,6 +25,7 @@ #include "wrap_DroppedFile.h" #include "wrap_FileData.h" #include "data/wrap_Data.h" +#include "data/wrap_DataModule.h" #include "physfs/Filesystem.h" @@ -445,10 +446,19 @@ int w_remove(lua_State *L) int w_read(lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - int64 len = (int64) luaL_optinteger(L, 2, File::ALL); + love::data::ContainerType ctype = love::data::CONTAINER_STRING; + int startidx = 1; - Data *data = nullptr; + if (lua_type(L, 2) == LUA_TSTRING) + { + ctype = love::data::luax_checkcontainertype(L, 1); + startidx = 2; + } + + const char *filename = luaL_checkstring(L, startidx + 0); + int64 len = (int64) luaL_optinteger(L, startidx + 1, File::ALL); + + FileData *data = nullptr; try { data = instance()->read(filename, len); @@ -461,16 +471,24 @@ int w_read(lua_State *L) if (data == nullptr) return luax_ioError(L, "File could not be read."); - // Push the string. - lua_pushlstring(L, (const char *) data->getData(), data->getSize()); + int nret = 0; - // Push the size. - lua_pushinteger(L, data->getSize()); + if (ctype == love::data::CONTAINER_DATA) + { + luax_pushtype(L, data); + nret = 1; + } + else + { + lua_pushlstring(L, (const char *) data->getData(), data->getSize()); + lua_pushinteger(L, data->getSize()); + nret = 2; + } // Lua has a copy now, so we can free it. data->release(); - return 2; + return nret; } static int w_write_or_append(lua_State *L, File::Mode mode)