simObject.cc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. //-----------------------------------------------------------------------------
  33. IMPLEMENT_CONOBJECT(SimObject);
  34. namespace Sim
  35. {
  36. extern U32 gNextObjectId;
  37. extern SimIdDictionary *gIdDictionary;
  38. extern SimManagerNameDictionary *gNameDictionary;
  39. extern void cancelPendingEvents(SimObject *obj);
  40. }
  41. //-----------------------------------------------------------------------------
  42. SimObject::SimObject()
  43. {
  44. mFlags.set( ModStaticFields | ModDynamicFields );
  45. objectName = NULL;
  46. mInternalName = NULL;
  47. nextNameObject = (SimObject*)-1;
  48. nextManagerNameObject = (SimObject*)-1;
  49. nextIdObject = NULL;
  50. mId = 0;
  51. mIdString = StringTable->EmptyString;
  52. mGroup = 0;
  53. mNameSpace = NULL;
  54. mNotifyList = NULL;
  55. mTypeMask = 0;
  56. mScriptCallbackGuard = 0;
  57. mFieldDictionary = NULL;
  58. mCanSaveFieldDictionary = true;
  59. mClassName = NULL;
  60. mSuperClassName = NULL;
  61. mProgenitorFile = CodeBlock::getCurrentCodeBlockFullPath();
  62. mPeriodicTimerID = 0;
  63. }
  64. //---------------------------------------------------------------------------
  65. bool SimObject::isMethod( const char* methodName )
  66. {
  67. if( !methodName || !methodName[0] )
  68. return false;
  69. StringTableEntry stname = StringTable->insert( methodName );
  70. if( getNamespace() )
  71. return ( getNamespace()->lookup( stname ) != NULL );
  72. return false;
  73. }
  74. //---------------------------------------------------------------------------
  75. bool SimObject::registerObject()
  76. {
  77. AssertFatal( !mFlags.test( Added ), "reigsterObject - Object already registered!");
  78. mFlags.clear(Deleted | Removed);
  79. if( mId == 0 )
  80. {
  81. mId = Sim::gNextObjectId++;
  82. char idBuffer[64];
  83. dSprintf(idBuffer, sizeof(idBuffer), "%d", mId);
  84. mIdString = StringTable->insert( idBuffer );
  85. }
  86. AssertFatal(Sim::gIdDictionary && Sim::gNameDictionary,
  87. "SimObject::registerObject - tried to register an object before Sim::init()!");
  88. Sim::gIdDictionary->insert(this);
  89. Sim::gNameDictionary->insert(this);
  90. // Notify object
  91. bool ret = onAdd();
  92. if(!ret)
  93. unregisterObject();
  94. AssertFatal(!ret || isProperlyAdded(), "Object did not call SimObject::onAdd()");
  95. if ( isMethod( "onAdd" ) )
  96. Con::executef( this, 1, "onAdd" );
  97. return ret;
  98. }
  99. //---------------------------------------------------------------------------
  100. void SimObject::unregisterObject()
  101. {
  102. // Sanity!
  103. AssertISV( getScriptCallbackGuard() == 0, "SimObject::unregisterObject: Object is being unregistered whilst performing a script callback!" );
  104. if ( isMethod( "onRemove" ) )
  105. Con::executef( this, 1, "onRemove" );
  106. mFlags.set(Removed);
  107. // Notify object first
  108. onRemove();
  109. // Clear out any pending notifications before
  110. // we call our own, just in case they delete
  111. // something that we have referenced.
  112. clearAllNotifications();
  113. // Notify all objects that are waiting for delete
  114. // messages
  115. if (getGroup())
  116. getGroup()->removeObject(this);
  117. processDeleteNotifies();
  118. // Do removals from the Sim.
  119. Sim::gNameDictionary->remove(this);
  120. Sim::gIdDictionary->remove(this);
  121. Sim::cancelPendingEvents(this);
  122. }
  123. //---------------------------------------------------------------------------
  124. void SimObject::deleteObject()
  125. {
  126. // Sanity!
  127. AssertISV( getScriptCallbackGuard() == 0, "SimObject::deleteObject: Object is being deleted whilst performing a script callback!" );
  128. AssertFatal(mFlags.test(Added),
  129. "SimObject::deleteObject: Object not registered.");
  130. AssertFatal(!isDeleted(),"SimManager::deleteObject: "
  131. "Object has already been deleted");
  132. AssertFatal(!isRemoved(),"SimManager::deleteObject: "
  133. "Object in the process of being removed");
  134. mFlags.set(Deleted);
  135. unregisterObject();
  136. delete this;
  137. }
  138. //---------------------------------------------------------------------------
  139. void SimObject::setId(SimObjectId newId)
  140. {
  141. if(!mFlags.test(Added))
  142. {
  143. mId = newId;
  144. }
  145. else
  146. {
  147. // get this object out of the id dictionary if it's in it
  148. Sim::gIdDictionary->remove(this);
  149. // Free current Id.
  150. // Assign new one.
  151. mId = newId ? newId : Sim::gNextObjectId++;
  152. Sim::gIdDictionary->insert(this);
  153. }
  154. char idBuffer[64];
  155. dSprintf(idBuffer, sizeof(idBuffer), "%d", mId);
  156. mIdString = StringTable->insert( idBuffer );
  157. }
  158. void SimObject::assignName(const char *name)
  159. {
  160. if( dStricmp( getClassName(), name ) == 0 )
  161. Con::errorf( "SimObject::assignName - Assigning name '%s' to instance of object with type '%s'."
  162. " This can cause namespace linking issues.", getClassName(), name );
  163. // Is this name already registered?
  164. if ( Sim::gNameDictionary->find(name) != NULL )
  165. {
  166. // Yes, so error,
  167. Con::errorf( "SimObject::assignName() - Attempted to set object to name '%s' but it is already assigned to another object.", name );
  168. return;
  169. }
  170. StringTableEntry newName = NULL;
  171. if (name[0])
  172. newName = StringTable->insert(name);
  173. if (mGroup)
  174. mGroup->nameDictionary.remove(this);
  175. if (isProperlyAdded())
  176. {
  177. // Unlink the old namespaces.
  178. unlinkNamespaces();
  179. Sim::gNameDictionary->remove(this);
  180. }
  181. objectName = newName;
  182. if (mGroup)
  183. mGroup->nameDictionary.insert(this);
  184. if (isProperlyAdded())
  185. {
  186. // Link the new namespaces.
  187. linkNamespaces();
  188. Sim::gNameDictionary->insert(this);
  189. }
  190. }
  191. //---------------------------------------------------------------------------
  192. bool SimObject::registerObject(U32 id)
  193. {
  194. setId(id);
  195. return registerObject();
  196. }
  197. bool SimObject::registerObject(const char *name)
  198. {
  199. assignName(name);
  200. return registerObject();
  201. }
  202. bool SimObject::registerObject(const char *name, U32 id)
  203. {
  204. setId(id);
  205. assignName(name);
  206. return registerObject();
  207. }
  208. void SimObject::assignDynamicFieldsFrom(SimObject* parent)
  209. {
  210. if(parent->mFieldDictionary)
  211. {
  212. if( mFieldDictionary == NULL )
  213. mFieldDictionary = new SimFieldDictionary;
  214. mFieldDictionary->assignFrom(parent->mFieldDictionary);
  215. }
  216. }
  217. void SimObject::assignFieldsFrom(SimObject *parent)
  218. {
  219. // only allow field assigns from objects of the same class:
  220. if(getClassRep() == parent->getClassRep())
  221. {
  222. const AbstractClassRep::FieldList &list = getFieldList();
  223. // copy out all the fields:
  224. for(U32 i = 0; i < (U32)list.size(); i++)
  225. {
  226. const AbstractClassRep::Field* f = &list[i];
  227. S32 lastField = f->elementCount - 1;
  228. for(S32 j = 0; j <= lastField; j++)
  229. {
  230. const char* fieldVal = (*f->getDataFn)( this, Con::getData(f->type, (void *) (((const char *)parent) + f->offset), j, f->table, f->flag));
  231. //if(fieldVal)
  232. // Con::setData(f->type, (void *) (((const char *)this) + f->offset), j, 1, &fieldVal, f->table);
  233. if(fieldVal)
  234. {
  235. // code copied from SimObject::setDataField().
  236. // TODO: paxorr: abstract this into a better setData / getData that considers prot fields.
  237. FrameTemp<char> buffer(2048);
  238. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  239. ConsoleBaseType *cbt = ConsoleBaseType::getType( f->type );
  240. const char* szBuffer = cbt->prepData( fieldVal, buffer, 2048 );
  241. dMemset( bufferSecure, 0, 2048 );
  242. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  243. if((*f->setDataFn)( this, bufferSecure ) )
  244. Con::setData(f->type, (void *) (((const char *)this) + f->offset), j, 1, &fieldVal, f->table);
  245. }
  246. }
  247. }
  248. }
  249. assignDynamicFieldsFrom(parent);
  250. }
  251. bool SimObject::writeField(StringTableEntry fieldname, const char* value)
  252. {
  253. // Don't write empty fields.
  254. if (!value || !*value)
  255. return false;
  256. // Don't write ParentGroup
  257. if( fieldname == StringTable->insert("parentGroup") )
  258. return false;
  259. return true;
  260. }
  261. void SimObject::writeFields(Stream &stream, U32 tabStop)
  262. {
  263. const AbstractClassRep::FieldList &list = getFieldList();
  264. for(U32 i = 0; i < (U32)list.size(); i++)
  265. {
  266. const AbstractClassRep::Field* f = &list[i];
  267. if( f->type == AbstractClassRep::DepricatedFieldType ||
  268. f->type == AbstractClassRep::StartGroupFieldType ||
  269. f->type == AbstractClassRep::EndGroupFieldType) continue;
  270. // Fetch fieldname.
  271. StringTableEntry fieldName = StringTable->insert( f->pFieldname );
  272. // Fetch element count.
  273. const S32 elementCount = f->elementCount;
  274. // Skip if the field should not be written.
  275. // For now, we only deal with non-array fields.
  276. if ( elementCount == 1 &&
  277. f->writeDataFn != NULL &&
  278. f->writeDataFn( this, fieldName ) == false )
  279. continue;
  280. for(U32 j = 0; S32(j) < elementCount; j++)
  281. {
  282. char array[8];
  283. dSprintf( array, 8, "%d", j );
  284. const char *val = getDataField(fieldName, array );
  285. // Make a copy for the field check.
  286. if (!val)
  287. continue;
  288. U32 nBufferSize = dStrlen( val ) + 1;
  289. FrameTemp<char> valCopy( nBufferSize );
  290. dStrcpy( (char *)valCopy, val );
  291. if (!writeField(fieldName, valCopy))
  292. continue;
  293. val = valCopy;
  294. U32 expandedBufferSize = ( nBufferSize * 2 ) + 32;
  295. FrameTemp<char> expandedBuffer( expandedBufferSize );
  296. if(f->elementCount == 1)
  297. dSprintf(expandedBuffer, expandedBufferSize, "%s = \"", f->pFieldname);
  298. else
  299. dSprintf(expandedBuffer, expandedBufferSize, "%s[%d] = \"", f->pFieldname, j);
  300. // detect and collapse relative path information
  301. char fnBuf[1024];
  302. if (f->type == TypeFilename)
  303. {
  304. Con::collapsePath(fnBuf, 1024, val);
  305. val = fnBuf;
  306. }
  307. expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), val);
  308. dStrcat(expandedBuffer, "\";\r\n");
  309. stream.writeTabs(tabStop);
  310. stream.write(dStrlen(expandedBuffer),expandedBuffer);
  311. }
  312. }
  313. if(mFieldDictionary && mCanSaveFieldDictionary)
  314. mFieldDictionary->writeFields(this, stream, tabStop);
  315. }
  316. void SimObject::write(Stream &stream, U32 tabStop, U32 flags)
  317. {
  318. // Only output selected objects if they want that.
  319. if((flags & SelectedOnly) && !isSelected())
  320. return;
  321. stream.writeTabs(tabStop);
  322. char buffer[1024];
  323. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() ? getName() : "");
  324. stream.write(dStrlen(buffer), buffer);
  325. writeFields(stream, tabStop + 1);
  326. stream.writeTabs(tabStop);
  327. stream.write(4, "};\r\n");
  328. }
  329. bool SimObject::save(const char* pcFileName, bool bOnlySelected)
  330. {
  331. static const char *beginMessage = "//--- OBJECT WRITE BEGIN ---";
  332. static const char *endMessage = "//--- OBJECT WRITE END ---";
  333. FileStream stream;
  334. FileObject f;
  335. f.readMemory(pcFileName);
  336. // check for flags <selected, ...>
  337. U32 writeFlags = 0;
  338. if(bOnlySelected)
  339. writeFlags |= SimObject::SelectedOnly;
  340. if(!ResourceManager->openFileForWrite(stream, pcFileName))
  341. return false;
  342. char docRoot[256];
  343. char modRoot[256];
  344. dStrcpy(docRoot, pcFileName);
  345. char *p = dStrrchr(docRoot, '/');
  346. if (p) *++p = '\0';
  347. else docRoot[0] = '\0';
  348. dStrcpy(modRoot, pcFileName);
  349. p = dStrchr(modRoot, '/');
  350. if (p) *++p = '\0';
  351. else modRoot[0] = '\0';
  352. Con::setVariable("$DocRoot", docRoot);
  353. Con::setVariable("$ModRoot", modRoot);
  354. const char *buffer;
  355. while(!f.isEOF())
  356. {
  357. buffer = (const char *) f.readLine();
  358. if(!dStrcmp(buffer, beginMessage))
  359. break;
  360. stream.write(dStrlen(buffer), buffer);
  361. stream.write(2, "\r\n");
  362. }
  363. stream.write(dStrlen(beginMessage), beginMessage);
  364. stream.write(2, "\r\n");
  365. write(stream, 0, writeFlags);
  366. stream.write(dStrlen(endMessage), endMessage);
  367. stream.write(2, "\r\n");
  368. while(!f.isEOF())
  369. {
  370. buffer = (const char *) f.readLine();
  371. if(!dStrcmp(buffer, endMessage))
  372. break;
  373. }
  374. while(!f.isEOF())
  375. {
  376. buffer = (const char *) f.readLine();
  377. stream.write(dStrlen(buffer), buffer);
  378. stream.write(2, "\r\n");
  379. }
  380. Con::setVariable("$DocRoot", NULL);
  381. Con::setVariable("$ModRoot", NULL);
  382. return true;
  383. }
  384. void SimObject::setInternalName(const char* newname)
  385. {
  386. if(newname)
  387. mInternalName = StringTable->insert(newname);
  388. }
  389. StringTableEntry SimObject::getInternalName()
  390. {
  391. return mInternalName;
  392. }
  393. void SimObject::dumpClassHierarchy()
  394. {
  395. AbstractClassRep* pRep = getClassRep();
  396. while(pRep)
  397. {
  398. Con::warnf("%s ->", pRep->getClassName());
  399. pRep = pRep->getParentClass();
  400. }
  401. }
  402. const char *SimObject::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  403. {
  404. return mNameSpace->tabComplete(prevText, baseLen, fForward);
  405. }
  406. //-----------------------------------------------------------------------------
  407. void SimObject::setDataField(StringTableEntry slotName, const char *array, const char *value)
  408. {
  409. // first search the static fields if enabled
  410. if(mFlags.test(ModStaticFields))
  411. {
  412. const AbstractClassRep::Field *fld = findField(slotName);
  413. if(fld)
  414. {
  415. if( fld->type == AbstractClassRep::DepricatedFieldType ||
  416. fld->type == AbstractClassRep::StartGroupFieldType ||
  417. fld->type == AbstractClassRep::EndGroupFieldType)
  418. return;
  419. S32 array1 = array ? dAtoi(array) : 0;
  420. if(array1 >= 0 && array1 < fld->elementCount && fld->elementCount >= 1)
  421. {
  422. // If the set data notify callback returns true, then go ahead and
  423. // set the data, otherwise, assume the set notify callback has either
  424. // already set the data, or has deemed that the data should not
  425. // be set at all.
  426. FrameTemp<char> buffer(2048);
  427. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  428. // so that if the prep functions or any other functions use the string stack, the data
  429. // is not corrupted.
  430. ConsoleBaseType *cbt = ConsoleBaseType::getType( fld->type );
  431. AssertFatal( cbt != NULL, "Could not resolve Type Id." );
  432. const char* szBuffer = cbt->prepData( value, buffer, 2048 );
  433. dMemset( bufferSecure, 0, 2048 );
  434. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  435. if( (*fld->setDataFn)( this, bufferSecure ) )
  436. Con::setData(fld->type, (void *) (((const char *)this) + fld->offset), array1, 1, &value, fld->table);
  437. onStaticModified( slotName, value );
  438. return;
  439. }
  440. if(fld->validator)
  441. fld->validator->validateType(this, (void *) (((const char *)this) + fld->offset));
  442. onStaticModified( slotName, value );
  443. return;
  444. }
  445. }
  446. if(mFlags.test(ModDynamicFields))
  447. {
  448. if(!mFieldDictionary)
  449. mFieldDictionary = new SimFieldDictionary;
  450. if(!array)
  451. mFieldDictionary->setFieldValue(slotName, value);
  452. else
  453. {
  454. char buf[256];
  455. dStrcpy(buf, slotName);
  456. dStrcat(buf, array);
  457. mFieldDictionary->setFieldValue(StringTable->insert(buf), value);
  458. }
  459. }
  460. }
  461. //-----------------------------------------------------------------------------
  462. const char *SimObject::getDataField(StringTableEntry slotName, const char *array)
  463. {
  464. if(mFlags.test(ModStaticFields))
  465. {
  466. S32 array1 = array ? dAtoi(array) : -1;
  467. const AbstractClassRep::Field *fld = findField(slotName);
  468. if(fld)
  469. {
  470. if(array1 == -1 && fld->elementCount == 1)
  471. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), 0, fld->table, fld->flag) );
  472. if(array1 >= 0 && array1 < fld->elementCount)
  473. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), array1, fld->table, fld->flag) );// + typeSizes[fld.type] * array1));
  474. return "";
  475. }
  476. }
  477. if(mFlags.test(ModDynamicFields))
  478. {
  479. if(!mFieldDictionary)
  480. return "";
  481. if(!array)
  482. {
  483. if (const char* val = mFieldDictionary->getFieldValue(slotName))
  484. return val;
  485. }
  486. else
  487. {
  488. static char buf[256];
  489. dStrcpy(buf, slotName);
  490. dStrcat(buf, array);
  491. if (const char* val = mFieldDictionary->getFieldValue(StringTable->insert(buf)))
  492. return val;
  493. }
  494. }
  495. return "";
  496. }
  497. //-----------------------------------------------------------------------------
  498. const char *SimObject::getPrefixedDataField(StringTableEntry fieldName, const char *array)
  499. {
  500. // Sanity!
  501. AssertFatal( fieldName != NULL, "Cannot get field value with NULL field name." );
  502. // Fetch field value.
  503. const char* pFieldValue = getDataField( fieldName, array );
  504. // Sanity.
  505. AssertFatal( pFieldValue != NULL, "Field value cannot be NULL." );
  506. // Return without the prefix if there's no value.
  507. if ( *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. static S32 QSORT_CALLBACK compareFields(const void* a,const void* b)
  1114. {
  1115. const AbstractClassRep::Field* fa = *((const AbstractClassRep::Field**)a);
  1116. const AbstractClassRep::Field* fb = *((const AbstractClassRep::Field**)b);
  1117. return dStricmp(fa->pFieldname, fb->pFieldname);
  1118. }
  1119. void SimObject::dump()
  1120. {
  1121. const AbstractClassRep::FieldList &list = getFieldList();
  1122. char expandedBuffer[4096];
  1123. Con::printf("Static Fields:");
  1124. Vector<const AbstractClassRep::Field *> flist(__FILE__, __LINE__);
  1125. for(U32 i = 0; i < (U32)list.size(); i++)
  1126. flist.push_back(&list[i]);
  1127. dQsort(flist.address(),flist.size(),sizeof(AbstractClassRep::Field *),compareFields);
  1128. for(Vector<const AbstractClassRep::Field *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  1129. {
  1130. const AbstractClassRep::Field* f = *itr;
  1131. if( f->type == AbstractClassRep::DepricatedFieldType ||
  1132. f->type == AbstractClassRep::StartGroupFieldType ||
  1133. f->type == AbstractClassRep::EndGroupFieldType) continue;
  1134. for(U32 j = 0; S32(j) < f->elementCount; j++)
  1135. {
  1136. // [neo, 07/05/2007 - #3000]
  1137. // Some objects use dummy vars and projected fields so make sure we call the get functions
  1138. //const char *val = Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag);
  1139. 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));
  1140. if(!val /*|| !*val*/)
  1141. continue;
  1142. if(f->elementCount == 1)
  1143. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s = \"", f->pFieldname);
  1144. else
  1145. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s[%d] = \"", f->pFieldname, j);
  1146. expandEscape(expandedBuffer + dStrlen(expandedBuffer), val);
  1147. Con::printf("%s\"", expandedBuffer);
  1148. }
  1149. }
  1150. Con::printf("Dynamic Fields:");
  1151. if(getFieldDictionary())
  1152. getFieldDictionary()->printFields(this);
  1153. Con::printf("Methods:");
  1154. Namespace *ns = getNamespace();
  1155. Vector<Namespace::Entry *> vec(__FILE__, __LINE__);
  1156. if(ns)
  1157. ns->getEntryList(&vec);
  1158. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  1159. Con::printf(" %s() - %s", (*j)->mFunctionName, (*j)->mUsage ? (*j)->mUsage : "");
  1160. }