consoleObject.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 (auto walk = classLinkList; walk; walk = walk->nextClass)
  232. {
  233. walk->mFieldList.clear();
  234. walk->mFieldList.compact(); // Important: frees the internal buffer
  235. }
  236. for (U32 group = 0; group < NetClassGroupsCount; group++)
  237. for(U32 type = 0; type < NetClassTypesCount; type++)
  238. if( classTable[ group ][ type ] )
  239. SAFE_DELETE_ARRAY( classTable[ group ][ type ] );
  240. initialized = false;
  241. }
  242. AbstractClassRep *AbstractClassRep::getCommonParent( const AbstractClassRep *otherClass ) const
  243. {
  244. // CodeReview: This may be a noob way of doing it. There may be some kind of
  245. // super-spiffy algorithm to do what the code below does, but this appeared
  246. // to make sense to me, and it is pretty easy to see what it is doing [6/23/2007 Pat]
  247. static VectorPtr<AbstractClassRep *> thisClassHeirarchy;
  248. thisClassHeirarchy.clear();
  249. AbstractClassRep *walk = const_cast<AbstractClassRep *>( this );
  250. while( walk != NULL )
  251. {
  252. thisClassHeirarchy.push_front( walk );
  253. walk = walk->getParentClass();
  254. }
  255. static VectorPtr<AbstractClassRep *> compClassHeirarchy;
  256. compClassHeirarchy.clear();
  257. walk = const_cast<AbstractClassRep *>( otherClass );
  258. while( walk != NULL )
  259. {
  260. compClassHeirarchy.push_front( walk );
  261. walk = walk->getParentClass();
  262. }
  263. // Make sure we only iterate over the list the number of times we can
  264. S32 maxIterations = getMin( compClassHeirarchy.size(), thisClassHeirarchy.size() );
  265. U32 i = 0;
  266. for( ; i < maxIterations; i++ )
  267. {
  268. if( compClassHeirarchy[i] != thisClassHeirarchy[i] )
  269. break;
  270. }
  271. return compClassHeirarchy[i];
  272. }
  273. //------------------------------------------------------------------------------
  274. //-------------------------------------- ConsoleObject
  275. static char replacebuf[1024];
  276. static char* suppressSpaces(const char* in_pname)
  277. {
  278. U32 i = 0;
  279. char chr;
  280. do
  281. {
  282. chr = in_pname[i];
  283. replacebuf[i++] = (chr != 32) ? chr : '_';
  284. } while(chr);
  285. return replacebuf;
  286. }
  287. void ConsoleObject::addGroup(const char* in_pGroupname, const char* in_pGroupDocs)
  288. {
  289. // Remove spaces.
  290. char* pFieldNameBuf = suppressSpaces(in_pGroupname);
  291. // Append group type to fieldname.
  292. dStrcat(pFieldNameBuf, "_begingroup", 1024);
  293. // Create Field.
  294. AbstractClassRep::Field f;
  295. f.pFieldname = StringTable->insert(pFieldNameBuf);
  296. f.pGroupname = StringTable->insert(in_pGroupname);
  297. if(in_pGroupDocs)
  298. f.pFieldDocs = StringTable->insert(in_pGroupDocs);
  299. f.type = AbstractClassRep::StartGroupFieldType;
  300. f.elementCount = 0;
  301. f.groupExpand = false;
  302. f.validator = NULL;
  303. f.setDataFn = &defaultProtectedSetFn;
  304. f.getDataFn = &defaultProtectedGetFn;
  305. f.writeDataFn = &defaultProtectedWriteFn;
  306. f.networkMask = 0;
  307. // Add to field list.
  308. sg_tempFieldList.push_back(f);
  309. }
  310. void ConsoleObject::endGroup(const char* in_pGroupname)
  311. {
  312. // Remove spaces.
  313. char* pFieldNameBuf = suppressSpaces(in_pGroupname);
  314. // Append group type to fieldname.
  315. dStrcat(pFieldNameBuf, "_endgroup", 1024);
  316. // Create Field.
  317. AbstractClassRep::Field f;
  318. f.pFieldname = StringTable->insert(pFieldNameBuf);
  319. f.pGroupname = StringTable->insert(in_pGroupname);
  320. f.type = AbstractClassRep::EndGroupFieldType;
  321. f.groupExpand = false;
  322. f.validator = NULL;
  323. f.setDataFn = &defaultProtectedSetFn;
  324. f.getDataFn = &defaultProtectedGetFn;
  325. f.writeDataFn = &defaultProtectedWriteFn;
  326. f.elementCount = 0;
  327. f.networkMask = 0;
  328. // Add to field list.
  329. sg_tempFieldList.push_back(f);
  330. }
  331. void ConsoleObject::addArray( const char *arrayName, S32 count )
  332. {
  333. char *nameBuff = suppressSpaces(arrayName);
  334. dStrcat(nameBuff, "_beginarray", 1024);
  335. // Create Field.
  336. AbstractClassRep::Field f;
  337. f.pFieldname = StringTable->insert(nameBuff);
  338. f.pGroupname = StringTable->insert(arrayName);
  339. f.type = AbstractClassRep::StartArrayFieldType;
  340. f.elementCount = count;
  341. f.groupExpand = false;
  342. f.validator = NULL;
  343. f.setDataFn = &defaultProtectedSetFn;
  344. f.getDataFn = &defaultProtectedGetFn;
  345. f.writeDataFn = &defaultProtectedWriteFn;
  346. f.networkMask = 0;
  347. // Add to field list.
  348. sg_tempFieldList.push_back(f);
  349. }
  350. void ConsoleObject::endArray( const char *arrayName )
  351. {
  352. char *nameBuff = suppressSpaces(arrayName);
  353. dStrcat(nameBuff, "_endarray", 1024);
  354. // Create Field.
  355. AbstractClassRep::Field f;
  356. f.pFieldname = StringTable->insert(nameBuff);
  357. f.pGroupname = StringTable->insert(arrayName);
  358. f.type = AbstractClassRep::EndArrayFieldType;
  359. f.groupExpand = false;
  360. f.validator = NULL;
  361. f.setDataFn = &defaultProtectedSetFn;
  362. f.getDataFn = &defaultProtectedGetFn;
  363. f.writeDataFn = &defaultProtectedWriteFn;
  364. f.elementCount = 0;
  365. f.networkMask = 0;
  366. // Add to field list.
  367. sg_tempFieldList.push_back(f);
  368. }
  369. void ConsoleObject::addField(const char* in_pFieldname,
  370. const U32 in_fieldType,
  371. const dsize_t in_fieldOffset,
  372. const char* in_pFieldDocs,
  373. U32 flags )
  374. {
  375. addField(
  376. in_pFieldname,
  377. in_fieldType,
  378. in_fieldOffset,
  379. 1,
  380. in_pFieldDocs,
  381. flags );
  382. }
  383. void ConsoleObject::addField(const char* in_pFieldname,
  384. const U32 in_fieldType,
  385. const dsize_t in_fieldOffset,
  386. AbstractClassRep::WriteDataNotify in_writeDataFn,
  387. const char* in_pFieldDocs,
  388. U32 flags)
  389. {
  390. addField(
  391. in_pFieldname,
  392. in_fieldType,
  393. in_fieldOffset,
  394. in_writeDataFn,
  395. 1,
  396. in_pFieldDocs,
  397. flags);
  398. }
  399. void ConsoleObject::addField(const char* in_pFieldname,
  400. const U32 in_fieldType,
  401. const dsize_t in_fieldOffset,
  402. const U32 in_elementCount,
  403. const char* in_pFieldDocs,
  404. U32 flags)
  405. {
  406. addField(in_pFieldname,
  407. in_fieldType,
  408. in_fieldOffset,
  409. &defaultProtectedWriteFn,
  410. in_elementCount,
  411. in_pFieldDocs,
  412. flags);
  413. }
  414. void ConsoleObject::addField(const char* in_pFieldname,
  415. const U32 in_fieldType,
  416. const dsize_t in_fieldOffset,
  417. AbstractClassRep::WriteDataNotify in_writeDataFn,
  418. const U32 in_elementCount,
  419. const char* in_pFieldDocs,
  420. U32 flags)
  421. {
  422. AbstractClassRep::Field f;
  423. f.pFieldname = StringTable->insert(in_pFieldname);
  424. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  425. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  426. if (in_pFieldDocs)
  427. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  428. f.type = in_fieldType;
  429. f.offset = in_fieldOffset;
  430. f.elementCount = in_elementCount;
  431. f.validator = NULL;
  432. f.flag = flags;
  433. f.setDataFn = &defaultProtectedSetFn;
  434. f.getDataFn = &defaultProtectedGetFn;
  435. f.writeDataFn = in_writeDataFn;
  436. f.networkMask = 0;
  437. f.table = conType->getEnumTable();
  438. sg_tempFieldList.push_back(f);
  439. }
  440. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  441. const U32 in_fieldType,
  442. const dsize_t in_fieldOffset,
  443. AbstractClassRep::SetDataNotify in_setDataFn,
  444. AbstractClassRep::GetDataNotify in_getDataFn,
  445. const char* in_pFieldDocs,
  446. U32 flags)
  447. {
  448. addProtectedField(
  449. in_pFieldname,
  450. in_fieldType,
  451. in_fieldOffset,
  452. in_setDataFn,
  453. in_getDataFn,
  454. &defaultProtectedWriteFn,
  455. 1,
  456. in_pFieldDocs,
  457. flags);
  458. }
  459. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  460. const U32 in_fieldType,
  461. const dsize_t in_fieldOffset,
  462. AbstractClassRep::SetDataNotify in_setDataFn,
  463. AbstractClassRep::GetDataNotify in_getDataFn,
  464. AbstractClassRep::WriteDataNotify in_writeDataFn,
  465. const char* in_pFieldDocs,
  466. U32 flags)
  467. {
  468. addProtectedField(
  469. in_pFieldname,
  470. in_fieldType,
  471. in_fieldOffset,
  472. in_setDataFn,
  473. in_getDataFn,
  474. in_writeDataFn,
  475. 1,
  476. in_pFieldDocs,
  477. flags);
  478. }
  479. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  480. const U32 in_fieldType,
  481. const dsize_t in_fieldOffset,
  482. AbstractClassRep::SetDataNotify in_setDataFn,
  483. AbstractClassRep::GetDataNotify in_getDataFn,
  484. const U32 in_elementCount,
  485. const char* in_pFieldDocs,
  486. U32 flags)
  487. {
  488. addProtectedField(
  489. in_pFieldname,
  490. in_fieldType,
  491. in_fieldOffset,
  492. in_setDataFn,
  493. in_getDataFn,
  494. &defaultProtectedWriteFn,
  495. in_elementCount,
  496. in_pFieldDocs,
  497. flags);
  498. }
  499. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  500. const U32 in_fieldType,
  501. const dsize_t in_fieldOffset,
  502. AbstractClassRep::SetDataNotify in_setDataFn,
  503. AbstractClassRep::GetDataNotify in_getDataFn,
  504. AbstractClassRep::WriteDataNotify in_writeDataFn,
  505. const U32 in_elementCount,
  506. const char* in_pFieldDocs,
  507. U32 flags)
  508. {
  509. AbstractClassRep::Field f;
  510. f.pFieldname = StringTable->insert(in_pFieldname);
  511. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  512. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  513. if (in_pFieldDocs)
  514. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  515. f.type = in_fieldType;
  516. f.offset = in_fieldOffset;
  517. f.elementCount = in_elementCount;
  518. f.validator = NULL;
  519. f.flag = flags;
  520. f.setDataFn = in_setDataFn;
  521. f.getDataFn = in_getDataFn;
  522. f.writeDataFn = in_writeDataFn;
  523. f.networkMask = 0;
  524. f.table = conType->getEnumTable();
  525. sg_tempFieldList.push_back(f);
  526. }
  527. void ConsoleObject::addProtectedFieldV(const char* in_pFieldname,
  528. const U32 in_fieldType,
  529. const dsize_t in_fieldOffset,
  530. AbstractClassRep::SetDataNotify in_setDataFn,
  531. AbstractClassRep::GetDataNotify in_getDataFn,
  532. AbstractClassRep::WriteDataNotify in_writeDataFn,
  533. TypeValidator* v,
  534. const U32 in_elementCount,
  535. const char* in_pFieldDocs,
  536. U32 flags)
  537. {
  538. AbstractClassRep::Field f;
  539. f.pFieldname = StringTable->insert(in_pFieldname);
  540. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  541. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  542. if (in_pFieldDocs)
  543. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  544. f.type = in_fieldType;
  545. f.offset = in_fieldOffset;
  546. f.elementCount = in_elementCount;
  547. f.validator = v;
  548. f.flag = flags;
  549. f.setDataFn = in_setDataFn;
  550. f.getDataFn = in_getDataFn;
  551. f.writeDataFn = in_writeDataFn;
  552. f.networkMask = 0;
  553. sg_tempFieldList.push_back(f);
  554. }
  555. void ConsoleObject::addProtectedFieldV(const char* in_pFieldname,
  556. const U32 in_fieldType,
  557. const dsize_t in_fieldOffset,
  558. AbstractClassRep::SetDataNotify in_setDataFn,
  559. AbstractClassRep::GetDataNotify in_getDataFn,
  560. TypeValidator* v,
  561. const U32 in_elementCount,
  562. const char* in_pFieldDocs,
  563. U32 flags)
  564. {
  565. addProtectedFieldV(
  566. in_pFieldname,
  567. in_fieldType,
  568. in_fieldOffset,
  569. in_setDataFn,
  570. in_getDataFn,
  571. &defaultProtectedWriteFn,
  572. v,
  573. in_elementCount,
  574. in_pFieldDocs,
  575. flags);
  576. }
  577. void ConsoleObject::addProtectedFieldV(const char* in_pFieldname,
  578. const U32 in_fieldType,
  579. const dsize_t in_fieldOffset,
  580. AbstractClassRep::SetDataNotify in_setDataFn,
  581. AbstractClassRep::GetDataNotify in_getDataFn,
  582. TypeValidator* v,
  583. const char* in_pFieldDocs,
  584. U32 flags)
  585. {
  586. addProtectedFieldV(
  587. in_pFieldname,
  588. in_fieldType,
  589. in_fieldOffset,
  590. in_setDataFn,
  591. in_getDataFn,
  592. &defaultProtectedWriteFn,
  593. v,
  594. 1,
  595. in_pFieldDocs,
  596. flags);
  597. }
  598. void ConsoleObject::addFieldV(const char* in_pFieldname,
  599. const U32 in_fieldType,
  600. const dsize_t in_fieldOffset,
  601. TypeValidator* v,
  602. const U32 in_elementCount,
  603. const char* in_pFieldDocs)
  604. {
  605. AbstractClassRep::Field f;
  606. f.pFieldname = StringTable->insert(in_pFieldname);
  607. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  608. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  609. if (in_pFieldDocs)
  610. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  611. f.type = in_fieldType;
  612. f.offset = in_fieldOffset;
  613. f.table = NULL;
  614. f.setDataFn = &defaultProtectedSetFn;
  615. f.getDataFn = &defaultProtectedGetFn;
  616. f.writeDataFn = &defaultProtectedWriteFn;
  617. f.elementCount = in_elementCount;
  618. f.validator = v;
  619. f.networkMask = 0;
  620. sg_tempFieldList.push_back(f);
  621. }
  622. void ConsoleObject::addFieldV(const char* in_pFieldname,
  623. const U32 in_fieldType,
  624. const dsize_t in_fieldOffset,
  625. TypeValidator* v,
  626. const char* in_pFieldDocs)
  627. {
  628. addFieldV(in_pFieldname,
  629. in_fieldType,
  630. in_fieldOffset,
  631. v,
  632. 1,
  633. in_pFieldDocs);
  634. }
  635. void ConsoleObject::addDeprecatedField(const char *fieldName)
  636. {
  637. AbstractClassRep::Field f;
  638. f.pFieldname = StringTable->insert(fieldName);
  639. f.type = AbstractClassRep::DeprecatedFieldType;
  640. f.offset = 0;
  641. f.elementCount = 0;
  642. f.table = NULL;
  643. f.validator = NULL;
  644. f.setDataFn = &defaultProtectedSetFn;
  645. f.getDataFn = &defaultProtectedGetFn;
  646. f.writeDataFn = &defaultProtectedWriteFn;
  647. f.networkMask = 0;
  648. sg_tempFieldList.push_back(f);
  649. }
  650. //------------------------------------------------------------------
  651. bool ConsoleObject::removeField(const char* in_pFieldname)
  652. {
  653. for (U32 i = 0; i < sg_tempFieldList.size(); i++) {
  654. if (dStricmp(in_pFieldname, sg_tempFieldList[i].pFieldname) == 0) {
  655. sg_tempFieldList.erase(i);
  656. return true;
  657. }
  658. }
  659. return false;
  660. }
  661. //--------------------------------------
  662. void ConsoleObject::initPersistFields()
  663. {
  664. docsURL;
  665. }
  666. //--------------------------------------
  667. void ConsoleObject::consoleInit()
  668. {
  669. }
  670. //--------------------------------------
  671. AbstractClassRep* ConsoleObject::getClassRep() const
  672. {
  673. return NULL;
  674. }
  675. bool ConsoleObject::disableFieldSubstitutions(const char* fieldname)
  676. {
  677. StringTableEntry slotname = StringTable->insert(fieldname);
  678. for (U32 i = 0; i < sg_tempFieldList.size(); i++)
  679. {
  680. if (sg_tempFieldList[i].pFieldname == slotname)
  681. {
  682. sg_tempFieldList[i].doNotSubstitute = true;
  683. sg_tempFieldList[i].keepClearSubsOnly = false;
  684. return true;
  685. }
  686. }
  687. return false;
  688. }
  689. bool ConsoleObject::onlyKeepClearSubstitutions(const char* fieldname)
  690. {
  691. StringTableEntry slotname = StringTable->insert(fieldname);
  692. for (U32 i = 0; i < sg_tempFieldList.size(); i++)
  693. {
  694. if (sg_tempFieldList[i].pFieldname == slotname)
  695. {
  696. sg_tempFieldList[i].doNotSubstitute = false;
  697. sg_tempFieldList[i].keepClearSubsOnly = true;
  698. return true;
  699. }
  700. }
  701. return false;
  702. }
  703. String ConsoleObject::_getLogMessage(const char* fmt, va_list args) const
  704. {
  705. String objClass = "UnknownClass";
  706. if(getClassRep())
  707. objClass = getClassRep()->getClassName();
  708. String formattedMessage = String::VToString(fmt, args);
  709. return String::ToString("%s - Object at %x - %s",
  710. objClass.c_str(), this, formattedMessage.c_str());
  711. }
  712. void ConsoleObject::logMessage(const char* fmt, ...) const
  713. {
  714. va_list args;
  715. va_start(args, fmt);
  716. Con::printf(_getLogMessage(fmt, args));
  717. va_end(args);
  718. }
  719. void ConsoleObject::logWarning(const char* fmt, ...) const
  720. {
  721. va_list args;
  722. va_start(args, fmt);
  723. Con::warnf(_getLogMessage(fmt, args));
  724. va_end(args);
  725. }
  726. void ConsoleObject::logError(const char* fmt, ...) const
  727. {
  728. va_list args;
  729. va_start(args, fmt);
  730. Con::errorf(_getLogMessage(fmt, args));
  731. va_end(args);
  732. }
  733. //------------------------------------------------------------------------------
  734. static const char* returnClassList( Vector< AbstractClassRep* >& classes, U32 bufSize )
  735. {
  736. if( !classes.size() )
  737. return "";
  738. dQsort( classes.address(), classes.size(), sizeof( AbstractClassRep* ), ACRCompare );
  739. char* ret = Con::getReturnBuffer( bufSize );
  740. dStrcpy( ret, classes[ 0 ]->getClassName(), bufSize );
  741. for( U32 i = 1; i < classes.size(); i ++ )
  742. {
  743. dStrcat( ret, "\t", bufSize );
  744. dStrcat( ret, classes[ i ]->getClassName(), bufSize );
  745. }
  746. return ret;
  747. }
  748. //------------------------------------------------------------------------------
  749. DefineEngineFunction( isClass, bool, ( const char* identifier ),,
  750. "@brief Returns true if the passed identifier is the name of a declared class.\n\n"
  751. "@ingroup Console")
  752. {
  753. AbstractClassRep* rep = AbstractClassRep::findClassRep( identifier );
  754. return rep != NULL;
  755. }
  756. DefineEngineFunction( isMemberOfClass, bool, ( const char* className, const char* superClassName ),,
  757. "@brief Returns true if the class is derived from the super class.\n\n"
  758. "If either class doesn't exist this returns false.\n"
  759. "@param className The class name.\n"
  760. "@param superClassName The super class to look for.\n"
  761. "@ingroup Console")
  762. {
  763. AbstractClassRep *pRep = AbstractClassRep::findClassRep( className );
  764. while (pRep)
  765. {
  766. if( !dStricmp( pRep->getClassName(), superClassName ) )
  767. return true;
  768. pRep = pRep->getParentClass();
  769. }
  770. return false;
  771. }
  772. DefineEngineFunction( getDescriptionOfClass, const char*, ( const char* className ),,
  773. "@brief Returns the description string for the named class.\n\n"
  774. "@param className The name of the class.\n"
  775. "@return The class description in string format.\n"
  776. "@ingroup Console")
  777. {
  778. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  779. if( rep )
  780. return rep->getDescription();
  781. Con::errorf( "getDescriptionOfClass - no class called '%s'", className );
  782. return "";
  783. }
  784. DefineEngineFunction( getCategoryOfClass, const char*, ( const char* className ),,
  785. "@brief Returns the category of the given class.\n\n"
  786. "@param className The name of the class.\n"
  787. "@ingroup Console")
  788. {
  789. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  790. if (rep == NULL)
  791. {
  792. Con::errorf("getCategoryOfClass - no class called '%s'", className);
  793. return "";
  794. }
  795. while (rep && rep->getParentClass())
  796. {
  797. if (dStrcmp(rep->getCategory(), "") != 0)
  798. break;
  799. rep = rep->getParentClass();
  800. }
  801. return rep ? rep->getCategory() : "";
  802. }
  803. DefineEngineFunction( enumerateConsoleClasses, const char*, ( const char* className ), ( "" ),
  804. "@brief Returns a list of classes that derive from the named class.\n\n"
  805. "If the named class is omitted this dumps all the classes.\n"
  806. "@param className The optional base class name.\n"
  807. "@return A tab delimited list of classes.\n"
  808. "@ingroup Editors\n"
  809. "@internal")
  810. {
  811. AbstractClassRep *base = NULL;
  812. if(className && *className)
  813. {
  814. base = AbstractClassRep::findClassRep(className);
  815. if(!base)
  816. return "";
  817. }
  818. Vector<AbstractClassRep*> classes;
  819. U32 bufSize = 0;
  820. for(AbstractClassRep *rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  821. {
  822. if( !base || rep->isClass(base))
  823. {
  824. classes.push_back(rep);
  825. bufSize += dStrlen(rep->getClassName()) + 1;
  826. }
  827. }
  828. return returnClassList( classes, bufSize );
  829. }
  830. DefineEngineFunction( enumerateConsoleClassesByCategory, const char*, ( String category ),,
  831. "@brief Provide a list of classes that belong to the given category.\n\n"
  832. "@param category The category name.\n"
  833. "@return A tab delimited list of classes.\n"
  834. "@ingroup Editors\n"
  835. "@internal")
  836. {
  837. U32 categoryLength = category.length();
  838. U32 bufSize = 0;
  839. Vector< AbstractClassRep* > classes;
  840. for( AbstractClassRep* rep = AbstractClassRep::getClassList(); rep != NULL; rep = rep->getNextClass() )
  841. {
  842. const String& repCategory = rep->getCategory();
  843. if( repCategory.length() >= categoryLength
  844. && ( repCategory.compare( category, categoryLength, String::NoCase ) == 0 )
  845. && ( repCategory[ categoryLength ] == ' ' || repCategory[ categoryLength ] == '\0' ) )
  846. {
  847. classes.push_back( rep );
  848. bufSize += dStrlen( rep->getClassName() ) + 1;
  849. }
  850. }
  851. return returnClassList( classes, bufSize );
  852. }
  853. DefineEngineFunction( dumpNetStats, void, (),,
  854. "@brief Dumps network statistics for each class to the console.\n\n"
  855. "The returned <i>avg</i>, <i>min</i> and <i>max</i> values are in bits sent per update. "
  856. "The <i>num</i> value is the total number of events collected.\n"
  857. "@note This method only works when TORQUE_NET_STATS is defined in torqueConfig.h.\n"
  858. "@ingroup Networking\n" )
  859. {
  860. #ifdef TORQUE_NET_STATS
  861. for (AbstractClassRep * rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  862. {
  863. if (rep->mNetStatPack.numEvents || rep->mNetStatUnpack.numEvents || rep->mNetStatWrite.numEvents || rep->mNetStatRead.numEvents)
  864. {
  865. Con::printf("class %s net info",rep->getClassName());
  866. if (rep->mNetStatPack.numEvents)
  867. Con::printf(" packUpdate: avg (%f), min (%i), max (%i), num (%i)",
  868. F32(rep->mNetStatPack.total)/F32(rep->mNetStatPack.numEvents),
  869. rep->mNetStatPack.min,
  870. rep->mNetStatPack.max,
  871. rep->mNetStatPack.numEvents);
  872. if (rep->mNetStatUnpack.numEvents)
  873. Con::printf(" unpackUpdate: avg (%f), min (%i), max (%i), num (%i)",
  874. F32(rep->mNetStatUnpack.total)/F32(rep->mNetStatUnpack.numEvents),
  875. rep->mNetStatUnpack.min,
  876. rep->mNetStatUnpack.max,
  877. rep->mNetStatUnpack.numEvents);
  878. if (rep->mNetStatWrite.numEvents)
  879. Con::printf(" write: avg (%f), min (%i), max (%i), num (%i)",
  880. F32(rep->mNetStatWrite.total)/F32(rep->mNetStatWrite.numEvents),
  881. rep->mNetStatWrite.min,
  882. rep->mNetStatWrite.max,
  883. rep->mNetStatWrite.numEvents);
  884. if (rep->mNetStatRead.numEvents)
  885. Con::printf(" read: avg (%f), min (%i), max (%i), num (%i)",
  886. F32(rep->mNetStatRead.total)/F32(rep->mNetStatRead.numEvents),
  887. rep->mNetStatRead.min,
  888. rep->mNetStatRead.max,
  889. rep->mNetStatRead.numEvents);
  890. S32 sum = 0;
  891. for (S32 i=0; i<32; i++)
  892. sum += rep->mDirtyMaskFrequency[i];
  893. if (sum)
  894. {
  895. Con::printf(" Mask bits:");
  896. for (S32 i=0; i<8; i++)
  897. {
  898. F32 avg0 = rep->mDirtyMaskFrequency[i] ? F32(rep->mDirtyMaskTotal[i])/F32(rep->mDirtyMaskFrequency[i]) : 0.0f;
  899. F32 avg8 = rep->mDirtyMaskFrequency[i+8] ? F32(rep->mDirtyMaskTotal[i+8])/F32(rep->mDirtyMaskFrequency[i+8]) : 0.0f;
  900. F32 avg16 = rep->mDirtyMaskFrequency[i+16] ? F32(rep->mDirtyMaskTotal[i+16])/F32(rep->mDirtyMaskFrequency[i+16]) : 0.0f;
  901. F32 avg24 = rep->mDirtyMaskFrequency[i+24] ? F32(rep->mDirtyMaskTotal[i+24])/F32(rep->mDirtyMaskFrequency[i+24]) : 0.0f;
  902. Con::printf(" %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i, (%6.2f)",
  903. i ,rep->mDirtyMaskFrequency[i],avg0,
  904. i+8 ,rep->mDirtyMaskFrequency[i+8],avg8,
  905. i+16,rep->mDirtyMaskFrequency[i+16],avg16,
  906. i+24,rep->mDirtyMaskFrequency[i+24],avg24);
  907. }
  908. }
  909. }
  910. rep->resetNetStats();
  911. }
  912. #endif
  913. }
  914. DefineEngineFunction( sizeof, S32, ( const char *objectOrClass ),,
  915. "@brief Determines the memory consumption of a class or object.\n\n"
  916. "@param objectOrClass The object or class being measured.\n"
  917. "@return Returns the total size of an object in bytes.\n"
  918. "@ingroup Debugging\n")
  919. {
  920. AbstractClassRep *acr = NULL;
  921. SimObject *obj = Sim::findObject(objectOrClass);
  922. if(obj)
  923. acr = obj->getClassRep();
  924. if(!acr)
  925. acr = AbstractClassRep::findClassRep(objectOrClass);
  926. if(acr)
  927. return acr->getSizeof();
  928. if(dStricmp("ConsoleObject", objectOrClass) == 0)
  929. return sizeof(ConsoleObject);
  930. Con::warnf("could not find a class rep for that object or class name.");
  931. return 0;
  932. }
  933. DefineEngineFunction(linkNamespaces, bool, ( String childNSName, String parentNSName ),,
  934. "@brief Links childNS to parentNS.\n\n"
  935. "Links childNS to parentNS, or nothing if parentNS is NULL.\n"
  936. "Will unlink the namespace from previous namespace if a parent already exists.\n"
  937. "@internal\n")
  938. {
  939. StringTableEntry childNSSTE = StringTable->insert(childNSName.c_str());
  940. StringTableEntry parentNSSTE = StringTable->insert(parentNSName.c_str());
  941. Namespace *childNS = Namespace::find(childNSSTE);
  942. Namespace *parentNS = Namespace::find(parentNSSTE);
  943. if (!childNS)
  944. {
  945. return false;
  946. }
  947. Namespace *currentParent = childNS->getParent();
  948. // Link to new NS if applicable
  949. if (currentParent != parentNS)
  950. {
  951. if (currentParent != NULL)
  952. {
  953. if (!childNS->unlinkClass(currentParent))
  954. {
  955. return false;
  956. }
  957. }
  958. if (parentNS != NULL)
  959. {
  960. return childNS->classLinkTo(parentNS);
  961. }
  962. }
  963. return true;
  964. }