simObject.cc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. // Return without the prefix if there's no value.
  505. if ( pFieldValue == NULL || *pFieldValue == 0 )
  506. return StringTable->EmptyString;
  507. // Fetch the field prefix.
  508. StringTableEntry fieldPrefix = getDataFieldPrefix( fieldName );
  509. // Sanity!
  510. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  511. // Calculate a buffer size including prefix.
  512. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  513. // Fetch a buffer.
  514. char* pValueBuffer = Con::getReturnBuffer( valueBufferSize );
  515. // Format the value buffer.
  516. dSprintf( pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue );
  517. return pValueBuffer;
  518. }
  519. //-----------------------------------------------------------------------------
  520. void SimObject::setPrefixedDataField(StringTableEntry fieldName, const char *array, const char *value)
  521. {
  522. // Sanity!
  523. AssertFatal( fieldName != NULL, "Cannot set object field value with NULL field name." );
  524. AssertFatal( value != NULL, "Field value cannot be NULL." );
  525. // Set value without prefix if there's no value.
  526. if ( *value == 0 )
  527. {
  528. setDataField( fieldName, NULL, value );
  529. return;
  530. }
  531. // Fetch the field prefix.
  532. StringTableEntry fieldPrefix = getDataFieldPrefix( fieldName );
  533. // Sanity.
  534. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  535. // Do we have a field prefix?
  536. if ( fieldPrefix == StringTable->EmptyString )
  537. {
  538. // No, so set the data field in the usual way.
  539. setDataField( fieldName, NULL, value );
  540. return;
  541. }
  542. // Yes, so fetch the length of the field prefix.
  543. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  544. // Yes, so does it start with the object field prefix?
  545. if ( dStrnicmp( value, fieldPrefix, fieldPrefixLength ) != 0 )
  546. {
  547. // No, so set the data field in the usual way.
  548. setDataField( fieldName, NULL, value );
  549. return;
  550. }
  551. // Yes, so set the data excluding the prefix.
  552. setDataField( fieldName, NULL, value + fieldPrefixLength );
  553. }
  554. //-----------------------------------------------------------------------------
  555. const char *SimObject::getPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const S32 fieldType )
  556. {
  557. // Sanity!
  558. AssertFatal( fieldName != NULL, "Cannot get field value with NULL field name." );
  559. // Fetch field value.
  560. const char* pFieldValue = getDataField( fieldName, array );
  561. // Sanity.
  562. AssertFatal( pFieldValue != NULL, "Field value cannot be NULL." );
  563. // Return the field if no field type is specified.
  564. if ( fieldType == -1 )
  565. return pFieldValue;
  566. // Return without the prefix if there's no value.
  567. if ( *pFieldValue == 0 )
  568. return StringTable->EmptyString;
  569. // Fetch the console base type.
  570. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType( fieldType );
  571. // Did we find the console base type?
  572. if ( pConsoleBaseType == NULL )
  573. {
  574. // No, so warn.
  575. Con::warnf("getPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  576. fieldType, fieldName, pFieldValue );
  577. }
  578. // Fetch the field prefix.
  579. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  580. // Sanity!
  581. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  582. // Calculate a buffer size including prefix.
  583. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  584. // Fetch a buffer.
  585. char* pValueBuffer = Con::getReturnBuffer( valueBufferSize );
  586. // Format the value buffer.
  587. dSprintf( pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue );
  588. return pValueBuffer;
  589. }
  590. //-----------------------------------------------------------------------------
  591. void SimObject::setPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const char *value, const S32 fieldType )
  592. {
  593. // Sanity!
  594. AssertFatal( fieldName != NULL, "Cannot set object field value with NULL field name." );
  595. AssertFatal( value != NULL, "Field value cannot be NULL." );
  596. // Set value without prefix if no field type was specified.
  597. if ( fieldType == -1 )
  598. {
  599. setDataField( fieldName, NULL, value );
  600. return;
  601. }
  602. // Fetch the console base type.
  603. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType( fieldType );
  604. // Did we find the console base type?
  605. if ( pConsoleBaseType == NULL )
  606. {
  607. // No, so warn.
  608. Con::warnf("setPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  609. fieldType, fieldName, value );
  610. }
  611. // Set value without prefix if there's no value or we didn't find the console base type.
  612. if ( *value == 0 || pConsoleBaseType == NULL )
  613. {
  614. setDataField( fieldName, NULL, value );
  615. return;
  616. }
  617. // Fetch the field prefix.
  618. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  619. // Sanity.
  620. AssertFatal( fieldPrefix != NULL, "Field prefix cannot be NULL." );
  621. // Do we have a field prefix?
  622. if ( fieldPrefix == StringTable->EmptyString )
  623. {
  624. // No, so set the data field in the usual way.
  625. setDataField( fieldName, NULL, value );
  626. return;
  627. }
  628. // Yes, so fetch the length of the field prefix.
  629. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  630. // Yes, so does it start with the object field prefix?
  631. if ( dStrnicmp( value, fieldPrefix, fieldPrefixLength ) != 0 )
  632. {
  633. // No, so set the data field in the usual way.
  634. setDataField( fieldName, NULL, value );
  635. return;
  636. }
  637. // Yes, so set the data excluding the prefix.
  638. setDataField( fieldName, NULL, value + fieldPrefixLength );
  639. }
  640. //-----------------------------------------------------------------------------
  641. StringTableEntry SimObject::getDataFieldPrefix( StringTableEntry fieldName )
  642. {
  643. // Sanity!
  644. AssertFatal( fieldName != NULL, "Cannot get field prefix with NULL field name." );
  645. // Find the field.
  646. const AbstractClassRep::Field* pField = findField( fieldName );
  647. // Return nothing if field was not found.
  648. if ( pField == NULL )
  649. return StringTable->EmptyString;
  650. // Yes, so fetch the console base type.
  651. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType( pField->type );
  652. // Fetch the type prefix.
  653. return pConsoleBaseType->getTypePrefix();
  654. }
  655. //-----------------------------------------------------------------------------
  656. U32 SimObject::getDataFieldType( StringTableEntry slotName, const char* array )
  657. {
  658. const AbstractClassRep::Field* field = findField( slotName );
  659. if( field )
  660. return field->type;
  661. return 0;
  662. }
  663. SimObject::~SimObject()
  664. {
  665. delete mFieldDictionary;
  666. AssertFatal(nextNameObject == (SimObject*)-1,avar(
  667. "SimObject::~SimObject: Not removed from dictionary: name %s, id %i",
  668. objectName, mId));
  669. AssertFatal(nextManagerNameObject == (SimObject*)-1,avar(
  670. "SimObject::~SimObject: Not removed from manager dictionary: name %s, id %i",
  671. objectName,mId));
  672. AssertFatal(mFlags.test(Added) == 0, "SimObject::object "
  673. "missing call to SimObject::onRemove");
  674. }
  675. //---------------------------------------------------------------------------
  676. bool SimObject::isLocked()
  677. {
  678. if(!mFieldDictionary)
  679. return false;
  680. const char * val = mFieldDictionary->getFieldValue( StringTable->insert( "locked", false ) );
  681. return( val ? dAtob(val) : false );
  682. }
  683. void SimObject::setLocked( bool b = true )
  684. {
  685. setDataField(StringTable->insert("locked", false), NULL, b ? "true" : "false" );
  686. }
  687. bool SimObject::isHidden()
  688. {
  689. if(!mFieldDictionary)
  690. return false;
  691. const char * val = mFieldDictionary->getFieldValue( StringTable->insert( "hidden", false ) );
  692. return( val ? dAtob(val) : false );
  693. }
  694. void SimObject::setHidden(bool b = true)
  695. {
  696. setDataField(StringTable->insert("hidden", false), NULL, b ? "true" : "false" );
  697. }
  698. //---------------------------------------------------------------------------
  699. bool SimObject::onAdd()
  700. {
  701. mFlags.set(Added);
  702. linkNamespaces();
  703. // onAdd() should return FALSE if there was an error
  704. return true;
  705. }
  706. void SimObject::onRemove()
  707. {
  708. mFlags.clear(Added);
  709. unlinkNamespaces();
  710. }
  711. void SimObject::onGroupAdd()
  712. {
  713. }
  714. void SimObject::onGroupRemove()
  715. {
  716. }
  717. void SimObject::onDeleteNotify(SimObject*)
  718. {
  719. }
  720. void SimObject::onNameChange(const char*)
  721. {
  722. }
  723. void SimObject::onStaticModified(const char* slotName, const char* newValue)
  724. {
  725. }
  726. bool SimObject::processArguments(S32 argc, const char**)
  727. {
  728. return argc == 0;
  729. }
  730. bool SimObject::isChildOfGroup(SimGroup* pGroup)
  731. {
  732. if(!pGroup)
  733. return false;
  734. //if we *are* the group in question,
  735. //return true:
  736. if(pGroup == dynamic_cast<SimGroup*>(this))
  737. return true;
  738. SimGroup* temp = mGroup;
  739. while(temp)
  740. {
  741. if(temp == pGroup)
  742. return true;
  743. temp = temp->mGroup;
  744. }
  745. return false;
  746. }
  747. //---------------------------------------------------------------------------
  748. static Chunker<SimObject::Notify> notifyChunker(128000);
  749. SimObject::Notify *SimObject::mNotifyFreeList = NULL;
  750. SimObject::Notify *SimObject::allocNotify()
  751. {
  752. if(mNotifyFreeList)
  753. {
  754. SimObject::Notify *ret = mNotifyFreeList;
  755. mNotifyFreeList = ret->next;
  756. return ret;
  757. }
  758. return notifyChunker.alloc();
  759. }
  760. void SimObject::freeNotify(SimObject::Notify* note)
  761. {
  762. AssertFatal(note->type != SimObject::Notify::Invalid, "Invalid notify");
  763. note->type = SimObject::Notify::Invalid;
  764. note->next = mNotifyFreeList;
  765. mNotifyFreeList = note;
  766. }
  767. //------------------------------------------------------------------------------
  768. SimObject::Notify* SimObject::removeNotify(void *ptr, SimObject::Notify::Type type)
  769. {
  770. Notify **list = &mNotifyList;
  771. while(*list)
  772. {
  773. if((*list)->ptr == ptr && (*list)->type == type)
  774. {
  775. SimObject::Notify *ret = *list;
  776. *list = ret->next;
  777. return ret;
  778. }
  779. list = &((*list)->next);
  780. }
  781. return NULL;
  782. }
  783. void SimObject::deleteNotify(SimObject* obj)
  784. {
  785. AssertFatal(!obj->isDeleted(),
  786. "SimManager::deleteNotify: Object is being deleted");
  787. Notify *note = allocNotify();
  788. note->ptr = (void *) this;
  789. note->next = obj->mNotifyList;
  790. note->type = Notify::DeleteNotify;
  791. obj->mNotifyList = note;
  792. note = allocNotify();
  793. note->ptr = (void *) obj;
  794. note->next = mNotifyList;
  795. note->type = Notify::ClearNotify;
  796. mNotifyList = note;
  797. //obj->deleteNotifyList.pushBack(this);
  798. //clearNotifyList.pushBack(obj);
  799. }
  800. void SimObject::registerReference(SimObject **ptr)
  801. {
  802. Notify *note = allocNotify();
  803. note->ptr = (void *) ptr;
  804. note->next = mNotifyList;
  805. note->type = Notify::ObjectRef;
  806. mNotifyList = note;
  807. }
  808. void SimObject::unregisterReference(SimObject **ptr)
  809. {
  810. Notify *note = removeNotify((void *) ptr, Notify::ObjectRef);
  811. if(note)
  812. freeNotify(note);
  813. }
  814. void SimObject::clearNotify(SimObject* obj)
  815. {
  816. Notify *note = obj->removeNotify((void *) this, Notify::DeleteNotify);
  817. if(note)
  818. freeNotify(note);
  819. note = removeNotify((void *) obj, Notify::ClearNotify);
  820. if(note)
  821. freeNotify(note);
  822. }
  823. void SimObject::processDeleteNotifies()
  824. {
  825. // clear out any delete notifies and
  826. // object refs.
  827. while(mNotifyList)
  828. {
  829. Notify *note = mNotifyList;
  830. mNotifyList = note->next;
  831. AssertFatal(note->type != Notify::ClearNotify, "Clear notes should be all gone.");
  832. if(note->type == Notify::DeleteNotify)
  833. {
  834. SimObject *obj = (SimObject *) note->ptr;
  835. Notify *cnote = obj->removeNotify((void *)this, Notify::ClearNotify);
  836. obj->onDeleteNotify(this);
  837. freeNotify(cnote);
  838. }
  839. else
  840. {
  841. // it must be an object ref - a pointer refs this object
  842. *((SimObject **) note->ptr) = NULL;
  843. }
  844. freeNotify(note);
  845. }
  846. }
  847. void SimObject::clearAllNotifications()
  848. {
  849. for(Notify **cnote = &mNotifyList; *cnote; )
  850. {
  851. Notify *temp = *cnote;
  852. if(temp->type == Notify::ClearNotify)
  853. {
  854. *cnote = temp->next;
  855. Notify *note = ((SimObject *) temp->ptr)->removeNotify((void *) this, Notify::DeleteNotify);
  856. freeNotify(temp);
  857. freeNotify(note);
  858. }
  859. else
  860. cnote = &(temp->next);
  861. }
  862. }
  863. //---------------------------------------------------------------------------
  864. void SimObject::initPersistFields()
  865. {
  866. Parent::initPersistFields();
  867. addGroup("SimBase");
  868. addField("canSaveDynamicFields", TypeBool, Offset(mCanSaveFieldDictionary, SimObject), &writeCanSaveDynamicFields, "");
  869. addField("internalName", TypeString, Offset(mInternalName, SimObject), &writeInternalName, "");
  870. addProtectedField("parentGroup", TypeSimObjectPtr, Offset(mGroup, SimObject), &setParentGroup, &defaultProtectedGetFn, &writeParentGroup, "Group hierarchy parent of the object." );
  871. endGroup("SimBase");
  872. // Namespace Linking.
  873. addGroup("Namespace Linking");
  874. addProtectedField("superclass", TypeString, Offset(mSuperClassName, SimObject), &setSuperClass, &defaultProtectedGetFn, &writeSuperclass, "Script Class of object.");
  875. addProtectedField("class", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn, &writeClass, "Script SuperClass of object.");
  876. endGroup("Namespace Linking");
  877. }
  878. //-----------------------------------------------------------------------------
  879. SimObject* SimObject::clone( const bool copyDynamicFields )
  880. {
  881. // Craete cloned object.
  882. SimObject* pCloneObject = dynamic_cast<SimObject*>( ConsoleObject::create(getClassName()) );
  883. if (!pCloneObject)
  884. {
  885. Con::errorf("SimObject::clone() - Unable to create cloned object.");
  886. return NULL;
  887. }
  888. // Register object.
  889. if ( !pCloneObject->registerObject() )
  890. {
  891. Con::warnf("SimObject::clone() - Unable to register cloned object.");
  892. delete pCloneObject;
  893. return NULL;
  894. }
  895. // Copy object.
  896. copyTo( pCloneObject );
  897. // Copy over dynamic fields if requested.
  898. if ( copyDynamicFields )
  899. pCloneObject->assignDynamicFieldsFrom( this );
  900. return pCloneObject;
  901. }
  902. //-----------------------------------------------------------------------------
  903. void SimObject::copyTo(SimObject* object)
  904. {
  905. object->mClassName = mClassName;
  906. object->mSuperClassName = mSuperClassName;
  907. object->mNameSpace = NULL;
  908. object->linkNamespaces();
  909. }
  910. //-----------------------------------------------------------------------------
  911. bool SimObject::setParentGroup(void* obj, const char* data)
  912. {
  913. SimGroup *parent = NULL;
  914. SimObject *object = static_cast<SimObject*>(obj);
  915. if(Sim::findObject(data, parent))
  916. parent->addObject(object);
  917. // always return false, because we've set mGroup when we called addObject
  918. return false;
  919. }
  920. bool SimObject::addToSet(SimObjectId spid)
  921. {
  922. if (mFlags.test(Added) == false)
  923. return false;
  924. SimObject* ptr = Sim::findObject(spid);
  925. if (ptr)
  926. {
  927. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  928. AssertFatal(sp != 0,
  929. "SimObject::addToSet: "
  930. "ObjectId does not refer to a set object");
  931. if (sp)
  932. {
  933. sp->addObject(this);
  934. return true;
  935. }
  936. }
  937. return false;
  938. }
  939. bool SimObject::addToSet(const char *ObjectName)
  940. {
  941. if (mFlags.test(Added) == false)
  942. return false;
  943. SimObject* ptr = Sim::findObject(ObjectName);
  944. if (ptr)
  945. {
  946. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  947. AssertFatal(sp != 0,
  948. "SimObject::addToSet: "
  949. "ObjectName does not refer to a set object");
  950. if (sp)
  951. {
  952. sp->addObject(this);
  953. return true;
  954. }
  955. }
  956. return false;
  957. }
  958. bool SimObject::removeFromSet(SimObjectId sid)
  959. {
  960. if (mFlags.test(Added) == false)
  961. return false;
  962. SimSet *set;
  963. if(Sim::findObject(sid, set))
  964. {
  965. set->removeObject(this);
  966. return true;
  967. }
  968. return false;
  969. }
  970. bool SimObject::removeFromSet(const char *objectName)
  971. {
  972. if (mFlags.test(Added) == false)
  973. return false;
  974. SimSet *set;
  975. if(Sim::findObject(objectName, set))
  976. {
  977. set->removeObject(this);
  978. return true;
  979. }
  980. return false;
  981. }
  982. void SimObject::inspectPreApply()
  983. {
  984. }
  985. void SimObject::inspectPostApply()
  986. {
  987. }
  988. //-----------------------------------------------------------------------------
  989. void SimObject::linkNamespaces()
  990. {
  991. // Don't link if we already have a namespace linkage in place.
  992. AssertWarn(mNameSpace == NULL, "SimObject::linkNamespaces -- Namespace linkage already in place");
  993. if (mNameSpace)
  994. return;
  995. // Start with the C++ Class namespace.
  996. StringTableEntry parent = StringTable->insert( getClassName() );
  997. // Link SuperClass to C++ Class.
  998. if ( mSuperClassName && mSuperClassName[0] )
  999. {
  1000. if ( Con::linkNamespaces(parent, mSuperClassName) )
  1001. parent = mSuperClassName;
  1002. else
  1003. mSuperClassName = NULL;
  1004. }
  1005. // Link Class to SuperClass or Class to C++ Class.
  1006. if ( mClassName && mClassName[0] )
  1007. {
  1008. if ( Con::linkNamespaces(parent, mClassName) )
  1009. parent = mClassName;
  1010. else
  1011. mClassName = NULL;
  1012. }
  1013. // Get the object's name.
  1014. StringTableEntry objectName = getName();
  1015. // Link Object Name to Class/SuperClass/C++ Class.
  1016. if ( objectName && objectName[0] )
  1017. {
  1018. if ( Con::linkNamespaces(parent, objectName) )
  1019. parent = objectName;
  1020. }
  1021. // Store our namespace.
  1022. mNameSpace = Con::lookupNamespace(parent);
  1023. }
  1024. //-----------------------------------------------------------------------------
  1025. void SimObject::unlinkNamespaces()
  1026. {
  1027. // Stop if there is no assigned namespace.
  1028. if (!mNameSpace)
  1029. return;
  1030. // Get the object's name.
  1031. StringTableEntry child = getName();
  1032. // Unlink any possible namespace combination.
  1033. if ( child && child[0] )
  1034. {
  1035. // Object Name/Class
  1036. if ( mClassName && mClassName[0] )
  1037. {
  1038. if( Con::unlinkNamespaces(mClassName, child) )
  1039. child = mClassName;
  1040. }
  1041. // Object Name/SuperClass or Class/SuperClass
  1042. if ( mSuperClassName && mSuperClassName[0] )
  1043. {
  1044. if ( Con::unlinkNamespaces(mSuperClassName, child) )
  1045. child = mSuperClassName;
  1046. }
  1047. // Object Name/C++ Class or SuperClass/C++ Class
  1048. Con::unlinkNamespaces(getClassName(), child);
  1049. }
  1050. else
  1051. {
  1052. // No Object Name, so get the Class namespace.
  1053. child = mClassName;
  1054. // Unlink any possible namespace combination.
  1055. if ( child && child[0] )
  1056. {
  1057. // Class/SuperClass
  1058. if ( mSuperClassName && mSuperClassName[0] )
  1059. {
  1060. if ( Con::unlinkNamespaces(mSuperClassName, child) )
  1061. child = mSuperClassName;
  1062. }
  1063. // Class/C++ Class or SuperClass/C++ Class
  1064. Con::unlinkNamespaces(getClassName(), child);
  1065. }
  1066. else
  1067. {
  1068. // SuperClass/C++ Class
  1069. if ( mSuperClassName && mSuperClassName[0] )
  1070. Con::unlinkNamespaces(getClassName(), mSuperClassName);
  1071. }
  1072. }
  1073. // Reset the namespace.
  1074. mNameSpace = NULL;
  1075. }
  1076. //-----------------------------------------------------------------------------
  1077. void SimObject::setClassNamespace( const char* classNamespace )
  1078. {
  1079. StringTableEntry oldClass = mClassName;
  1080. StringTableEntry newClass = StringTable->insert(classNamespace);
  1081. // Skip if no change.
  1082. if (oldClass == newClass)
  1083. return;
  1084. // Unlink the old namespaces.
  1085. if ( isProperlyAdded() )
  1086. unlinkNamespaces();
  1087. // Assign the new class namespace.
  1088. mClassName = newClass;
  1089. // Link the new namespaces.
  1090. if ( isProperlyAdded() )
  1091. linkNamespaces();
  1092. }
  1093. //-----------------------------------------------------------------------------
  1094. void SimObject::setSuperClassNamespace( const char* superClassNamespace )
  1095. {
  1096. StringTableEntry oldSuperClass = mSuperClassName;
  1097. StringTableEntry newSuperClass = StringTable->insert(superClassNamespace);
  1098. // Skip if no change.
  1099. if (oldSuperClass == newSuperClass)
  1100. return;
  1101. // Unlink the old namespaces.
  1102. if ( isProperlyAdded() )
  1103. unlinkNamespaces();
  1104. // Assign the new SuperClass namespace.
  1105. mSuperClassName = newSuperClass;
  1106. // Link the new namespaces.
  1107. if ( isProperlyAdded() )
  1108. linkNamespaces();
  1109. }
  1110. //-----------------------------------------------------------------------------
  1111. static S32 QSORT_CALLBACK compareFields(const void* a,const void* b)
  1112. {
  1113. const AbstractClassRep::Field* fa = *((const AbstractClassRep::Field**)a);
  1114. const AbstractClassRep::Field* fb = *((const AbstractClassRep::Field**)b);
  1115. return dStricmp(fa->pFieldname, fb->pFieldname);
  1116. }
  1117. void SimObject::dump()
  1118. {
  1119. const AbstractClassRep::FieldList &list = getFieldList();
  1120. char expandedBuffer[4096];
  1121. Con::printf("Static Fields:");
  1122. Vector<const AbstractClassRep::Field *> flist(__FILE__, __LINE__);
  1123. for(U32 i = 0; i < (U32)list.size(); i++)
  1124. flist.push_back(&list[i]);
  1125. dQsort(flist.address(),flist.size(),sizeof(AbstractClassRep::Field *),compareFields);
  1126. for(Vector<const AbstractClassRep::Field *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  1127. {
  1128. const AbstractClassRep::Field* f = *itr;
  1129. if( f->type == AbstractClassRep::DepricatedFieldType ||
  1130. f->type == AbstractClassRep::StartGroupFieldType ||
  1131. f->type == AbstractClassRep::EndGroupFieldType) continue;
  1132. for(U32 j = 0; S32(j) < f->elementCount; j++)
  1133. {
  1134. // [neo, 07/05/2007 - #3000]
  1135. // Some objects use dummy vars and projected fields so make sure we call the get functions
  1136. //const char *val = Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag);
  1137. 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));
  1138. if(!val /*|| !*val*/)
  1139. continue;
  1140. if(f->elementCount == 1)
  1141. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s = \"", f->pFieldname);
  1142. else
  1143. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s[%d] = \"", f->pFieldname, j);
  1144. expandEscape(expandedBuffer + dStrlen(expandedBuffer), val);
  1145. Con::printf("%s\"", expandedBuffer);
  1146. }
  1147. }
  1148. Con::printf("Dynamic Fields:");
  1149. if(getFieldDictionary())
  1150. getFieldDictionary()->printFields(this);
  1151. Con::printf("Methods:");
  1152. Namespace *ns = getNamespace();
  1153. Vector<Namespace::Entry *> vec(__FILE__, __LINE__);
  1154. if(ns)
  1155. ns->getEntryList(&vec);
  1156. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  1157. Con::printf(" %s() - %s", (*j)->mFunctionName, (*j)->mUsage ? (*j)->mUsage : "");
  1158. }