consoleObject.cpp 33 KB

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