From d587c206f0252141c1a2c17da23a85c5ecaff0c4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 3 Oct 2015 01:49:12 -0300 Subject: [PATCH] Added a new variant love.math.decompress(data, format), where data is any Data object. --- src/modules/math/Compressor.h | 3 --- src/modules/math/wrap_Math.cpp | 29 +++++++++++++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/modules/math/Compressor.h b/src/modules/math/Compressor.h index bb879e8e1..de71bd141 100644 --- a/src/modules/math/Compressor.h +++ b/src/modules/math/Compressor.h @@ -24,9 +24,6 @@ // LOVE #include "common/StringMap.h" -// C++ -#include - namespace love { namespace math diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 7bc4ae93e..34381d0fe 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -358,7 +358,7 @@ int w_compress(lua_State *L) Compressor::Format format = Compressor::FORMAT_LZ4; if (fstr && !Compressor::getConstant(fstr, format)) - return luaL_error(L, "Invalid compressed format: %s", fstr); + return luaL_error(L, "Invalid compressed data format: %s", fstr); int level = (int) luaL_optnumber(L, 3, -1); @@ -384,25 +384,34 @@ int w_decompress(lua_State *L) char *rawbytes = nullptr; size_t rawsize = 0; - if (lua_isstring(L, 1)) + if (luax_istype(L, 1, MATH_COMPRESSED_DATA_ID)) + { + CompressedData *data = luax_checkcompresseddata(L, 1); + rawsize = data->getDecompressedSize(); + luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(data, rawsize); }); + } + else { Compressor::Format format = Compressor::FORMAT_LZ4; const char *fstr = luaL_checkstring(L, 2); if (!Compressor::getConstant(fstr, format)) - return luaL_error(L, "Invalid compressed format: %s", fstr); + return luaL_error(L, "Invalid compressed data format: %s", fstr); size_t compressedsize = 0; - const char *cbytes = luaL_checklstring(L, 1, &compressedsize); + const char *cbytes = nullptr; + + if (luax_istype(L, 1, DATA_ID)) + { + Data *data = luax_checktype(L, 1, DATA_ID); + cbytes = (const char *) data->getData(); + compressedsize = data->getSize(); + } + else + cbytes = luaL_checklstring(L, 1, &compressedsize); luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(format, cbytes, compressedsize, rawsize); }); } - else - { - CompressedData *data = luax_checkcompresseddata(L, 1); - rawsize = data->getDecompressedSize(); - luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(data, rawsize); }); - } lua_pushlstring(L, rawbytes, rawsize); delete[] rawbytes;