diff --git a/core/quanta/src/main.cpp b/core/quanta/src/main.cpp index de04781c..e5911b84 100644 --- a/core/quanta/src/main.cpp +++ b/core/quanta/src/main.cpp @@ -8,10 +8,11 @@ int main(int argc, const char* argv[]) { - setlocale(LC_ALL, ""); +#ifdef WIN32 + setlocale(LC_ALL, ".UTF8"); +#endif #if !(defined(__ORBIS__) || defined(__PROSPERO__)) tzset(); - system("echo quanta engine init."); #endif quanta_app q_app; q_app.set_env("QUANTA_DYNAMIC", "1"); diff --git a/core/quanta/src/quanta.cpp b/core/quanta/src/quanta.cpp index 3d6142b3..db664b43 100644 --- a/core/quanta/src/quanta.cpp +++ b/core/quanta/src/quanta.cpp @@ -75,17 +75,6 @@ static void check_input(luakit::kit_state& lua) { #endif } -quanta_app::quanta_app() { - mz_zip_zero_struct(&m_archive); -} - -quanta_app::~quanta_app() { - if (m_archive.m_pState){ - mz_zip_reader_end(&m_archive); - mz_zip_zero_struct(&m_archive); - } -} - void quanta_app::set_signal(uint32_t n, bool b) { uint32_t mask = 1 << n; if (b) { @@ -154,7 +143,6 @@ void quanta_app::load(int argc, const char* argv[]) { if (i == 1){ //加载LUA配置 m_lua.set("platform", get_platform()); - m_lua.set_function("init_zip", [&](std::string zfile) { return initzip(zfile.c_str()); }); m_lua.set_function("set_env", [&](std::string key, std::string value) { return set_env(key, value, 1); }); m_lua.set_function("set_path", [&](std::string field, std::string path) { return set_path(field, path); }); m_lua.run_script(fmt::format("dofile('{}')", argv[1]), [&](std::string_view err) { @@ -177,8 +165,6 @@ luakit::lua_table quanta_app::init() { quanta.set_function("ignore_signal", [](int n) { signal(n, SIG_IGN); }); quanta.set_function("default_signal", [](int n) { signal(n, SIG_DFL); }); quanta.set_function("register_signal", [](int n) { signal(n, on_signal); }); - quanta.set_function("zload", [&](lua_State* L) { return zip_load(L); }); - quanta.set_function("zexist", [&](const char* fn) { return zip_exist(fn); }); quanta.set_function("getenv", [&](const char* key) { return get_env(key); }); quanta.set_function("setenv", [&](std::string key, std::string value) { return set_env(key, value, 1); }); @@ -188,9 +174,6 @@ luakit::lua_table quanta_app::init() { const char* env_service = get_env("QUANTA_SERVICE"); logger::get_logger()->option(env_log_path, env_service, env_index); } -#ifdef WIN32 - m_lua.run_script("os.setlocale('.UTF8')"); -#endif m_lua.run_script(fmt::format("require '{}'", get_env("QUANTA_SANDBOX")), [&](std::string_view err) { exception_handler("load sandbox err: {}", err); }); diff --git a/core/quanta/src/quanta.h b/core/quanta/src/quanta.h index 504dafff..18948260 100644 --- a/core/quanta/src/quanta.h +++ b/core/quanta/src/quanta.h @@ -2,16 +2,11 @@ #include #include "logger.h" -#include "miniz.h" class quanta_app final { public: - quanta_app(); - ~quanta_app(); - void run(); - bool initzip(const char* zfile); void setup(int argc, const char* argv[]); void load(int argc, const char* argv[]); void set_signal(uint32_t n, bool b = true); @@ -26,17 +21,9 @@ class quanta_app final void set_path(std::string field, std::string path); const char* get_env(const char* key); - int zip_load(lua_State* L); - bool zip_exist(const char* fname); - - int load_zip_file(lua_State* L); - int find_zip_file(lua_State* L, std::string filename); - int load_zip_data(lua_State* L, const char* filename, int index); - private: uint64_t m_signal = 0; luakit::kit_state m_lua; - mz_zip_archive m_archive; std::map m_environs; }; diff --git a/extend/lminiz/lminiz.vcxproj b/extend/lminiz/lminiz.vcxproj index d1b8c1d1..773bcc15 100644 --- a/extend/lminiz/lminiz.vcxproj +++ b/extend/lminiz/lminiz.vcxproj @@ -7,8 +7,8 @@ + - diff --git a/extend/lminiz/lminiz.vcxproj.filters b/extend/lminiz/lminiz.vcxproj.filters index 44b31350..f6685ade 100644 --- a/extend/lminiz/lminiz.vcxproj.filters +++ b/extend/lminiz/lminiz.vcxproj.filters @@ -1,10 +1,10 @@  - + src - + src diff --git a/extend/lminiz/src/lminiz.cpp b/extend/lminiz/src/lminiz.cpp index 159b68fb..8aadea46 100644 --- a/extend/lminiz/src/lminiz.cpp +++ b/extend/lminiz/src/lminiz.cpp @@ -1,14 +1,153 @@ #define LUA_LIB -#include "lua_kit.h" -#include "zipfile.h" +#include "miniz.h" +#include "lminiz.h" namespace lminiz { + thread_local mz_zip_archive thread_archive; + + static int find_zip_file(lua_State* L, std::string filename) { + size_t start_pos = 0; + luakit::lua_guard g(L); + lua_getglobal(L, LUA_LOADLIBNAME); + lua_getfield(L, -1, "path"); + std::string path = lua_tostring(L, -1); + while ((start_pos = filename.find(".", start_pos)) != std::string::npos) { + filename.replace(start_pos, strlen("."), LUA_DIRSEP); + start_pos += strlen(LUA_DIRSEP); + } + start_pos = 0; + while ((start_pos = path.find(LUA_PATH_MARK, start_pos)) != std::string::npos) { + path.replace(start_pos, strlen(LUA_PATH_MARK), filename); + start_pos += filename.size(); + } + start_pos = 0; + while ((start_pos = path.find(LUA_DIRSEP, start_pos)) != std::string::npos) { + path.replace(start_pos, strlen(LUA_DIRSEP), "/"); + start_pos += strlen("/"); + } + size_t cur = 0, pos = 0; + while ((pos = path.find(LUA_PATH_SEP, cur)) != std::string::npos) { + std::string sub = path.substr(cur, pos - cur); + int index = mz_zip_reader_locate_file(&thread_archive, sub.c_str(), nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); + if (index > 0) { + return index; + } + cur = pos + strlen(LUA_PATH_SEP); + } + if (path.size() > cur) { + std::string sub = path.substr(cur); + return mz_zip_reader_locate_file(&thread_archive, sub.c_str(), nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); + } + return -1; + } + + bool zip_exist(const char* fname) { + return mz_zip_reader_locate_file(&thread_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE) > 0; + } + + static int zip_load(lua_State* L) { + const char* fname = luaL_optstring(L, 1, nullptr); + int index = mz_zip_reader_locate_file(&thread_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); + if (index <= 0) return 0; + size_t size = 0; + const char* data = (const char*)mz_zip_reader_extract_to_heap(&thread_archive, index, &size, MZ_ZIP_FLAG_CASE_SENSITIVE); + if (!data) return 0; + lua_pushlstring(L, data, size); + delete[] data; + return 1; + } + + static int load_zip_data(lua_State* L, const char* filename, int index) { + size_t size = 0; + const char* data = (const char*)mz_zip_reader_extract_to_heap(&thread_archive, index, &size, MZ_ZIP_FLAG_CASE_SENSITIVE); + if (!data) { + lua_pushstring(L, "file read failed!"); + return LUA_ERRERR; + } + int status = luaL_loadbufferx(L, data, size, filename, luaL_optstring(L, 2, nullptr)); + delete[] data; + return status; + } + + static int load_zip_file(lua_State* L) { + const char* fname = luaL_optstring(L, 1, nullptr); + int index = mz_zip_reader_locate_file(&thread_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); + if (index <= 0) { + luaL_Buffer buf; + luaL_buffinit(L, &buf); + luaL_addstring(&buf, fname); + luaL_addstring(&buf, " not found in zip"); + luaL_pushresult(&buf); + return LUA_ERRERR; + } + return load_zip_data(L, fname, index); + } + + static void close_zip() { + if (thread_archive.m_pState) { + mz_zip_reader_end(&thread_archive); + mz_zip_zero_struct(&thread_archive); + } + } + + bool init_zip(lua_State* L, const char* zfile) { + close_zip(); + if (!mz_zip_reader_init_file(&thread_archive, zfile, 0)) { + return false; + } + luakit::kit_state lua(L); + lua.set_searchers([&](lua_State* L) { + const char* fname = luaL_checkstring(L, 1); + int index = find_zip_file(L, fname); + if (index < 0) { + luaL_Buffer buf; + luaL_buffinit(L, &buf); + luaL_addstring(&buf, fname); + luaL_addstring(&buf, " not found in zip"); + luaL_pushresult(&buf); + return 1; + } + if (load_zip_data(L, fname, index) == LUA_OK) { + lua_pushstring(L, fname); /* will be 2nd argument to module */ + return 2; /* return open function and file name */ + } + return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", lua_tostring(L, 1), fname, lua_tostring(L, -1)); + }); + lua.set_function("dofile", [&](lua_State* L) { + const char* fname = luaL_optstring(L, 1, NULL); + lua_settop(L, 1); + if (load_zip_file(L) != LUA_OK) { + return lua_error(L); + } + auto kf = [](lua_State* L, int d1, lua_KContext d2) { return lua_gettop(L) - 1; }; + lua_callk(L, 0, LUA_MULTRET, 0, kf); + return kf(L, 0, 0); + }); + lua.set_function("loadfile", [&](lua_State* L) { + int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ + if (load_zip_file(L) == LUA_OK) { + if (env != 0) { /* 'env' parameter? */ + lua_pushvalue(L, env); /* environment for loaded function */ + if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ + lua_pop(L, 1); /* remove 'env' if not used by previous call */ + } + return 1; + } + //error(message is on top of the stack)* / + lua_pushnil(L); + lua_insert(L, -2); + return 2; + }); + return true; + } + luakit::lua_table open_lminiz(lua_State* L) { luakit::kit_state kit_state(L); - luakit::lua_table miniz = kit_state.new_table("lminiz"); - miniz.set_function("open", open_xcel); + luakit::lua_table miniz = kit_state.new_table("zip"); + miniz.set_function("exist", zip_exist); + miniz.set_function("load", [](lua_State* L) { return zip_load(L); }); return miniz; } } @@ -18,4 +157,13 @@ extern "C" { auto miniz = lminiz::open_lminiz(L); return miniz.push_stack(); } + + LUALIB_API bool lua_initzip(lua_State* L, const char* zfile) { + return lminiz::init_zip(L, zfile); + } + + LUALIB_API void lua_closezip() { + lminiz::close_zip(); + } + } diff --git a/extend/lminiz/src/lminiz.h b/extend/lminiz/src/lminiz.h new file mode 100644 index 00000000..f5cf1a1c --- /dev/null +++ b/extend/lminiz/src/lminiz.h @@ -0,0 +1,10 @@ +#pragma once + +#include "lua_kit.h" + +extern "C" { + + LUALIB_API bool lua_initzip(lua_State* L, const char* zfile); + + LUALIB_API void lua_closezip(); +} diff --git a/extend/lminiz/src/zipfile.h b/extend/lminiz/src/zipfile.h deleted file mode 100644 index 56acac61..00000000 --- a/extend/lminiz/src/zipfile.h +++ /dev/null @@ -1,142 +0,0 @@ -#pragma once - -#include -#include "miniz.h" -#include "lua_kit.h" - -using namespace std; - -namespace lminiz -{ - class zip_file - { - public: - ~zip_file() { - mz_zip_reader_end(&m_archive); - } - - bool initzip(lua_State* L, const char* zfile) { - memset(&m_archive, 0, sizeof(m_archive)); - if (!mz_zip_reader_init_file(&m_archive, zfile, 0)) { - return false; - } - luakit::kit_state lua(L); - lua.set_searchers([&](lua_State* L) { - const char* fname = luaL_checkstring(L, 1); - int index = find_zip_file(L, fname); - if (index < 0) { - lua_pushstring(L, fmt::format("file {} not found in zip!", fname).c_str()); - return 1; - } - if (load_zip_data(L, fname, index) == LUA_OK) { - lua_pushstring(L, fname); /* will be 2nd argument to module */ - return 2; /* return open function and file name */ - } - return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", lua_tostring(L, 1), fname, lua_tostring(L, -1)); - }); - lua.set_function("dofile", [&](lua_State* L) { - const char* fname = luaL_optstring(L, 1, NULL); - lua_settop(L, 1); - if (load_zip_file(L) != LUA_OK) { - return lua_error(L); - } - auto kf = [](lua_State* L, int d1, lua_KContext d2) { return lua_gettop(L) - 1; }; - lua_callk(L, 0, LUA_MULTRET, 0, kf); - return kf(L, 0, 0); - }); - lua.set_function("loadfile", [&](lua_State* L) { - int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - if (load_zip_file(L) == LUA_OK) { - if (env != 0) { /* 'env' parameter? */ - lua_pushvalue(L, env); /* environment for loaded function */ - if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ - lua_pop(L, 1); /* remove 'env' if not used by previous call */ - } - return 1; - } - //error(message is on top of the stack)* / - lua_pushnil(L); - lua_insert(L, -2); - return 2; - }); - return true; - } - private: - int find_zip_file(lua_State* L, std::string filename) { - size_t start_pos = 0; - luakit::lua_guard g(L); - lua_getglobal(L, LUA_LOADLIBNAME); - lua_getfield(L, -1, "path"); - std::string path = lua_tostring(L, -1); - while ((start_pos = filename.find(".", start_pos)) != std::string::npos) { - filename.replace(start_pos, strlen("."), LUA_DIRSEP); - start_pos += strlen(LUA_DIRSEP); - } - start_pos = 0; - while ((start_pos = path.find(LUA_PATH_MARK, start_pos)) != std::string::npos) { - path.replace(start_pos, strlen(LUA_PATH_MARK), filename); - start_pos += filename.size(); - } - start_pos = 0; - while ((start_pos = path.find(LUA_DIRSEP, start_pos)) != std::string::npos) { - path.replace(start_pos, strlen(LUA_DIRSEP), "/"); - start_pos += strlen("/"); - } - size_t cur = 0, pos = 0; - while ((pos = path.find(LUA_PATH_SEP, cur)) != std::string::npos) { - std::string sub = path.substr(cur, pos - cur); - int index = mz_zip_reader_locate_file(&m_archive, sub.c_str(), nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); - if (index > 0) { - return index; - } - cur = pos + strlen(LUA_PATH_SEP); - } - if (path.size() > cur) { - std::string sub = path.substr(cur); - return mz_zip_reader_locate_file(&m_archive, sub.c_str(), nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); - } - return -1; - } - - bool zip_exist(const char* fname) { - return mz_zip_reader_locate_file(&m_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE) > 0; - } - - int zip_load(lua_State* L) { - const char* fname = luaL_optstring(L, 1, nullptr); - int index = mz_zip_reader_locate_file(&m_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); - if (index <= 0) return 0; - size_t size = 0; - const char* data = (const char*)mz_zip_reader_extract_to_heap(&m_archive, index, &size, MZ_ZIP_FLAG_CASE_SENSITIVE); - if (!data) return 0; - lua_pushlstring(L, data, size); - delete[] data; - return 1; - } - - int load_zip_file(lua_State* L) { - const char* fname = luaL_optstring(L, 1, nullptr); - int index = mz_zip_reader_locate_file(&m_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE); - if (index <= 0) { - lua_pushstring(L, fmt::format("file {} not found in zip!", fname).c_str()); - return LUA_ERRERR; - } - return load_zip_data(L, fname, index); - } - - int load_zip_data(lua_State* L, const char* filename, int index) { - size_t size = 0; - const char* data = (const char*)mz_zip_reader_extract_to_heap(&m_archive, index, &size, MZ_ZIP_FLAG_CASE_SENSITIVE); - if (!data) { - lua_pushstring(L, "file read failed!"); - return LUA_ERRERR; - } - int status = luaL_loadbufferx(L, data, size, filename, luaL_optstring(L, 2, nullptr)); - delete[] data; - return status; - } - - private: - mz_zip_archive m_archive; - }; -} diff --git a/extend/luaxlsx/luaxlsx.vcxproj b/extend/luaxlsx/luaxlsx.vcxproj index daa43c27..c50a36b2 100644 --- a/extend/luaxlsx/luaxlsx.vcxproj +++ b/extend/luaxlsx/luaxlsx.vcxproj @@ -7,8 +7,8 @@ + - diff --git a/extend/luaxlsx/luaxlsx.vcxproj.filters b/extend/luaxlsx/luaxlsx.vcxproj.filters index b80f83fc..e19b1227 100644 --- a/extend/luaxlsx/luaxlsx.vcxproj.filters +++ b/extend/luaxlsx/luaxlsx.vcxproj.filters @@ -1,10 +1,10 @@  - + ../lminiz/src - + ../lminiz/src diff --git a/script/basic/library.lua b/script/basic/library.lua index a0cc42b7..4d2cbccb 100644 --- a/script/basic/library.lua +++ b/script/basic/library.lua @@ -36,7 +36,9 @@ if qgetenv("QUANTA_DYNAMIC") then require("lsqlite") --lmdb require("lmdb") - --lmdb + --lminiz + require("lminiz") + --luaxml require("luaxml") end end diff --git a/script/kernel/protobuf_mgr.lua b/script/kernel/protobuf_mgr.lua index 4b1c0919..5cf4d3da 100644 --- a/script/kernel/protobuf_mgr.lua +++ b/script/kernel/protobuf_mgr.lua @@ -77,7 +77,7 @@ function ProtobufMgr:load_protos() local proto_file = env_get("QUANTA_PROTO_FILE") if proto_file then if env_get("QUANTA_ZIP_MODE") then - protobuf.load(quanta.zload(proto_file)) + protobuf.load(zip.zload(proto_file)) log_debug("[ProtobufMgr][load_protos] load zip pb file: {}", proto_file) else protobuf.loadfile(proto_file) diff --git a/script/sandbox.lua b/script/sandbox.lua index 3beaf3c4..441fc8bb 100644 --- a/script/sandbox.lua +++ b/script/sandbox.lua @@ -10,7 +10,6 @@ local sgsub = string.gsub local sformat = string.format local traceback = debug.traceback local qgetenv = quanta.getenv -local zexist = quanta.zexist local LOG_LEVEL = log.LOG_LEVEL local QUANTA_ZIP = qgetenv("QUANTA_ZIP_MODE") @@ -66,7 +65,7 @@ end local function fexist(fname) if QUANTA_ZIP then - return zexist(fname) + return zip.zexist(fname) end local file = iopen(fname) if file then diff --git a/server/test/xml_test.lua b/server/test/xml_test.lua index 6f8bd094..4676c5f9 100644 --- a/server/test/xml_test.lua +++ b/server/test/xml_test.lua @@ -3,7 +3,7 @@ local log_dump = logger.dump -local xml = [[ +local cxml = [[ @@ -100,7 +100,7 @@ local xml = [[ ]] -local xlua, err = xml.load(xml) +local xlua, err = xml.load(cxml) -log_dump("luaxml load err: {}, xml:{}", err, xlua) +log_dump("luaxml 中国 load err: {}, xml:{}", err, xlua)