From a66d4f3e5a5536a4fae48c5aa2cf9bb026864b34 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Sat, 6 Jan 2024 13:36:14 +0100 Subject: [PATCH] add override_rule_exec_limit to config this is mainly for pack authors, so they can check how far the pack is off or to suggest a new default limit --- src/core/tracker.cpp | 18 +++++++++++++----- src/core/tracker.h | 4 ++++ src/poptracker.cpp | 7 +++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/core/tracker.cpp b/src/core/tracker.cpp index 57efa81f..9d559ef5 100644 --- a/src/core/tracker.cpp +++ b/src/core/tracker.cpp @@ -16,6 +16,8 @@ const LuaInterface::MethodMap Tracker::Lua_Methods = { LUA_METHOD(Tracker, UiHint, const char*, const char*), }; +int Tracker::_execLimit = Tracker::DEFAULT_EXEC_LIMIT; + static LayoutNode blankLayoutNode = LayoutNode::FromJSON(json({})); static JsonItem blankItem = JsonItem::FromJSON(json({})); static Map blankMap = Map::FromJSON(json({})); @@ -60,7 +62,7 @@ static void lua_timeout_hook(lua_State *L, lua_Debug *) // This functaion can be used internally when the return type is uncertain. // Use carefully; the return value(s) and error handler will still be on the lua stack -static int RunLuaFunction_inner(lua_State *L, const std::string name) +static int RunLuaFunction_inner(lua_State *L, const std::string name, int execLimit) { lua_pushcfunction(L, lua_error_handler); @@ -94,10 +96,11 @@ static int RunLuaFunction_inner(lua_State *L, const std::string name) ++argc; } - constexpr int exec_limit = 500000; - lua_sethook(L, lua_timeout_hook, LUA_MASKCOUNT, exec_limit); + if (execLimit > 0) + lua_sethook(L, lua_timeout_hook, LUA_MASKCOUNT, execLimit); auto res = lua_pcall(L, argc, 1, -argc-2); - lua_sethook(L, nullptr, 0, 0); + if (execLimit > 0) + lua_sethook(L, nullptr, 0, 0); if (res != LUA_OK) { auto err = lua_tostring(L, -1); @@ -121,7 +124,7 @@ int Tracker::runLuaFunction(lua_State *L, const std::string name) int Tracker::runLuaFunction(lua_State* L, const std::string name, int &out) { - int callStatus = RunLuaFunction_inner(L, name); + int callStatus = RunLuaFunction_inner(L, name, _execLimit); if (callStatus != LUA_OK) { // RunLuaFunction_inner handles popping the stack in case of errors return callStatus; @@ -985,3 +988,8 @@ bool Tracker::loadState(nlohmann::json& state) return true; } + +void Tracker::setExecLimit(int execLimit) +{ + _execLimit = execLimit; +} diff --git a/src/core/tracker.h b/src/core/tracker.h index 39f8de43..284924c6 100644 --- a/src/core/tracker.h +++ b/src/core/tracker.h @@ -22,6 +22,7 @@ class Tracker final : public LuaInterface { friend class LuaInterface; public: + static constexpr int DEFAULT_EXEC_LIMIT = 500000; Tracker(Pack* pack, lua_State *L); virtual ~Tracker(); @@ -96,6 +97,7 @@ class Tracker final : public LuaInterface { bool changeItemState(const std::string& id, BaseItem::Action action); + static void setExecLimit(int execLimit); protected: Pack* _pack; @@ -114,6 +116,8 @@ class Tracker final : public LuaInterface { std::list* _parents = nullptr; + static int _execLimit; + AccessibilityLevel isReachable(const Location& location, const LocationSection& section, std::list& parents); bool isVisible(const Location& location, const LocationSection& section, std::list& parents); AccessibilityLevel isReachable(const Location& location, std::list& parents); diff --git a/src/poptracker.cpp b/src/poptracker.cpp index cbd61a1f..dbf477bf 100644 --- a/src/poptracker.cpp +++ b/src/poptracker.cpp @@ -165,6 +165,13 @@ PopTracker::PopTracker(int argc, char** argv, bool cli, const json& args) if (_config["split_map_locations"].is_boolean()) Ui::MapWidget::SplitRects = _config["split_map_locations"]; + if (_config["override_rule_exec_limit"].is_number_unsigned()) { + if (_config["override_rule_exec_limit"] > std::numeric_limits::max()) + _config["override_rule_exec_limit"] = std::numeric_limits::max(); + Tracker::setExecLimit(_config["override_rule_exec_limit"]); + } else if (!_config["override_rule_exec_limit"].is_null()) + _config["override_rule_exec_limit"] = nullptr; // clear invalid value + saveConfig(); _ui = nullptr; // UI init moved to start()