fix(datamodel): change service run order such that joints are computed before scripts

This commit is contained in:
maelstrom 2025-09-06 23:49:50 +02:00
parent 28f416bf63
commit 2a62884993
5 changed files with 20 additions and 11 deletions

View file

@ -9,7 +9,7 @@ class NoSuchInstance : public Error {
class NoSuchService : public Error { class NoSuchService : public Error {
public: 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 { class ServiceAlreadyExists : public Error {

View file

@ -34,8 +34,13 @@ void DataModel::Init(bool runMode) {
// Init all services // Init all services
for (auto [_, service] : this->services) { for (auto [_, service] : this->services) {
service->InitService(); service->InitService();
if (runMode) service->OnRun();
} }
// Deterministic run order
if (!runMode) return;
if (auto service = FindService<Workspace>()) service->OnRun();
if (auto service = FindService<ServerScriptService>()) service->OnRun();
} }
void DataModel::SaveToFile(std::optional<std::string> path) { void DataModel::SaveToFile(std::optional<std::string> path) {
@ -114,12 +119,13 @@ result<std::shared_ptr<Service>, NoSuchService> DataModel::GetService(std::strin
} }
result<nullable std::shared_ptr<Service>, NoSuchService> DataModel::FindService(std::string className) { result<nullable std::shared_ptr<Service>, 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) if (services.count(className) != 0)
return std::dynamic_pointer_cast<Service>(services[className]); return std::dynamic_pointer_cast<Service>(services[className]);
if (!INSTANCE_MAP[className] || ~(INSTANCE_MAP[className]->flags & (INSTANCE_NOTCREATABLE | INSTANCE_SERVICE)) == 0) {
return NoSuchService(className);
}
return nullptr; return nullptr;
} }

View file

@ -21,13 +21,14 @@ class DEF_INST_SERVICE_(hidden) ScriptContext : public Service {
std::vector<SleepingThread> sleepingThreads; std::vector<SleepingThread> sleepingThreads;
int lastScriptSourceId = 0; int lastScriptSourceId = 0;
protected: protected:
void InitService() override;
bool initialized = false; bool initialized = false;
public: public:
ScriptContext(); ScriptContext();
~ScriptContext(); ~ScriptContext();
void InitService() override;
lua_State* state; lua_State* state;
void PushThreadSleep(lua_State* thread, float delay); void PushThreadSleep(lua_State* thread, float delay);
void RunSleepingThreads(); void RunSleepingThreads();

View file

@ -8,13 +8,14 @@
class DEF_INST_SERVICE_(explorer_icon="server-scripts", hidden) ServerScriptService : public Service { class DEF_INST_SERVICE_(explorer_icon="server-scripts", hidden) ServerScriptService : public Service {
AUTOGEN_PREAMBLE AUTOGEN_PREAMBLE
protected: protected:
void InitService() override;
void OnRun() override;
bool initialized = false; bool initialized = false;
public: public:
ServerScriptService(); ServerScriptService();
~ServerScriptService(); ~ServerScriptService();
void InitService() override;
void OnRun() override;
static inline std::shared_ptr<Instance> Create() { return std::make_shared<ServerScriptService>(); }; static inline std::shared_ptr<Instance> Create() { return std::make_shared<ServerScriptService>(); };
}; };

View file

@ -33,13 +33,14 @@ class DEF_INST_SERVICE_(explorer_icon="workspace") Workspace : public Service {
std::shared_ptr<PhysWorld> physicsWorld; std::shared_ptr<PhysWorld> physicsWorld;
friend PhysWorld; friend PhysWorld;
protected: protected:
void InitService() override;
void OnRun() override;
bool initialized = false; bool initialized = false;
public: public:
Workspace(); Workspace();
~Workspace(); ~Workspace();
void InitService() override;
void OnRun() override;
std::recursive_mutex queueLock; std::recursive_mutex queueLock;