simObject.cc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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 "sim/simObject.h"
  23. #include "sim/simObjectTimerEvent.h"
  24. #include "console/consoleInternal.h"
  25. #include "console/codeBlock.h"
  26. #include "console/consoleInternal.h"
  27. #include "memory/frameAllocator.h"
  28. #include "io/fileStream.h"
  29. #include "io/fileObject.h"
  30. #include "console/ConsoleTypeValidators.h"
  31. #include "simObject_ScriptBinding.h"
  32. #include <algorithm>
  33. //-----------------------------------------------------------------------------
  34. IMPLEMENT_CONOBJECT(SimObject);
  35. namespace Sim
  36. {
  37. extern U32 gNextObjectId;
  38. extern SimIdDictionary *gIdDictionary;
  39. extern SimManagerNameDictionary *gNameDictionary;
  40. extern void cancelPendingEvents(SimObject *obj);
  41. }
  42. //-----------------------------------------------------------------------------
  43. SimObject::SimObject()
  44. {
  45. mFlags.set( ModStaticFields | ModDynamicFields );
  46. objectName = NULL;
  47. mInternalName = NULL;
  48. nextNameObject = (SimObject*)-1;
  49. nextManagerNameObject = (SimObject*)-1;
  50. nextIdObject = NULL;
  51. mId = 0;
  52. mIdString = StringTable->EmptyString;
  53. mGroup = 0;
  54. mNameSpace = NULL;
  55. mNotifyList = NULL;
  56. mTypeMask = 0;
  57. mScriptCallbackGuard = 0;
  58. mFieldDictionary = NULL;
  59. mCanSaveFieldDictionary = true;
  60. mClassName = NULL;
  61. mSuperClassName = NULL;
  62. mProgenitorFile = CodeBlock::getCurrentCodeBlockFullPath();
  63. mPeriodicTimerID = 0;
  64. bIsEventRaised = false;
  65. }
  66. //---------------------------------------------------------------------------
  67. bool SimObject::isMethod( const char* methodName )
  68. {
  69. if( !methodName || !methodName[0] )
  70. return false;
  71. StringTableEntry stname = StringTable->insert( methodName );
  72. if( getNamespace() )
  73. return ( getNamespace()->lookup( stname ) != NULL );
  74. return false;
  75. }
  76. //---------------------------------------------------------------------------
  77. bool SimObject::registerObject()
  78. {
  79. AssertFatal( !mFlags.test( Added ), "reigsterObject - Object already registered!");
  80. mFlags.clear(Deleted | Removed);
  81. if( mId == 0 )
  82. {
  83. mId = Sim::gNextObjectId++;
  84. char idBuffer[64];
  85. dSprintf(idBuffer, sizeof(idBuffer), "%d", mId);
  86. mIdString = StringTable->insert( idBuffer );
  87. }
  88. AssertFatal(Sim::gIdDictionary && Sim::gNameDictionary,
  89. "SimObject::registerObject - tried to register an object before Sim::init()!");
  90. Sim::gIdDictionary->insert(this);
  91. Sim::gNameDictionary->insert(this);
  92. // Notify object
  93. bool ret = onAdd();
  94. if(!ret)
  95. unregisterObject();
  96. AssertFatal(!ret || isProperlyAdded(), "Object did not call SimObject::onAdd()");
  97. if ( isMethod( "onAdd" ) )
  98. Con::executef( this, 1, "onAdd" );
  99. return ret;
  100. }
  101. //---------------------------------------------------------------------------
  102. void SimObject::unregisterObject()
  103. {
  104. // Sanity!
  105. AssertISV( getScriptCallbackGuard() == 0, "SimObject::unregisterObject: Object is being unregistered whilst performing a script callback!" );
  106. if ( isMethod( "onRemove" ) )
  107. Con::executef( this, 1, "onRemove" );
  108. mFlags.set(Removed);
  109. // Notify object first
  110. onRemove();
  111. // Clear out any pending notifications before
  112. // we call our own, just in case they delete
  113. // something that we have referenced.
  114. clearAllNotifications();
  115. // Notify all objects that are waiting for delete
  116. // messages
  117. if (getGroup())
  118. getGroup()->removeObject(this);
  119. processDeleteNotifies();
  120. // Do removals from the Sim.
  121. Sim::gNameDictionary->remove(this);
  122. Sim::gIdDictionary->remove(this);
  123. Sim::cancelPendingEvents(this);
  124. }
  125. //---------------------------------------------------------------------------
  126. void SimObject::deleteObject()
  127. {
  128. // Sanity!
  129. AssertISV( getScriptCallbackGuard() == 0, "SimObject::deleteObject: Object is being deleted whilst performing a script callback!" );
  130. AssertFatal(mFlags.test(Added),
  131. "SimObject::deleteObject: Object not registered.");
  132. AssertFatal(!isDeleted(),"SimManager::deleteObject: "
  133. "Object has already been deleted");
  134. AssertFatal(!isRemoved(),"SimManager::deleteObject: "
  135. "Object in the process of being removed");
  136. mFlags.set(Deleted);
  137. unregisterObject();
  138. delete this;
  139. }
  140. //---------------------------------------------------------------------------
  141. void SimObject::setId(SimObjectId newId)
  142. {
  143. if(!mFlags.test(Added))
  144. {
  145. mId = newId;
  146. }
  147. else
  148. {
  149. // get this object out of the id dictionary if it's in it
  150. Sim::gIdDictionary->remove(this);
  151. // Free current Id.
  152. // Assign new one.
  153. mId = newId ? newId : Sim::gNextObjectId++;
  154. Sim::gIdDictionary->insert(this);
  155. }
  156. char idBuffer[64];
  157. dSprintf(idBuffer, sizeof(idBuffer), "%d", mId);
  158. mIdString = StringTable->insert( idBuffer );
  159. }
  160. void SimObject::assignName(const char *name)
  161. {
  162. if( dStricmp( getClassName(), name ) == 0 )
  163. Con::errorf( "SimObject::assignName - Assigning name '%s' to instance of object with type '%s'."
  164. " This can cause namespace linking issues.", getClassName(), name );
  165. // Is this name already registered?
  166. if ( Sim::gNameDictionary->find(name) != NULL )
  167. {
  168. // Yes, so error,
  169. Con::errorf( "SimObject::assignName() - Attempted to set object to name '%s' but it is already assigned to another object.", name );
  170. return;
  171. }
  172. StringTableEntry newName = NULL;
  173. if (name[0])
  174. newName = StringTable->insert(name);
  175. if (mGroup)
  176. mGroup->nameDictionary.remove(this);
  177. if (isProperlyAdded())
  178. {
  179. // Unlink the old namespaces.
  180. unlinkNamespaces();
  181. Sim::gNameDictionary->remove(this);
  182. }
  183. objectName = newName;
  184. if (mGroup)
  185. mGroup->nameDictionary.insert(this);
  186. if (isProperlyAdded())
  187. {
  188. // Link the new namespaces.
  189. linkNamespaces();
  190. Sim::gNameDictionary->insert(this);
  191. }
  192. }
  193. //---------------------------------------------------------------------------
  194. bool SimObject::registerObject(U32 id)
  195. {
  196. setId(id);
  197. return registerObject();
  198. }
  199. bool SimObject::registerObject(const char *name)
  200. {
  201. assignName(name);
  202. return registerObject();
  203. }
  204. bool SimObject::registerObject(const char *name, U32 id)
  205. {
  206. setId(id);
  207. assignName(name);
  208. return registerObject();
  209. }
  210. void SimObject::assignDynamicFieldsFrom(SimObject* parent)
  211. {
  212. if(parent->mFieldDictionary)
  213. {
  214. if( mFieldDictionary == NULL )
  215. mFieldDictionary = new SimFieldDictionary;
  216. mFieldDictionary->assignFrom(parent->mFieldDictionary);
  217. }
  218. }
  219. void SimObject::assignFieldsFrom(SimObject *parent)
  220. {
  221. // only allow field assigns from objects of the same class:
  222. if(getClassRep() == parent->getClassRep())
  223. {
  224. const AbstractClassRep::FieldList &list = getFieldList();
  225. // copy out all the fields:
  226. for(U32 i = 0; i < (U32)list.size(); i++)
  227. {
  228. const AbstractClassRep::Field* f = &list[i];
  229. S32 lastField = f->elementCount - 1;
  230. for(S32 j = 0; j <= lastField; j++)
  231. {
  232. const char* fieldVal = (*f->getDataFn)( this, Con::getData(f->type, (void *) (((const char *)parent) + f->offset), j, f->table, f->flag));
  233. //if(fieldVal)
  234. // Con::setData(f->type, (void *) (((const char *)this) + f->offset), j, 1, &fieldVal, f->table);
  235. if(fieldVal)
  236. {
  237. // code copied from SimObject::setDataField().
  238. // TODO: paxorr: abstract this into a better setData / getData that considers prot fields.
  239. FrameTemp<char> buffer(2048);
  240. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  241. ConsoleBaseType *cbt = ConsoleBaseType::getType( f->type );
  242. const char* szBuffer = cbt->prepData( fieldVal, buffer, 2048 );
  243. dMemset( bufferSecure, 0, 2048 );
  244. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  245. if((*f->setDataFn)( this, bufferSecure ) )
  246. Con::setData(f->type, (void *) (((const char *)this) + f->offset), j, 1, &fieldVal, f->table);
  247. }
  248. }
  249. }
  250. }
  251. assignDynamicFieldsFrom(parent);
  252. }
  253. bool SimObject::writeField(StringTableEntry fieldname, const char* value)
  254. {
  255. // Don't write empty fields.
  256. if (!value || !*value)
  257. return false;
  258. // Don't write ParentGroup
  259. if( fieldname == StringTable->insert("parentGroup") )
  260. return false;
  261. return true;
  262. }
  263. void SimObject::writeFields(Stream &stream, U32 tabStop)
  264. {
  265. const AbstractClassRep::FieldList &list = getFieldList();
  266. for(U32 i = 0; i < (U32)list.size(); i++)
  267. {
  268. const AbstractClassRep::Field* f = &list[i];
  269. if( f->type == AbstractClassRep::DepricatedFieldType ||
  270. f->type == AbstractClassRep::StartGroupFieldType ||
  271. f->type == AbstractClassRep::EndGroupFieldType) continue;
  272. // Fetch fieldname.
  273. StringTableEntry fieldName = StringTable->insert( f->pFieldname );
  274. // Fetch element count.
  275. const S32 elementCount = f->elementCount;
  276. // Skip if the field should not be written.
  277. // For now, we only deal with non-array fields.
  278. if ( elementCount == 1 &&
  279. f->writeDataFn != NULL &&
  280. f->writeDataFn( this, fieldName ) == false )
  281. continue;
  282. for(U32 j = 0; S32(j) < elementCount; j++)
  283. {
  284. char array[8];
  285. dSprintf( array, 8, "%d", j );
  286. const char *val = getDataField(fieldName, array );
  287. // Make a copy for the field check.
  288. if (!val)
  289. continue;
  290. U32 nBufferSize = dStrlen( val ) + 1;
  291. FrameTemp<char> valCopy( nBufferSize );
  292. dStrcpy( (char *)valCopy, val );
  293. if (!writeField(fieldName, valCopy))
  294. continue;
  295. val = valCopy;
  296. U32 expandedBufferSize = ( nBufferSize * 2 ) + 32;
  297. FrameTemp<char> expandedBuffer( expandedBufferSize );
  298. if(f->elementCount == 1)
  299. dSprintf(expandedBuffer, expandedBufferSize, "%s = \"", f->pFieldname);
  300. else
  301. dSprintf(expandedBuffer, expandedBufferSize, "%s[%d] = \"", f->pFieldname, j);
  302. // detect and collapse relative path information
  303. char fnBuf[1024];
  304. if (f->type == TypeFilename)
  305. {
  306. Con::collapsePath(fnBuf, 1024, val);
  307. val = fnBuf;
  308. }
  309. expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), val);
  310. dStrcat(expandedBuffer, "\";\r\n");
  311. stream.writeTabs(tabStop);
  312. stream.write(dStrlen(expandedBuffer),expandedBuffer);
  313. }
  314. }
  315. if(mFieldDictionary && mCanSaveFieldDictionary)
  316. mFieldDictionary->writeFields(this, stream, tabStop);
  317. }
  318. void SimObject::write(Stream &stream, U32 tabStop, U32 flags)
  319. {
  320. // Only output selected objects if they want that.
  321. if((flags & SelectedOnly) && !isSelected())
  322. return;
  323. stream.writeTabs(tabStop);
  324. char buffer[1024];
  325. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() ? getName() : "");
  326. stream.write(dStrlen(buffer), buffer);
  327. writeFields(stream, tabStop + 1);
  328. stream.writeTabs(tabStop);
  329. stream.write(4, "};\r\n");
  330. }
  331. bool SimObject::save(const char* pcFileName, bool bOnlySelected)
  332. {
  333. static const char *beginMessage = "//--- OBJECT WRITE BEGIN ---";
  334. static const char *endMessage = "//--- OBJECT WRITE END ---";
  335. FileStream stream;
  336. FileObject f;
  337. f.readMemory(pcFileName);
  338. // check for flags <selected, ...>
  339. U32 writeFlags = 0;
  340. if(bOnlySelected)
  341. writeFlags |= SimObject::SelectedOnly;
  342. if(!ResourceManager->openFileForWrite(stream, pcFileName))
  343. return false;
  344. char docRoot[256];
  345. char modRoot[256];
  346. dStrcpy(docRoot, pcFileName);
  347. char *p = dStrrchr(docRoot, '/');
  348. if (p) *++p = '\0';
  349. else docRoot[0] = '\0';
  350. dStrcpy(modRoot, pcFileName);
  351. p = dStrchr(modRoot, '/');
  352. if (p) *++p = '\0';
  353. else modRoot[0] = '\0';
  354. Con::setVariable("$DocRoot", docRoot);
  355. Con::setVariable("$ModRoot", modRoot);
  356. const char *buffer;
  357. while(!f.isEOF())
  358. {
  359. buffer = (const char *) f.readLine();
  360. if(!dStrcmp(buffer, beginMessage))
  361. break;
  362. stream.write(dStrlen(buffer), buffer);
  363. stream.write(2, "\r\n");
  364. }
  365. stream.write(dStrlen(beginMessage), beginMessage);
  366. stream.write(2, "\r\n");
  367. write(stream, 0, writeFlags);
  368. stream.write(dStrlen(endMessage), endMessage);
  369. stream.write(2, "\r\n");
  370. while(!f.isEOF())
  371. {
  372. buffer = (const char *) f.readLine();
  373. if(!dStrcmp(buffer, endMessage))
  374. break;
  375. }
  376. while(!f.isEOF())
  377. {
  378. buffer = (const char *) f.readLine();
  379. stream.write(dStrlen(buffer), buffer);
  380. stream.write(2, "\r\n");
  381. }
  382. Con::setVariable("$DocRoot", NULL);
  383. Con::setVariable("$ModRoot", NULL);
  384. return true;
  385. }
  386. void SimObject::setInternalName(const char* newname)
  387. {
  388. if(newname)
  389. mInternalName = StringTable->insert(newname);
  390. }
  391. StringTableEntry SimObject::getInternalName()
  392. {
  393. return mInternalName;
  394. }
  395. void SimObject::dumpClassHierarchy()
  396. {
  397. AbstractClassRep* pRep = getClassRep();
  398. while(pRep)
  399. {
  400. Con::warnf("%s ->", pRep->getClassName());
  401. pRep = pRep->getParentClass();
  402. }
  403. }
  404. const char *SimObject::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  405. {
  406. return mNameSpace->tabComplete(prevText, baseLen, fForward);
  407. }
  408. //-----------------------------------------------------------------------------
  409. void SimObject::setDataField(StringTableEntry slotName, const char *array, const char *value)
  410. {
  411. // first search the static fields if enabled
  412. if(mFlags.test(ModStaticFields))
  413. {
  414. const AbstractClassRep::Field *fld = findField(slotName);
  415. if(fld)
  416. {
  417. if( fld->type == AbstractClassRep::DepricatedFieldType ||
  418. fld->type == AbstractClassRep::StartGroupFieldType ||
  419. fld->type == AbstractClassRep::EndGroupFieldType)
  420. return;
  421. S32 array1 = array ? dAtoi(array) : 0;
  422. if(array1 >= 0 && array1 < fld->elementCount && fld->elementCount >= 1)
  423. {
  424. // If the set data notify callback returns true, then go ahead and
  425. // set the data, otherwise, assume the set notify callback has either
  426. // already set the data, or has deemed that the data should not
  427. // be set at all.
  428. FrameTemp<char> buffer(2048);
  429. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  430. // so that if the prep functions or any other functions use the string stack, the data
  431. // is not corrupted.
  432. ConsoleBaseType *cbt = ConsoleBaseType::getType( fld->type );
  433. AssertFatal( cbt != NULL, "Could not resolve Type Id." );
  434. const char* szBuffer = cbt->prepData( value, buffer, 2048 );
  435. dMemset( bufferSecure, 0, 2048 );
  436. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  437. if( (*fld->setDataFn)( this, bufferSecure ) )
  438. Con::setData(fld->type, (void *) (((const char *)this) + fld->offset), array1, 1, &value, fld->table);
  439. onStaticModified( slotName, value );
  440. return;
  441. }
  442. if(fld->validator)
  443. fld->validator->validateType(this, (void *) (((const char *)this) + fld->offset));
  444. onStaticModified( slotName, value );
  445. return;
  446. }
  447. }
  448. if(mFlags.test(ModDynamicFields))
  449. {
  450. if(!mFieldDictionary)
  451. mFieldDictionary = new SimFieldDictionary;
  452. if(!array)
  453. mFieldDictionary->setFieldValue(slotName, value);
  454. else
  455. {
  456. char buf[256];
  457. dStrcpy(buf, slotName);
  458. dStrcat(buf, array);
  459. mFieldDictionary->setFieldValue(StringTable->insert(buf), value);
  460. }
  461. }
  462. }
  463. //-----------------------------------------------------------------------------
  464. const char *SimObject::getDataField(StringTableEntry slotName, const char *array)
  465. {
  466. if(mFlags.test(ModStaticFields))
  467. {
  468. S32 array1 = array ? dAtoi(array) : -1;
  469. const AbstractClassRep::Field *fld = findField(slotName);
  470. if(fld)
  471. {
  472. if(array1 == -1 && fld->elementCount == 1)
  473. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), 0, fld->table, fld->flag) );
  474. if(array1 >= 0 && array1 < fld->elementCount)
  475. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), array1, fld->table, fld->flag) );// + typeSizes[fld.type] * array1));
  476. return "";
  477. }
  478. }
  479. if(mFlags.test(ModDynamicFields))
  480. {
  481. if(!mFieldDictionary)
  482. return "";
  483. if(!array)
  484. {
  485. if (const char* val = mFieldDictionary->getFieldValue(slotName))
  486. return val;
  487. }
  488. else
  489. {
  490. static char buf[256];
  491. dStrcpy(buf, slotName);
  492. dStrcat(buf, array);
  493. if (const char* val = mFieldDictionary->getFieldValue(StringTable->insert(buf)))
  494. return val;
  495. }
  496. }
  497. return "";
  498. }
  499. //-----------------------------------------------------------------------------
  500. const char *SimObject::getPrefixedDataField(StringTableEntry fieldName, const char *array)
  501. {
  502. // Sanity!
  503. AssertFatal( fieldName != NULL, "Cannot get field value with NULL field name." );
  504. // Fetch field value.
  505. const char* pFieldValue = getDataField( fieldName, array );
  506. // Return without the prefix if there's no value.
  507. if ( pFieldValue == NULL || *pFieldValue == 0 )
  508. return StringTable->EmptyString;
  509. // Fetch the field prefix.
  510. StringTableEntry fieldPrefix = getDataFieldPrefix( fieldName );
  511. // Sanity!
  512. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  513. // Calculate a buffer size including prefix.
  514. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  515. // Fetch a buffer.
  516. char* pValueBuffer = Con::getReturnBuffer( valueBufferSize );
  517. // Format the value buffer.
  518. dSprintf( pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue );
  519. return pValueBuffer;
  520. }
  521. //-----------------------------------------------------------------------------
  522. void SimObject::setPrefixedDataField(StringTableEntry fieldName, const char *array, const char *value)
  523. {
  524. // Sanity!
  525. AssertFatal( fieldName != NULL, "Cannot set object field value with NULL field name." );
  526. AssertFatal( value != NULL, "Field value cannot be NULL." );
  527. // Set value without prefix if there's no value.
  528. if ( *value == 0 )
  529. {
  530. setDataField( fieldName, NULL, value );
  531. return;
  532. }
  533. // Fetch the field prefix.
  534. StringTableEntry fieldPrefix = getDataFieldPrefix( fieldName );
  535. // Sanity.
  536. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  537. // Do we have a field prefix?
  538. if ( fieldPrefix == StringTable->EmptyString )
  539. {
  540. // No, so set the data field in the usual way.
  541. setDataField( fieldName, NULL, value );
  542. return;
  543. }
  544. // Yes, so fetch the length of the field prefix.
  545. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  546. // Yes, so does it start with the object field prefix?
  547. if ( dStrnicmp( value, fieldPrefix, fieldPrefixLength ) != 0 )
  548. {
  549. // No, so set the data field in the usual way.
  550. setDataField( fieldName, NULL, value );
  551. return;
  552. }
  553. // Yes, so set the data excluding the prefix.
  554. setDataField( fieldName, NULL, value + fieldPrefixLength );
  555. }
  556. //-----------------------------------------------------------------------------
  557. const char *SimObject::getPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const S32 fieldType )
  558. {
  559. // Sanity!
  560. AssertFatal( fieldName != NULL, "Cannot get field value with NULL field name." );
  561. // Fetch field value.
  562. const char* pFieldValue = getDataField( fieldName, array );
  563. // Sanity.
  564. AssertFatal( pFieldValue != NULL, "Field value cannot be NULL." );
  565. // Return the field if no field type is specified.
  566. if ( fieldType == -1 )
  567. return pFieldValue;
  568. // Return without the prefix if there's no value.
  569. if ( *pFieldValue == 0 )
  570. return StringTable->EmptyString;
  571. // Fetch the console base type.
  572. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType( fieldType );
  573. // Did we find the console base type?
  574. if ( pConsoleBaseType == NULL )
  575. {
  576. // No, so warn.
  577. Con::warnf("getPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  578. fieldType, fieldName, pFieldValue );
  579. }
  580. // Fetch the field prefix.
  581. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  582. // Sanity!
  583. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  584. // Calculate a buffer size including prefix.
  585. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  586. // Fetch a buffer.
  587. char* pValueBuffer = Con::getReturnBuffer( valueBufferSize );
  588. // Format the value buffer.
  589. dSprintf( pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue );
  590. return pValueBuffer;
  591. }
  592. //-----------------------------------------------------------------------------
  593. void SimObject::setPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const char *value, const S32 fieldType )
  594. {
  595. // Sanity!
  596. AssertFatal( fieldName != NULL, "Cannot set object field value with NULL field name." );
  597. AssertFatal( value != NULL, "Field value cannot be NULL." );
  598. // Set value without prefix if no field type was specified.
  599. if ( fieldType == -1 )
  600. {
  601. setDataField( fieldName, NULL, value );
  602. return;
  603. }
  604. // Fetch the console base type.
  605. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType( fieldType );
  606. // Did we find the console base type?
  607. if ( pConsoleBaseType == NULL )
  608. {
  609. // No, so warn.
  610. Con::warnf("setPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  611. fieldType, fieldName, value );
  612. }
  613. // Set value without prefix if there's no value or we didn't find the console base type.
  614. if ( *value == 0 || pConsoleBaseType == NULL )
  615. {
  616. setDataField( fieldName, NULL, value );
  617. return;
  618. }
  619. // Fetch the field prefix.
  620. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  621. // Sanity.
  622. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  623. // Do we have a field prefix?
  624. if ( fieldPrefix == StringTable->EmptyString )
  625. {
  626. // No, so set the data field in the usual way.
  627. setDataField( fieldName, NULL, value );
  628. return;
  629. }
  630. // Yes, so fetch the length of the field prefix.
  631. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  632. // Yes, so does it start with the object field prefix?
  633. if ( dStrnicmp( value, fieldPrefix, fieldPrefixLength ) != 0 )
  634. {
  635. // No, so set the data field in the usual way.
  636. setDataField( fieldName, NULL, value );
  637. return;
  638. }
  639. // Yes, so set the data excluding the prefix.
  640. setDataField( fieldName, NULL, value + fieldPrefixLength );
  641. }
  642. //-----------------------------------------------------------------------------
  643. StringTableEntry SimObject::getDataFieldPrefix( StringTableEntry fieldName )
  644. {
  645. // Sanity!
  646. AssertFatal( fieldName != NULL, "Cannot get field prefix with NULL field name." );
  647. // Find the field.
  648. const AbstractClassRep::Field* pField = findField( fieldName );
  649. // Return nothing if field was not found.
  650. if ( pField == NULL )
  651. return StringTable->EmptyString;
  652. // Yes, so fetch the console base type.
  653. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType( pField->type );
  654. // Fetch the type prefix.
  655. return pConsoleBaseType->getTypePrefix();
  656. }
  657. //-----------------------------------------------------------------------------
  658. U32 SimObject::getDataFieldType( StringTableEntry slotName, const char* array )
  659. {
  660. const AbstractClassRep::Field* field = findField( slotName );
  661. if( field )
  662. return field->type;
  663. return 0;
  664. }
  665. SimObject::~SimObject()
  666. {
  667. delete mFieldDictionary;
  668. AssertFatal(nextNameObject == (SimObject*)-1,avar(
  669. "SimObject::~SimObject: Not removed from dictionary: name %s, id %i",
  670. objectName, mId));
  671. AssertFatal(nextManagerNameObject == (SimObject*)-1,avar(
  672. "SimObject::~SimObject: Not removed from manager dictionary: name %s, id %i",
  673. objectName,mId));
  674. AssertFatal(mFlags.test(Added) == 0, "SimObject::object "
  675. "missing call to SimObject::onRemove");
  676. }
  677. //---------------------------------------------------------------------------
  678. bool SimObject::isLocked()
  679. {
  680. if(!mFieldDictionary)
  681. return false;
  682. const char * val = mFieldDictionary->getFieldValue( StringTable->insert( "locked", false ) );
  683. return( val ? dAtob(val) : false );
  684. }
  685. void SimObject::setLocked( bool b = true )
  686. {
  687. setDataField(StringTable->insert("locked", false), NULL, b ? "true" : "false" );
  688. }
  689. bool SimObject::isHidden()
  690. {
  691. if(!mFieldDictionary)
  692. return false;
  693. const char * val = mFieldDictionary->getFieldValue( StringTable->insert( "hidden", false ) );
  694. return( val ? dAtob(val) : false );
  695. }
  696. void SimObject::setHidden(bool b = true)
  697. {
  698. setDataField(StringTable->insert("hidden", false), NULL, b ? "true" : "false" );
  699. }
  700. //---------------------------------------------------------------------------
  701. bool SimObject::onAdd()
  702. {
  703. mFlags.set(Added);
  704. linkNamespaces();
  705. // onAdd() should return FALSE if there was an error
  706. return true;
  707. }
  708. void SimObject::onRemove()
  709. {
  710. mFlags.clear(Added);
  711. unlinkNamespaces();
  712. }
  713. void SimObject::onGroupAdd()
  714. {
  715. }
  716. void SimObject::onGroupRemove()
  717. {
  718. }
  719. void SimObject::onDeleteNotify(SimObject*)
  720. {
  721. }
  722. void SimObject::onNameChange(const char*)
  723. {
  724. }
  725. void SimObject::onStaticModified(const char* slotName, const char* newValue)
  726. {
  727. }
  728. bool SimObject::processArguments(S32 argc, const char**)
  729. {
  730. return argc == 0;
  731. }
  732. bool SimObject::isChildOfGroup(SimGroup* pGroup)
  733. {
  734. if(!pGroup)
  735. return false;
  736. //if we *are* the group in question,
  737. //return true:
  738. if(pGroup == dynamic_cast<SimGroup*>(this))
  739. return true;
  740. SimGroup* temp = mGroup;
  741. while(temp)
  742. {
  743. if(temp == pGroup)
  744. return true;
  745. temp = temp->mGroup;
  746. }
  747. return false;
  748. }
  749. //---------------------------------------------------------------------------
  750. static Chunker<SimObject::Notify> notifyChunker(128000);
  751. SimObject::Notify *SimObject::mNotifyFreeList = NULL;
  752. SimObject::Notify *SimObject::allocNotify()
  753. {
  754. if(mNotifyFreeList)
  755. {
  756. SimObject::Notify *ret = mNotifyFreeList;
  757. mNotifyFreeList = ret->next;
  758. return ret;
  759. }
  760. return notifyChunker.alloc();
  761. }
  762. void SimObject::freeNotify(SimObject::Notify* note)
  763. {
  764. AssertFatal(note->type != SimObject::Notify::Invalid, "Invalid notify");
  765. note->type = SimObject::Notify::Invalid;
  766. note->next = mNotifyFreeList;
  767. mNotifyFreeList = note;
  768. }
  769. //------------------------------------------------------------------------------
  770. SimObject::Notify* SimObject::removeNotify(void *ptr, SimObject::Notify::Type type)
  771. {
  772. Notify **list = &mNotifyList;
  773. while(*list)
  774. {
  775. if((*list)->ptr == ptr && (*list)->type == type)
  776. {
  777. SimObject::Notify *ret = *list;
  778. *list = ret->next;
  779. return ret;
  780. }
  781. list = &((*list)->next);
  782. }
  783. return NULL;
  784. }
  785. void SimObject::deleteNotify(SimObject* obj)
  786. {
  787. AssertFatal(!obj->isDeleted(),
  788. "SimManager::deleteNotify: Object is being deleted");
  789. Notify *note = allocNotify();
  790. note->ptr = (void *) this;
  791. note->next = obj->mNotifyList;
  792. note->type = Notify::DeleteNotify;
  793. obj->mNotifyList = note;
  794. note = allocNotify();
  795. note->ptr = (void *) obj;
  796. note->next = mNotifyList;
  797. note->type = Notify::ClearNotify;
  798. mNotifyList = note;
  799. //obj->deleteNotifyList.pushBack(this);
  800. //clearNotifyList.pushBack(obj);
  801. }
  802. void SimObject::registerReference(SimObject **ptr)
  803. {
  804. Notify *note = allocNotify();
  805. note->ptr = (void *) ptr;
  806. note->next = mNotifyList;
  807. note->type = Notify::ObjectRef;
  808. mNotifyList = note;
  809. }
  810. void SimObject::unregisterReference(SimObject **ptr)
  811. {
  812. Notify *note = removeNotify((void *) ptr, Notify::ObjectRef);
  813. if(note)
  814. freeNotify(note);
  815. }
  816. void SimObject::clearNotify(SimObject* obj)
  817. {
  818. Notify *note = obj->removeNotify((void *) this, Notify::DeleteNotify);
  819. if(note)
  820. freeNotify(note);
  821. note = removeNotify((void *) obj, Notify::ClearNotify);
  822. if(note)
  823. freeNotify(note);
  824. }
  825. void SimObject::processDeleteNotifies()
  826. {
  827. // clear out any delete notifies and
  828. // object refs.
  829. while(mNotifyList)
  830. {
  831. Notify *note = mNotifyList;
  832. mNotifyList = note->next;
  833. AssertFatal(note->type != Notify::ClearNotify, "Clear notes should be all gone.");
  834. if(note->type == Notify::DeleteNotify)
  835. {
  836. SimObject *obj = (SimObject *) note->ptr;
  837. Notify *cnote = obj->removeNotify((void *)this, Notify::ClearNotify);
  838. obj->onDeleteNotify(this);
  839. freeNotify(cnote);
  840. }
  841. else
  842. {
  843. // it must be an object ref - a pointer refs this object
  844. *((SimObject **) note->ptr) = NULL;
  845. }
  846. freeNotify(note);
  847. }
  848. }
  849. void SimObject::clearAllNotifications()
  850. {
  851. for(Notify **cnote = &mNotifyList; *cnote; )
  852. {
  853. Notify *temp = *cnote;
  854. if(temp->type == Notify::ClearNotify)
  855. {
  856. *cnote = temp->next;
  857. Notify *note = ((SimObject *) temp->ptr)->removeNotify((void *) this, Notify::DeleteNotify);
  858. freeNotify(temp);
  859. freeNotify(note);
  860. }
  861. else
  862. cnote = &(temp->next);
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. void SimObject::initPersistFields()
  867. {
  868. Parent::initPersistFields();
  869. addGroup("SimBase");
  870. addField("canSaveDynamicFields", TypeBool, Offset(mCanSaveFieldDictionary, SimObject), &writeCanSaveDynamicFields, "");
  871. addField("internalName", TypeString, Offset(mInternalName, SimObject), &writeInternalName, "");
  872. addProtectedField("parentGroup", TypeSimObjectPtr, Offset(mGroup, SimObject), &setParentGroup, &defaultProtectedGetFn, &writeParentGroup, "Group hierarchy parent of the object." );
  873. endGroup("SimBase");
  874. // Namespace Linking.
  875. addGroup("Namespace Linking");
  876. addProtectedField("superclass", TypeString, Offset(mSuperClassName, SimObject), &setSuperClass, &defaultProtectedGetFn, &writeSuperclass, "Script Class of object.");
  877. addProtectedField("class", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn, &writeClass, "Script SuperClass of object.");
  878. endGroup("Namespace Linking");
  879. }
  880. //-----------------------------------------------------------------------------
  881. SimObject* SimObject::clone( const bool copyDynamicFields )
  882. {
  883. // Craete cloned object.
  884. SimObject* pCloneObject = dynamic_cast<SimObject*>( ConsoleObject::create(getClassName()) );
  885. if (!pCloneObject)
  886. {
  887. Con::errorf("SimObject::clone() - Unable to create cloned object.");
  888. return NULL;
  889. }
  890. // Register object.
  891. if ( !pCloneObject->registerObject() )
  892. {
  893. Con::warnf("SimObject::clone() - Unable to register cloned object.");
  894. delete pCloneObject;
  895. return NULL;
  896. }
  897. // Copy object.
  898. copyTo( pCloneObject );
  899. // Copy over dynamic fields if requested.
  900. if ( copyDynamicFields )
  901. pCloneObject->assignDynamicFieldsFrom( this );
  902. return pCloneObject;
  903. }
  904. //-----------------------------------------------------------------------------
  905. void SimObject::copyTo(SimObject* object)
  906. {
  907. object->mClassName = mClassName;
  908. object->mSuperClassName = mSuperClassName;
  909. object->mNameSpace = NULL;
  910. object->linkNamespaces();
  911. }
  912. //-----------------------------------------------------------------------------
  913. bool SimObject::setParentGroup(void* obj, const char* data)
  914. {
  915. SimGroup *parent = NULL;
  916. SimObject *object = static_cast<SimObject*>(obj);
  917. if(Sim::findObject(data, parent))
  918. parent->addObject(object);
  919. // always return false, because we've set mGroup when we called addObject
  920. return false;
  921. }
  922. bool SimObject::addToSet(SimObjectId spid)
  923. {
  924. if (mFlags.test(Added) == false)
  925. return false;
  926. SimObject* ptr = Sim::findObject(spid);
  927. if (ptr)
  928. {
  929. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  930. AssertFatal(sp != 0,
  931. "SimObject::addToSet: "
  932. "ObjectId does not refer to a set object");
  933. if (sp)
  934. {
  935. sp->addObject(this);
  936. return true;
  937. }
  938. }
  939. return false;
  940. }
  941. bool SimObject::addToSet(const char *ObjectName)
  942. {
  943. if (mFlags.test(Added) == false)
  944. return false;
  945. SimObject* ptr = Sim::findObject(ObjectName);
  946. if (ptr)
  947. {
  948. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  949. AssertFatal(sp != 0,
  950. "SimObject::addToSet: "
  951. "ObjectName does not refer to a set object");
  952. if (sp)
  953. {
  954. sp->addObject(this);
  955. return true;
  956. }
  957. }
  958. return false;
  959. }
  960. bool SimObject::removeFromSet(SimObjectId sid)
  961. {
  962. if (mFlags.test(Added) == false)
  963. return false;
  964. SimSet *set;
  965. if(Sim::findObject(sid, set))
  966. {
  967. set->removeObject(this);
  968. return true;
  969. }
  970. return false;
  971. }
  972. bool SimObject::removeFromSet(const char *objectName)
  973. {
  974. if (mFlags.test(Added) == false)
  975. return false;
  976. SimSet *set;
  977. if(Sim::findObject(objectName, set))
  978. {
  979. set->removeObject(this);
  980. return true;
  981. }
  982. return false;
  983. }
  984. void SimObject::inspectPreApply()
  985. {
  986. }
  987. void SimObject::inspectPostApply()
  988. {
  989. }
  990. //-----------------------------------------------------------------------------
  991. void SimObject::linkNamespaces()
  992. {
  993. // Don't link if we already have a namespace linkage in place.
  994. AssertWarn(mNameSpace == NULL, "SimObject::linkNamespaces -- Namespace linkage already in place");
  995. if (mNameSpace)
  996. return;
  997. // Start with the C++ Class namespace.
  998. StringTableEntry parent = StringTable->insert( getClassName() );
  999. // Link SuperClass to C++ Class.
  1000. if ( mSuperClassName && mSuperClassName[0] )
  1001. {
  1002. if ( Con::linkNamespaces(parent, mSuperClassName) )
  1003. parent = mSuperClassName;
  1004. else
  1005. mSuperClassName = NULL;
  1006. }
  1007. // Link Class to SuperClass or Class to C++ Class.
  1008. if ( mClassName && mClassName[0] )
  1009. {
  1010. if ( Con::linkNamespaces(parent, mClassName) )
  1011. parent = mClassName;
  1012. else
  1013. mClassName = NULL;
  1014. }
  1015. // Get the object's name.
  1016. StringTableEntry objectName = getName();
  1017. // Link Object Name to Class/SuperClass/C++ Class.
  1018. if ( objectName && objectName[0] )
  1019. {
  1020. if ( Con::linkNamespaces(parent, objectName) )
  1021. parent = objectName;
  1022. }
  1023. // Store our namespace.
  1024. mNameSpace = Con::lookupNamespace(parent);
  1025. }
  1026. //-----------------------------------------------------------------------------
  1027. void SimObject::unlinkNamespaces()
  1028. {
  1029. // Stop if there is no assigned namespace.
  1030. if (!mNameSpace)
  1031. return;
  1032. // Get the object's name.
  1033. StringTableEntry child = getName();
  1034. // Unlink any possible namespace combination.
  1035. if ( child && child[0] )
  1036. {
  1037. // Object Name/Class
  1038. if ( mClassName && mClassName[0] )
  1039. {
  1040. if( Con::unlinkNamespaces(mClassName, child) )
  1041. child = mClassName;
  1042. }
  1043. // Object Name/SuperClass or Class/SuperClass
  1044. if ( mSuperClassName && mSuperClassName[0] )
  1045. {
  1046. if ( Con::unlinkNamespaces(mSuperClassName, child) )
  1047. child = mSuperClassName;
  1048. }
  1049. // Object Name/C++ Class or SuperClass/C++ Class
  1050. Con::unlinkNamespaces(getClassName(), child);
  1051. }
  1052. else
  1053. {
  1054. // No Object Name, so get the Class namespace.
  1055. child = mClassName;
  1056. // Unlink any possible namespace combination.
  1057. if ( child && child[0] )
  1058. {
  1059. // Class/SuperClass
  1060. if ( mSuperClassName && mSuperClassName[0] )
  1061. {
  1062. if ( Con::unlinkNamespaces(mSuperClassName, child) )
  1063. child = mSuperClassName;
  1064. }
  1065. // Class/C++ Class or SuperClass/C++ Class
  1066. Con::unlinkNamespaces(getClassName(), child);
  1067. }
  1068. else
  1069. {
  1070. // SuperClass/C++ Class
  1071. if ( mSuperClassName && mSuperClassName[0] )
  1072. Con::unlinkNamespaces(getClassName(), mSuperClassName);
  1073. }
  1074. }
  1075. // Reset the namespace.
  1076. mNameSpace = NULL;
  1077. }
  1078. //-----------------------------------------------------------------------------
  1079. void SimObject::setClassNamespace( const char* classNamespace )
  1080. {
  1081. StringTableEntry oldClass = mClassName;
  1082. StringTableEntry newClass = StringTable->insert(classNamespace);
  1083. // Skip if no change.
  1084. if (oldClass == newClass)
  1085. return;
  1086. // Unlink the old namespaces.
  1087. if ( isProperlyAdded() )
  1088. unlinkNamespaces();
  1089. // Assign the new class namespace.
  1090. mClassName = newClass;
  1091. // Link the new namespaces.
  1092. if ( isProperlyAdded() )
  1093. linkNamespaces();
  1094. }
  1095. //-----------------------------------------------------------------------------
  1096. void SimObject::setSuperClassNamespace( const char* superClassNamespace )
  1097. {
  1098. StringTableEntry oldSuperClass = mSuperClassName;
  1099. StringTableEntry newSuperClass = StringTable->insert(superClassNamespace);
  1100. // Skip if no change.
  1101. if (oldSuperClass == newSuperClass)
  1102. return;
  1103. // Unlink the old namespaces.
  1104. if ( isProperlyAdded() )
  1105. unlinkNamespaces();
  1106. // Assign the new SuperClass namespace.
  1107. mSuperClassName = newSuperClass;
  1108. // Link the new namespaces.
  1109. if ( isProperlyAdded() )
  1110. linkNamespaces();
  1111. }
  1112. //-----------------------------------------------------------------------------
  1113. void SimObject::addListener(std::string objID)
  1114. {
  1115. for (auto iter = mListenerList.begin(); iter != mListenerList.end(); ++iter)
  1116. {
  1117. if (iter->objID == objID)
  1118. {
  1119. iter->doomed = false;
  1120. return;
  1121. }
  1122. }
  1123. OtoListener listener = OtoListener();
  1124. listener.objID = objID;
  1125. listener.doomed = false;
  1126. mListenerList.push_back(listener);
  1127. }
  1128. //-----------------------------------------------------------------------------
  1129. void SimObject::removeListener(std::string objID)
  1130. {
  1131. for (auto iter = mListenerList.begin(); iter != mListenerList.end(); ++iter)
  1132. {
  1133. if (iter->objID == objID)
  1134. {
  1135. iter->doomed = true;
  1136. }
  1137. }
  1138. if (!bIsEventRaised)
  1139. {
  1140. mListenerList.erase(std::remove_if(mListenerList.begin(), mListenerList.end(), [](OtoListener listener){ return listener.doomed; }), mListenerList.end());
  1141. }
  1142. }
  1143. //-----------------------------------------------------------------------------
  1144. void SimObject::removeAllListeners()
  1145. {
  1146. if (bIsEventRaised)
  1147. {
  1148. for (auto iter = mListenerList.begin(); iter != mListenerList.end(); ++iter)
  1149. {
  1150. iter->doomed = true;
  1151. }
  1152. }
  1153. else
  1154. {
  1155. mListenerList.clear();
  1156. }
  1157. }
  1158. //-----------------------------------------------------------------------------
  1159. void SimObject::postEvent(std::string eventName, std::string data)
  1160. {
  1161. std::string onEvent = "on" + eventName;
  1162. if (mListenerList.empty())
  1163. {
  1164. return;
  1165. }
  1166. if (bIsEventRaised)
  1167. {
  1168. Con::warnf("SimObject::postEvent() - To avoid circular events, you cannot raise the event '%s' until a previous event has finished.", eventName.c_str());
  1169. return;
  1170. }
  1171. bIsEventRaised = true;
  1172. for (auto iter = mListenerList.begin(); iter != mListenerList.end(); ++iter)
  1173. {
  1174. SimObject* pSimObject = dynamic_cast<SimObject*>(Sim::findObject(iter->objID.c_str()));
  1175. // Did we find the object?
  1176. if (pSimObject)
  1177. {
  1178. if (!iter->doomed && pSimObject->isMethod(onEvent.c_str()))
  1179. {
  1180. Con::executef(pSimObject, 3, onEvent.c_str(), data.c_str());
  1181. }
  1182. }
  1183. else
  1184. {
  1185. //it must have been deleted
  1186. iter->doomed = true;
  1187. }
  1188. }
  1189. bIsEventRaised = false;
  1190. //now to remove all doomed listeners
  1191. mListenerList.erase(std::remove_if(mListenerList.begin(), mListenerList.end(), [](OtoListener listener){ return listener.doomed; }), mListenerList.end());
  1192. }
  1193. //-----------------------------------------------------------------------------
  1194. static S32 QSORT_CALLBACK compareFields(const void* a,const void* b)
  1195. {
  1196. const AbstractClassRep::Field* fa = *((const AbstractClassRep::Field**)a);
  1197. const AbstractClassRep::Field* fb = *((const AbstractClassRep::Field**)b);
  1198. return dStricmp(fa->pFieldname, fb->pFieldname);
  1199. }
  1200. void SimObject::dump()
  1201. {
  1202. const AbstractClassRep::FieldList &list = getFieldList();
  1203. char expandedBuffer[4096];
  1204. Con::printf("Static Fields:");
  1205. Vector<const AbstractClassRep::Field *> flist(__FILE__, __LINE__);
  1206. for(U32 i = 0; i < (U32)list.size(); i++)
  1207. flist.push_back(&list[i]);
  1208. dQsort(flist.address(),flist.size(),sizeof(AbstractClassRep::Field *),compareFields);
  1209. for(Vector<const AbstractClassRep::Field *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  1210. {
  1211. const AbstractClassRep::Field* f = *itr;
  1212. if( f->type == AbstractClassRep::DepricatedFieldType ||
  1213. f->type == AbstractClassRep::StartGroupFieldType ||
  1214. f->type == AbstractClassRep::EndGroupFieldType) continue;
  1215. for(U32 j = 0; S32(j) < f->elementCount; j++)
  1216. {
  1217. // [neo, 07/05/2007 - #3000]
  1218. // Some objects use dummy vars and projected fields so make sure we call the get functions
  1219. //const char *val = Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag);
  1220. const char *val = (*f->getDataFn)( this, Con::getData(f->type, (void *) (((const char *)this) + f->offset), j, f->table, f->flag) );// + typeSizes[fld.type] * array1));
  1221. if(!val /*|| !*val*/)
  1222. continue;
  1223. if(f->elementCount == 1)
  1224. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s = \"", f->pFieldname);
  1225. else
  1226. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s[%d] = \"", f->pFieldname, j);
  1227. expandEscape(expandedBuffer + dStrlen(expandedBuffer), val);
  1228. Con::printf("%s\"", expandedBuffer);
  1229. }
  1230. }
  1231. Con::printf("Dynamic Fields:");
  1232. if(getFieldDictionary())
  1233. getFieldDictionary()->printFields(this);
  1234. Con::printf("Methods:");
  1235. Namespace *ns = getNamespace();
  1236. Vector<Namespace::Entry *> vec(__FILE__, __LINE__);
  1237. if(ns)
  1238. ns->getEntryList(&vec);
  1239. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  1240. Con::printf(" %s() - %s", (*j)->mFunctionName, (*j)->mUsage ? (*j)->mUsage : "");
  1241. }