Skip to content

Commit 0817af7

Browse files
committed
LoadCompressedTextFile
1 parent c1bd82b commit 0817af7

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

LuaSTG/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ set(LUASTG_ENGINE_SOURCES
146146
LuaSTG/AppFrameLua.cpp
147147
LuaSTG/AppFrameRender.cpp
148148
LuaSTG/AppFrameRenderEx.cpp
149+
LuaSTG/AppFrameFileEx.cpp
149150
LuaSTG/LConfig.h
150151
LuaSTG/LMathConstant.hpp
151152
LuaSTG/Main.cpp

LuaSTG/LuaSTG/AppFrame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ namespace LuaSTGPlus
195195
// Read a text file from a resource package.
196196
// It is possible to read other files, but you may get meaningless results.
197197
int LoadTextFile(lua_State* L, const char* path, const char *packname) noexcept;
198+
int LoadCompressedTextFile(lua_State* L, const char* path, const char *packname) noexcept;
198199

199200
// TODO: LoadBinaryFile?
200201

LuaSTG/LuaSTG/AppFrameFileEx.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "stb_image.h"
2+
3+
#include "AppFrame.h"
4+
#include "Core/FileManager.hpp"
5+
6+
using namespace LuaSTGPlus;
7+
8+
int AppFrame::LoadCompressedTextFile(lua_State* L_, const char* path, const char *packname)noexcept
9+
{
10+
if (ResourceMgr::GetResourceLoadingLog()) {
11+
if (packname)
12+
spdlog::info("[luastg] Reading compressed text file '{}' in package '{}'", packname, path);
13+
else
14+
spdlog::info("[luastg] Reading compressed text file '{}'", path);
15+
}
16+
bool loaded = false;
17+
std::vector<uint8_t> src, decompressed;
18+
if (packname)
19+
{
20+
auto& arc = GFileManager().getFileArchive(packname);
21+
if (!arc.empty())
22+
{
23+
loaded = arc.load(path, src);
24+
}
25+
}
26+
else
27+
{
28+
loaded = GFileManager().loadEx(path, src);
29+
}
30+
if (!loaded) {
31+
spdlog::error("[luastg] Unable to load file '{}'", path);
32+
return 0;
33+
}
34+
int decompressed_len;
35+
char* decompressed_buf = stbi_zlib_decode_malloc((char*)src.data(), src.size(), &decompressed_len);
36+
if (!decompressed_buf) {
37+
spdlog::error("[luastg] Unable to allocate buffer for compressed text file '{}'", path);
38+
return 0;
39+
}
40+
if (stbi_zlib_decode_buffer(decompressed_buf, decompressed_len, (char*)src.data(), src.size()) == -1) {
41+
spdlog::error("[luastg] Unable to decompress text file '{}'", path);
42+
return 0;
43+
}
44+
lua_pushlstring(L_, decompressed_buf, decompressed_len);
45+
return 1;
46+
}

LuaSTG/LuaSTG/LuaBinding/LW_LuaSTG.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ void LuaSTGPlus::BuiltInFunctionWrapper::Register(lua_State* L)noexcept
9797
static int LoadTextFile(lua_State* L)noexcept
9898
{
9999
return LAPP.LoadTextFile(L, luaL_checkstring(L, 1), luaL_optstring(L, 2, NULL));
100+
101+
}
102+
static int LoadCompressedTextFile(lua_State* L)noexcept
103+
{
104+
return LAPP.LoadCompressedTextFile(L, luaL_checkstring(L, 1), luaL_optstring(L, 2, NULL));
100105
}
101106
#pragma endregion
102107

@@ -256,6 +261,7 @@ void LuaSTGPlus::BuiltInFunctionWrapper::Register(lua_State* L)noexcept
256261
{ "Log", &WrapperImplement::Log },
257262
{ "DoFile", &WrapperImplement::DoFile },
258263
{ "LoadTextFile", &WrapperImplement::LoadTextFile },
264+
{ "LoadCompressedTextFile", &WrapperImplement::LoadCompressedTextFile },
259265
#pragma endregion
260266

261267
#pragma region 窗口与交换链控制函数

0 commit comments

Comments
 (0)