consoleObject.cpp 32 KB

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