Pyrogenesis  trunk
ComponentManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_COMPONENTMANAGER
19 #define INCLUDED_COMPONENTMANAGER
20 
21 #include "Entity.h"
22 #include "Components.h"
26 #include "ps/Filesystem.h"
27 
28 #include <boost/random/linear_congruential.hpp>
29 #include <boost/unordered_map.hpp>
30 
31 #include <map>
32 #include <unordered_map>
33 
34 class IComponent;
35 class CParamNode;
36 class CMessage;
37 class CSimContext;
39 
41 {
43 public:
44  // We can't use EInterfaceId/etc directly, since scripts dynamically generate new IDs
45  // and casting arbitrary ints to enums is undefined behaviour, so use 'int' typedefs
46  typedef int InterfaceId;
47  typedef int ComponentTypeId;
48  typedef int MessageTypeId;
49 
50 private:
51  // Component allocation types
52  typedef IComponent* (*AllocFunc)(ScriptInterface& scriptInterface, JS::HandleValue ctor);
53  typedef void (*DeallocFunc)(IComponent*);
54 
55  // ComponentTypes come in three types:
56  // Native: normal C++ component
57  // ScriptWrapper: C++ component that wraps a JS component implementation
58  // Script: a ScriptWrapper linked to a specific JS component implementation
60  {
64  };
65 
66  // Representation of a component type, to be used when instantiating components
68  {
70  InterfaceId iid;
73  std::string name;
74  std::string schema; // RelaxNG fragment
75  DefPersistentRooted<JS::Value> ctor; // only valid if type == CT_Script
76 
77  // TODO: Constructor, move assignment operator and move constructor only have to be
78  // explicitly defined for Visual Studio. VS2013 is still behind on C++11 support
79  // What's missing is what they call "Rvalue references v3.0", see
80  // https://msdn.microsoft.com/en-us/library/hh567368.aspx#rvref
82  ComponentType (EComponentTypeType type, InterfaceId iid, AllocFunc alloc,
83  DeallocFunc dealloc, std::string name, std::string schema, DefPersistentRooted<JS::Value> ctor) :
84  type(type),
85  iid(iid),
86  alloc(alloc),
87  dealloc(dealloc),
88  name(name),
89  schema(schema),
90  ctor(std::move(ctor))
91  {
92  }
93 
95  {
96  type = std::move(other.type);
97  iid = std::move(other.iid);
98  alloc = std::move(other.alloc);
99  dealloc = std::move(other.dealloc);
100  name = std::move(other.name);
101  schema = std::move(other.schema);
102  ctor = std::move(other.ctor);
103  return *this;
104  }
105 
107  {
108  type = std::move(other.type);
109  iid = std::move(other.iid);
110  alloc = std::move(other.alloc);
111  dealloc = std::move(other.dealloc);
112  name = std::move(other.name);
113  schema = std::move(other.schema);
114  ctor = std::move(other.ctor);
115  }
116  };
117 
119  {
121  std::vector<std::string> templates;
122  };
123 
124 public:
125  CComponentManager(CSimContext&, shared_ptr<ScriptRuntime> rt, bool skipScriptFunctions = false);
127 
128  void LoadComponentTypes();
129 
130  /**
131  * Load a script and execute it in a new function scope.
132  * @param filename VFS path to load
133  * @param hotload set to true if this script has been loaded before, and redefinitions of
134  * existing components should not be considered errors
135  */
136  bool LoadScript(const VfsPath& filename, bool hotload = false);
137 
138  void RegisterMessageType(MessageTypeId mtid, const char* name);
139 
140  void RegisterComponentType(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema);
141  void RegisterComponentTypeScriptWrapper(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema);
142 
144 
145  /**
146  * Subscribe the current component type to the given message type.
147  * Each component's HandleMessage will be called on any BroadcastMessage of this message type,
148  * or on any PostMessage of this type targeted at the component's entity.
149  * Must only be called by a component type's ClassInit.
150  */
151  void SubscribeToMessageType(MessageTypeId mtid);
152 
153  /**
154  * Subscribe the current component type to all messages of the given message type.
155  * Each component's HandleMessage will be called on any BroadcastMessage or PostMessage of this message type,
156  * regardless of the entity.
157  * Must only be called by a component type's ClassInit.
158  */
159  void SubscribeGloballyToMessageType(MessageTypeId mtid);
160 
161  /**
162  * Subscribe the given component instance to all messages of the given message type.
163  * The component's HandleMessage will be called on any BroadcastMessage or PostMessage of
164  * this message type, regardless of the entity.
165  *
166  * This can be called at any time (including inside the HandleMessage callback for this message type).
167  *
168  * The component type must not have statically subscribed to this message type in its ClassInit.
169  *
170  * The subscription status is not saved or network-synchronised. Components must remember to
171  * resubscribe in their Deserialize methods if they still want the message.
172  *
173  * This is primarily intended for Interpolate and RenderSubmit messages, to avoid the cost of
174  * sending the message to components that do not currently need to do any rendering.
175  */
176  void DynamicSubscriptionNonsync(MessageTypeId mtid, IComponent* component, bool enabled);
177 
178  /**
179  * @param cname Requested component type name (not including any "CID" or "CCmp" prefix)
180  * @return The component type id, or CID__Invalid if not found
181  */
182  ComponentTypeId LookupCID(const std::string& cname) const;
183 
184  /**
185  * @return The name of the given component type, or "" if not found
186  */
187  std::string LookupComponentTypeName(ComponentTypeId cid) const;
188 
189  /**
190  * Set up an empty SYSTEM_ENTITY. Must be called after ResetState() and before GetSystemEntity().
191  */
192  void InitSystemEntity();
193 
194  /**
195  * Returns a CEntityHandle with id SYSTEM_ENTITY.
196  */
198 
199  /**
200  * Returns a CEntityHandle with id @p ent.
201  * If @p allowCreate is true and there is no existing CEntityHandle, a new handle will be allocated.
202  */
203  CEntityHandle LookupEntityHandle(entity_id_t ent, bool allowCreate = false);
204 
205  /**
206  * Returns a new entity ID that has never been used before.
207  * This affects the simulation state so it must only be called in network-synchronised ways.
208  */
210 
211  /**
212  * Returns a new local entity ID that has never been used before.
213  * This entity will not be synchronised over the network, stored in saved games, etc.
214  */
216 
217  /**
218  * Returns a new entity ID that has never been used before.
219  * If possible, returns preferredId, and ensures this ID won't be allocated again.
220  * This affects the simulation state so it must only be called in network-synchronised ways.
221  */
223 
224  /**
225  * Constructs a component of type 'cid', initialised with data 'paramNode',
226  * and attaches it to entity 'ent'.
227  *
228  * @return true on success; false on failure, and logs an error message
229  */
230  bool AddComponent(CEntityHandle ent, ComponentTypeId cid, const CParamNode& paramNode);
231 
232  /**
233  * Add all system components to the system entity (skip the scripted components or the AI components on demand)
234  */
235  void AddSystemComponents(bool skipScriptedComponents, bool skipAI);
236 
237  /**
238  * Adds an externally-created component, so that it is returned by QueryInterface
239  * but does not get destroyed and does not receive messages from the component manager.
240  * (This is intended for unit tests that need to add mock objects the tested components
241  * expect to exist.)
242  */
243  void AddMockComponent(CEntityHandle ent, InterfaceId iid, IComponent& component);
244 
245  /**
246  * Allocates a component object of type 'cid', and attaches it to entity 'ent'.
247  * (The component's Init is not called here - either Init or Deserialize must be called
248  * before using the returned object.)
249  */
250  IComponent* ConstructComponent(CEntityHandle ent, ComponentTypeId cid);
251 
252  /**
253  * Constructs an entity based on the given template, and adds it the world with
254  * entity ID @p ent. There should not be any existing components with that entity ID.
255  * @return ent, or INVALID_ENTITY on error
256  */
257  entity_id_t AddEntity(const std::wstring& templateName, entity_id_t ent);
258 
259  /**
260  * Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called.
261  * Has no effect if the entity does not exist, or has already been added to the destruction queue.
262  */
264 
265  /**
266  * Does the actual destruction of components from DestroyComponentsSoon.
267  * This must not be called if the component manager is on the call stack (since it
268  * will break internal iterators).
269  */
271 
272  IComponent* QueryInterface(entity_id_t ent, InterfaceId iid) const;
273 
274  typedef std::pair<entity_id_t, IComponent*> InterfacePair;
275  typedef std::vector<InterfacePair> InterfaceList;
276  typedef boost::unordered_map<entity_id_t, IComponent*> InterfaceListUnordered;
277 
278  InterfaceList GetEntitiesWithInterface(InterfaceId iid) const;
279  const InterfaceListUnordered& GetEntitiesWithInterfaceUnordered(InterfaceId iid) const;
280 
281  /**
282  * Send a message, targeted at a particular entity. The message will be received by any
283  * components of that entity which subscribed to the message type, and by any other components
284  * that subscribed globally to the message type.
285  */
286  void PostMessage(entity_id_t ent, const CMessage& msg);
287 
288  /**
289  * Send a message, not targeted at any particular entity. The message will be received by any
290  * components that subscribed (either globally or not) to the message type.
291  */
292  void BroadcastMessage(const CMessage& msg);
293 
294  /**
295  * Resets the dynamic simulation state (deletes all entities, resets entity ID counters;
296  * doesn't unload/reload component scripts).
297  */
298  void ResetState();
299 
300  /**
301  * Initializes the random number generator with a seed determined by the host.
302  */
303  void SetRNGSeed(u32 seed);
304 
305  // Various state serialization functions:
306  bool ComputeStateHash(std::string& outHash, bool quick);
307  bool DumpDebugState(std::ostream& stream, bool includeDebugInfo);
308  // FlushDestroyedComponents must be called before SerializeState (since the destruction queue
309  // won't get serialized)
310  bool SerializeState(std::ostream& stream);
311  bool DeserializeState(std::istream& stream);
312 
313  std::string GenerateSchema();
314 
316 
317 private:
318  // Implementations of functions exposed to scripts
319  static void Script_RegisterComponentType_Common(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent);
320  static void Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor);
321  static void Script_RegisterSystemComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor);
322  static void Script_ReRegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor);
323  static void Script_RegisterInterface(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name);
324  static void Script_RegisterMessageType(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name);
325  static void Script_RegisterGlobal(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name, JS::HandleValue value);
326  static IComponent* Script_QueryInterface(ScriptInterface::CxPrivate* pCxPrivate, int ent, int iid);
327  static std::vector<int> Script_GetEntitiesWithInterface(ScriptInterface::CxPrivate* pCxPrivate, int iid);
328  static std::vector<IComponent*> Script_GetComponentsWithInterface(ScriptInterface::CxPrivate* pCxPrivate, int iid);
329  static void Script_PostMessage(ScriptInterface::CxPrivate* pCxPrivate, int ent, int mtid, JS::HandleValue data);
330  static void Script_BroadcastMessage(ScriptInterface::CxPrivate* pCxPrivate, int mtid, JS::HandleValue data);
331  static int Script_AddEntity(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
332  static int Script_AddLocalEntity(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
333  static void Script_DestroyEntity(ScriptInterface::CxPrivate* pCxPrivate, int ent);
335  static JS::Value Script_ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& fileName);
336  static JS::Value Script_ReadCivJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& fileName);
337  static std::vector<std::string> Script_FindJSONFiles(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& subPath, bool recursive);
338  static JS::Value ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, const std::wstring& fileName);
339 
340  // callback function to handle recursively finding files in a directory
341  static Status FindJSONFilesCallback(const VfsPath&, const CFileInfo&, const uintptr_t);
342 
343  CMessage* ConstructMessage(int mtid, JS::HandleValue data);
344  void SendGlobalMessage(entity_id_t ent, const CMessage& msg);
345 
348 
349  ComponentTypeId GetScriptWrapper(InterfaceId iid);
350 
352 
355 
357 
358  ComponentTypeId m_CurrentComponent; // used when loading component types
360 
361  // TODO: some of these should be vectors
362  std::map<ComponentTypeId, ComponentType> m_ComponentTypesById;
363  std::vector<CComponentManager::ComponentTypeId> m_ScriptedSystemComponents;
364  std::vector<boost::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId
365  std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId;
366  std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions;
367  std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions;
368  std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName;
369  std::map<std::string, MessageTypeId> m_MessageTypeIdsByName;
370  std::map<MessageTypeId, std::string> m_MessageTypeNamesById;
371  std::map<std::string, InterfaceId> m_InterfaceIdsByName;
372 
373  std::map<MessageTypeId, CDynamicSubscription> m_DynamicMessageSubscriptionsNonsync;
374  std::map<IComponent*, std::set<MessageTypeId> > m_DynamicMessageSubscriptionsNonsyncByComponent;
375 
376  std::unordered_map<entity_id_t, SEntityComponentCache*> m_ComponentCaches;
377 
378  // TODO: maintaining both ComponentsBy* is nasty; can we get rid of one,
379  // while keeping QueryInterface and PostMessage sufficiently efficient?
380 
381  std::vector<entity_id_t> m_DestructionQueue;
382 
386 
387  boost::rand48 m_RNG;
388 
389  friend class TestComponentManager;
390 };
391 
392 #endif // INCLUDED_COMPONENTMANAGER
static std::vector< std::string > Script_FindJSONFiles(ScriptInterface::CxPrivate *pCxPrivate, const std::wstring &subPath, bool recursive)
Definition: ComponentManager.cpp:1223
An entity initialisation parameter node.
Definition: ParamNode.h:148
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
Definition: ComponentManager.cpp:574
Definition: IComponent.h:33
void FlushDestroyedComponents()
Does the actual destruction of components from DestroyComponentsSoon.
Definition: ComponentManager.cpp:902
std::map< MessageTypeId, std::vector< ComponentTypeId > > m_GlobalMessageSubscriptions
Definition: ComponentManager.h:367
void ResetState()
Resets the dynamic simulation state (deletes all entities, resets entity ID counters; doesn&#39;t unload/...
Definition: ComponentManager.cpp:503
void FlattenDynamicSubscriptions()
Definition: ComponentManager.cpp:592
void AddMockComponent(CEntityHandle ent, InterfaceId iid, IComponent &component)
Adds an externally-created component, so that it is returned by QueryInterface but does not get destr...
Definition: ComponentManager.cpp:800
void BroadcastMessage(const CMessage &msg)
Send a message, not targeted at any particular entity.
Definition: ComponentManager.cpp:1031
static void Script_RegisterComponentType_Common(ScriptInterface::CxPrivate *pCxPrivate, int iid, const std::string &cname, JS::HandleValue ctor, bool reRegister, bool systemComponent)
Definition: ComponentManager.cpp:149
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...
Definition: ComponentManager.cpp:852
std::unordered_map< entity_id_t, SEntityComponentCache * > m_ComponentCaches
Definition: ComponentManager.h:376
void SetRNGSeed(u32 seed)
Initializes the random number generator with a seed determined by the host.
Definition: ComponentManager.cpp:541
std::vector< boost::unordered_map< entity_id_t, IComponent * > > m_ComponentsByInterface
Definition: ComponentManager.h:364
static int Script_AddEntity(ScriptInterface::CxPrivate *pCxPrivate, const std::string &templateName)
Definition: ComponentManager.cpp:466
int InterfaceId
Definition: ComponentManager.h:46
AllocFunc alloc
Definition: ComponentManager.h:71
int MessageTypeId
Definition: ComponentManager.h:48
InterfaceId iid
Definition: ComponentManager.h:70
entity_id_t m_NextLocalEntityId
Definition: ComponentManager.h:385
DeallocFunc dealloc
Definition: ComponentManager.h:72
~CComponentManager()
Definition: ComponentManager.cpp:110
static void Script_RegisterComponentType(ScriptInterface::CxPrivate *pCxPrivate, int iid, const std::string &cname, JS::HandleValue ctor)
Definition: ComponentManager.cpp:319
IComponent * QueryInterface(entity_id_t ent, InterfaceId iid) const
Definition: ComponentManager.cpp:953
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:79
std::string schema
Definition: ComponentManager.h:74
static void Script_RegisterGlobal(ScriptInterface::CxPrivate *pCxPrivate, const std::string &name, JS::HandleValue value)
Definition: ComponentManager.cpp:385
CComponentManager(CSimContext &, shared_ptr< ScriptRuntime > rt, bool skipScriptFunctions=false)
Definition: ComponentManager.cpp:56
static void Script_BroadcastMessage(ScriptInterface::CxPrivate *pCxPrivate, int mtid, JS::HandleValue data)
Definition: ComponentManager.cpp:453
ScriptInterface & GetScriptInterface()
Definition: ComponentManager.h:315
CSimContext & m_SimContext
Definition: ComponentManager.h:354
friend class TestComponentManager
Definition: ComponentManager.h:389
Definition: unique_range.h:196
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:315
std::vector< std::string > templates
Definition: ComponentManager.h:121
Definition: ComponentManager.h:67
ComponentType(EComponentTypeType type, InterfaceId iid, AllocFunc alloc, DeallocFunc dealloc, std::string name, std::string schema, DefPersistentRooted< JS::Value > ctor)
Definition: ComponentManager.h:82
Definition: ComponentManager.h:118
static JS::Value Script_ReadJSONFile(ScriptInterface::CxPrivate *pCxPrivate, const std::wstring &fileName)
Definition: ComponentManager.cpp:1188
Contains pointers to various &#39;global&#39; objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition: SimContext.h:32
bool SerializeState(std::ostream &stream)
Definition: ComponentManagerSerialization.cpp:180
void LoadComponentTypes()
Definition: ComponentManager.cpp:115
std::map< std::string, MessageTypeId > m_MessageTypeIdsByName
Definition: ComponentManager.h:369
static JS::Value Script_ReadCivJSONFile(ScriptInterface::CxPrivate *pCxPrivate, const std::wstring &fileName)
Definition: ComponentManager.cpp:1193
entity_id_t m_NextEntityId
Definition: ComponentManager.h:384
const entity_id_t SYSTEM_ENTITY
Entity ID for singleton &#39;system&#39; components.
Definition: Entity.h:43
CMessage * ConstructMessage(int mtid, JS::HandleValue data)
Definition: ComponentManager.cpp:425
static IComponent * Script_QueryInterface(ScriptInterface::CxPrivate *pCxPrivate, int ent, int iid)
Definition: ComponentManager.cpp:394
static void Script_DestroyEntity(ScriptInterface::CxPrivate *pCxPrivate, int ent)
Definition: ComponentManager.cpp:490
void RegisterMessageType(MessageTypeId mtid, const char *name)
Definition: ComponentManager.cpp:568
CEntityHandle LookupEntityHandle(entity_id_t ent, bool allowCreate=false)
Returns a CEntityHandle with id ent.
Definition: ComponentManager.cpp:830
std::vector< InterfacePair > InterfaceList
Definition: ComponentManager.h:275
boost::rand48 m_RNG
Definition: ComponentManager.h:387
static JS::Value ReadJSONFile(ScriptInterface::CxPrivate *pCxPrivate, const std::wstring &filePath, const std::wstring &fileName)
Definition: ComponentManager.cpp:1198
const InterfaceListUnordered & GetEntitiesWithInterfaceUnordered(InterfaceId iid) const
Definition: ComponentManager.cpp:993
static std::vector< IComponent * > Script_GetComponentsWithInterface(ScriptInterface::CxPrivate *pCxPrivate, int iid)
Definition: ComponentManager.cpp:414
std::map< std::string, ComponentTypeId > m_ComponentTypeIdsByName
Definition: ComponentManager.h:368
EComponentTypeType
Definition: ComponentManager.h:59
uint32_t u32
Definition: types.h:39
InterfaceList GetEntitiesWithInterface(InterfaceId iid) const
Definition: ComponentManager.cpp:971
bool m_CurrentlyHotloading
Definition: ComponentManager.h:359
std::map< ComponentTypeId, std::map< entity_id_t, IComponent * > > m_ComponentsByTypeId
Definition: ComponentManager.h:365
static Status FindJSONFilesCallback(const VfsPath &, const CFileInfo &, const uintptr_t)
Definition: ComponentManager.cpp:1210
Config::Value_type Value
Definition: json_spirit_value.h:181
Definition: ComponentManager.h:40
Definition: path.h:77
NONCOPYABLE(CComponentManager)
bool ComputeStateHash(std::string &outHash, bool quick)
Definition: ComponentManagerSerialization.cpp:98
std::map< std::string, InterfaceId > m_InterfaceIdsByName
Definition: ComponentManager.h:371
int ComponentTypeId
Definition: ComponentManager.h:47
static int Script_AddLocalEntity(ScriptInterface::CxPrivate *pCxPrivate, const std::string &templateName)
Definition: ComponentManager.cpp:478
static void Script_FlushDestroyedEntities(ScriptInterface::CxPrivate *pCxPrivate)
Definition: ComponentManager.cpp:497
CEntityHandle AllocateEntityHandle(entity_id_t ent)
Definition: ComponentManager.cpp:815
ScriptInterface m_ScriptInterface
Definition: ComponentManager.h:353
static void Script_RegisterSystemComponentType(ScriptInterface::CxPrivate *pCxPrivate, int iid, const std::string &cname, JS::HandleValue ctor)
Definition: ComponentManager.cpp:326
static std::vector< int > Script_GetEntitiesWithInterface(ScriptInterface::CxPrivate *pCxPrivate, int iid)
Definition: ComponentManager.cpp:401
void RegisterComponentType(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char *, const std::string &schema)
Definition: ComponentManager.cpp:546
std::map< MessageTypeId, std::string > m_MessageTypeNamesById
Definition: ComponentManager.h:370
void PostMessage(entity_id_t ent, const CMessage &msg)
Send a message, targeted at a particular entity.
Definition: ComponentManager.cpp:1004
A list of components that are dynamically subscribed to a particular message.
Definition: DynamicSubscription.h:35
entity_id_t GetId() const
Definition: Entity.h:88
void MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid)
Definition: ComponentManager.cpp:563
Definition: ComponentManager.h:63
std::string name
Definition: ComponentManager.h:73
i64 Status
Error handling system.
Definition: status.h:171
void InitSystemEntity()
Set up an empty SYSTEM_ENTITY.
Definition: ComponentManager.cpp:845
std::map< MessageTypeId, CDynamicSubscription > m_DynamicMessageSubscriptionsNonsync
Definition: ComponentManager.h:373
void SubscribeGloballyToMessageType(MessageTypeId mtid)
Subscribe the current component type to all messages of the given message type.
Definition: ComponentManager.cpp:583
void SendGlobalMessage(entity_id_t ent, const CMessage &msg)
Definition: ComponentManager.cpp:1056
entity_id_t AllocateNewLocalEntity()
Returns a new local entity ID that has never been used before.
Definition: ComponentManager.cpp:681
static void Script_RegisterMessageType(ScriptInterface::CxPrivate *pCxPrivate, const std::string &name)
Definition: ComponentManager.cpp:362
bool DeserializeState(std::istream &stream)
Definition: ComponentManagerSerialization.cpp:296
std::string LookupComponentTypeName(ComponentTypeId cid) const
Definition: ComponentManager.cpp:644
Definition: ComponentManager.h:61
bool AddComponent(CEntityHandle ent, ComponentTypeId cid, const CParamNode &paramNode)
Constructs a component of type &#39;cid&#39;, initialised with data &#39;paramNode&#39;, and attaches it to entity &#39;e...
Definition: ComponentManager.cpp:703
bool DumpDebugState(std::ostream &stream, bool includeDebugInfo)
Definition: ComponentManagerSerialization.cpp:49
bool LoadScript(const VfsPath &filename, bool hotload=false)
Load a script and execute it in a new function scope.
Definition: ComponentManager.cpp:137
IComponent *(* AllocFunc)(ScriptInterface &scriptInterface, JS::HandleValue ctor)
Definition: ComponentManager.h:52
ComponentTypeId m_CurrentComponent
Definition: ComponentManager.h:358
ComponentTypeId m_NextScriptComponentTypeId
Definition: ComponentManager.h:383
VfsPath path
Definition: ComponentManager.h:120
std::map< MessageTypeId, std::vector< ComponentTypeId > > m_LocalMessageSubscriptions
Definition: ComponentManager.h:366
IComponent * ConstructComponent(CEntityHandle ent, ComponentTypeId cid)
Allocates a component object of type &#39;cid&#39;, and attaches it to entity &#39;ent&#39;.
Definition: ComponentManager.cpp:740
void RegisterComponentTypeScriptWrapper(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char *, const std::string &schema)
Definition: ComponentManager.cpp:554
std::vector< entity_id_t > m_DestructionQueue
Definition: ComponentManager.h:381
ComponentType(ComponentType &&other)
Definition: ComponentManager.h:106
ComponentTypeId LookupCID(const std::string &cname) const
Definition: ComponentManager.cpp:636
Definition: ScriptInterface.h:99
std::string GenerateSchema()
Definition: ComponentManager.cpp:1103
std::map< ComponentTypeId, ComponentType > m_ComponentTypesById
Definition: ComponentManager.h:362
CEntityHandle GetSystemEntity()
Returns a CEntityHandle with id SYSTEM_ENTITY.
Definition: ComponentManager.h:197
boost::unordered_map< entity_id_t, IComponent * > InterfaceListUnordered
Definition: ComponentManager.h:276
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
std::map< IComponent *, std::set< MessageTypeId > > m_DynamicMessageSubscriptionsNonsyncByComponent
Definition: ComponentManager.h:374
void DestroyComponentsSoon(entity_id_t ent)
Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called...
Definition: ComponentManager.cpp:897
std::vector< CComponentManager::ComponentTypeId > m_ScriptedSystemComponents
Definition: ComponentManager.h:363
void(* DeallocFunc)(IComponent *)
Definition: ComponentManager.h:53
static void Script_RegisterInterface(ScriptInterface::CxPrivate *pCxPrivate, const std::string &name)
Definition: ComponentManager.cpp:338
void RemoveComponentDynamicSubscriptions(IComponent *component)
Definition: ComponentManager.cpp:618
Definition: ComponentManager.h:62
ComponentType & operator=(ComponentType &&other)
Definition: ComponentManager.h:94
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
ComponentType()
Definition: ComponentManager.h:81
std::pair< entity_id_t, IComponent * > InterfacePair
Definition: ComponentManager.h:274
Definition: file_system.h:41
DefPersistentRooted< JS::Value > ctor
Definition: ComponentManager.h:75
CEntityHandle m_SystemEntity
Definition: ComponentManager.h:356
ComponentTypeId GetScriptWrapper(InterfaceId iid)
Definition: ComponentManager.cpp:652
static void Script_PostMessage(ScriptInterface::CxPrivate *pCxPrivate, int ent, int mtid, JS::HandleValue data)
Definition: ComponentManager.cpp:440
Definition: Message.h:24
EComponentTypeType type
Definition: ComponentManager.h:69
void DynamicSubscriptionNonsync(MessageTypeId mtid, IComponent *component, bool enabled)
Subscribe the given component instance to all messages of the given message type. ...
Definition: ComponentManager.cpp:602
entity_id_t AllocateNewEntity()
Returns a new entity ID that has never been used before.
Definition: ComponentManager.cpp:674
void AddSystemComponents(bool skipScriptedComponents, bool skipAI)
Add all system components to the system entity (skip the scripted components or the AI components on ...
Definition: ComponentManager.cpp:713
static void Script_ReRegisterComponentType(ScriptInterface::CxPrivate *pCxPrivate, int iid, const std::string &cname, JS::HandleValue ctor)
Definition: ComponentManager.cpp:333