2
0

simObject.cc 46 KB

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