mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-04 18:51:17 -06:00
Add the ability to schedule scripts based on wallclock time (closes #1773)
This commit is contained in:
@@ -115,6 +115,8 @@ public:
|
||||
double timeout, std::string preScript = "", std::string postScript = "");
|
||||
void removeRepeatedScript(std::string_view identifier);
|
||||
|
||||
void scheduleScript(std::string script, double delay);
|
||||
|
||||
std::vector<std::string> allLuaFunctions() const;
|
||||
const std::vector<LuaLibrary>& allLuaLibraries() const;
|
||||
|
||||
@@ -158,6 +160,15 @@ private:
|
||||
};
|
||||
std::vector<RepeatedScriptInfo> _repeatedScripts;
|
||||
|
||||
struct ScheduledScriptInfo {
|
||||
// The script that should be executed
|
||||
std::string script;
|
||||
|
||||
// The application timestamp at which time the script should be executed
|
||||
double timestamp = 0.0;
|
||||
};
|
||||
std::vector<ScheduledScriptInfo> _scheduledScripts;
|
||||
|
||||
// Logging variables
|
||||
bool _logFileExists = false;
|
||||
bool _logScripts = true;
|
||||
|
||||
@@ -572,6 +572,15 @@ void ScriptEngine::postSync(bool isMaster) {
|
||||
info.lastRun = now;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < _scheduledScripts.size(); i++) {
|
||||
const ScheduledScriptInfo& info = _scheduledScripts[i];
|
||||
if (now > info.timestamp) {
|
||||
runScript({ info.script });
|
||||
_scheduledScripts.erase(_scheduledScripts.begin() + i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::queueScript(Script script) {
|
||||
@@ -636,6 +645,14 @@ void ScriptEngine::removeRepeatedScript(std::string_view identifier) {
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::scheduleScript(std::string script, double delay) {
|
||||
double now =
|
||||
global::sessionRecordingHandler->isSavingFramesDuringPlayback() ?
|
||||
global::sessionRecordingHandler->currentApplicationInterpolationTime() :
|
||||
global::windowDelegate->applicationTime();
|
||||
_scheduledScripts.emplace_back(std::move(script), now + delay);
|
||||
}
|
||||
|
||||
void ScriptEngine::addBaseLibrary() {
|
||||
ZoneScoped;
|
||||
|
||||
@@ -715,7 +732,8 @@ void ScriptEngine::addBaseLibrary() {
|
||||
codegen::lua::DirectoryForPath,
|
||||
codegen::lua::UnzipFile,
|
||||
codegen::lua::RegisterRepeatedScript,
|
||||
codegen::lua::RemoveRepeatedScript
|
||||
codegen::lua::RemoveRepeatedScript,
|
||||
codegen::lua::ScheduleScript
|
||||
}
|
||||
};
|
||||
addLibrary(lib);
|
||||
|
||||
@@ -312,6 +312,15 @@ std::vector<std::filesystem::path> walkCommon(const std::filesystem::path& path,
|
||||
openspace::global::scriptEngine->removeRepeatedScript(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a `script` to be run in `delay` seconds. The delay is measured in wallclock
|
||||
* time, which is seconds that occur in the real world, not in relation to the in-game
|
||||
* time.
|
||||
*/
|
||||
[[codegen::luawrap]] void scheduleScript(std::string script, double delay) {
|
||||
openspace::global::scriptEngine->scheduleScript(std::move(script), delay);
|
||||
}
|
||||
|
||||
#include "scriptengine_lua_codegen.cpp"
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user