Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
CComponentManager Class Reference

#include <ComponentManager.h>

Classes

struct  ComponentType
 
struct  FindJSONFilesCallbackData
 

Public Types

typedef int InterfaceId
 
typedef int ComponentTypeId
 
typedef int MessageTypeId
 
typedef std::vector< std::pair
< entity_id_t, IComponent * > > 
InterfaceList
 
typedef boost::unordered_map
< entity_id_t, IComponent * > 
InterfaceListUnordered
 

Public Member Functions

 CComponentManager (CSimContext &, bool skipScriptFunctions=false)
 
 ~CComponentManager ()
 
void LoadComponentTypes ()
 
bool LoadScript (const VfsPath &filename, bool hotload=false)
 Load a script and execute it in a new function scope. More...
 
void RegisterMessageType (MessageTypeId mtid, const char *name)
 
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 SubscribeToMessageType (MessageTypeId mtid)
 Subscribe the current component type to the given message type. More...
 
void SubscribeGloballyToMessageType (MessageTypeId mtid)
 Subscribe the current component type to all messages of the given message type. More...
 
ComponentTypeId LookupCID (const std::string &cname) const
 
std::string LookupComponentTypeName (ComponentTypeId cid) const
 
void InitSystemEntity ()
 Set up an empty SYSTEM_ENTITY. More...
 
CEntityHandle GetSystemEntity ()
 Returns a CEntityHandle with id SYSTEM_ENTITY. More...
 
CEntityHandle LookupEntityHandle (entity_id_t ent, bool allowCreate=false)
 Returns a CEntityHandle with id ent. More...
 
entity_id_t AllocateNewEntity ()
 Returns a new entity ID that has never been used before. More...
 
entity_id_t AllocateNewLocalEntity ()
 Returns a new local entity ID that has never been used before. More...
 
entity_id_t AllocateNewEntity (entity_id_t preferredId)
 Returns a new entity ID that has never been used before. More...
 
bool AddComponent (CEntityHandle ent, ComponentTypeId cid, const CParamNode &paramNode)
 Constructs a component of type 'cid', initialised with data 'paramNode', and attaches it to entity 'ent'. More...
 
void AddMockComponent (CEntityHandle ent, InterfaceId iid, IComponent &component)
 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. More...
 
IComponentConstructComponent (CEntityHandle ent, ComponentTypeId cid)
 Allocates a component object of type 'cid', and attaches it to entity 'ent'. More...
 
entity_id_t AddEntity (const std::wstring &templateName, entity_id_t ent)
 Constructs an entity based on the given template, and adds it the world with entity ID ent. More...
 
void DestroyComponentsSoon (entity_id_t ent)
 Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called. More...
 
void FlushDestroyedComponents ()
 Does the actual destruction of components from DestroyComponentsSoon. More...
 
IComponentQueryInterface (entity_id_t ent, InterfaceId iid) const
 
InterfaceList GetEntitiesWithInterface (InterfaceId iid) const
 
const InterfaceListUnorderedGetEntitiesWithInterfaceUnordered (InterfaceId iid) const
 
void PostMessage (entity_id_t ent, const CMessage &msg) const
 Send a message, targeted at a particular entity. More...
 
void BroadcastMessage (const CMessage &msg) const
 Send a message, not targeted at any particular entity. More...
 
