simComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "sim/simBase.h"
  24. #include "console/consoleTypes.h"
  25. #include "component/simComponent.h"
  26. #include "io/stream.h"
  27. #include "behaviors/behaviorTemplate.h"
  28. SimComponent::SimComponent() : mOwner( NULL )
  29. {
  30. mComponentList.clear();
  31. mMutex = Mutex::createMutex();
  32. mEnabled = true;
  33. }
  34. SimComponent::~SimComponent()
  35. {
  36. Mutex::destroyMutex( mMutex );
  37. mMutex = NULL;
  38. }
  39. IMPLEMENT_CONOBJECT(SimComponent);
  40. bool SimComponent::onAdd()
  41. {
  42. if( !Parent::onAdd() )
  43. return false;
  44. if( !_registerComponents( this ) )
  45. return false;
  46. return true;
  47. }
  48. bool SimComponent::_registerComponents( SimComponent *owner )
  49. {
  50. // This method will return true if the object contains no components. See the
  51. // documentation for SimComponent::onComponentRegister for more information
  52. // on this behavior.
  53. bool ret = true;
  54. // If this doesn't contain components, don't even lock the list.
  55. if( hasComponents() )
  56. {
  57. VectorPtr<SimComponent *> &components = lockComponentList();
  58. for( SimComponentIterator i = components.begin(); i != components.end(); i++ )
  59. {
  60. if( !(*i)->onComponentRegister( owner ) )
  61. {
  62. ret = false;
  63. break;
  64. }
  65. AssertFatal( (*i)->mOwner == owner, "Component failed to call parent onComponentRegister!" );
  66. // Recurse
  67. if( !(*i)->_registerComponents( owner ) )
  68. {
  69. ret = false;
  70. break;
  71. }
  72. }
  73. unlockComponentList();
  74. }
  75. return ret;
  76. }
  77. void SimComponent::_unregisterComponents()
  78. {
  79. if( !hasComponents() )
  80. return;
  81. VectorPtr<SimComponent *> &components = lockComponentList();
  82. for( SimComponentIterator i = components.begin(); i != components.end(); i++ )
  83. {
  84. (*i)->onComponentUnRegister();
  85. AssertFatal( (*i)->mOwner == NULL, "Component failed to call parent onUnRegister" );
  86. // Recurse
  87. (*i)->_unregisterComponents();
  88. }
  89. unlockComponentList();
  90. }
  91. void SimComponent::onRemove()
  92. {
  93. _unregisterComponents();
  94. // Delete all components
  95. VectorPtr<SimComponent *>&componentList = lockComponentList();
  96. while(componentList.size() > 0)
  97. {
  98. SimComponent *c = componentList[0];
  99. componentList.erase( componentList.begin() );
  100. if( c->isProperlyAdded() )
  101. c->deleteObject();
  102. else if( !c->isRemoved() && !c->isDeleted() )
  103. delete c;
  104. // else, something else is deleting this, don't mess with it
  105. }
  106. unlockComponentList();
  107. Parent::onRemove();
  108. }
  109. //////////////////////////////////////////////////////////////////////////
  110. bool SimComponent::processArguments(S32 argc, const char **argv)
  111. {
  112. for(S32 i = 0; i < argc; i++)
  113. {
  114. SimComponent *obj = dynamic_cast<SimComponent*> (Sim::findObject(argv[i]) );
  115. if(obj)
  116. addComponent(obj);
  117. else
  118. Con::printf("SimComponent::processArguments - Invalid Component Object \"%s\"", argv[i]);
  119. }
  120. return true;
  121. }
  122. //////////////////////////////////////////////////////////////////////////
  123. void SimComponent::initPersistFields()
  124. {
  125. addProtectedField( "Enabled", TypeBool, Offset(mEnabled, SimComponent), &setEnabled, &defaultProtectedGetFn, &writeEnabled, "" );
  126. Parent::initPersistFields();
  127. }
  128. // Add Component to this one
  129. bool SimComponent::addComponent( SimComponent *component )
  130. {
  131. AssertFatal( dynamic_cast<SimObject*>(component), "SimComponent - Cannot add non SimObject derived components!" );
  132. MutexHandle mh;
  133. if( mh.lock( mMutex, true ) )
  134. {
  135. for( SimComponentIterator nItr = mComponentList.begin(); nItr != mComponentList.end(); nItr++ )
  136. {
  137. SimComponent *pComponent = dynamic_cast<SimComponent*>(*nItr);
  138. AssertFatal( pComponent, "SimComponent::addComponent - NULL component in list!" );
  139. if( pComponent == component )
  140. return true;
  141. }
  142. if(component->onComponentAdd(this))
  143. {
  144. component->mOwner = this;
  145. mComponentList.push_back( component );
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. // Remove Component from this one
  152. bool SimComponent::removeComponent( SimComponent *component )
  153. {
  154. MutexHandle mh;
  155. if( mh.lock( mMutex, true ) )
  156. {
  157. for( SimComponentIterator nItr = mComponentList.begin(); nItr != mComponentList.end(); nItr++ )
  158. {
  159. SimComponent *pComponent = dynamic_cast<SimComponent*>(*nItr);
  160. AssertFatal( pComponent, "SimComponent::removeComponent - NULL component in list!" );
  161. if( pComponent == component )
  162. {
  163. AssertFatal( component->mOwner == this, "Somehow we contain a component who doesn't think we are it's owner." );
  164. (*nItr)->onComponentRemove(this);
  165. component->mOwner = NULL;
  166. mComponentList.erase( nItr );
  167. return true;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. //////////////////////////////////////////////////////////////////////////
  174. bool SimComponent::onComponentAdd(SimComponent *target)
  175. {
  176. Con::executef(this, 2, "onComponentAdd", Con::getIntArg(target->getId()));
  177. return true;
  178. }
  179. void SimComponent::onComponentRemove(SimComponent *target)
  180. {
  181. Con::executef(this, 2, "onComponentRemove", Con::getIntArg(target->getId()));
  182. }
  183. //////////////////////////////////////////////////////////////////////////
  184. bool SimComponent::writeField(StringTableEntry fieldname, const char* value)
  185. {
  186. if (!Parent::writeField(fieldname, value))
  187. return false;
  188. if( fieldname == StringTable->insert("owner") )
  189. return false;
  190. return true;
  191. }
  192. void SimComponent::write(Stream &stream, U32 tabStop, U32 flags /* = 0 */)
  193. {
  194. #if 1
  195. Parent::write( stream, tabStop, flags );
  196. #else
  197. MutexHandle handle;
  198. handle.lock(mMutex); // When this goes out of scope, it will unlock it
  199. // export selected only?
  200. if((flags & SelectedOnly) && !isSelected())
  201. {
  202. for(U32 i = 0; i < mComponentList.size(); i++)
  203. mComponentList[i]->write(stream, tabStop, flags);
  204. return;
  205. }
  206. stream.writeTabs(tabStop);
  207. char buffer[1024];
  208. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() ? getName() : "");
  209. stream.write(dStrlen(buffer), buffer);
  210. writeFields(stream, tabStop + 1);
  211. if(mComponentList.size())
  212. {
  213. stream.write(2, "\r\n");
  214. stream.writeTabs(tabStop+1);
  215. stream.writeLine((U8 *)"// Note: This is a list of behaviors, not arbitrary SimObjects as in a SimGroup or SimSet.\r\n");
  216. for(U32 i = 0; i < mComponentList.size(); i++)
  217. mComponentList[i]->write(stream, tabStop + 1, flags);
  218. }
  219. stream.writeTabs(tabStop);
  220. stream.write(4, "};\r\n");
  221. #endif
  222. }
  223. //////////////////////////////////////////////////////////////////////////
  224. bool SimComponent::callMethodOnComponents( U32 argc, const char* argv[], const char** result )
  225. {
  226. const char *cbName = StringTable->insert(argv[0]);
  227. if (isEnabled())
  228. {
  229. if(isMethod(cbName))
  230. {
  231. // This component can handle the given method
  232. *result = Con::execute( this, argc, argv, true );
  233. return true;
  234. }
  235. else if( getComponentCount() > 0 )
  236. {
  237. // Need to try the component's children
  238. bool handled = false;
  239. VectorPtr<SimComponent *>&componentList = lockComponentList();
  240. for( SimComponentIterator nItr = (componentList.end()-1); nItr >= componentList.begin(); nItr-- )
  241. {
  242. argv[0] = cbName;
  243. SimComponent *pComponent = (*nItr);
  244. AssertFatal( pComponent, "SimComponent::callMethodOnComponents - NULL component in list!" );
  245. // Call on children
  246. handled = pComponent->callMethodOnComponents( argc, argv, result );
  247. if (handled)
  248. break;
  249. }
  250. unlockComponentList();
  251. if (handled)
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. //////////////////////////////////////////////////////////////////////////
  258. // Console Methods
  259. //////////////////////////////////////////////////////////////////////////
  260. ConsoleMethod( SimComponent, addComponents, bool, 3, 64, "%obj.addComponents( %compObjName, %compObjName2, ... );\n"
  261. "Adds additional components to current list.\n"
  262. "@param Up to 62 component names\n"
  263. "@return Returns true on success, false otherwise.")
  264. {
  265. for(S32 i = 2; i < argc; i++)
  266. {
  267. SimComponent *obj = dynamic_cast<SimComponent*> (Sim::findObject(argv[i]) );
  268. if(obj)
  269. object->addComponent(obj);
  270. else
  271. Con::printf("SimComponent::addComponents - Invalid Component Object \"%s\"", argv[i]);
  272. }
  273. return true;
  274. }
  275. ConsoleMethod( SimComponent, removeComponents, bool, 3, 64, "%obj.removeComponents( %compObjName, %compObjName2, ... );\n"
  276. "Removes components by name from current list.\n"
  277. "@param objNamex Up to 62 component names\n"
  278. "@return Returns true on success, false otherwise.")
  279. {
  280. for(S32 i = 2; i < argc; i++)
  281. {
  282. SimComponent *obj = dynamic_cast<SimComponent*> (Sim::findObject(argv[i]) );
  283. if(obj)
  284. object->removeComponent(obj);
  285. else
  286. Con::printf("SimComponent::removeComponents - Invalid Component Object \"%s\"", argv[i]);
  287. }
  288. return true;
  289. }
  290. ConsoleMethod( SimComponent, getComponentCount, S32, 2, 2, "() Get the current component count\n"
  291. "@return The number of components in the list as an integer")
  292. {
  293. return object->getComponentCount();
  294. }
  295. ConsoleMethod( SimComponent, getComponent, S32, 3, 3, "(idx) Get the component corresponding to the given index.\n"
  296. "@param idx An integer index value corresponding to the desired component.\n"
  297. "@return The id of the component at the given index as an integer")
  298. {
  299. S32 idx = dAtoi(argv[2]);
  300. if(idx < 0 || idx >= (S32)object->getComponentCount())
  301. {
  302. Con::errorf("SimComponent::getComponent - Invalid index %d", idx);
  303. return 0;
  304. }
  305. SimComponent *c = object->getComponent(idx);
  306. return c ? c->getId() : 0;
  307. }
  308. ConsoleMethod(SimComponent, setEnabled, void, 3, 3, "(enabled) Sets or unsets the enabled flag\n"
  309. "@param enabled Boolean value\n"
  310. "@return No return value")
  311. {
  312. object->setEnabled(dAtob(argv[2]));
  313. }
  314. ConsoleMethod(SimComponent, isEnabled, bool, 2, 2, "() Check whether SimComponent is currently enabled\n"
  315. "@return true if enabled and false if not")
  316. {
  317. return object->isEnabled();
  318. }