forked from mirrors/0ad
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:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user