Skip to content

Commit

Permalink
Lua: add elapsed time to OnFrame handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Mar 10, 2024
1 parent e780ac3 commit 9868166
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
4 changes: 3 additions & 1 deletion api/lua/definition/poptracker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ function ScriptHost:RemoveVariableWatch(name) end
function ScriptHost:CreateLuaItem() end

---Add a handler/callback that runs on every frame.
---Available since 0.25.9.
---@param name string identifier/name of this callback
---@param callback fun():nil called every frame
---@param callback fun(elapsedSeconds:number):nil called every frame, argument is elapsed time in seconds since last call
---@return string reference for RemoveOnFrameHandler
function ScriptHost:AddOnFrameHandler(name, callback) end

---Remove a handler/callback added by AddOnFrameHandler(name).
---Available since 0.25.9.
---@param name string identifier/name of the handler to remove
---@return boolean true on success
function ScriptHost:RemoveOnFrameHandler(name) end
Expand Down
2 changes: 2 additions & 0 deletions doc/PACKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ The following interfaces are provided:
* `ref :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
* `ref :AddOnFrameHandler(name,callback)`: callback(elapsed) will be called every frame, available since 0.25.9
* `bool :RemoveOnFrameHandler(name)`: remove a frame callback


### global AutoTracker
Expand Down
27 changes: 17 additions & 10 deletions src/core/scripthost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,16 @@ std::string ScriptHost::AddOnFrameHandler(const std::string& name, LuaRef callba
RemoveOnFrameHandler(name);
if (!callback.valid())
luaL_error(_L, "Invalid callback");
_onFrameCallbacks.push_back({name, callback});
_onFrameHandlers.push_back(OnFrameHandler{callback.ref, name, Ui::getMicroTicks()});
return name;
}

bool ScriptHost::RemoveOnFrameHandler(const std::string& name)
{
for (auto it = _onFrameCallbacks.begin(); it != _onFrameCallbacks.end(); it++) {
if (it->first == name) {
luaL_unref(_L, LUA_REGISTRYINDEX, it->second.ref);
_onFrameCallbacks.erase(it);
for (auto it = _onFrameHandlers.begin(); it != _onFrameHandlers.end(); it++) {
if (it->name == name) {
luaL_unref(_L, LUA_REGISTRYINDEX, it->callback);
_onFrameHandlers.erase(it);
return true;
}
}
Expand All @@ -396,15 +396,22 @@ bool ScriptHost::onFrame()
{
bool res = autoTrack();

for (size_t i=0; i<_onFrameCallbacks.size(); i++) {
auto name = _onFrameCallbacks[i].first;
for (size_t i=0; i<_onFrameHandlers.size(); i++) {
auto name = _onFrameHandlers[i].name;
auto now = Ui::getMicroTicks();
auto elapsedUs = now - _onFrameHandlers[i].lastTimestamp;
double elapsed = (double)elapsedUs / 1000000.0;
_onFrameHandlers[i].lastTimestamp = now;

// For now we use the same exec limit as in Tracker, which is 3-4ms for pure Lua on a fast PC.
runLuaFunction(_onFrameCallbacks[i].second.ref, name, nullptr, Tracker::getExecLimit());
runLuaFunction(_onFrameHandlers[i].callback, name, [elapsed](lua_State *L) {
Lua(L).Push(elapsed);
return 1;
}, Tracker::getExecLimit());

if (_onFrameCallbacks.size() <= i)
if (_onFrameHandlers.size() <= i)
break; // callback does not exist anymore
if (_onFrameCallbacks[i].first != name)
if (_onFrameHandlers[i].name != name)
i--; // callback was modified in callback
}

Expand Down
12 changes: 10 additions & 2 deletions src/core/scripthost.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "luaitem.h"
#include <string>
#include <vector> // TODO: replace by new[] uint8_t?
#include "../uilib/timer.h"

class ScriptHost;

Expand Down Expand Up @@ -59,15 +60,22 @@ class ScriptHost : public LuaInterface<ScriptHost> {
int callback;
std::set<std::string> names;
};


struct OnFrameHandler
{
int callback;
std::string name;
Ui::microtick_t lastTimestamp;
};

protected:
lua_State *_L;
Pack *_pack;
Tracker *_tracker;
std::vector<MemoryWatch> _memoryWatches;
std::vector<std::pair<std::string, CodeWatch> > _codeWatches;
std::vector<std::pair<std::string, VarWatch> > _varWatches;
std::vector<std::pair<std::string, LuaRef> > _onFrameCallbacks;
std::vector<OnFrameHandler> _onFrameHandlers;
AutoTracker *_autoTracker = nullptr;

private:
Expand Down

0 comments on commit 9868166

Please sign in to comment.