Skip to content

Commit

Permalink
expose BulkUpdate to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Dec 31, 2023
1 parent dbb8a7b commit c0a5c65
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/PACKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 24 additions & 6 deletions src/core/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -768,13 +788,10 @@ AccessibilityLevel Tracker::isReachable(const std::list< std::list<std::string>
// '$' 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) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Tracker final : public LuaInterface<Tracker> {
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;
};


Expand Down

0 comments on commit c0a5c65

Please sign in to comment.