1
0
mirror of https://github.com/0O0o0oOoO00/Alas.git synced 2026-05-14 11:49:26 +08:00

add: load cracker after lua state paused

This commit is contained in:
0O0o0oOoO00
2025-11-20 18:09:00 +08:00
parent eec19dc03a
commit b8cc8c1258
4 changed files with 91 additions and 20 deletions

View File

@@ -42,6 +42,50 @@ static fnset_TimeScaleT get_Time_set_timeScale() {
return fn;
}
std::atomic_bool g_pause_flag = ATOMIC_FLAG_INIT;
std::atomic_bool g_loaded_flag = ATOMIC_FLAG_INIT;
extern bool g_is_game_loaded;
void cracker_loaded() {
g_loaded_flag = true;
g_loaded_flag.notify_all();
}
void wait_pause() {
g_pause_flag.wait(false);
}
void lua_paused() {
g_pause_flag = true;
g_pause_flag.notify_all();
}
void wait_cracker_loaded() {
g_loaded_flag.wait(false);
}
void debug_hook(lua_State *L, lua_Debug *ar) {
if (g_is_game_loaded) {
SPDLOG_INFO("pause triggered, notify to load cracker");
lua_paused();
wait_cracker_loaded();
}
}
void setup_pause_hook(lua_State *L) {
int ret = lua_sethook(L, debug_hook, LUA_MASKCALL, 0);
if (ret != 1) {
SPDLOG_INFO("set pause hook failed with code: {}", ret);
}
}
void remove_pause_hook(lua_State *L) {
int ret = lua_sethook(L, nullptr, 0, 0);
if (ret != 1) {
SPDLOG_INFO("remove pause hook failed with code: {}", ret);
}
}
#define IS_ENABLED(f) m_flag.f.load()
#define ENABLE(n) \
@@ -57,16 +101,18 @@ static fnset_TimeScaleT get_Time_set_timeScale() {
SPDLOG_INFO("{} called", #f); \
}
Cracker::Cracker()
Cracker::Cracker(bool need_pause)
: m_state([]() -> lua_State* {
auto new_tr = lua_newthread(Utils::get_lua_state());
SPDLOG_INFO("new lua_State: {}", (void*)new_tr);
return new_tr;
}()) {
try {
load_lua_resources();
hook_all_lua_functions();
load_real_lua_func();
if (need_pause) {
load_cracker_with_pause();
} else {
load_cracker_without_pause();
}
} catch(std::exception& e) {
SPDLOG_ERROR("Cracker load error: {}", e.what());
exit(-1);
@@ -81,8 +127,8 @@ Cracker& Cracker::Instance() {
return *instance;
}
#else
Cracker& Cracker::Instance() {
static auto* instance = new Cracker;
Cracker& Cracker::Instance(bool need_pause) {
static auto* instance = new Cracker(need_pause);
return *instance;
}
#endif
@@ -1581,6 +1627,24 @@ void Cracker::apply_config(Config& config) {
}
}
void Cracker::load_cracker_with_pause() {
SPDLOG_INFO("Load cracker with lua pause");
setup_pause_hook(Utils::get_lua_state());
wait_pause();
load_lua_resources();
hook_all_lua_functions();
load_real_lua_func();
remove_pause_hook(Utils::get_lua_state());
cracker_loaded();
}
void Cracker::load_cracker_without_pause() {
SPDLOG_INFO("Load cracker directly");
load_lua_resources();
hook_all_lua_functions();
load_real_lua_func();
}
void Cracker::load_real_lua_func() {
m_real_func.Chapter_IsAutoFight = m_state["Chapter"]["IsAutoFight"];
m_real_func.LevelStageView_tryAutoTrigger = m_state["LevelStageView"]["tryAutoTrigger"];