Skip to content

Commit

Permalink
Lua: Allow watch for ANY code using "*"
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Dec 3, 2023
1 parent 465186b commit a5075ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/PACKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The following interfaces are provided:
* `require` can be used instead (since PopTracker 0.21.0)
* `bool :AddMemoryWatch(name,addr,len,callback,interal)`: add a memory watch for auto-tracking, see [AUTOTRACKING.md](AUTOTRACKING.md)
* `bool :RemoveMemoryWatch(name)`: remove memory watch by name, available since 0.11.0
* `bool :AddWatchForCode(name,code,callback)`: callback(code) will be called whenever an item changed state that canProvide(code). Only available in PopTracker, since 0.11.0, will return a reference (name) to the watch since 0.18.2
* `bool :AddWatchForCode(name,code,callback)`: callback(code) will be called whenever an item changed state that canProvide(code). Only available in PopTracker, since 0.11.0, will return a reference (name) to the watch since 0.18.2. Use "*" to trigger for all codes since 0.25.5.
* `bool :RemoveWatchForCode(name)`: remove watch by name
* `LuaItem :CreateLuaItem()`: create a LuaItem (custom item) instance

Expand Down
8 changes: 6 additions & 2 deletions src/core/scripthost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ ScriptHost::ScriptHost(Pack* pack, lua_State *L, Tracker *tracker)
// NOTE: since watches can change in a callback, we use vector
auto& pair = _codeWatches[i];
auto name = pair.first;
if (item.canProvideCode(pair.second.code)) {
bool isWildcard = pair.second.code == "*";
if (isWildcard || item.canProvideCode(pair.second.code)) {
printf("Item %s changed, which can provide code \"%s\" for watch \"%s\"\n",
id.c_str(), pair.second.code.c_str(), pair.first.c_str());
lua_rawgeti(_L, LUA_REGISTRYINDEX, pair.second.callback);
lua_pushstring(_L, pair.second.code.c_str()); // arg1: code
if (isWildcard)
lua_pushstring(_L, item.getCodesString().c_str()); // arg1: item code(s)
else
lua_pushstring(_L, pair.second.code.c_str()); // arg1: watched code
if (lua_pcall(_L, 1, 0, 0)) {
printf("Error calling Memory Watch Callback for %s: %s\n",
pair.first.c_str(), lua_tostring(_L, -1));
Expand Down

0 comments on commit a5075ef

Please sign in to comment.