Add ability to schedule Lua scripts (#3416)

This commit is contained in:
Alexander Bock
2024-09-23 11:34:51 +02:00
committed by GitHub
parent 24a2247aa4
commit db23148af0
3 changed files with 121 additions and 2 deletions

View File

@@ -102,7 +102,6 @@ public:
void addLibrary(LuaLibrary library);
bool hasLibrary(const std::string& name);
virtual void preSync(bool isMaster) override;
virtual void encode(SyncBuffer* syncBuffer) override;
virtual void decode(SyncBuffer* syncBuffer) override;
@@ -111,6 +110,11 @@ public:
void queueScript(Script script);
void queueScript(std::string script);
// Runs the `script` every `timeout` seconds wallclock time
void registerRepeatedScript(std::string identifier, std::string script,
double timeout, std::string preScript = "", std::string postScript = "");
void removeRepeatedScript(std::string_view identifier);
std::vector<std::string> allLuaFunctions() const;
const std::vector<LuaLibrary>& allLuaLibraries() const;
@@ -141,6 +145,19 @@ private:
std::vector<std::string> _scriptsToSync;
struct RepeatedScriptInfo {
/// This script is run everytime `timeout` seconds have passed
std::string script;
/// This script is run when the repeated script is unregistered
std::string postScript;
std::string identifier;
double timeout = 0.0;
double lastRun = 0.0;
};
std::vector<RepeatedScriptInfo> _repeatedScripts;
// Logging variables
bool _logFileExists = false;
bool _logScripts = true;