diff --git a/binaries/data/mods/public/simulation/components/AIInterface.js b/binaries/data/mods/public/simulation/components/AIInterface.js index 4ad57b7011..6c694abe1f 100644 --- a/binaries/data/mods/public/simulation/components/AIInterface.js +++ b/binaries/data/mods/public/simulation/components/AIInterface.js @@ -228,4 +228,4 @@ AIInterface.prototype.OnGlobalValueModification = function(msg) } }; -Engine.RegisterComponentType(IID_AIInterface, "AIInterface", AIInterface); +Engine.RegisterSystemComponentType(IID_AIInterface, "AIInterface", AIInterface); diff --git a/binaries/data/mods/public/simulation/components/AuraManager.js b/binaries/data/mods/public/simulation/components/AuraManager.js index 89adfc90d8..4cbda68229 100644 --- a/binaries/data/mods/public/simulation/components/AuraManager.js +++ b/binaries/data/mods/public/simulation/components/AuraManager.js @@ -172,4 +172,4 @@ AuraManager.prototype.ApplyTemplateModifications = function(valueName, value, pl return value; }; -Engine.RegisterComponentType(IID_AuraManager, "AuraManager", AuraManager); +Engine.RegisterSystemComponentType(IID_AuraManager, "AuraManager", AuraManager); diff --git a/binaries/data/mods/public/simulation/components/Barter.js b/binaries/data/mods/public/simulation/components/Barter.js index a9d10e7a47..20c66d9aa8 100644 --- a/binaries/data/mods/public/simulation/components/Barter.js +++ b/binaries/data/mods/public/simulation/components/Barter.js @@ -142,5 +142,5 @@ Barter.prototype.ProgressTimeout = function(data) } } -Engine.RegisterComponentType(IID_Barter, "Barter", Barter); +Engine.RegisterSystemComponentType(IID_Barter, "Barter", Barter); diff --git a/binaries/data/mods/public/simulation/components/EndGameManager.js b/binaries/data/mods/public/simulation/components/EndGameManager.js index aa3afaed8e..5558c63484 100644 --- a/binaries/data/mods/public/simulation/components/EndGameManager.js +++ b/binaries/data/mods/public/simulation/components/EndGameManager.js @@ -123,4 +123,4 @@ EndGameManager.prototype.CheckConquestCriticalEntitiesNow = function() cmpRangeManager.SetLosRevealAll(-1, true); }; -Engine.RegisterComponentType(IID_EndGameManager, "EndGameManager", EndGameManager); +Engine.RegisterSystemComponentType(IID_EndGameManager, "EndGameManager", EndGameManager); diff --git a/binaries/data/mods/public/simulation/components/GuiInterface.js b/binaries/data/mods/public/simulation/components/GuiInterface.js index 4dc3ffa7ea..d7d20c9fca 100644 --- a/binaries/data/mods/public/simulation/components/GuiInterface.js +++ b/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -2020,4 +2020,4 @@ GuiInterface.prototype.ScriptCall = function(player, name, args) throw new Error("Invalid GuiInterface Call name \""+name+"\""); }; -Engine.RegisterComponentType(IID_GuiInterface, "GuiInterface", GuiInterface); +Engine.RegisterSystemComponentType(IID_GuiInterface, "GuiInterface", GuiInterface); diff --git a/binaries/data/mods/public/simulation/components/PlayerManager.js b/binaries/data/mods/public/simulation/components/PlayerManager.js index 73c8192203..038ee3ea18 100644 --- a/binaries/data/mods/public/simulation/components/PlayerManager.js +++ b/binaries/data/mods/public/simulation/components/PlayerManager.js @@ -66,4 +66,4 @@ PlayerManager.prototype.GetAllPlayerEntities = function() { return this.playerEntities; }; -Engine.RegisterComponentType(IID_PlayerManager, "PlayerManager", PlayerManager); +Engine.RegisterSystemComponentType(IID_PlayerManager, "PlayerManager", PlayerManager); diff --git a/binaries/data/mods/public/simulation/components/TechnologyTemplateManager.js b/binaries/data/mods/public/simulation/components/TechnologyTemplateManager.js index 2d29862513..0c8b83d3b7 100644 --- a/binaries/data/mods/public/simulation/components/TechnologyTemplateManager.js +++ b/binaries/data/mods/public/simulation/components/TechnologyTemplateManager.js @@ -49,4 +49,4 @@ TechnologyTemplateManager.prototype.GetAllTechs = function() return this.allTechs; } -Engine.RegisterComponentType(IID_TechnologyTemplateManager, "TechnologyTemplateManager", TechnologyTemplateManager); +Engine.RegisterSystemComponentType(IID_TechnologyTemplateManager, "TechnologyTemplateManager", TechnologyTemplateManager); diff --git a/binaries/data/mods/public/simulation/components/Timer.js b/binaries/data/mods/public/simulation/components/Timer.js index 20cf763e28..10f86216e1 100644 --- a/binaries/data/mods/public/simulation/components/Timer.js +++ b/binaries/data/mods/public/simulation/components/Timer.js @@ -113,4 +113,4 @@ Timer.prototype.OnUpdate = function(msg) } } -Engine.RegisterComponentType(IID_Timer, "Timer", Timer); +Engine.RegisterSystemComponentType(IID_Timer, "Timer", Timer); diff --git a/binaries/data/mods/public/simulation/components/ValueModificationManager.js b/binaries/data/mods/public/simulation/components/ValueModificationManager.js index bc4ab64cf3..754155cbd4 100644 --- a/binaries/data/mods/public/simulation/components/ValueModificationManager.js +++ b/binaries/data/mods/public/simulation/components/ValueModificationManager.js @@ -12,5 +12,5 @@ ValueModificationManager.prototype.ApplyModifications = function(valueName, curr return ApplyValueModificationsToEntity(valueName, currentValue, entity); }; -Engine.RegisterComponentType(IID_ValueModificationManager, "ValueModificationManager", ValueModificationManager); +Engine.RegisterSystemComponentType(IID_ValueModificationManager, "ValueModificationManager", ValueModificationManager); diff --git a/binaries/data/mods/public/simulation/components/tests/setup.js b/binaries/data/mods/public/simulation/components/tests/setup.js index a5db9eb1cf..100caca2b5 100644 --- a/binaries/data/mods/public/simulation/components/tests/setup.js +++ b/binaries/data/mods/public/simulation/components/tests/setup.js @@ -11,6 +11,12 @@ Engine.RegisterComponentType = function(iid, name, ctor) g_ComponentTypes[name] = { iid: iid, ctor: ctor }; }; +Engine.RegisterSystemComponentType = function(iid, name, ctor) +{ + TS_ASSERT(!g_ComponentTypes[name]); + g_ComponentTypes[name] = { iid: iid, ctor: ctor }; +}; + Engine.RegisterInterface = function(name) { global["IID_"+name] = g_NewIID++; diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 6eef06c515..75976cb298 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -95,56 +95,8 @@ public: static void ResetComponentState(CComponentManager& componentManager, bool skipScriptedComponents, bool skipAI) { componentManager.ResetState(); - - CParamNode noParam; - CComponentManager::ComponentTypeId cid; - componentManager.InitSystemEntity(); - CEntityHandle systemEntity = componentManager.GetSystemEntity(); - - // Add native system components: - componentManager.AddComponent(systemEntity, CID_TemplateManager, noParam); - - componentManager.AddComponent(systemEntity, CID_CommandQueue, noParam); - componentManager.AddComponent(systemEntity, CID_ObstructionManager, noParam); - componentManager.AddComponent(systemEntity, CID_ParticleManager, noParam); - componentManager.AddComponent(systemEntity, CID_Pathfinder, noParam); - componentManager.AddComponent(systemEntity, CID_ProjectileManager, noParam); - componentManager.AddComponent(systemEntity, CID_RangeManager, noParam); - componentManager.AddComponent(systemEntity, CID_SoundManager, noParam); - componentManager.AddComponent(systemEntity, CID_Terrain, noParam); - componentManager.AddComponent(systemEntity, CID_TerritoryManager, noParam); - componentManager.AddComponent(systemEntity, CID_WaterManager, noParam); - - // Add scripted system components: - if (!skipScriptedComponents) - { - // TODO: Load this from a file to allow modders to add scripted components - // without having to recompile. -#define LOAD_SCRIPTED_COMPONENT(name) \ - cid = componentManager.LookupCID(name); \ - if (cid == CID__Invalid) \ - LOGERROR(L"Can't find component type " L##name); \ - componentManager.AddComponent(systemEntity, cid, noParam) - - LOAD_SCRIPTED_COMPONENT("AIInterface"); - LOAD_SCRIPTED_COMPONENT("AuraManager"); - LOAD_SCRIPTED_COMPONENT("Barter"); - LOAD_SCRIPTED_COMPONENT("EndGameManager"); - LOAD_SCRIPTED_COMPONENT("GuiInterface"); - LOAD_SCRIPTED_COMPONENT("PlayerManager"); - LOAD_SCRIPTED_COMPONENT("TechnologyTemplateManager"); - LOAD_SCRIPTED_COMPONENT("Timer"); - LOAD_SCRIPTED_COMPONENT("ValueModificationManager"); - -#undef LOAD_SCRIPTED_COMPONENT - - if (!skipAI) - { - componentManager.AddComponent(systemEntity, CID_AIManager, noParam); - } - - } + componentManager.AddSystemComponents(skipScriptedComponents, skipAI); } static bool LoadDefaultScripts(CComponentManager& componentManager, std::set* loadedScripts); diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp index b479b663b5..dee5572481 100644 --- a/source/simulation2/system/ComponentManager.cpp +++ b/source/simulation2/system/ComponentManager.cpp @@ -70,6 +70,7 @@ CComponentManager::CComponentManager(CSimContext& context, shared_ptr ("RegisterComponentType"); + m_ScriptInterface.RegisterFunction ("RegisterSystemComponentType"); m_ScriptInterface.RegisterFunction ("ReRegisterComponentType"); m_ScriptInterface.RegisterFunction ("RegisterInterface"); m_ScriptInterface.RegisterFunction ("RegisterMessageType"); @@ -144,7 +145,7 @@ bool CComponentManager::LoadScript(const VfsPath& filename, bool hotload) return ok; } -void CComponentManager::Script_RegisterComponentType_Common(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor, bool reRegister) +void CComponentManager::Script_RegisterComponentType_Common(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor, bool reRegister, bool systemComponent) { CComponentManager* componentManager = static_cast (pCxPrivate->pCBData); JSContext* cx = componentManager->m_ScriptInterface.GetContext(); @@ -172,6 +173,8 @@ void CComponentManager::Script_RegisterComponentType_Common(ScriptInterface::CxP // Allocate a new cid number cid = componentManager->m_NextScriptComponentTypeId++; componentManager->m_ComponentTypeIdsByName[cname] = cid; + if (systemComponent) + componentManager->MarkScriptedComponentForSystemEntity(cid); } else { @@ -312,14 +315,21 @@ void CComponentManager::Script_RegisterComponentType_Common(ScriptInterface::CxP void CComponentManager::Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor) { CComponentManager* componentManager = static_cast (pCxPrivate->pCBData); - componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, false); + componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, false, false); + componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading); +} + +void CComponentManager::Script_RegisterSystemComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor) +{ + CComponentManager* componentManager = static_cast (pCxPrivate->pCBData); + componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, false, true); componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading); } void CComponentManager::Script_ReRegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor) { CComponentManager* componentManager = static_cast (pCxPrivate->pCBData); - componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, true); + componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, true, false); } void CComponentManager::Script_RegisterInterface(ScriptInterface::CxPrivate* pCxPrivate, std::string name) @@ -527,6 +537,11 @@ void CComponentManager::RegisterComponentTypeScriptWrapper(InterfaceId iid, Comp // TODO: merge with RegisterComponentType } +void CComponentManager::MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid) +{ + m_ScriptedSystemComponents.push_back(cid); +} + void CComponentManager::RegisterMessageType(MessageTypeId mtid, const char* name) { m_MessageTypeIdsByName[name] = mtid; @@ -618,6 +633,32 @@ bool CComponentManager::AddComponent(CEntityHandle ent, ComponentTypeId cid, con return true; } +void CComponentManager::AddSystemComponents(bool skipScriptedComponents, bool skipAI) +{ + CParamNode noParam; + AddComponent(m_SystemEntity, CID_TemplateManager, noParam); + AddComponent(m_SystemEntity, CID_CommandQueue, noParam); + AddComponent(m_SystemEntity, CID_ObstructionManager, noParam); + AddComponent(m_SystemEntity, CID_ParticleManager, noParam); + AddComponent(m_SystemEntity, CID_Pathfinder, noParam); + AddComponent(m_SystemEntity, CID_ProjectileManager, noParam); + AddComponent(m_SystemEntity, CID_RangeManager, noParam); + AddComponent(m_SystemEntity, CID_SoundManager, noParam); + AddComponent(m_SystemEntity, CID_Terrain, noParam); + AddComponent(m_SystemEntity, CID_TerritoryManager, noParam); + AddComponent(m_SystemEntity, CID_WaterManager, noParam); + + // Add scripted system components: + if (!skipScriptedComponents) + { + + for (uint32_t i = 0; i < m_ScriptedSystemComponents.size(); ++i) + AddComponent(m_SystemEntity, m_ScriptedSystemComponents[i], noParam); + if (!skipAI) + AddComponent(m_SystemEntity, CID_AIManager, noParam); + } +} + IComponent* CComponentManager::ConstructComponent(CEntityHandle ent, ComponentTypeId cid) { std::map::const_iterator it = m_ComponentTypesById.find(cid); diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h index aabc8bef8d..00f2f691af 100644 --- a/source/simulation2/system/ComponentManager.h +++ b/source/simulation2/system/ComponentManager.h @@ -95,6 +95,8 @@ public: void RegisterComponentType(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema); void RegisterComponentTypeScriptWrapper(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema); + + void MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid); /** * Subscribe the current component type to the given message type. @@ -166,6 +168,11 @@ public: */ bool AddComponent(CEntityHandle ent, ComponentTypeId cid, const CParamNode& paramNode); + /** + * Add all system components to the system entity (skip the scripted components or the AI components on demand) + */ + void AddSystemComponents(bool skipScriptedComponents, bool skipAI); + /** * Adds an externally-created component, so that it is returned by QueryInterface * but does not get destroyed and does not receive messages from the component manager. @@ -242,8 +249,9 @@ public: private: // Implementations of functions exposed to scripts - static void Script_RegisterComponentType_Common(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor, bool reRegister); + static void Script_RegisterComponentType_Common(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor, bool reRegister, bool systemComponent); static void Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor); + static void Script_RegisterSystemComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor); static void Script_ReRegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor); static void Script_RegisterInterface(ScriptInterface::CxPrivate* pCxPrivate, std::string name); static void Script_RegisterMessageType(ScriptInterface::CxPrivate* pCxPrivate, std::string name); @@ -281,6 +289,7 @@ private: // TODO: some of these should be vectors std::map m_ComponentTypesById; + std::vector m_ScriptedSystemComponents; std::vector > m_ComponentsByInterface; // indexed by InterfaceId std::map > m_ComponentsByTypeId; std::map > m_LocalMessageSubscriptions;