simSet.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "string/stringTable.h"
  25. #include "console/console.h"
  26. #include "io/fileStream.h"
  27. #include "input/actionMap.h"
  28. #include "io/resource/resourceManager.h"
  29. #include "io/fileObject.h"
  30. #include "console/consoleInternal.h"
  31. #include "debug/profiler.h"
  32. #include "console/consoleTypeValidators.h"
  33. #include "memory/frameAllocator.h"
  34. //////////////////////////////////////////////////////////////////////////
  35. // Sim Set
  36. //////////////////////////////////////////////////////////////////////////
  37. void SimSet::addObject(SimObject* obj)
  38. {
  39. lock();
  40. objectList.pushBack(obj);
  41. deleteNotify(obj);
  42. unlock();
  43. }
  44. void SimSet::removeObject(SimObject* obj)
  45. {
  46. lock();
  47. objectList.remove(obj);
  48. clearNotify(obj);
  49. unlock();
  50. }
  51. void SimSet::pushObject(SimObject* pObj)
  52. {
  53. lock();
  54. objectList.pushBackForce(pObj);
  55. deleteNotify(pObj);
  56. unlock();
  57. }
  58. void SimSet::popObject()
  59. {
  60. MutexHandle handle;
  61. handle.lock(mMutex);
  62. if (objectList.size() == 0)
  63. {
  64. AssertWarn(false, "Stack underflow in SimSet::popObject");
  65. return;
  66. }
  67. SimObject* pObject = objectList[objectList.size() - 1];
  68. objectList.removeStable(pObject);
  69. clearNotify(pObject);
  70. }
  71. //-----------------------------------------------------------------------------
  72. void SimSet::callOnChildren( const char * method, S32 argc, const char *argv[], bool executeOnChildGroups )
  73. {
  74. // Prep the arguments for the console exec...
  75. // Make sure and leave args[1] empty.
  76. const char* args[21];
  77. args[0] = method;
  78. for (S32 i = 0; i < argc; i++)
  79. args[i + 2] = argv[i];
  80. for( iterator i = begin(); i != end(); i++ )
  81. {
  82. SimObject *childObj = static_cast<SimObject*>(*i);
  83. if( childObj->isMethod( method ) )
  84. Con::execute(childObj, argc + 2, args);
  85. if( executeOnChildGroups )
  86. {
  87. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  88. if ( childSet )
  89. childSet->callOnChildren( method, argc, argv, executeOnChildGroups );
  90. }
  91. }
  92. }
  93. bool SimSet::reOrder( SimObject *obj, SimObject *target )
  94. {
  95. MutexHandle handle;
  96. handle.lock(mMutex);
  97. iterator itrS, itrD;
  98. if ( (itrS = find(begin(),end(),obj)) == end() )
  99. {
  100. return false; // object must be in list
  101. }
  102. if ( obj == target )
  103. {
  104. return true; // don't reorder same object but don't indicate error
  105. }
  106. if ( !target ) // if no target, then put to back of list
  107. {
  108. if ( itrS != (end()-1) ) // don't move if already last object
  109. {
  110. objectList.erase(itrS); // remove object from its current location
  111. objectList.push_back(obj); // push it to the back of the list
  112. }
  113. }
  114. else // if target, insert object in front of target
  115. {
  116. if ( (itrD = find(begin(),end(),target)) == end() )
  117. return false; // target must be in list
  118. objectList.erase(itrS);
  119. //Tinman - once itrS has been erased, itrD won't be pointing at the same place anymore - re-find...
  120. itrD = find(begin(),end(),target);
  121. objectList.insert(itrD,obj);
  122. }
  123. return true;
  124. }
  125. void SimSet::onDeleteNotify(SimObject *object)
  126. {
  127. removeObject(object);
  128. Parent::onDeleteNotify(object);
  129. }
  130. void SimSet::onRemove()
  131. {
  132. MutexHandle handle;
  133. handle.lock(mMutex);
  134. objectList.sortId();
  135. if (objectList.size())
  136. {
  137. // This backwards iterator loop doesn't work if the
  138. // list is empty, check the size first.
  139. for (SimObjectList::iterator ptr = objectList.end() - 1;
  140. ptr >= objectList.begin(); ptr--)
  141. {
  142. clearNotify(*ptr);
  143. }
  144. }
  145. handle.unlock();
  146. Parent::onRemove();
  147. }
  148. void SimSet::write(Stream &stream, U32 tabStop, U32 flags)
  149. {
  150. MutexHandle handle;
  151. handle.lock(mMutex);
  152. // export selected only?
  153. if((flags & SelectedOnly) && !isSelected())
  154. {
  155. for(U32 i = 0; i < (U32)size(); i++)
  156. (*this)[i]->write(stream, tabStop, flags);
  157. return;
  158. }
  159. stream.writeTabs(tabStop);
  160. char buffer[1024];
  161. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() ? getName() : "");
  162. stream.write(dStrlen(buffer), buffer);
  163. writeFields(stream, tabStop + 1);
  164. if(size())
  165. {
  166. stream.write(2, "\r\n");
  167. for(U32 i = 0; i < (U32)size(); i++)
  168. (*this)[i]->write(stream, tabStop + 1, flags);
  169. }
  170. stream.writeTabs(tabStop);
  171. stream.write(4, "};\r\n");
  172. }
  173. void SimSet::deleteObjects( void )
  174. {
  175. lock();
  176. while(size() > 0 )
  177. {
  178. objectList[0]->deleteObject();
  179. }
  180. unlock();
  181. }
  182. void SimSet::clear()
  183. {
  184. lock();
  185. while (size() > 0)
  186. removeObject(objectList.last());
  187. unlock();
  188. }
  189. SimObject* SimSet::findObjectByInternalName(const char* internalName, bool searchChildren)
  190. {
  191. iterator i;
  192. for (i = begin(); i != end(); i++)
  193. {
  194. SimObject *childObj = static_cast<SimObject*>(*i);
  195. if(childObj->getInternalName() == internalName)
  196. return childObj;
  197. else if (searchChildren)
  198. {
  199. SimSet* childSet = dynamic_cast<SimSet*>(*i);
  200. if (childSet)
  201. {
  202. SimObject* found = childSet->findObjectByInternalName(internalName, searchChildren);
  203. if (found) return found;
  204. }
  205. }
  206. }
  207. return NULL;
  208. }
  209. //////////////////////////////////////////////////////////////////////////
  210. IMPLEMENT_CONOBJECT_CHILDREN(SimSet);
  211. inline void SimSetIterator::Stack::push_back(SimSet* set)
  212. {
  213. increment();
  214. last().set = set;
  215. last().itr = set->begin();
  216. }
  217. //-----------------------------------------------------------------------------
  218. SimSetIterator::SimSetIterator(SimSet* set)
  219. {
  220. VECTOR_SET_ASSOCIATION(stack);
  221. if (!set->empty())
  222. stack.push_back(set);
  223. }
  224. //-----------------------------------------------------------------------------
  225. SimObject* SimSetIterator::operator++()
  226. {
  227. SimSet* set;
  228. if ((set = dynamic_cast<SimSet*>(*stack.last().itr)) != 0)
  229. {
  230. if (!set->empty())
  231. {
  232. stack.push_back(set);
  233. return *stack.last().itr;
  234. }
  235. }
  236. while (++stack.last().itr == stack.last().set->end())
  237. {
  238. stack.pop_back();
  239. if (stack.empty())
  240. return 0;
  241. }
  242. return *stack.last().itr;
  243. }
  244. SimObject* SimGroupIterator::operator++()
  245. {
  246. SimGroup* set;
  247. if ((set = dynamic_cast<SimGroup*>(*stack.last().itr)) != 0)
  248. {
  249. if (!set->empty())
  250. {
  251. stack.push_back(set);
  252. return *stack.last().itr;
  253. }
  254. }
  255. while (++stack.last().itr == stack.last().set->end())
  256. {
  257. stack.pop_back();
  258. if (stack.empty())
  259. return 0;
  260. }
  261. return *stack.last().itr;
  262. }
  263. //////////////////////////////////////////////////////////////////////////
  264. // SimGroup
  265. //////////////////////////////////////////////////////////////////////////
  266. SimGroup::~SimGroup()
  267. {
  268. lock();
  269. for (iterator itr = begin(); itr != end(); itr++)
  270. nameDictionary.remove(*itr);
  271. // XXX Move this later into Group Class
  272. // If we have any objects at this point, they should
  273. // already have been removed from the manager, so we
  274. // can just delete them directly.
  275. objectList.sortId();
  276. while (!objectList.empty())
  277. {
  278. delete objectList.last();
  279. objectList.decrement();
  280. }
  281. unlock();
  282. }
  283. //////////////////////////////////////////////////////////////////////////
  284. void SimGroup::addObject(SimObject* obj)
  285. {
  286. lock();
  287. // Make sure we aren't adding ourself. This isn't the most robust check
  288. // but it should be good enough to prevent some self-foot-shooting.
  289. if(obj == this)
  290. {
  291. Con::errorf("SimGroup::addObject - (%d) can't add self!", getIdString());
  292. unlock();
  293. return;
  294. }
  295. if (obj->mGroup != this)
  296. {
  297. if (obj->mGroup)
  298. obj->mGroup->removeObject(obj);
  299. nameDictionary.insert(obj);
  300. obj->mGroup = this;
  301. objectList.push_back(obj); // force it into the object list
  302. // doesn't get a delete notify
  303. obj->onGroupAdd();
  304. }
  305. unlock();
  306. }
  307. void SimGroup::removeObject(SimObject* obj)
  308. {
  309. lock();
  310. if (obj->mGroup == this)
  311. {
  312. obj->onGroupRemove();
  313. nameDictionary.remove(obj);
  314. objectList.remove(obj);
  315. obj->mGroup = 0;
  316. }
  317. unlock();
  318. }
  319. //////////////////////////////////////////////////////////////////////////
  320. void SimGroup::onRemove()
  321. {
  322. lock();
  323. objectList.sortId();
  324. if (objectList.size())
  325. {
  326. // This backwards iterator loop doesn't work if the
  327. // list is empty, check the size first.
  328. for (SimObjectList::iterator ptr = objectList.end() - 1;
  329. ptr >= objectList.begin(); ptr--)
  330. {
  331. if ( (*ptr)->isProperlyAdded() )
  332. {
  333. (*ptr)->onGroupRemove();
  334. (*ptr)->mGroup = NULL;
  335. (*ptr)->unregisterObject();
  336. (*ptr)->mGroup = this;
  337. }
  338. }
  339. }
  340. SimObject::onRemove();
  341. unlock();
  342. }
  343. //////////////////////////////////////////////////////////////////////////
  344. SimObject *SimGroup::findObject(const char *namePath)
  345. {
  346. // find the end of the object name
  347. S32 len;
  348. for(len = 0; namePath[len] != 0 && namePath[len] != '/'; len++)
  349. ;
  350. StringTableEntry stName = StringTable->lookupn(namePath, len);
  351. if(!stName)
  352. return NULL;
  353. SimObject *root = nameDictionary.find(stName);
  354. if(!root)
  355. return NULL;
  356. if(namePath[len] == 0)
  357. return root;
  358. return root->findObject(namePath + len + 1);
  359. }
  360. SimObject *SimSet::findObject(const char *namePath)
  361. {
  362. // find the end of the object name
  363. S32 len;
  364. for(len = 0; namePath[len] != 0 && namePath[len] != '/'; len++)
  365. ;
  366. StringTableEntry stName = StringTable->lookupn(namePath, len);
  367. if(!stName)
  368. return NULL;
  369. lock();
  370. for(SimSet::iterator i = begin(); i != end(); i++)
  371. {
  372. if((*i)->getName() == stName)
  373. {
  374. unlock();
  375. if(namePath[len] == 0)
  376. return *i;
  377. return (*i)->findObject(namePath + len + 1);
  378. }
  379. }
  380. unlock();
  381. return NULL;
  382. }
  383. SimObject* SimObject::findObject(const char* )
  384. {
  385. return NULL;
  386. }
  387. //////////////////////////////////////////////////////////////////////////
  388. bool SimGroup::processArguments(S32, const char **)
  389. {
  390. return true;
  391. }
  392. //////////////////////////////////////////////////////////////////////////
  393. IMPLEMENT_CONOBJECT(SimGroup);
  394. //////////////////////////////////////////////////////////////////////////
  395. // Console Methods
  396. //////////////////////////////////////////////////////////////////////////
  397. ConsoleMethod(SimSet, listObjects, void, 2, 2, "() Prints the object data within the set\n"
  398. "@return No return value")
  399. {
  400. object->lock();
  401. SimSet::iterator itr;
  402. for(itr = object->begin(); itr != object->end(); itr++)
  403. {
  404. SimObject *obj = *itr;
  405. bool isSet = dynamic_cast<SimSet *>(obj) != 0;
  406. const char *name = obj->getName();
  407. if(name)
  408. Con::printf(" %d,\"%s\": %s %s", obj->getId(), name,
  409. obj->getClassName(), isSet ? "(g)":"");
  410. else
  411. Con::printf(" %d: %s %s", obj->getId(), obj->getClassName(),
  412. isSet ? "(g)" : "");
  413. }
  414. object->unlock();
  415. }
  416. ConsoleMethod(SimSet, add, void, 3, 0, "(obj1,...) Adds given listy of objects to the SimSet.\n"
  417. "@param obj_i (i is unilimited) Variable list of objects to add\n"
  418. "@return No return value")
  419. {
  420. for(S32 i = 2; i < argc; i++)
  421. {
  422. SimObject *obj = Sim::findObject(argv[i]);
  423. if(obj)
  424. object->addObject(obj);
  425. else
  426. Con::printf("Set::add: Object \"%s\" doesn't exist", argv[i]);
  427. }
  428. }
  429. ConsoleMethod(SimSet, remove, void, 3, 0, "(obj1,...) Removes given listy of objects from the SimSet.\n"
  430. "@param obj_i (i is unilimited) Variable list of objects to remove\n"
  431. "@return No return value")
  432. {
  433. for(S32 i = 2; i < argc; i++)
  434. {
  435. SimObject *obj = Sim::findObject(argv[i]);
  436. object->lock();
  437. if(obj && object->find(object->begin(),object->end(),obj) != object->end())
  438. object->removeObject(obj);
  439. else
  440. Con::printf("Set::remove: Object \"%s\" does not exist in set", argv[i]);
  441. object->unlock();
  442. }
  443. }
  444. //-----------------------------------------------------------------------------
  445. ConsoleMethod(SimSet, deleteObjects, void, 2, 2, "() Deletes all the objects in the SimSet.\n"
  446. "@return No return value")
  447. {
  448. object->deleteObjects();
  449. }
  450. //-----------------------------------------------------------------------------
  451. ConsoleMethod(SimSet, clear, void, 2, 2, "() Clears the Simset\n"
  452. "@return No return value")
  453. {
  454. object->clear();
  455. }
  456. //-----------------------------------------------------------------------------
  457. ConsoleMethod( SimSet, callOnChildren, void, 3, 0,
  458. "( string method, string args... ) Call a method on all objects contained in the set.\n\n"
  459. "@param method The name of the method to call.\n"
  460. "@param args The arguments to the method.\n\n"
  461. "@note This method recurses into all SimSets that are children to the set.\n\n"
  462. "@see callOnChildrenNoRecurse" )
  463. {
  464. object->callOnChildren( argv[2], argc - 3, argv + 3 );
  465. }
  466. //////////////////////////////////////////////////////////////////////////-
  467. // Make Sure Child 1 is Ordered Just Under Child 2.
  468. //////////////////////////////////////////////////////////////////////////-
  469. ConsoleMethod(SimSet, reorderChild, void, 4,4," (child1, child2) Uses simset reorder to push child 1 before child 2 - both must already be child controls of this control\n"
  470. "@param child1 The child you wish to set first\n"
  471. "@param child2 The child you wish to set after child1\n"
  472. "@return No return value.")
  473. {
  474. SimObject* pObject = Sim::findObject(argv[2]);
  475. SimObject* pTarget = Sim::findObject(argv[3]);
  476. if(pObject && pTarget)
  477. {
  478. object->reOrder(pObject,pTarget);
  479. }
  480. }
  481. ConsoleMethod(SimSet, getCount, S32, 2, 2, "() @return Returns the number of objects in the SimSet")
  482. {
  483. return object->size();
  484. }
  485. ConsoleMethod(SimSet, getObject, S32, 3, 3, "(objIndex) @return Returns the ID of the desired object or -1 on failure")
  486. {
  487. S32 objectIndex = dAtoi(argv[2]);
  488. if(objectIndex < 0 || objectIndex >= S32(object->size()))
  489. {
  490. Con::printf("Set::getObject index out of range.");
  491. return -1;
  492. }
  493. return ((*object)[objectIndex])->getId();
  494. }
  495. ConsoleMethod(SimSet, isMember, bool, 3, 3, "(object) @return Returns true if specified object is a member of the set, and false otherwise")
  496. {
  497. SimObject *testObject = Sim::findObject(argv[2]);
  498. if(!testObject)
  499. {
  500. Con::printf("SimSet::isMember: %s is not an object.", argv[2]);
  501. return false;
  502. }
  503. object->lock();
  504. for(SimSet::iterator i = object->begin(); i != object->end(); i++)
  505. {
  506. if(*i == testObject)
  507. {
  508. object->unlock();
  509. return true;
  510. }
  511. }
  512. object->unlock();
  513. return false;
  514. }
  515. ConsoleMethod( SimSet, findObjectByInternalName, S32, 3, 4, "(string name, [bool searchChildren]) Returns the object with given internal name\n"
  516. "@param name The internal name of the object you wish to find\n"
  517. "@param searchChildren Set this true if you wish to search all children as well.\n"
  518. "@return Returns the ID of the object.")
  519. {
  520. StringTableEntry pcName = StringTable->insert(argv[2]);
  521. bool searchChildren = false;
  522. if (argc > 3)
  523. searchChildren = dAtob(argv[3]);
  524. SimObject* child = object->findObjectByInternalName(pcName, searchChildren);
  525. if(child)
  526. return child->getId();
  527. return 0;
  528. }
  529. ConsoleMethod(SimSet, bringToFront, void, 3, 3, "(object) Brings object to front of set.\n"
  530. "@return No return value.")
  531. {
  532. SimObject *obj = Sim::findObject(argv[2]);
  533. if(!obj)
  534. return;
  535. object->bringObjectToFront(obj);
  536. }
  537. ConsoleMethod(SimSet, pushToBack, void, 3, 3, "(object) Sends item to back of set.\n"
  538. "@return No return value.")
  539. {
  540. SimObject *obj = Sim::findObject(argv[2]);
  541. if(!obj)
  542. return;
  543. object->pushObjectToBack(obj);
  544. }