From 7a057fba91ab06424c616605a3ea2a9e8a235c12 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Fri, 2 May 2025 18:10:59 -0500 Subject: [PATCH 1/6] `Core.cpp`: remove unnecessary casts `std::thread`'s constructor is a template, no reason to type-erase here --- library/Core.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index d949db688c..bfaa30a034 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -269,10 +269,10 @@ struct IODATA // A thread function... for handling hotkeys. This is needed because // all the plugin commands are expected to be run from foreign threads. // Running them from one of the main DF threads will result in deadlock! -void fHKthread(void * iodata) +static void fHKthread(IODATA * iodata) { - Core * core = ((IODATA*) iodata)->core; - PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr; + Core * core = iodata->core; + PluginManager * plug_mgr = iodata->plug_mgr; if(plug_mgr == 0 || core == 0) { std::cerr << "Hotkey thread has croaked." << std::endl; @@ -1874,7 +1874,7 @@ bool Core::InitSimulationThread() std::cerr << "Starting DF input capture thread.\n"; // set up hotkey capture - d->hotkeythread = std::thread(fHKthread, (void *) temp); + d->hotkeythread = std::thread(fHKthread, temp); started = true; modstate = 0; From 5040c012f3f90a78183708c508dc8fb8bdd5f30a Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Sat, 3 May 2025 01:59:06 -0500 Subject: [PATCH 2/6] remove type erasure from `RunCoreQueryLoop` --- library/Core.cpp | 45 ++++++++++++-------------------------- library/LuaTools.cpp | 27 +++++++---------------- library/include/LuaTools.h | 6 ++--- 3 files changed, 25 insertions(+), 53 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index bfaa30a034..f2f965a7de 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -323,62 +323,45 @@ static std::string dfhack_version_desc() return s.str(); } -namespace { - struct ScriptArgs { - const std::string *pcmd; - std::vector *pargs; - }; - struct ScriptEnableState { - const std::string *pcmd; - bool pstate; - }; -} - -static bool init_run_script(color_ostream &out, lua_State *state, void *info) +static bool init_run_script(color_ostream &out, lua_State *state, const std::string& pcmd, std::vector& pargs) { - auto args = (ScriptArgs*)info; - if (!lua_checkstack(state, args->pargs->size()+10)) + if (!lua_checkstack(state, pargs.size()+10)) return false; Lua::PushDFHack(state); lua_getfield(state, -1, "run_script"); lua_remove(state, -2); - lua_pushstring(state, args->pcmd->c_str()); - for (size_t i = 0; i < args->pargs->size(); i++) - lua_pushstring(state, (*args->pargs)[i].c_str()); + lua_pushstring(state, pcmd.c_str()); + for (auto& arg : pargs) + lua_pushstring(state, arg.c_str()); return true; } static command_result runLuaScript(color_ostream &out, std::string name, std::vector &args) { - ScriptArgs data; - data.pcmd = &name; - data.pargs = &args; - - bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(true), init_run_script, &data); + using namespace std::placeholders; + auto init_fn = std::bind(init_run_script, _1, _2, name, args); + bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(true), init_fn); return ok ? CR_OK : CR_FAILURE; } -static bool init_enable_script(color_ostream &out, lua_State *state, void *info) +static bool init_enable_script(color_ostream &out, lua_State *state, std::string& name, bool enable) { - auto args = (ScriptEnableState*)info; if (!lua_checkstack(state, 4)) return false; Lua::PushDFHack(state); lua_getfield(state, -1, "enable_script"); lua_remove(state, -2); - lua_pushstring(state, args->pcmd->c_str()); - lua_pushboolean(state, args->pstate); + lua_pushstring(state, name.c_str()); + lua_pushboolean(state, enable); return true; } static command_result enableLuaScript(color_ostream &out, std::string name, bool state) { - ScriptEnableState data; - data.pcmd = &name; - data.pstate = state; - - bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(), init_enable_script, &data); + using namespace std::placeholders; + auto init_fn = std::bind(init_enable_script, _1, _2, name, state); + bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(), init_fn); return ok ? CR_OK : CR_FAILURE; } diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 2d9df61ab4..95f8017dd8 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -1193,9 +1193,7 @@ static int resume_query_loop(color_ostream &out, return rv; } -bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, - bool (*init)(color_ostream&, lua_State*, void*), - void *arg) +bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, DFHack::Lua::init_fn init) { if (!lua_checkstack(state, 20)) return false; @@ -1213,7 +1211,7 @@ bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, int base = lua_gettop(state); - if (!init(out, state, arg)) + if (!init(out, state)) { lua_settop(state, base); return false; @@ -1283,21 +1281,13 @@ bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, return (rv == LUA_OK); } -namespace { - struct InterpreterArgs { - const char *prompt; - const char *hfile; - }; -} - -static bool init_interpreter(color_ostream &out, lua_State *state, void *info) +static bool init_interpreter(color_ostream &out, lua_State *state, const char* prompt, const char* hfile) { - auto args = (InterpreterArgs*)info; lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_DFHACK_TOKEN); lua_getfield(state, -1, "interpreter"); lua_remove(state, -2); - lua_pushstring(state, args->prompt); - lua_pushstring(state, args->hfile); + lua_pushstring(state, prompt); + lua_pushstring(state, hfile); return true; } @@ -1312,11 +1302,10 @@ bool DFHack::Lua::InterpreterLoop(color_ostream &out, lua_State *state, if (!prompt) prompt = "lua"; - InterpreterArgs args; - args.prompt = prompt; - args.hfile = hfile; + using namespace std::placeholders; + auto init_fn = std::bind(init_interpreter, _1, _2, prompt, hfile); - return RunCoreQueryLoop(out, state, init_interpreter, &args); + return RunCoreQueryLoop(out, state, init_fn); } static bool do_invoke_cleanup(lua_State *L, int nargs, int errorfun, bool success) diff --git a/library/include/LuaTools.h b/library/include/LuaTools.h index 8b71862ec3..e5a83721cc 100644 --- a/library/include/LuaTools.h +++ b/library/include/LuaTools.h @@ -286,9 +286,9 @@ namespace DFHack::Lua { * is done inside CoreSuspender, while waiting for input happens * without the suspend lock. */ - DFHACK_EXPORT bool RunCoreQueryLoop(color_ostream &out, lua_State *state, - bool (*init)(color_ostream&, lua_State*, void*), - void *arg); + using init_fn = std::function; + + DFHACK_EXPORT bool RunCoreQueryLoop(color_ostream &out, lua_State *state, init_fn init); /** * Attempt to interrupt the currently-executing lua function by raising a lua error From 53f7af165230661fe39baf7f98f432019e412406 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Sat, 3 May 2025 02:03:04 -0500 Subject: [PATCH 3/6] `Core`: remove more unneeded type erasures --- library/Core.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index f2f965a7de..48da47ef13 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1391,9 +1391,8 @@ static void run_dfhack_init(color_ostream &out, Core *core) } // Load dfhack.init in a dedicated thread (non-interactive console mode) -void fInitthread(void * iodata) +static void fInitthread(IODATA * iod) { - IODATA * iod = ((IODATA*) iodata); Core * core = iod->core; color_ostream_proxy out(core->getConsole()); @@ -1401,13 +1400,12 @@ void fInitthread(void * iodata) } // A thread function... for the interactive console. -void fIOthread(void * iodata) +static void fIOthread(IODATA * iod) { static const std::filesystem::path HISTORY_FILE = CONFIG_PATH / "dfhack.history"; - IODATA * iod = ((IODATA*) iodata); Core * core = iod->core; - PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr; + PluginManager * plug_mgr = iod->plug_mgr; CommandHistory main_history; main_history.load(HISTORY_FILE.c_str()); @@ -1847,12 +1845,12 @@ bool Core::InitSimulationThread() { std::cerr << "Starting IO thread.\n"; // create IO thread - d->iothread = std::thread{fIOthread, (void*)temp}; + d->iothread = std::thread{fIOthread, temp}; } else { std::cerr << "Starting dfhack.init thread.\n"; - d->iothread = std::thread{fInitthread, (void*)temp}; + d->iothread = std::thread{fInitthread, temp}; } std::cerr << "Starting DF input capture thread.\n"; From 608ae95a36bde75b868cf44fc7588e2595ff1722 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Sat, 3 May 2025 11:30:03 -0500 Subject: [PATCH 4/6] eliminate another type erasure in `Core` --- library/Core.cpp | 4 ++-- library/include/Core.h | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 48da47ef13..b7a4bc5227 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -2022,8 +2022,8 @@ void Core::doUpdate(color_ostream &out) } // detect if the game was loaded or unloaded in the meantime - void *new_wdata = NULL; - void *new_mapdata = NULL; + df::world_data* new_wdata = nullptr; + df::map_block**** new_mapdata = nullptr; if (df::global::world && !is_load_save) { df::world_data *wdata = df::global::world->world_data; diff --git a/library/include/Core.h b/library/include/Core.h index dd84d49df0..383a0a9c95 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -50,6 +50,8 @@ struct lua_State; namespace df { struct viewscreen; + struct world_data; + struct map_block; } namespace DFHack @@ -300,9 +302,9 @@ namespace DFHack bool SelectHotkey(int key, int modifiers); // for state change tracking - void *last_world_data_ptr; + df::world_data *last_world_data_ptr; // for state change tracking - void *last_local_map_ptr; + df::map_block**** last_local_map_ptr; friend struct Screen::Hide; df::viewscreen *top_viewscreen; bool last_pause_state; From 5e50664b8f0477656c96d49946105652cbf2359a Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Sat, 3 May 2025 13:28:19 -0500 Subject: [PATCH 5/6] merge `Process.cpp` into a single source file --- scripts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts b/scripts index f1e74867f2..def13b7c6f 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit f1e74867f22c5f51eb4bfe380744b43f062d0b02 +Subproject commit def13b7c6f1bb8b036968490cae696e18cb25bbc From bb332c91677afc118cc4d33e40ea551526392944 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Sun, 4 May 2025 12:06:56 -0500 Subject: [PATCH 6/6] Revert "merge `Process.cpp` into a single source file" This reverts commit 5e50664b8f0477656c96d49946105652cbf2359a. --- scripts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts b/scripts index def13b7c6f..f1e74867f2 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit def13b7c6f1bb8b036968490cae696e18cb25bbc +Subproject commit f1e74867f22c5f51eb4bfe380744b43f062d0b02