consoleObject.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. docsURL;
  574. }
  575. //--------------------------------------
  576. void ConsoleObject::consoleInit()
  577. {
  578. }
  579. //--------------------------------------
  580. AbstractClassRep* ConsoleObject::getClassRep() const
  581. {
  582. return NULL;
  583. }
  584. bool ConsoleObject::disableFieldSubstitutions(const char* fieldname)
  585. {
  586. StringTableEntry slotname = StringTable->insert(fieldname);
  587. for (U32 i = 0; i < sg_tempFieldList.size(); i++)
  588. {
  589. if (sg_tempFieldList[i].pFieldname == slotname)
  590. {
  591. sg_tempFieldList[i].doNotSubstitute = true;
  592. sg_tempFieldList[i].keepClearSubsOnly = false;
  593. return true;
  594. }
  595. }
  596. return false;
  597. }
  598. bool ConsoleObject::onlyKeepClearSubstitutions(const char* fieldname)
  599. {
  600. StringTableEntry slotname = StringTable->insert(fieldname);
  601. for (U32 i = 0; i < sg_tempFieldList.size(); i++)
  602. {
  603. if (sg_tempFieldList[i].pFieldname == slotname)
  604. {
  605. sg_tempFieldList[i].doNotSubstitute = false;
  606. sg_tempFieldList[i].keepClearSubsOnly = true;
  607. return true;
  608. }
  609. }
  610. return false;
  611. }
  612. String ConsoleObject::_getLogMessage(const char* fmt, va_list args) const
  613. {
  614. String objClass = "UnknownClass";
  615. if(getClassRep())
  616. objClass = getClassRep()->getClassName();
  617. String formattedMessage = String::VToString(fmt, args);
  618. return String::ToString("%s - Object at %x - %s",
  619. objClass.c_str(), this, formattedMessage.c_str());
  620. }
  621. void ConsoleObject::logMessage(const char* fmt, ...) const
  622. {
  623. va_list args;
  624. va_start(args, fmt);
  625. Con::printf(_getLogMessage(fmt, args));
  626. va_end(args);
  627. }
  628. void ConsoleObject::logWarning(const char* fmt, ...) const
  629. {
  630. va_list args;
  631. va_start(args, fmt);
  632. Con::warnf(_getLogMessage(fmt, args));
  633. va_end(args);
  634. }
  635. void ConsoleObject::logError(const char* fmt, ...) const
  636. {
  637. va_list args;
  638. va_start(args, fmt);
  639. Con::errorf(_getLogMessage(fmt, args));
  640. va_end(args);
  641. }
  642. //------------------------------------------------------------------------------
  643. static const char* returnClassList( Vector< AbstractClassRep* >& classes, U32 bufSize )
  644. {
  645. if( !classes.size() )
  646. return "";
  647. dQsort( classes.address(), classes.size(), sizeof( AbstractClassRep* ), ACRCompare );
  648. char* ret = Con::getReturnBuffer( bufSize );
  649. dStrcpy( ret, classes[ 0 ]->getClassName(), bufSize );
  650. for( U32 i = 1; i < classes.size(); i ++ )
  651. {
  652. dStrcat( ret, "\t", bufSize );
  653. dStrcat( ret, classes[ i ]->getClassName(), bufSize );
  654. }
  655. return ret;
  656. }
  657. //------------------------------------------------------------------------------
  658. DefineEngineFunction( isClass, bool, ( const char* identifier ),,
  659. "@brief Returns true if the passed identifier is the name of a declared class.\n\n"
  660. "@ingroup Console")
  661. {
  662. AbstractClassRep* rep = AbstractClassRep::findClassRep( identifier );
  663. return rep != NULL;
  664. }
  665. DefineEngineFunction( isMemberOfClass, bool, ( const char* className, const char* superClassName ),,
  666. "@brief Returns true if the class is derived from the super class.\n\n"
  667. "If either class doesn't exist this returns false.\n"
  668. "@param className The class name.\n"
  669. "@param superClassName The super class to look for.\n"
  670. "@ingroup Console")
  671. {
  672. AbstractClassRep *pRep = AbstractClassRep::findClassRep( className );
  673. while (pRep)
  674. {
  675. if( !dStricmp( pRep->getClassName(), superClassName ) )
  676. return true;
  677. pRep = pRep->getParentClass();
  678. }
  679. return false;
  680. }
  681. DefineEngineFunction( getDescriptionOfClass, const char*, ( const char* className ),,
  682. "@brief Returns the description string for the named class.\n\n"
  683. "@param className The name of the class.\n"
  684. "@return The class description in string format.\n"
  685. "@ingroup Console")
  686. {
  687. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  688. if( rep )
  689. return rep->getDescription();
  690. Con::errorf( "getDescriptionOfClass - no class called '%s'", className );
  691. return "";
  692. }
  693. DefineEngineFunction( getCategoryOfClass, const char*, ( const char* className ),,
  694. "@brief Returns the category of the given class.\n\n"
  695. "@param className The name of the class.\n"
  696. "@ingroup Console")
  697. {
  698. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  699. if( rep )
  700. return rep->getCategory();
  701. Con::errorf( "getCategoryOfClass - no class called '%s'", className );
  702. return "";
  703. }
  704. DefineEngineFunction( enumerateConsoleClasses, const char*, ( const char* className ), ( "" ),
  705. "@brief Returns a list of classes that derive from the named class.\n\n"
  706. "If the named class is omitted this dumps all the classes.\n"
  707. "@param className The optional base class name.\n"
  708. "@return A tab delimited list of classes.\n"
  709. "@ingroup Editors\n"
  710. "@internal")
  711. {
  712. AbstractClassRep *base = NULL;
  713. if(className && *className)
  714. {
  715. base = AbstractClassRep::findClassRep(className);
  716. if(!base)
  717. return "";
  718. }
  719. Vector<AbstractClassRep*> classes;
  720. U32 bufSize = 0;
  721. for(AbstractClassRep *rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  722. {
  723. if( !base || rep->isClass(base))
  724. {
  725. classes.push_back(rep);
  726. bufSize += dStrlen(rep->getClassName()) + 1;
  727. }
  728. }
  729. return returnClassList( classes, bufSize );
  730. }
  731. DefineEngineFunction( enumerateConsoleClassesByCategory, const char*, ( String category ),,
  732. "@brief Provide a list of classes that belong to the given category.\n\n"
  733. "@param category The category name.\n"
  734. "@return A tab delimited list of classes.\n"
  735. "@ingroup Editors\n"
  736. "@internal")
  737. {
  738. U32 categoryLength = category.length();
  739. U32 bufSize = 0;
  740. Vector< AbstractClassRep* > classes;
  741. for( AbstractClassRep* rep = AbstractClassRep::getClassList(); rep != NULL; rep = rep->getNextClass() )
  742. {
  743. const String& repCategory = rep->getCategory();
  744. if( repCategory.length() >= categoryLength
  745. && ( repCategory.compare( category, categoryLength, String::NoCase ) == 0 )
  746. && ( repCategory[ categoryLength ] == ' ' || repCategory[ categoryLength ] == '\0' ) )
  747. {
  748. classes.push_back( rep );
  749. bufSize += dStrlen( rep->getClassName() ) + 1;
  750. }
  751. }
  752. return returnClassList( classes, bufSize );
  753. }
  754. DefineEngineFunction( dumpNetStats, void, (),,
  755. "@brief Dumps network statistics for each class to the console.\n\n"
  756. "The returned <i>avg</i>, <i>min</i> and <i>max</i> values are in bits sent per update. "
  757. "The <i>num</i> value is the total number of events collected.\n"
  758. "@note This method only works when TORQUE_NET_STATS is defined in torqueConfig.h.\n"
  759. "@ingroup Networking\n" )
  760. {
  761. #ifdef TORQUE_NET_STATS
  762. for (AbstractClassRep * rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  763. {
  764. if (rep->mNetStatPack.numEvents || rep->mNetStatUnpack.numEvents || rep->mNetStatWrite.numEvents || rep->mNetStatRead.numEvents)
  765. {
  766. Con::printf("class %s net info",rep->getClassName());
  767. if (rep->mNetStatPack.numEvents)
  768. Con::printf(" packUpdate: avg (%f), min (%i), max (%i), num (%i)",
  769. F32(rep->mNetStatPack.total)/F32(rep->mNetStatPack.numEvents),
  770. rep->mNetStatPack.min,
  771. rep->mNetStatPack.max,
  772. rep->mNetStatPack.numEvents);
  773. if (rep->mNetStatUnpack.numEvents)
  774. Con::printf(" unpackUpdate: avg (%f), min (%i), max (%i), num (%i)",
  775. F32(rep->mNetStatUnpack.total)/F32(rep->mNetStatUnpack.numEvents),
  776. rep->mNetStatUnpack.min,
  777. rep->mNetStatUnpack.max,
  778. rep->mNetStatUnpack.numEvents);
  779. if (rep->mNetStatWrite.numEvents)
  780. Con::printf(" write: avg (%f), min (%i), max (%i), num (%i)",
  781. F32(rep->mNetStatWrite.total)/F32(rep->mNetStatWrite.numEvents),
  782. rep->mNetStatWrite.min,
  783. rep->mNetStatWrite.max,
  784. rep->mNetStatWrite.numEvents);
  785. if (rep->mNetStatRead.numEvents)
  786. Con::printf(" read: avg (%f), min (%i), max (%i), num (%i)",
  787. F32(rep->mNetStatRead.total)/F32(rep->mNetStatRead.numEvents),
  788. rep->mNetStatRead.min,
  789. rep->mNetStatRead.max,
  790. rep->mNetStatRead.numEvents);
  791. S32 sum = 0;
  792. for (S32 i=0; i<32; i++)
  793. sum += rep->mDirtyMaskFrequency[i];
  794. if (sum)
  795. {
  796. Con::printf(" Mask bits:");
  797. for (S32 i=0; i<8; i++)
  798. {
  799. F32 avg0 = rep->mDirtyMaskFrequency[i] ? F32(rep->mDirtyMaskTotal[i])/F32(rep->mDirtyMaskFrequency[i]) : 0.0f;
  800. F32 avg8 = rep->mDirtyMaskFrequency[i+8] ? F32(rep->mDirtyMaskTotal[i+8])/F32(rep->mDirtyMaskFrequency[i+8]) : 0.0f;
  801. F32 avg16 = rep->mDirtyMaskFrequency[i+16] ? F32(rep->mDirtyMaskTotal[i+16])/F32(rep->mDirtyMaskFrequency[i+16]) : 0.0f;
  802. F32 avg24 = rep->mDirtyMaskFrequency[i+24] ? F32(rep->mDirtyMaskTotal[i+24])/F32(rep->mDirtyMaskFrequency[i+24]) : 0.0f;
  803. Con::printf(" %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i, (%6.2f)",
  804. i ,rep->mDirtyMaskFrequency[i],avg0,
  805. i+8 ,rep->mDirtyMaskFrequency[i+8],avg8,
  806. i+16,rep->mDirtyMaskFrequency[i+16],avg16,
  807. i+24,rep->mDirtyMaskFrequency[i+24],avg24);
  808. }
  809. }
  810. }
  811. rep->resetNetStats();
  812. }
  813. #endif
  814. }
  815. DefineEngineFunction( sizeof, S32, ( const char *objectOrClass ),,
  816. "@brief Determines the memory consumption of a class or object.\n\n"
  817. "@param objectOrClass The object or class being measured.\n"
  818. "@return Returns the total size of an object in bytes.\n"
  819. "@ingroup Debugging\n")
  820. {
  821. AbstractClassRep *acr = NULL;
  822. SimObject *obj = Sim::findObject(objectOrClass);
  823. if(obj)
  824. acr = obj->getClassRep();
  825. if(!acr)
  826. acr = AbstractClassRep::findClassRep(objectOrClass);
  827. if(acr)
  828. return acr->getSizeof();
  829. if(dStricmp("ConsoleObject", objectOrClass) == 0)
  830. return sizeof(ConsoleObject);
  831. Con::warnf("could not find a class rep for that object or class name.");
  832. return 0;
  833. }
  834. DefineEngineFunction(linkNamespaces, bool, ( String childNSName, String parentNSName ),,
  835. "@brief Links childNS to parentNS.\n\n"
  836. "Links childNS to parentNS, or nothing if parentNS is NULL.\n"
  837. "Will unlink the namespace from previous namespace if a parent already exists.\n"
  838. "@internal\n")
  839. {
  840. StringTableEntry childNSSTE = StringTable->insert(childNSName.c_str());
  841. StringTableEntry parentNSSTE = StringTable->insert(parentNSName.c_str());
  842. Namespace *childNS = Namespace::find(childNSSTE);
  843. Namespace *parentNS = Namespace::find(parentNSSTE);
  844. if (!childNS)
  845. {
  846. return false;
  847. }
  848. Namespace *currentParent = childNS->getParent();
  849. // Link to new NS if applicable
  850. if (currentParent != parentNS)
  851. {
  852. if (currentParent != NULL)
  853. {
  854. if (!childNS->unlinkClass(currentParent))
  855. {
  856. return false;
  857. }
  858. }
  859. if (parentNS != NULL)
  860. {
  861. return childNS->classLinkTo(parentNS);
  862. }
  863. }
  864. return true;
  865. }