From 088d8f80dfcd6b4c3f3ccd0ae79ad5a1b72efea1 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Fri, 29 Dec 2023 17:05:09 +0100 Subject: [PATCH] expose BulkUpdate to Lua --- doc/PACKS.md | 1 + src/core/tracker.cpp | 30 ++++++++++++++++++++++++------ src/core/tracker.h | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/doc/PACKS.md b/doc/PACKS.md index 24631a5c..d542f984 100644 --- a/doc/PACKS.md +++ b/doc/PACKS.md @@ -97,6 +97,7 @@ The following interfaces are provided: * `int :ProviderCountForCode(code)`: number of items that provide the code (sum of count for consumables) * `mixed :FindObjectForCode(string)`: returns items for `code` or location section for `@location/section` * `void :UiHint(name,string)`: sends a hint to the Ui, see [Ui Hints](#ui-hints). Only available in PopTracker, since 0.11.0 +* `bool .BulkUpdate`: can be set to true from Lua to pause running logic rules. ### global ScriptHost diff --git a/src/core/tracker.cpp b/src/core/tracker.cpp index b8ac36b3..be202311 100644 --- a/src/core/tracker.cpp +++ b/src/core/tracker.cpp @@ -159,7 +159,8 @@ bool Tracker::AddItems(const std::string& file) { auto& item = _jsonItems.back(); item.setID(++_lastItemID); item.onChange += {this, [this](void* sender) { - if (!_bulkUpdate) _reachableCache.clear(); + if (!_bulkUpdate) + _reachableCache.clear(); _providerCountCache.clear(); JsonItem* i = (JsonItem*)sender; if (i->getType() == BaseItem::Type::COMPOSITE_TOGGLE) { @@ -460,12 +461,31 @@ int Tracker::Lua_Index(lua_State *L, const char* key) { if (strcmp(key, "ActiveVariantUID") == 0) { lua_pushstring(L, _pack->getVariant().c_str()); return 1; + } else if (strcmp(key, "BulkUpdate") == 0) { + lua_pushboolean(L, _bulkUpdate); + return 1; } else { printf("Tracker::Lua_Index(\"%s\") unknown\n", key); } return 0; } +bool Tracker::Lua_NewIndex(lua_State *L, const char* key) { + if (strcmp(key, "ActiveVariantUID") == 0) { + luaL_error(L, "Tried to write read-only property Tracker.%s", key); + } else if (strcmp(key, "BulkUpdate") == 0) { + bool val = lua_isnumber(L, -1) ? (lua_tonumber(L, -1) != 0) : lua_toboolean(L, -1); + if (!val) { + for (const auto& id: _bulkItemUpdates) + onStateChanged.emit(this, id); + _bulkItemUpdates.clear(); + } + _bulkUpdate = val; + return true; + } + return false; +} + const Map& Tracker::getMap(const std::string& name) const { const auto it = _maps.find(name); @@ -768,13 +788,10 @@ AccessibilityLevel Tracker::isReachable(const std::list< std::list // '$' calls into Lua, now also supported by ProviderCountForCode // other: references codes (with or without count) else { + // NOTE: we don't use _reachableCache here, instead ProvideCountForCode has a cache _parents = &parents; int n = ProviderCountForCode(s); _parents = nullptr; -#if 0 - _reachableCache[s] = n; // FIXME: test if commenting this out has an impact - // NOTE: we currently don't cache count results for code, only '@' -#endif if (n >= count) continue; if (optional) { @@ -856,7 +873,8 @@ LuaItem * Tracker::CreateLuaItem() LuaItem& i = _luaItems.back(); i.setID(++_lastItemID); i.onChange += {this, [this](void* sender) { - if (!_bulkUpdate) _reachableCache.clear(); + if (!_bulkUpdate) + _reachableCache.clear(); _providerCountCache.clear(); LuaItem* i = (LuaItem*)sender; if (_bulkUpdate) diff --git a/src/core/tracker.h b/src/core/tracker.h index 39f8de43..631ecc7e 100644 --- a/src/core/tracker.h +++ b/src/core/tracker.h @@ -126,7 +126,7 @@ class Tracker final : public LuaInterface { static const LuaInterface::MethodMap Lua_Methods; virtual int Lua_Index(lua_State *L, const char* key) override; - + virtual bool Lua_NewIndex(lua_State *L, const char* key) override; };