From 2a628849935f22fb30af45d9a90fc9169fcfdbe3 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Sat, 6 Sep 2025 23:49:50 +0200 Subject: [PATCH] fix(datamodel): change service run order such that joints are computed before scripts --- core/src/error/instance.h | 2 +- core/src/objects/datamodel.cpp | 16 +++++++++++----- core/src/objects/service/script/scriptcontext.h | 3 ++- .../objects/service/script/serverscriptservice.h | 5 +++-- core/src/objects/service/workspace.h | 5 +++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/core/src/error/instance.h b/core/src/error/instance.h index 940b2c0..88d48d9 100644 --- a/core/src/error/instance.h +++ b/core/src/error/instance.h @@ -9,7 +9,7 @@ class NoSuchInstance : public Error { class NoSuchService : public Error { public: - inline NoSuchService(std::string className) : Error("NoSuchService", "Cannot insert service of unknown type " + className) {} + inline NoSuchService(std::string className) : Error("NoSuchService", "Unknown service type " + className) {} }; class ServiceAlreadyExists : public Error { diff --git a/core/src/objects/datamodel.cpp b/core/src/objects/datamodel.cpp index 2cd6cd4..a1934f9 100644 --- a/core/src/objects/datamodel.cpp +++ b/core/src/objects/datamodel.cpp @@ -34,8 +34,13 @@ void DataModel::Init(bool runMode) { // Init all services for (auto [_, service] : this->services) { service->InitService(); - if (runMode) service->OnRun(); } + + // Deterministic run order + if (!runMode) return; + + if (auto service = FindService()) service->OnRun(); + if (auto service = FindService()) service->OnRun(); } void DataModel::SaveToFile(std::optional path) { @@ -114,12 +119,13 @@ result, NoSuchService> DataModel::GetService(std::strin } result, NoSuchService> DataModel::FindService(std::string className) { - if (!INSTANCE_MAP[className] || (INSTANCE_MAP[className]->flags ^ (INSTANCE_NOTCREATABLE | INSTANCE_SERVICE)) != 0) { - return NoSuchService(className); - } - if (services.count(className) != 0) return std::dynamic_pointer_cast(services[className]); + + if (!INSTANCE_MAP[className] || ~(INSTANCE_MAP[className]->flags & (INSTANCE_NOTCREATABLE | INSTANCE_SERVICE)) == 0) { + return NoSuchService(className); + } + return nullptr; } diff --git a/core/src/objects/service/script/scriptcontext.h b/core/src/objects/service/script/scriptcontext.h index 569339b..1200081 100644 --- a/core/src/objects/service/script/scriptcontext.h +++ b/core/src/objects/service/script/scriptcontext.h @@ -21,13 +21,14 @@ class DEF_INST_SERVICE_(hidden) ScriptContext : public Service { std::vector sleepingThreads; int lastScriptSourceId = 0; protected: - void InitService() override; bool initialized = false; public: ScriptContext(); ~ScriptContext(); + void InitService() override; + lua_State* state; void PushThreadSleep(lua_State* thread, float delay); void RunSleepingThreads(); diff --git a/core/src/objects/service/script/serverscriptservice.h b/core/src/objects/service/script/serverscriptservice.h index 1224704..1fc75c8 100644 --- a/core/src/objects/service/script/serverscriptservice.h +++ b/core/src/objects/service/script/serverscriptservice.h @@ -8,13 +8,14 @@ class DEF_INST_SERVICE_(explorer_icon="server-scripts", hidden) ServerScriptService : public Service { AUTOGEN_PREAMBLE protected: - void InitService() override; - void OnRun() override; bool initialized = false; public: ServerScriptService(); ~ServerScriptService(); + void InitService() override; + void OnRun() override; + static inline std::shared_ptr Create() { return std::make_shared(); }; }; \ No newline at end of file diff --git a/core/src/objects/service/workspace.h b/core/src/objects/service/workspace.h index 31ef49f..3049413 100644 --- a/core/src/objects/service/workspace.h +++ b/core/src/objects/service/workspace.h @@ -33,13 +33,14 @@ class DEF_INST_SERVICE_(explorer_icon="workspace") Workspace : public Service { std::shared_ptr physicsWorld; friend PhysWorld; protected: - void InitService() override; - void OnRun() override; bool initialized = false; public: Workspace(); ~Workspace(); + + void InitService() override; + void OnRun() override; std::recursive_mutex queueLock;