simObject.cc 46 KB

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