Allow system components to be registered on the scripting side (so mods can make their own system components).

This was SVN commit r15157.
This commit is contained in:
sanderd17
2014-05-18 13:44:08 +00:00
parent 2b05a8d233
commit 7ff5fbda2b
13 changed files with 70 additions and 62 deletions
@@ -228,4 +228,4 @@ AIInterface.prototype.OnGlobalValueModification = function(msg)
}
};
Engine.RegisterComponentType(IID_AIInterface, "AIInterface", AIInterface);
Engine.RegisterSystemComponentType(IID_AIInterface, "AIInterface", AIInterface);
@@ -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);
@@ -142,5 +142,5 @@ Barter.prototype.ProgressTimeout = function(data)
}
}
Engine.RegisterComponentType(IID_Barter, "Barter", Barter);
Engine.RegisterSystemComponentType(IID_Barter, "Barter", Barter);
@@ -123,4 +123,4 @@ EndGameManager.prototype.CheckConquestCriticalEntitiesNow = function()
cmpRangeManager.SetLosRevealAll(-1, true);
};
Engine.RegisterComponentType(IID_EndGameManager, "EndGameManager", EndGameManager);
Engine.RegisterSystemComponentType(IID_EndGameManager, "EndGameManager", EndGameManager);
@@ -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);
@@ -66,4 +66,4 @@ PlayerManager.prototype.GetAllPlayerEntities = function()
{
return this.playerEntities;
};
Engine.RegisterComponentType(IID_PlayerManager, "PlayerManager", PlayerManager);
Engine.RegisterSystemComponentType(IID_PlayerManager, "PlayerManager", PlayerManager);
@@ -49,4 +49,4 @@ TechnologyTemplateManager.prototype.GetAllTechs = function()
return this.allTechs;
}
Engine.RegisterComponentType(IID_TechnologyTemplateManager, "TechnologyTemplateManager", TechnologyTemplateManager);
Engine.RegisterSystemComponentType(IID_TechnologyTemplateManager, "TechnologyTemplateManager", TechnologyTemplateManager);
@@ -113,4 +113,4 @@ Timer.prototype.OnUpdate = function(msg)
}
}
Engine.RegisterComponentType(IID_Timer, "Timer", Timer);
Engine.RegisterSystemComponentType(IID_Timer, "Timer", Timer);
@@ -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);
@@ -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++;
+1 -49
View File
@@ -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<VfsPath>* loadedScripts);
+44 -3
View File
@@ -70,6 +70,7 @@ CComponentManager::CComponentManager(CSimContext& context, shared_ptr<ScriptRunt
if (!skipScriptFunctions)
{
m_ScriptInterface.RegisterFunction<void, int, std::string, CScriptVal, CComponentManager::Script_RegisterComponentType> ("RegisterComponentType");
m_ScriptInterface.RegisterFunction<void, int, std::string, CScriptVal, CComponentManager::Script_RegisterSystemComponentType> ("RegisterSystemComponentType");
m_ScriptInterface.RegisterFunction<void, int, std::string, CScriptVal, CComponentManager::Script_ReRegisterComponentType> ("ReRegisterComponentType");
m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterInterface> ("RegisterInterface");
m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterMessageType> ("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<CComponentManager*> (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<CComponentManager*> (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<CComponentManager*> (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<CComponentManager*> (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<ComponentTypeId, ComponentType>::const_iterator it = m_ComponentTypesById.find(cid);
+10 -1
View File
@@ -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<ComponentTypeId, ComponentType> m_ComponentTypesById;
std::vector<CComponentManager::ComponentTypeId> m_ScriptedSystemComponents;
std::vector<boost::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId;
std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions;