consoleObject.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "console/consoleObject.h"
  24. #include "core/stringTable.h"
  25. #include "core/crc.h"
  26. #include "core/dataChunker.h"
  27. #include "console/console.h"
  28. #include "console/consoleInternal.h"
  29. #include "console/typeValidators.h"
  30. #include "console/simObject.h"
  31. #include "console/engineTypes.h"
  32. #include "console/engineAPI.h"
  33. IMPLEMENT_SCOPE( ConsoleAPI, Console,,
  34. "Functionality related to the legacy TorqueScript console system." );
  35. IMPLEMENT_NONINSTANTIABLE_CLASS( ConsoleObject,
  36. "Legacy console system root class. Will disappear." )
  37. END_IMPLEMENT_CLASS;
  38. AbstractClassRep * AbstractClassRep::classLinkList = NULL;
  39. AbstractClassRep::FieldList sg_tempFieldList( __FILE__, __LINE__ );
  40. U32 AbstractClassRep::NetClassCount [NetClassGroupsCount][NetClassTypesCount] = {{0, },};
  41. U32 AbstractClassRep::NetClassBitSize[NetClassGroupsCount][NetClassTypesCount] = {{0, },};
  42. AbstractClassRep ** AbstractClassRep::classTable[NetClassGroupsCount][NetClassTypesCount];
  43. U32 AbstractClassRep::classCRC[NetClassGroupsCount] = {CRC::INITIAL_CRC_VALUE, };
  44. bool AbstractClassRep::initialized = false;
  45. //-----------------------------------------------------------------------------
  46. void AbstractClassRep::init()
  47. {
  48. // Only add the renderable and selectable globals for
  49. // classes derived from SceneObject which are the only
  50. // objects for which these work.
  51. if ( isSubclassOf( "SceneObject" ) )
  52. {
  53. Con::addVariable( avar( "$%s::isRenderable", getClassName() ), TypeBool, &mIsRenderEnabled,
  54. "@brief Disables rendering of all instances of this type.\n\n" );
  55. Con::addVariable( avar( "$%s::isSelectable", getClassName() ), TypeBool, &mIsSelectionEnabled,
  56. "@brief Disables selection of all instances of this type.\n\n" );
  57. }
  58. }
  59. const AbstractClassRep::Field *AbstractClassRep::findField(StringTableEntry name) const
  60. {
  61. for(U32 i = 0; i < mFieldList.size(); i++)
  62. if(mFieldList[i].pFieldname == name)
  63. return &mFieldList[i];
  64. return NULL;
  65. }
  66. AbstractClassRep* AbstractClassRep::findClassRep(const char* in_pClassName)
  67. {
  68. AssertFatal(initialized,
  69. "AbstractClassRep::findClassRep() - Tried to find an AbstractClassRep before AbstractClassRep::initialize().");
  70. for (AbstractClassRep *walk = classLinkList; walk; walk = walk->nextClass)
  71. if (!dStricmp(walk->getClassName(), in_pClassName))
  72. return walk;
  73. return NULL;
  74. }
  75. AbstractClassRep* AbstractClassRep::findClassRep( U32 groupId, U32 typeId, U32 classId )
  76. {
  77. AssertFatal(initialized,
  78. "AbstractClassRep::findClasRep() - Tried to create an object before AbstractClassRep::initialize().");
  79. AssertFatal(classId < NetClassCount[groupId][typeId],
  80. "AbstractClassRep::findClassRep() - Class id out of range.");
  81. AssertFatal(classTable[groupId][typeId][classId] != NULL,
  82. "AbstractClassRep::findClassRep() - No class with requested ID type.");
  83. // Look up the specified class and create it.
  84. if(classTable[groupId][typeId][classId])
  85. return classTable[groupId][typeId][classId];
  86. return NULL;
  87. }
  88. //--------------------------------------
  89. void AbstractClassRep::registerClassRep(AbstractClassRep* in_pRep)
  90. {
  91. AssertFatal(in_pRep != NULL, "AbstractClassRep::registerClassRep was passed a NULL pointer!");
  92. #ifdef TORQUE_DEBUG // assert if this class is already registered.
  93. for(AbstractClassRep *walk = classLinkList; walk; walk = walk->nextClass)
  94. {
  95. AssertFatal(dStrcmp(in_pRep->mClassName, walk->mClassName),
  96. "Duplicate class name registered in AbstractClassRep::registerClassRep()");
  97. }
  98. #endif
  99. in_pRep->nextClass = classLinkList;
  100. classLinkList = in_pRep;
  101. }
  102. //--------------------------------------
  103. void AbstractClassRep::removeClassRep(AbstractClassRep* in_pRep)
  104. {
  105. for( AbstractClassRep *walk = classLinkList; walk; walk = walk->nextClass )
  106. {
  107. // This is the case that will most likely get hit.
  108. if( walk->nextClass == in_pRep )
  109. walk->nextClass = walk->nextClass->nextClass;
  110. else if( walk == in_pRep )
  111. {
  112. AssertFatal( in_pRep == classLinkList, "Pat failed in his logic for un linking RuntimeClassReps from the class linked list" );
  113. classLinkList = in_pRep->nextClass;
  114. }
  115. }
  116. }
  117. //--------------------------------------
  118. ConsoleObject* AbstractClassRep::create(const char* in_pClassName)
  119. {
  120. AssertFatal(initialized,
  121. "AbstractClassRep::create() - Tried to create an object before AbstractClassRep::initialize().");
  122. const AbstractClassRep *rep = AbstractClassRep::findClassRep(in_pClassName);
  123. if(rep)
  124. return rep->create();
  125. AssertWarn(0, avar("Couldn't find class rep for dynamic class: %s", in_pClassName));
  126. return NULL;
  127. }
  128. //--------------------------------------
  129. ConsoleObject* AbstractClassRep::create(const U32 groupId, const U32 typeId, const U32 in_classId)
  130. {
  131. AbstractClassRep* classRep = findClassRep( groupId, typeId, in_classId );
  132. if( !classRep )
  133. return NULL;
  134. return classRep->create();
  135. }
  136. //--------------------------------------
  137. static S32 QSORT_CALLBACK ACRCompare(const void *aptr, const void *bptr)
  138. {
  139. const AbstractClassRep *a = *((const AbstractClassRep **) aptr);
  140. const AbstractClassRep *b = *((const AbstractClassRep **) bptr);
  141. if(a->mClassType != b->mClassType)
  142. return a->mClassType - b->mClassType;
  143. return dStrnatcasecmp(a->getClassName(), b->getClassName());
  144. }
  145. void AbstractClassRep::initialize()
  146. {
  147. AssertFatal(!initialized, "Duplicate call to AbstractClassRep::initialize()!");
  148. Vector<AbstractClassRep *> dynamicTable(__FILE__, __LINE__);
  149. AbstractClassRep *walk;
  150. // Initialize namespace references...
  151. for (walk = classLinkList; walk; walk = walk->nextClass)
  152. {
  153. walk->mNamespace = Con::lookupNamespace(StringTable->insert(walk->getClassName()));
  154. walk->mNamespace->mUsage = walk->getDocString();
  155. walk->mNamespace->mClassRep = walk;
  156. }
  157. // Initialize field lists... (and perform other console registration).
  158. for (walk = classLinkList; walk; walk = walk->nextClass)
  159. {
  160. // sg_tempFieldList is used as a staging area for field lists
  161. // (see addField, addGroup, etc.)
  162. sg_tempFieldList.setSize(0);
  163. walk->init();
  164. // So if we have things in it, copy it over...
  165. if (sg_tempFieldList.size() != 0)
  166. walk->mFieldList = sg_tempFieldList;
  167. // And of course delete it every round.
  168. sg_tempFieldList.clear();
  169. }
  170. // Calculate counts and bit sizes for the various NetClasses.
  171. for (U32 group = 0; group < NetClassGroupsCount; group++)
  172. {
  173. U32 groupMask = 1 << group;
  174. // Specifically, for each NetClass of each NetGroup...
  175. for(U32 type = 0; type < NetClassTypesCount; type++)
  176. {
  177. // Go through all the classes and find matches...
  178. for (walk = classLinkList; walk; walk = walk->nextClass)
  179. {
  180. if(walk->mClassType == type && walk->mClassGroupMask & groupMask)
  181. dynamicTable.push_back(walk);
  182. }
  183. // Set the count for this NetGroup and NetClass
  184. NetClassCount[group][type] = dynamicTable.size();
  185. if(!NetClassCount[group][type])
  186. continue; // If no classes matched, skip to next.
  187. // Sort by type and then by name.
  188. dQsort((void *) &dynamicTable[0], dynamicTable.size(), sizeof(AbstractClassRep *), ACRCompare);
  189. // Allocate storage in the classTable
  190. classTable[group][type] = new AbstractClassRep*[NetClassCount[group][type]];
  191. // Fill this in and assign class ids for this group.
  192. for(U32 i = 0; i < NetClassCount[group][type];i++)
  193. {
  194. classTable[group][type][i] = dynamicTable[i];
  195. dynamicTable[i]->mClassId[group] = i;
  196. }
  197. // And calculate the size of bitfields for this group and type.
  198. NetClassBitSize[group][type] =
  199. getBinLog2(getNextPow2(NetClassCount[group][type] + 1));
  200. AssertFatal(NetClassCount[group][type] < (1 << NetClassBitSize[group][type]), "NetClassBitSize too small!");
  201. dynamicTable.clear();
  202. }
  203. }
  204. // Ok, we're golden!
  205. initialized = true;
  206. }
  207. void AbstractClassRep::shutdown()
  208. {
  209. AssertFatal( initialized, "AbstractClassRep::shutdown - not initialized" );
  210. // Release storage allocated to the class table.
  211. for (U32 group = 0; group < NetClassGroupsCount; group++)
  212. for(U32 type = 0; type < NetClassTypesCount; type++)
  213. if( classTable[ group ][ type ] )
  214. SAFE_DELETE_ARRAY( classTable[ group ][ type ] );
  215. initialized = false;
  216. }
  217. AbstractClassRep *AbstractClassRep::getCommonParent( const AbstractClassRep *otherClass ) const
  218. {
  219. // CodeReview: This may be a noob way of doing it. There may be some kind of
  220. // super-spiffy algorithm to do what the code below does, but this appeared
  221. // to make sense to me, and it is pretty easy to see what it is doing [6/23/2007 Pat]
  222. static VectorPtr<AbstractClassRep *> thisClassHeirarchy;
  223. thisClassHeirarchy.clear();
  224. AbstractClassRep *walk = const_cast<AbstractClassRep *>( this );
  225. while( walk != NULL )
  226. {
  227. thisClassHeirarchy.push_front( walk );
  228. walk = walk->getParentClass();
  229. }
  230. static VectorPtr<AbstractClassRep *> compClassHeirarchy;
  231. compClassHeirarchy.clear();
  232. walk = const_cast<AbstractClassRep *>( otherClass );
  233. while( walk != NULL )
  234. {
  235. compClassHeirarchy.push_front( walk );
  236. walk = walk->getParentClass();
  237. }
  238. // Make sure we only iterate over the list the number of times we can
  239. S32 maxIterations = getMin( compClassHeirarchy.size(), thisClassHeirarchy.size() );
  240. U32 i = 0;
  241. for( ; i < maxIterations; i++ )
  242. {
  243. if( compClassHeirarchy[i] != thisClassHeirarchy[i] )
  244. break;
  245. }
  246. return compClassHeirarchy[i];
  247. }
  248. //------------------------------------------------------------------------------
  249. //-------------------------------------- ConsoleObject
  250. static char replacebuf[1024];
  251. static char* suppressSpaces(const char* in_pname)
  252. {
  253. U32 i = 0;
  254. char chr;
  255. do
  256. {
  257. chr = in_pname[i];
  258. replacebuf[i++] = (chr != 32) ? chr : '_';
  259. } while(chr);
  260. return replacebuf;
  261. }
  262. void ConsoleObject::addGroup(const char* in_pGroupname, const char* in_pGroupDocs)
  263. {
  264. // Remove spaces.
  265. char* pFieldNameBuf = suppressSpaces(in_pGroupname);
  266. // Append group type to fieldname.
  267. dStrcat(pFieldNameBuf, "_begingroup");
  268. // Create Field.
  269. AbstractClassRep::Field f;
  270. f.pFieldname = StringTable->insert(pFieldNameBuf);
  271. f.pGroupname = in_pGroupname;
  272. if(in_pGroupDocs)
  273. f.pFieldDocs = in_pGroupDocs;
  274. f.type = AbstractClassRep::StartGroupFieldType;
  275. f.elementCount = 0;
  276. f.groupExpand = false;
  277. f.validator = NULL;
  278. f.setDataFn = &defaultProtectedSetFn;
  279. f.getDataFn = &defaultProtectedGetFn;
  280. // Add to field list.
  281. sg_tempFieldList.push_back(f);
  282. }
  283. void ConsoleObject::endGroup(const char* in_pGroupname)
  284. {
  285. // Remove spaces.
  286. char* pFieldNameBuf = suppressSpaces(in_pGroupname);
  287. // Append group type to fieldname.
  288. dStrcat(pFieldNameBuf, "_endgroup");
  289. // Create Field.
  290. AbstractClassRep::Field f;
  291. f.pFieldname = StringTable->insert(pFieldNameBuf);
  292. f.pGroupname = in_pGroupname;
  293. f.type = AbstractClassRep::EndGroupFieldType;
  294. f.groupExpand = false;
  295. f.validator = NULL;
  296. f.setDataFn = &defaultProtectedSetFn;
  297. f.getDataFn = &defaultProtectedGetFn;
  298. f.elementCount = 0;
  299. // Add to field list.
  300. sg_tempFieldList.push_back(f);
  301. }
  302. void ConsoleObject::addArray( const char *arrayName, S32 count )
  303. {
  304. char *nameBuff = suppressSpaces(arrayName);
  305. dStrcat(nameBuff, "_beginarray");
  306. // Create Field.
  307. AbstractClassRep::Field f;
  308. f.pFieldname = StringTable->insert(nameBuff);
  309. f.pGroupname = arrayName;
  310. f.type = AbstractClassRep::StartArrayFieldType;
  311. f.elementCount = count;
  312. f.groupExpand = false;
  313. f.validator = NULL;
  314. f.setDataFn = &defaultProtectedSetFn;
  315. f.getDataFn = &defaultProtectedGetFn;
  316. // Add to field list.
  317. sg_tempFieldList.push_back(f);
  318. }
  319. void ConsoleObject::endArray( const char *arrayName )
  320. {
  321. char *nameBuff = suppressSpaces(arrayName);
  322. dStrcat(nameBuff, "_endarray");
  323. // Create Field.
  324. AbstractClassRep::Field f;
  325. f.pFieldname = StringTable->insert(nameBuff);
  326. f.pGroupname = arrayName;
  327. f.type = AbstractClassRep::EndArrayFieldType;
  328. f.groupExpand = false;
  329. f.validator = NULL;
  330. f.setDataFn = &defaultProtectedSetFn;
  331. f.getDataFn = &defaultProtectedGetFn;
  332. f.elementCount = 0;
  333. // Add to field list.
  334. sg_tempFieldList.push_back(f);
  335. }
  336. void ConsoleObject::addField(const char* in_pFieldname,
  337. const U32 in_fieldType,
  338. const dsize_t in_fieldOffset,
  339. const char* in_pFieldDocs,
  340. U32 flags )
  341. {
  342. addField(
  343. in_pFieldname,
  344. in_fieldType,
  345. in_fieldOffset,
  346. 1,
  347. in_pFieldDocs,
  348. flags );
  349. }
  350. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  351. const U32 in_fieldType,
  352. const dsize_t in_fieldOffset,
  353. AbstractClassRep::SetDataNotify in_setDataFn,
  354. AbstractClassRep::GetDataNotify in_getDataFn,
  355. const char* in_pFieldDocs,
  356. U32 flags )
  357. {
  358. addProtectedField(
  359. in_pFieldname,
  360. in_fieldType,
  361. in_fieldOffset,
  362. in_setDataFn,
  363. in_getDataFn,
  364. 1,
  365. in_pFieldDocs,
  366. flags );
  367. }
  368. void ConsoleObject::addField(const char* in_pFieldname,
  369. const U32 in_fieldType,
  370. const dsize_t in_fieldOffset,
  371. const U32 in_elementCount,
  372. const char* in_pFieldDocs,
  373. U32 flags )
  374. {
  375. AbstractClassRep::Field f;
  376. f.pFieldname = StringTable->insert(in_pFieldname);
  377. if(in_pFieldDocs)
  378. f.pFieldDocs = in_pFieldDocs;
  379. f.type = in_fieldType;
  380. f.offset = in_fieldOffset;
  381. f.elementCount = in_elementCount;
  382. f.validator = NULL;
  383. f.flag = flags;
  384. f.setDataFn = &defaultProtectedSetFn;
  385. f.getDataFn = &defaultProtectedGetFn;
  386. ConsoleBaseType* conType = ConsoleBaseType::getType( in_fieldType );
  387. AssertFatal( conType, "ConsoleObject::addField - invalid console type" );
  388. f.table = conType->getEnumTable();
  389. sg_tempFieldList.push_back(f);
  390. }
  391. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  392. const U32 in_fieldType,
  393. const dsize_t in_fieldOffset,
  394. AbstractClassRep::SetDataNotify in_setDataFn,
  395. AbstractClassRep::GetDataNotify in_getDataFn,
  396. const U32 in_elementCount,
  397. const char* in_pFieldDocs,
  398. U32 flags )
  399. {
  400. AbstractClassRep::Field f;
  401. f.pFieldname = StringTable->insert(in_pFieldname);
  402. if(in_pFieldDocs)
  403. f.pFieldDocs = in_pFieldDocs;
  404. f.type = in_fieldType;
  405. f.offset = in_fieldOffset;
  406. f.elementCount = in_elementCount;
  407. f.validator = NULL;
  408. f.flag = flags;
  409. f.setDataFn = in_setDataFn;
  410. f.getDataFn = in_getDataFn;
  411. ConsoleBaseType* conType = ConsoleBaseType::getType( in_fieldType );
  412. AssertFatal( conType, "ConsoleObject::addProtectedField - invalid console type" );
  413. f.table = conType->getEnumTable();
  414. sg_tempFieldList.push_back(f);
  415. }
  416. void ConsoleObject::addFieldV(const char* in_pFieldname,
  417. const U32 in_fieldType,
  418. const dsize_t in_fieldOffset,
  419. TypeValidator *v,
  420. const char* in_pFieldDocs)
  421. {
  422. AbstractClassRep::Field f;
  423. f.pFieldname = StringTable->insert(in_pFieldname);
  424. if(in_pFieldDocs)
  425. f.pFieldDocs = in_pFieldDocs;
  426. f.type = in_fieldType;
  427. f.offset = in_fieldOffset;
  428. f.elementCount = 1;
  429. f.table = NULL;
  430. f.setDataFn = &defaultProtectedSetFn;
  431. f.getDataFn = &defaultProtectedGetFn;
  432. f.validator = v;
  433. v->fieldIndex = sg_tempFieldList.size();
  434. sg_tempFieldList.push_back(f);
  435. }
  436. void ConsoleObject::addDeprecatedField(const char *fieldName)
  437. {
  438. AbstractClassRep::Field f;
  439. f.pFieldname = StringTable->insert(fieldName);
  440. f.type = AbstractClassRep::DeprecatedFieldType;
  441. f.offset = 0;
  442. f.elementCount = 0;
  443. f.table = NULL;
  444. f.validator = NULL;
  445. f.setDataFn = &defaultProtectedSetFn;
  446. f.getDataFn = &defaultProtectedGetFn;
  447. sg_tempFieldList.push_back(f);
  448. }
  449. bool ConsoleObject::removeField(const char* in_pFieldname)
  450. {
  451. for (U32 i = 0; i < sg_tempFieldList.size(); i++) {
  452. if (dStricmp(in_pFieldname, sg_tempFieldList[i].pFieldname) == 0) {
  453. sg_tempFieldList.erase(i);
  454. return true;
  455. }
  456. }
  457. return false;
  458. }
  459. //--------------------------------------
  460. void ConsoleObject::initPersistFields()
  461. {
  462. }
  463. //--------------------------------------
  464. void ConsoleObject::consoleInit()
  465. {
  466. }
  467. //--------------------------------------
  468. AbstractClassRep* ConsoleObject::getClassRep() const
  469. {
  470. return NULL;
  471. }
  472. String ConsoleObject::_getLogMessage(const char* fmt, va_list args) const
  473. {
  474. String objClass = "UnknownClass";
  475. if(getClassRep())
  476. objClass = getClassRep()->getClassName();
  477. String formattedMessage = String::VToString(fmt, args);
  478. return String::ToString("%s - Object at %x - %s",
  479. objClass.c_str(), this, formattedMessage.c_str());
  480. }
  481. void ConsoleObject::logMessage(const char* fmt, ...) const
  482. {
  483. va_list args;
  484. va_start(args, fmt);
  485. Con::printf(_getLogMessage(fmt, args));
  486. va_end(args);
  487. }
  488. void ConsoleObject::logWarning(const char* fmt, ...) const
  489. {
  490. va_list args;
  491. va_start(args, fmt);
  492. Con::warnf(_getLogMessage(fmt, args));
  493. va_end(args);
  494. }
  495. void ConsoleObject::logError(const char* fmt, ...) const
  496. {
  497. va_list args;
  498. va_start(args, fmt);
  499. Con::errorf(_getLogMessage(fmt, args));
  500. va_end(args);
  501. }
  502. //------------------------------------------------------------------------------
  503. static const char* returnClassList( Vector< AbstractClassRep* >& classes, U32 bufSize )
  504. {
  505. if( !classes.size() )
  506. return "";
  507. dQsort( classes.address(), classes.size(), sizeof( AbstractClassRep* ), ACRCompare );
  508. char* ret = Con::getReturnBuffer( bufSize );
  509. dStrcpy( ret, classes[ 0 ]->getClassName() );
  510. for( U32 i = 1; i < classes.size(); i ++ )
  511. {
  512. dStrcat( ret, "\t" );
  513. dStrcat( ret, classes[ i ]->getClassName() );
  514. }
  515. return ret;
  516. }
  517. //------------------------------------------------------------------------------
  518. DefineEngineFunction( isClass, bool, ( const char* identifier ),,
  519. "@brief Returns true if the passed identifier is the name of a declared class.\n\n"
  520. "@ingroup Console")
  521. {
  522. AbstractClassRep* rep = AbstractClassRep::findClassRep( identifier );
  523. return rep != NULL;
  524. }
  525. DefineEngineFunction( isMemberOfClass, bool, ( const char* className, const char* superClassName ),,
  526. "@brief Returns true if the class is derived from the super class.\n\n"
  527. "If either class doesn't exist this returns false.\n"
  528. "@param className The class name.\n"
  529. "@param superClassName The super class to look for.\n"
  530. "@ingroup Console")
  531. {
  532. AbstractClassRep *pRep = AbstractClassRep::findClassRep( className );
  533. while (pRep)
  534. {
  535. if( !dStricmp( pRep->getClassName(), superClassName ) )
  536. return true;
  537. pRep = pRep->getParentClass();
  538. }
  539. return false;
  540. }
  541. DefineEngineFunction( getDescriptionOfClass, const char*, ( const char* className ),,
  542. "@brief Returns the description string for the named class.\n\n"
  543. "@param className The name of the class.\n"
  544. "@return The class description in string format.\n"
  545. "@ingroup Console")
  546. {
  547. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  548. if( rep )
  549. return rep->getDescription();
  550. Con::errorf( "getDescriptionOfClass - no class called '%s'", className );
  551. return "";
  552. }
  553. DefineEngineFunction( getCategoryOfClass, const char*, ( const char* className ),,
  554. "@brief Returns the category of the given class.\n\n"
  555. "@param className The name of the class.\n"
  556. "@ingroup Console")
  557. {
  558. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  559. if( rep )
  560. return rep->getCategory();
  561. Con::errorf( "getCategoryOfClass - no class called '%s'", className );
  562. return "";
  563. }
  564. DefineEngineFunction( enumerateConsoleClasses, const char*, ( const char* className ), ( "" ),
  565. "@brief Returns a list of classes that derive from the named class.\n\n"
  566. "If the named class is omitted this dumps all the classes.\n"
  567. "@param className The optional base class name.\n"
  568. "@return A tab delimited list of classes.\n"
  569. "@ingroup Editors\n"
  570. "@internal")
  571. {
  572. AbstractClassRep *base = NULL;
  573. if(className && *className)
  574. {
  575. base = AbstractClassRep::findClassRep(className);
  576. if(!base)
  577. return "";
  578. }
  579. Vector<AbstractClassRep*> classes;
  580. U32 bufSize = 0;
  581. for(AbstractClassRep *rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  582. {
  583. if( !base || rep->isClass(base))
  584. {
  585. classes.push_back(rep);
  586. bufSize += dStrlen(rep->getClassName()) + 1;
  587. }
  588. }
  589. return returnClassList( classes, bufSize );
  590. }
  591. DefineEngineFunction( enumerateConsoleClassesByCategory, const char*, ( String category ),,
  592. "@brief Provide a list of classes that belong to the given category.\n\n"
  593. "@param category The category name.\n"
  594. "@return A tab delimited list of classes.\n"
  595. "@ingroup Editors\n"
  596. "@internal")
  597. {
  598. U32 categoryLength = category.length();
  599. U32 bufSize = 0;
  600. Vector< AbstractClassRep* > classes;
  601. for( AbstractClassRep* rep = AbstractClassRep::getClassList(); rep != NULL; rep = rep->getNextClass() )
  602. {
  603. const String& repCategory = rep->getCategory();
  604. if( repCategory.length() >= categoryLength
  605. && ( repCategory.compare( category, categoryLength, String::NoCase ) == 0 )
  606. && ( repCategory[ categoryLength ] == ' ' || repCategory[ categoryLength ] == '\0' ) )
  607. {
  608. classes.push_back( rep );
  609. bufSize += dStrlen( rep->getClassName() + 1 );
  610. }
  611. }
  612. return returnClassList( classes, bufSize );
  613. }
  614. DefineEngineFunction( dumpNetStats, void, (),,
  615. "@brief Dumps network statistics for each class to the console.\n\n"
  616. "The returned <i>avg</i>, <i>min</i> and <i>max</i> values are in bits sent per update. "
  617. "The <i>num</i> value is the total number of events collected.\n"
  618. "@note This method only works when TORQUE_NET_STATS is defined in torqueConfig.h.\n"
  619. "@ingroup Networking\n" )
  620. {
  621. #ifdef TORQUE_NET_STATS
  622. for (AbstractClassRep * rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  623. {
  624. if (rep->mNetStatPack.numEvents || rep->mNetStatUnpack.numEvents || rep->mNetStatWrite.numEvents || rep->mNetStatRead.numEvents)
  625. {
  626. Con::printf("class %s net info",rep->getClassName());
  627. if (rep->mNetStatPack.numEvents)
  628. Con::printf(" packUpdate: avg (%f), min (%i), max (%i), num (%i)",
  629. F32(rep->mNetStatPack.total)/F32(rep->mNetStatPack.numEvents),
  630. rep->mNetStatPack.min,
  631. rep->mNetStatPack.max,
  632. rep->mNetStatPack.numEvents);
  633. if (rep->mNetStatUnpack.numEvents)
  634. Con::printf(" unpackUpdate: avg (%f), min (%i), max (%i), num (%i)",
  635. F32(rep->mNetStatUnpack.total)/F32(rep->mNetStatUnpack.numEvents),
  636. rep->mNetStatUnpack.min,
  637. rep->mNetStatUnpack.max,
  638. rep->mNetStatUnpack.numEvents);
  639. if (rep->mNetStatWrite.numEvents)
  640. Con::printf(" write: avg (%f), min (%i), max (%i), num (%i)",
  641. F32(rep->mNetStatWrite.total)/F32(rep->mNetStatWrite.numEvents),
  642. rep->mNetStatWrite.min,
  643. rep->mNetStatWrite.max,
  644. rep->mNetStatWrite.numEvents);
  645. if (rep->mNetStatRead.numEvents)
  646. Con::printf(" read: avg (%f), min (%i), max (%i), num (%i)",
  647. F32(rep->mNetStatRead.total)/F32(rep->mNetStatRead.numEvents),
  648. rep->mNetStatRead.min,
  649. rep->mNetStatRead.max,
  650. rep->mNetStatRead.numEvents);
  651. S32 sum = 0;
  652. for (S32 i=0; i<32; i++)
  653. sum += rep->mDirtyMaskFrequency[i];
  654. if (sum)
  655. {
  656. Con::printf(" Mask bits:");
  657. for (S32 i=0; i<8; i++)
  658. {
  659. F32 avg0 = rep->mDirtyMaskFrequency[i] ? F32(rep->mDirtyMaskTotal[i])/F32(rep->mDirtyMaskFrequency[i]) : 0.0f;
  660. F32 avg8 = rep->mDirtyMaskFrequency[i+8] ? F32(rep->mDirtyMaskTotal[i+8])/F32(rep->mDirtyMaskFrequency[i+8]) : 0.0f;
  661. F32 avg16 = rep->mDirtyMaskFrequency[i+16] ? F32(rep->mDirtyMaskTotal[i+16])/F32(rep->mDirtyMaskFrequency[i+16]) : 0.0f;
  662. F32 avg24 = rep->mDirtyMaskFrequency[i+24] ? F32(rep->mDirtyMaskTotal[i+24])/F32(rep->mDirtyMaskFrequency[i+24]) : 0.0f;
  663. Con::printf(" %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i, (%6.2f)",
  664. i ,rep->mDirtyMaskFrequency[i],avg0,
  665. i+8 ,rep->mDirtyMaskFrequency[i+8],avg8,
  666. i+16,rep->mDirtyMaskFrequency[i+16],avg16,
  667. i+24,rep->mDirtyMaskFrequency[i+24],avg24);
  668. }
  669. }
  670. }
  671. rep->resetNetStats();
  672. }
  673. #endif
  674. }
  675. DefineEngineFunction( sizeof, S32, ( const char *objectOrClass ),,
  676. "@brief Determines the memory consumption of a class or object.\n\n"
  677. "@param objectOrClass The object or class being measured.\n"
  678. "@return Returns the total size of an object in bytes.\n"
  679. "@ingroup Debugging\n")
  680. {
  681. AbstractClassRep *acr = NULL;
  682. SimObject *obj = Sim::findObject(objectOrClass);
  683. if(obj)
  684. acr = obj->getClassRep();
  685. if(!acr)
  686. acr = AbstractClassRep::findClassRep(objectOrClass);
  687. if(acr)
  688. return acr->getSizeof();
  689. if(dStricmp("ConsoleObject", objectOrClass) == 0)
  690. return sizeof(ConsoleObject);
  691. Con::warnf("could not find a class rep for that object or class name.");
  692. return 0;
  693. }
  694. DefineEngineFunction(linkNamespaces, bool, ( String childNSName, String parentNSName ),,
  695. "@brief Links childNS to parentNS.\n\n"
  696. "Links childNS to parentNS, or nothing if parentNS is NULL.\n"
  697. "Will unlink the namespace from previous namespace if a parent already exists.\n"
  698. "@internal\n")
  699. {
  700. StringTableEntry childNSSTE = StringTable->insert(childNSName.c_str());
  701. StringTableEntry parentNSSTE = StringTable->insert(parentNSName.c_str());
  702. Namespace *childNS = Namespace::find(childNSSTE);
  703. Namespace *parentNS = Namespace::find(parentNSSTE);
  704. if (!childNS)
  705. {
  706. return false;
  707. }
  708. Namespace *currentParent = childNS->getParent();
  709. // Link to new NS if applicable
  710. if (currentParent != parentNS)
  711. {
  712. if (currentParent != NULL)
  713. {
  714. if (!childNS->unlinkClass(currentParent))
  715. {
  716. return false;
  717. }
  718. }
  719. if (parentNS != NULL)
  720. {
  721. return childNS->classLinkTo(parentNS);
  722. }
  723. }
  724. return true;
  725. }