simComponent.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include "simComponent_ScriptBinding.h"
  29. SimComponent::SimComponent() : mOwner( NULL )
  30. {
  31. mComponentList.clear();
  32. mMutex = Mutex::createMutex();
  33. mEnabled = true;
  34. }
  35. SimComponent::~SimComponent()
  36. {
  37. Mutex::destroyMutex( mMutex );
  38. mMutex = NULL;
  39. }
  40. IMPLEMENT_CONOBJECT(SimComponent);
  41. bool SimComponent::onAdd()
  42. {
  43. if( !Parent::onAdd() )
  44. return false;
  45. if( !_registerComponents( this ) )
  46. return false;
  47. return true;
  48. }
  49. bool SimComponent::_registerComponents( SimComponent *owner )
  50. {
  51. // This method will return true if the object contains no components. See the
  52. // documentation for SimComponent::onComponentRegister for more information
  53. // on this behavior.
  54. bool ret = true;
  55. // If this doesn't contain components, don't even lock the list.
  56. if( hasComponents() )
  57. {
  58. VectorPtr<SimComponent *> &components = lockComponentList();
  59. for( SimComponentIterator i = components.begin(); i != components.end(); i++ )
  60. {
  61. if( !(*i)->onComponentRegister( owner ) )
  62. {
  63. ret = false;
  64. break;
  65. }
  66. AssertFatal( (*i)->mOwner == owner, "Component failed to call parent onComponentRegister!" );
  67. // Recurse
  68. if( !(*i)->_registerComponents( owner ) )
  69. {
  70. ret = false;
  71. break;
  72. }
  73. }
  74. unlockComponentList();
  75. }
  76. return ret;
  77. }
  78. void SimComponent::_unregisterComponents()
  79. {
  80. if( !hasComponents() )
  81. return;
  82. VectorPtr<SimComponent *> &components = lockComponentList();
  83. for( SimComponentIterator i = components.begin(); i != components.end(); i++ )
  84. {
  85. (*i)->onComponentUnRegister();
  86. AssertFatal( (*i)->mOwner == NULL, "Component failed to call parent onUnRegister" );
  87. // Recurse
  88. (*i)->_unregisterComponents();
  89. }
  90. unlockComponentList();
  91. }
  92. void SimComponent::onRemove()
  93. {
  94. _unregisterComponents();
  95. // Delete all components
  96. VectorPtr<SimComponent *>&componentList = lockComponentList();
  97. while(componentList.size() > 0)
  98. {
  99. SimComponent *c = componentList[0];
  100. componentList.erase( componentList.begin() );
  101. if( c->isProperlyAdded() )
  102. c->deleteObject();
  103. else if( !c->isRemoved() && !c->isDeleted() )
  104. delete c;
  105. // else, something else is deleting this, don't mess with it
  106. }
  107. unlockComponentList();
  108. Parent::onRemove();
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. bool SimComponent::processArguments(S32 argc, const char **argv)
  112. {
  113. for(S32 i = 0; i < argc; i++)
  114. {
  115. SimComponent *obj = dynamic_cast<SimComponent*> (Sim::findObject(argv[i]) );
  116. if(obj)
  117. addComponent(obj);
  118. else
  119. Con::printf("SimComponent::processArguments - Invalid Component Object \"%s\"", argv[i]);
  120. }
  121. return true;
  122. }
  123. //////////////////////////////////////////////////////////////////////////
  124. void SimComponent::initPersistFields()
  125. {
  126. addProtectedField( "Enabled", TypeBool, Offset(mEnabled, SimComponent), &setEnabled, &defaultProtectedGetFn, &writeEnabled, "" );
  127. Parent::initPersistFields();
  128. }
  129. // Add Component to this one
  130. bool SimComponent::addComponent( SimComponent *component )
  131. {
  132. AssertFatal( dynamic_cast<SimObject*>(component), "SimComponent - Cannot add non SimObject derived components!" );
  133. MutexHandle mh;
  134. if( mh.lock( mMutex, true ) )
  135. {
  136. for( SimComponentIterator nItr = mComponentList.begin(); nItr != mComponentList.end(); nItr++ )
  137. {
  138. SimComponent *pComponent = dynamic_cast<SimComponent*>(*nItr);
  139. AssertFatal( pComponent, "SimComponent::addComponent - NULL component in list!" );
  140. if( pComponent == component )
  141. return true;
  142. }
  143. if(component->onComponentAdd(this))
  144. {
  145. component->mOwner = this;
  146. mComponentList.push_back( component );
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. // Remove Component from this one
  153. bool SimComponent::removeComponent( SimComponent *component )
  154. {
  155. MutexHandle mh;
  156. if( mh.lock( mMutex, true ) )
  157. {
  158. for( SimComponentIterator nItr = mComponentList.begin(); nItr != mComponentList.end(); nItr++ )
  159. {
  160. SimComponent *pComponent = dynamic_cast<SimComponent*>(*nItr);
  161. AssertFatal( pComponent, "SimComponent::removeComponent - NULL component in list!" );
  162. if( pComponent == component )
  163. {
  164. AssertFatal( component->mOwner == this, "Somehow we contain a component who doesn't think we are it's owner." );
  165. (*nItr)->onComponentRemove(this);
  166. component->mOwner = NULL;
  167. mComponentList.erase( nItr );
  168. return true;
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174. //////////////////////////////////////////////////////////////////////////
  175. bool SimComponent::onComponentAdd(SimComponent *target)
  176. {
  177. Con::executef(this, 2, "onComponentAdd", Con::getIntArg(target->getId()));
  178. return true;
  179. }
  180. void SimComponent::onComponentRemove(SimComponent *target)
  181. {
  182. Con::executef(this, 2, "onComponentRemove", Con::getIntArg(target->getId()));
  183. }
  184. //////////////////////////////////////////////////////////////////////////
  185. bool SimComponent::writeField(StringTableEntry fieldname, const char* value)
  186. {
  187. if (!Parent::writeField(fieldname, value))
  188. return false;
  189. if( fieldname == StringTable->insert("owner") )
  190. return false;
  191. return true;
  192. }
  193. void SimComponent::write(Stream &stream, U32 tabStop, U32 flags /* = 0 */)
  194. {
  195. #if 1
  196. Parent::write( stream, tabStop, flags );
  197. #else
  198. MutexHandle handle;
  199. handle.lock(mMutex); // When this goes out of scope, it will unlock it
  200. // export selected only?
  201. if((flags & SelectedOnly) && !isSelected())
  202. {
  203. for(U32 i = 0; i < mComponentList.size(); i++)
  204. mComponentList[i]->write(stream, tabStop, flags);
  205. return;
  206. }
  207. stream.writeTabs(tabStop);
  208. char buffer[1024];
  209. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() ? getName() : "");
  210. stream.write(dStrlen(buffer), buffer);
  211. writeFields(stream, tabStop + 1);
  212. if(mComponentList.size())
  213. {
  214. stream.write(2, "\r\n");
  215. stream.writeTabs(tabStop+1);
  216. stream.writeLine((U8 *)"// Note: This is a list of behaviors, not arbitrary SimObjects as in a SimGroup or SimSet.\r\n");
  217. for(U32 i = 0; i < mComponentList.size(); i++)
  218. mComponentList[i]->write(stream, tabStop + 1, flags);
  219. }
  220. stream.writeTabs(tabStop);
  221. stream.write(4, "};\r\n");
  222. #endif
  223. }
  224. //////////////////////////////////////////////////////////////////////////
  225. bool SimComponent::callMethodOnComponents( U32 argc, const char* argv[], const char** result )
  226. {
  227. const char *cbName = StringTable->insert(argv[0]);
  228. if (isEnabled())
  229. {
  230. if(isMethod(cbName))
  231. {
  232. // This component can handle the given method
  233. *result = Con::execute( this, argc, argv, true );
  234. return true;
  235. }
  236. else if( getComponentCount() > 0 )
  237. {
  238. // Need to try the component's children
  239. bool handled = false;
  240. VectorPtr<SimComponent *>&componentList = lockComponentList();
  241. for( SimComponentIterator nItr = (componentList.end()-1); nItr >= componentList.begin(); nItr-- )
  242. {
  243. argv[0] = cbName;
  244. SimComponent *pComponent = (*nItr);
  245. AssertFatal( pComponent, "SimComponent::callMethodOnComponents - NULL component in list!" );
  246. // Call on children
  247. handled = pComponent->callMethodOnComponents( argc, argv, result );
  248. if (handled)
  249. break;
  250. }
  251. unlockComponentList();
  252. if (handled)
  253. return true;
  254. }
  255. }
  256. return false;
  257. }