void ResetState ()
 Resets the dynamic simulation state (deletes all entities, resets entity ID counters; doesn't unload/reload component scripts). More...
 
bool ComputeStateHash (std::string &outHash, bool quick)
 
bool DumpDebugState (std::ostream &stream, bool includeDebugInfo)
 
bool SerializeState (std::ostream &stream)
 
bool DeserializeState (std::istream &stream)
 
std::string GenerateSchema ()
 
ScriptInterfaceGetScriptInterface ()
 

Private Types

enum  EComponentTypeType { CT_Native, CT_ScriptWrapper, CT_Script }
 
typedef IComponent *(* AllocFunc )(ScriptInterface &scriptInterface, jsval ctor)
 
typedef void(* DeallocFunc )(IComponent *)
 

Private Member Functions

 NONCOPYABLE (CComponentManager)
 
CMessageConstructMessage (int mtid, CScriptVal data)
 
void SendGlobalMessage (entity_id_t ent, const CMessage &msg) const
 
ComponentTypeId GetScriptWrapper (InterfaceId iid)
 
CEntityHandle AllocateEntityHandle (entity_id_t ent)
 

Static Private Member Functions

static void Script_RegisterComponentType (void *cbdata, int iid, std::string cname, CScriptVal ctor)
 
static void Script_RegisterInterface (void *cbdata, std::string name)
 
static void Script_RegisterMessageType (void *cbdata, std::string name)
 
static void Script_RegisterGlobal (void *cbdata, std::string name, CScriptVal value)
 
static IComponentScript_QueryInterface (void *cbdata, int ent, int iid)
 
static std::vector< int > Script_GetEntitiesWithInterface (void *cbdata, int iid)
 
static std::vector< IComponent * > Script_GetComponentsWithInterface (void *cbdata, int iid)
 
static void Script_PostMessage (void *cbdata, int ent, int mtid, CScriptVal data)
 
static void Script_BroadcastMessage (void *cbdata, int mtid, CScriptVal data)
 
static int Script_AddEntity (void *cbdata, std::string templateName)
 
static int Script_AddLocalEntity (void *cbdata, std::string templateName)
 
static void Script_DestroyEntity (void *cbdata, int ent)
 
static CScriptVal Script_ReadJSONFile (void *cbdata, std::wstring fileName)
 
static CScriptVal Script_ReadCivJSONFile (void *cbdata, std::wstring fileName)
 
static std::vector< std::string > Script_FindJSONFiles (void *cbdata, std::wstring subPath, bool recursive)
 
static CScriptVal ReadJSONFile (void *cbdata, std::wstring filePath, std::wstring fileName)
 
static Status FindJSONFilesCallback (const VfsPath &, const CFileInfo &, const uintptr_t)
 

Private Attributes

ScriptInterface m_ScriptInterface
 
CSimContextm_SimContext
 
CEntityHandle m_SystemEntity
 
ComponentTypeId m_CurrentComponent
 
bool m_CurrentlyHotloading
 
std::map< ComponentTypeId,
ComponentType
m_ComponentTypesById
 
std::vector
< boost::unordered_map
< entity_id_t, IComponent * > > 
m_ComponentsByInterface
 
std::map< ComponentTypeId,
std::map< entity_id_t,
IComponent * > > 
m_ComponentsByTypeId
 
std::map< MessageTypeId,
std::vector< ComponentTypeId > > 
m_LocalMessageSubscriptions
 
std::map< MessageTypeId,
std::vector< ComponentTypeId > > 
m_GlobalMessageSubscriptions
 
std::map< std::string,
ComponentTypeId
m_ComponentTypeIdsByName
 
std::map< std::string,
MessageTypeId
m_MessageTypeIdsByName
 
std::map< MessageTypeId,
std::string > 
m_MessageTypeNamesById
 
std::map< std::string,
InterfaceId
m_InterfaceIdsByName
 
std::map< entity_id_t,
SEntityComponentCache * > 
m_ComponentCaches
 
std::vector< entity_id_tm_DestructionQueue
 
ComponentTypeId m_NextScriptComponentTypeId
 
entity_id_t m_NextEntityId
 
entity_id_t m_NextLocalEntityId
 
boost::rand48 m_RNG
 

Friends

class TestComponentManager
 

Detailed Description

Definition at line 37 of file ComponentManager.h.

Member Typedef Documentation

typedef IComponent*(* CComponentManager::AllocFunc)(ScriptInterface &scriptInterface, jsval ctor)
private

Definition at line 49 of file ComponentManager.h.

Definition at line 44 of file ComponentManager.h.

typedef void(* CComponentManager::DeallocFunc)(IComponent *)
private

Definition at line 50 of file ComponentManager.h.

Definition at line 43 of file ComponentManager.h.

typedef std::vector<std::pair<entity_id_t, IComponent*> > CComponentManager::InterfaceList

Definition at line 206 of file ComponentManager.h.

Definition at line 207 of file ComponentManager.h.

Definition at line 45 of file ComponentManager.h.

Member Enumeration Documentation

Enumerator
CT_Native 
CT_ScriptWrapper 
CT_Script 

Definition at line 56 of file ComponentManager.h.

Constructor & Destructor Documentation

CComponentManager::CComponentManager ( CSimContext context,
bool  skipScriptFunctions = false 
)

Definition at line 55 of file ComponentManager.cpp.

CComponentManager::~CComponentManager ( )

Definition at line 108 of file ComponentManager.cpp.

Member Function Documentation

bool CComponentManager::AddComponent ( CEntityHandle  ent,
ComponentTypeId  cid,
const CParamNode paramNode 
)

Constructs a component of type 'cid', initialised with data 'paramNode', and attaches it to entity 'ent'.

Returns
true on success; false on failure, and logs an error message

Definition at line 583 of file ComponentManager.cpp.

entity_id_t CComponentManager::AddEntity ( const std::wstring &  templateName,
entity_id_t  ent 
)

Constructs an entity based on the given template, and adds it the world with entity ID ent.

There should not be any existing components with that entity ID.

Returns
ent, or INVALID_ENTITY on error

Definition at line 702 of file ComponentManager.cpp.

void CComponentManager::AddMockComponent ( CEntityHandle  ent,
InterfaceId  iid,
IComponent component 
)

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.

(This is intended for unit tests that need to add mock objects the tested components expect to exist.)

Definition at line 650 of file ComponentManager.cpp.

CEntityHandle CComponentManager::AllocateEntityHandle ( entity_id_t  ent)
private

Definition at line 665 of file ComponentManager.cpp.

entity_id_t CComponentManager::AllocateNewEntity ( )

Returns a new entity ID that has never been used before.

This affects the simulation state so it must only be called in network-synchronised ways.

Definition at line 555 of file ComponentManager.cpp.

entity_id_t CComponentManager::AllocateNewEntity ( entity_id_t  preferredId)

Returns a new entity ID that has never been used before.

If possible, returns preferredId, and ensures this ID won't be allocated again. This affects the simulation state so it must only be called in network-synchronised ways.

Definition at line 569 of file ComponentManager.cpp.

entity_id_t CComponentManager::AllocateNewLocalEntity ( )

Returns a new local entity ID that has never been used before.

This entity will not be synchronised over the network, stored in saved games, etc.

Definition at line 562 of file ComponentManager.cpp.

void CComponentManager::BroadcastMessage ( const CMessage msg) const

Send a message, not targeted at any particular entity.

The message will be received by any components that subscribed (either globally or not) to the message type.

Definition at line 873 of file ComponentManager.cpp.

bool CComponentManager::ComputeStateHash ( std::string &  outHash,
bool  quick 
)

Definition at line 96 of file ComponentManagerSerialization.cpp.

IComponent * CComponentManager::ConstructComponent ( CEntityHandle  ent,
ComponentTypeId  cid 
)

Allocates a component object of type 'cid', and attaches it to entity 'ent'.

(The component's Init is not called here - either Init or Deserialize must be called before using the returned object.)

Definition at line 593 of file ComponentManager.cpp.

CMessage * CComponentManager::ConstructMessage ( int  mtid,
CScriptVal  data 
)
private

Definition at line 378 of file ComponentManager.cpp.

bool CComponentManager::DeserializeState ( std::istream &  stream)

Definition at line 255 of file ComponentManagerSerialization.cpp.

void CComponentManager::DestroyComponentsSoon ( entity_id_t  ent)

Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called.

Has no effect if the entity does not exist, or has already been added to the destruction queue.

Definition at line 748 of file ComponentManager.cpp.

bool CComponentManager::DumpDebugState ( std::ostream &  stream,
bool  includeDebugInfo 
)

Definition at line 47 of file ComponentManagerSerialization.cpp.

Status CComponentManager::FindJSONFilesCallback ( const VfsPath pathname,
const CFileInfo fileInfo,
const uintptr_t  cbData 
)
staticprivate

Definition at line 1023 of file ComponentManager.cpp.

void CComponentManager::FlushDestroyedComponents ( )

Does the actual destruction of components from DestroyComponentsSoon.

This must not be called if the component manager is on the call stack (since it will break internal iterators).

Definition at line 753 of file ComponentManager.cpp.

std::string CComponentManager::GenerateSchema ( )

Definition at line 934 of file ComponentManager.cpp.

CComponentManager::InterfaceList CComponentManager::GetEntitiesWithInterface ( InterfaceId  iid) const

Definition at line 815 of file ComponentManager.cpp.

const CComponentManager::InterfaceListUnordered & CComponentManager::GetEntitiesWithInterfaceUnordered ( InterfaceId  iid) const

Definition at line 837 of file ComponentManager.cpp.

ScriptInterface& CComponentManager::GetScriptInterface ( )
inline

Definition at line 241 of file ComponentManager.h.

CComponentManager::ComponentTypeId CComponentManager::GetScriptWrapper ( InterfaceId  iid)
private

Definition at line 542 of file ComponentManager.cpp.

CEntityHandle CComponentManager::GetSystemEntity ( )
inline

Returns a CEntityHandle with id SYSTEM_ENTITY.

Definition at line 134 of file ComponentManager.h.

void CComponentManager::InitSystemEntity ( )

Set up an empty SYSTEM_ENTITY.

Must be called after ResetState() and before GetSystemEntity().

Definition at line 695 of file ComponentManager.cpp.

void CComponentManager::LoadComponentTypes ( )

Definition at line 113 of file ComponentManager.cpp.

bool CComponentManager::LoadScript ( const VfsPath filename,
bool  hotload = false 
)

Load a script and execute it in a new function scope.

Parameters
filenameVFS path to load
hotloadset to true if this script has been loaded before, and redefinitions of existing components should not be considered errors

Definition at line 135 of file ComponentManager.cpp.

CComponentManager::ComponentTypeId CComponentManager::LookupCID ( const std::string &  cname) const
Parameters
cnameRequested component type name (not including any "CID" or "CCmp" prefix)
Returns
The component type id, or CID__Invalid if not found

Definition at line 526 of file ComponentManager.cpp.

std::string CComponentManager::LookupComponentTypeName ( ComponentTypeId  cid) const
Returns
The name of the given component type, or "" if not found

Definition at line 534 of file ComponentManager.cpp.

CEntityHandle CComponentManager::LookupEntityHandle ( entity_id_t  ent,
bool  allowCreate = false 
)

Returns a CEntityHandle with id ent.

If allowCreate is true and there is no existing CEntityHandle, a new handle will be allocated.

Definition at line 680 of file ComponentManager.cpp.

CComponentManager::NONCOPYABLE ( CComponentManager  )
private
void CComponentManager::PostMessage ( entity_id_t  ent,
const CMessage msg 
) const

Send a message, targeted at a particular entity.

The message will be received by any components of that entity which subscribed to the message type, and by any other components that subscribed globally to the message type.

Definition at line 848 of file ComponentManager.cpp.

IComponent * CComponentManager::QueryInterface ( entity_id_t  ent,
InterfaceId  iid 
) const

Definition at line 797 of file ComponentManager.cpp.

CScriptVal CComponentManager::ReadJSONFile ( void *  cbdata,
std::wstring  filePath,
std::wstring  fileName 
)
staticprivate

Definition at line 1014 of file ComponentManager.cpp.

void CComponentManager::RegisterComponentType ( InterfaceId  iid,
ComponentTypeId  cid,
AllocFunc  alloc,
DeallocFunc  dealloc,
const char *  name,
const std::string &  schema 
)

Definition at line 485 of file ComponentManager.cpp.

void CComponentManager::RegisterComponentTypeScriptWrapper ( InterfaceId  iid,
ComponentTypeId  cid,
AllocFunc  alloc,
DeallocFunc  dealloc,
const char *  name,
const std::string &  schema 
)

Definition at line 493 of file ComponentManager.cpp.

void CComponentManager::RegisterMessageType ( MessageTypeId  mtid,
const char *  name 
)

Definition at line 502 of file ComponentManager.cpp.

void CComponentManager::ResetState ( )

Resets the dynamic simulation state (deletes all entities, resets entity ID counters; doesn't unload/reload component scripts).

Definition at line 451 of file ComponentManager.cpp.

int CComponentManager::Script_AddEntity ( void *  cbdata,
std::string  templateName 
)
staticprivate

Definition at line 420 of file ComponentManager.cpp.

int CComponentManager::Script_AddLocalEntity ( void *  cbdata,
std::string  templateName 
)
staticprivate

Definition at line 432 of file ComponentManager.cpp.

void CComponentManager::Script_BroadcastMessage ( void *  cbdata,
int  mtid,
CScriptVal  data 
)
staticprivate

Definition at line 407 of file ComponentManager.cpp.

void CComponentManager::Script_DestroyEntity ( void *  cbdata,
int  ent 
)
staticprivate

Definition at line 444 of file ComponentManager.cpp.

std::vector< std::string > CComponentManager::Script_FindJSONFiles ( void *  cbdata,
std::wstring  subPath,
bool  recursive 
)
staticprivate

Definition at line 1036 of file ComponentManager.cpp.

std::vector< IComponent * > CComponentManager::Script_GetComponentsWithInterface ( void *  cbdata,
int  iid 
)
staticprivate

Definition at line 367 of file ComponentManager.cpp.

std::vector< int > CComponentManager::Script_GetEntitiesWithInterface ( void *  cbdata,
int  iid 
)
staticprivate

Definition at line 354 of file ComponentManager.cpp.

void CComponentManager::Script_PostMessage ( void *  cbdata,
int  ent,
int  mtid,
CScriptVal  data 
)
staticprivate

Definition at line 394 of file ComponentManager.cpp.

IComponent * CComponentManager::Script_QueryInterface ( void *  cbdata,
int  ent,
int  iid 
)
staticprivate

Definition at line 347 of file ComponentManager.cpp.

CScriptVal CComponentManager::Script_ReadCivJSONFile ( void *  cbdata,
std::wstring  fileName 
)
staticprivate

Definition at line 1009 of file ComponentManager.cpp.

CScriptVal CComponentManager::Script_ReadJSONFile ( void *  cbdata,
std::wstring  fileName 
)
staticprivate

Definition at line 1004 of file ComponentManager.cpp.

void CComponentManager::Script_RegisterComponentType ( void *  cbdata,
int  iid,
std::string  cname,
CScriptVal  ctor 
)
staticprivate

Definition at line 146 of file ComponentManager.cpp.

void CComponentManager::Script_RegisterGlobal ( void *  cbdata,
std::string  name,
CScriptVal  value 
)
staticprivate

Definition at line 338 of file ComponentManager.cpp.

void CComponentManager::Script_RegisterInterface ( void *  cbdata,
std::string  name 
)
staticprivate

Definition at line 297 of file ComponentManager.cpp.

void CComponentManager::Script_RegisterMessageType ( void *  cbdata,
std::string  name 
)
staticprivate

Definition at line 318 of file ComponentManager.cpp.

void CComponentManager::SendGlobalMessage ( entity_id_t  ent,
const CMessage msg 
) const
private

Definition at line 898 of file ComponentManager.cpp.

bool CComponentManager::SerializeState ( std::ostream &  stream)

Definition at line 172 of file ComponentManagerSerialization.cpp.

void CComponentManager::SubscribeGloballyToMessageType ( MessageTypeId  mtid)

Subscribe the current component type to all messages of the given message type.

Each component's HandleMessage will be called on any BroadcastMessage or PostMessage of this message type, regardless of the entity. Must only be called by a component type's ClassInit.

Definition at line 517 of file ComponentManager.cpp.

void CComponentManager::SubscribeToMessageType ( MessageTypeId  mtid)

Subscribe the current component type to the given message type.

Each component's HandleMessage will be called on any BroadcastMessage of this message type, or on any PostMessage of this type targeted at the component's entity. Must only be called by a component type's ClassInit.

Definition at line 508 of file ComponentManager.cpp.

Friends And Related Function Documentation

friend class TestComponentManager
friend

Definition at line 305 of file ComponentManager.h.

Member Data Documentation

std::map<entity_id_t, SEntityComponentCache*> CComponentManager::m_ComponentCaches
private

Definition at line 292 of file ComponentManager.h.

std::vector<boost::unordered_map<entity_id_t, IComponent*> > CComponentManager::m_ComponentsByInterface
private

Definition at line 283 of file ComponentManager.h.

std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > CComponentManager::m_ComponentsByTypeId
private

Definition at line 284 of file ComponentManager.h.

std::map<std::string, ComponentTypeId> CComponentManager::m_ComponentTypeIdsByName
private

Definition at line 287 of file ComponentManager.h.

std::map<ComponentTypeId, ComponentType> CComponentManager::m_ComponentTypesById
private

Definition at line 282 of file ComponentManager.h.

ComponentTypeId CComponentManager::m_CurrentComponent
private

Definition at line 278 of file ComponentManager.h.

bool CComponentManager::m_CurrentlyHotloading
private

Definition at line 279 of file ComponentManager.h.

std::vector<entity_id_t> CComponentManager::m_DestructionQueue
private

Definition at line 297 of file ComponentManager.h.

std::map<MessageTypeId, std::vector<ComponentTypeId> > CComponentManager::m_GlobalMessageSubscriptions
private

Definition at line 286 of file ComponentManager.h.

std::map<std::string, InterfaceId> CComponentManager::m_InterfaceIdsByName
private

Definition at line 290 of file ComponentManager.h.

std::map<MessageTypeId, std::vector<ComponentTypeId> > CComponentManager::m_LocalMessageSubscriptions
private

Definition at line 285 of file ComponentManager.h.

std::map<std::string, MessageTypeId> CComponentManager::m_MessageTypeIdsByName
private

Definition at line 288 of file ComponentManager.h.

std::map<MessageTypeId, std::string> CComponentManager::m_MessageTypeNamesById
private

Definition at line 289 of file ComponentManager.h.

entity_id_t CComponentManager::m_NextEntityId
private

Definition at line 300 of file ComponentManager.h.

entity_id_t CComponentManager::m_NextLocalEntityId
private

Definition at line 301 of file ComponentManager.h.

ComponentTypeId CComponentManager::m_NextScriptComponentTypeId
private

Definition at line 299 of file ComponentManager.h.

boost::rand48 CComponentManager::m_RNG
private

Definition at line 303 of file ComponentManager.h.

ScriptInterface CComponentManager::m_ScriptInterface
private

Definition at line 273 of file ComponentManager.h.

CSimContext& CComponentManager::m_SimContext
private

Definition at line 274 of file ComponentManager.h.

CEntityHandle CComponentManager::m_SystemEntity
private

Definition at line 276 of file ComponentManager.h.


The documentation for this class was generated from the following files: