Skip to content

Commit

Permalink
add override_rule_exec_limit to config
Browse files Browse the repository at this point in the history
this is mainly for pack authors, so they can check how far the pack is off
or to suggest a new default limit
  • Loading branch information
black-sliver committed Jan 7, 2024
1 parent 588de95 commit a66d4f3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/core/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const LuaInterface<Tracker>::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({}));
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -985,3 +988,8 @@ bool Tracker::loadState(nlohmann::json& state)

return true;
}

void Tracker::setExecLimit(int execLimit)
{
_execLimit = execLimit;
}
4 changes: 4 additions & 0 deletions src/core/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Tracker final : public LuaInterface<Tracker> {
friend class LuaInterface;

public:
static constexpr int DEFAULT_EXEC_LIMIT = 500000;

Tracker(Pack* pack, lua_State *L);
virtual ~Tracker();
Expand Down Expand Up @@ -96,6 +97,7 @@ class Tracker final : public LuaInterface<Tracker> {

bool changeItemState(const std::string& id, BaseItem::Action action);

static void setExecLimit(int execLimit);

protected:
Pack* _pack;
Expand All @@ -114,6 +116,8 @@ class Tracker final : public LuaInterface<Tracker> {

std::list<std::string>* _parents = nullptr;

static int _execLimit;

AccessibilityLevel isReachable(const Location& location, const LocationSection& section, std::list<std::string>& parents);
bool isVisible(const Location& location, const LocationSection& section, std::list<std::string>& parents);
AccessibilityLevel isReachable(const Location& location, std::list<std::string>& parents);
Expand Down
7 changes: 7 additions & 0 deletions src/poptracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>::max())
_config["override_rule_exec_limit"] = std::numeric_limits<int>::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()
Expand Down

0 comments on commit a66d4f3

Please sign in to comment.