Skip to content

Commit

Permalink
若干代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed May 5, 2024
1 parent 6d60019 commit 3fa885c
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 191 deletions.
5 changes: 3 additions & 2 deletions core/quanta/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
17 changes: 0 additions & 17 deletions core/quanta/src/quanta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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); });

Expand All @@ -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);
});
Expand Down
13 changes: 0 additions & 13 deletions core/quanta/src/quanta.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
#include <map>

#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);
Expand All @@ -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<std::string, std::string> m_environs;
};

Expand Down
2 changes: 1 addition & 1 deletion extend/lminiz/lminiz.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\lminiz.h" />
<ClInclude Include="src\miniz.h" />
<ClInclude Include="src\zipfile.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\lminiz.cpp" />
Expand Down
4 changes: 2 additions & 2 deletions extend/lminiz/lminiz.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="src\miniz.h">
<ClInclude Include="src\lminiz.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\zipfile.h">
<ClInclude Include="src\miniz.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
Expand Down
156 changes: 152 additions & 4 deletions extend/lminiz/src/lminiz.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
Expand All @@ -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();
}

}
10 changes: 10 additions & 0 deletions extend/lminiz/src/lminiz.h
Original file line number Diff line number Diff line change
@@ -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();
}
Loading

0 comments on commit 3fa885c

Please sign in to comment.