consoleObject.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  420. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  421. if (in_pFieldDocs)
  422. f.pFieldDocs = in_pFieldDocs;
  423. f.type = in_fieldType;
  424. f.offset = in_fieldOffset;
  425. f.elementCount = in_elementCount;
  426. f.validator = NULL;
  427. f.flag = flags;
  428. f.setDataFn = &defaultProtectedSetFn;
  429. f.getDataFn = &defaultProtectedGetFn;
  430. f.writeDataFn = in_writeDataFn;
  431. f.networkMask = 0;
  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. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  507. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  508. if (in_pFieldDocs)
  509. f.pFieldDocs = in_pFieldDocs;
  510. f.type = in_fieldType;
  511. f.offset = in_fieldOffset;
  512. f.elementCount = in_elementCount;
  513. f.validator = NULL;
  514. f.flag = flags;
  515. f.setDataFn = in_setDataFn;
  516. f.getDataFn = in_getDataFn;
  517. f.writeDataFn = in_writeDataFn;
  518. f.networkMask = 0;
  519. f.table = conType->getEnumTable();
  520. sg_tempFieldList.push_back(f);
  521. }
  522. void ConsoleObject::addProtectedFieldV(const char* in_pFieldname,
  523. const U32 in_fieldType,
  524. const dsize_t in_fieldOffset,
  525. AbstractClassRep::SetDataNotify in_setDataFn,
  526. AbstractClassRep::GetDataNotify in_getDataFn,
  527. AbstractClassRep::WriteDataNotify in_writeDataFn,
  528. TypeValidator* v,
  529. const U32 in_elementCount,
  530. const char* in_pFieldDocs,
  531. U32 flags)
  532. {
  533. AbstractClassRep::Field f;
  534. f.pFieldname = StringTable->insert(in_pFieldname);
  535. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  536. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  537. if (in_pFieldDocs)
  538. f.pFieldDocs = in_pFieldDocs;
  539. f.type = in_fieldType;
  540. f.offset = in_fieldOffset;
  541. f.elementCount = in_elementCount;
  542. f.validator = v;
  543. f.flag = flags;
  544. f.setDataFn = in_setDataFn;
  545. f.getDataFn = in_getDataFn;
  546. f.writeDataFn = in_writeDataFn;
  547. f.networkMask = 0;
  548. v->fieldIndex = sg_tempFieldList.size();
  549. sg_tempFieldList.push_back(f);
  550. }
  551. void ConsoleObject::addProtectedFieldV(const char* in_pFieldname,
  552. const U32 in_fieldType,
  553. const dsize_t in_fieldOffset,
  554. AbstractClassRep::SetDataNotify in_setDataFn,
  555. AbstractClassRep::GetDataNotify in_getDataFn,
  556. TypeValidator* v,
  557. const U32 in_elementCount,
  558. const char* in_pFieldDocs,
  559. U32 flags)
  560. {
  561. addProtectedFieldV(
  562. in_pFieldname,
  563. in_fieldType,
  564. in_fieldOffset,
  565. in_setDataFn,
  566. in_getDataFn,
  567. &defaultProtectedWriteFn,
  568. v,
  569. in_elementCount,
  570. in_pFieldDocs,
  571. flags);
  572. }
  573. void ConsoleObject::addProtectedFieldV(const char* in_pFieldname,
  574. const U32 in_fieldType,
  575. const dsize_t in_fieldOffset,
  576. AbstractClassRep::SetDataNotify in_setDataFn,
  577. AbstractClassRep::GetDataNotify in_getDataFn,
  578. TypeValidator* v,
  579. const char* in_pFieldDocs,
  580. U32 flags)
  581. {
  582. addProtectedFieldV(
  583. in_pFieldname,
  584. in_fieldType,
  585. in_fieldOffset,
  586. in_setDataFn,
  587. in_getDataFn,
  588. &defaultProtectedWriteFn,
  589. v,
  590. 1,
  591. in_pFieldDocs,
  592. flags);
  593. }
  594. void ConsoleObject::addFieldV(const char* in_pFieldname,
  595. const U32 in_fieldType,
  596. const dsize_t in_fieldOffset,
  597. TypeValidator* v,
  598. const U32 in_elementCount,
  599. const char* in_pFieldDocs)
  600. {
  601. AbstractClassRep::Field f;
  602. f.pFieldname = StringTable->insert(in_pFieldname);
  603. ConsoleBaseType* conType = ConsoleBaseType::getType(in_fieldType);
  604. AssertFatal(conType, avar("ConsoleObject::addProtectedField[%s] - invalid console type", in_pFieldname));
  605. if (in_pFieldDocs)
  606. f.pFieldDocs = in_pFieldDocs;
  607. f.type = in_fieldType;
  608. f.offset = in_fieldOffset;
  609. f.table = NULL;
  610. f.setDataFn = &defaultProtectedSetFn;
  611. f.getDataFn = &defaultProtectedGetFn;
  612. f.writeDataFn = &defaultProtectedWriteFn;
  613. f.elementCount = in_elementCount;
  614. f.validator = v;
  615. f.networkMask = 0;
  616. v->fieldIndex = sg_tempFieldList.size();
  617. sg_tempFieldList.push_back(f);
  618. }
  619. void ConsoleObject::addFieldV(const char* in_pFieldname,
  620. const U32 in_fieldType,
  621. const dsize_t in_fieldOffset,
  622. TypeValidator* v,
  623. const char* in_pFieldDocs)
  624. {
  625. addFieldV(in_pFieldname,
  626. in_fieldType,
  627. in_fieldOffset,
  628. v,
  629. 1,
  630. in_pFieldDocs);
  631. }
  632. void ConsoleObject::addDeprecatedField(const char *fieldName)
  633. {
  634. AbstractClassRep::Field f;
  635. f.pFieldname = StringTable->insert(fieldName);
  636. f.type = AbstractClassRep::DeprecatedFieldType;
  637. f.offset = 0;
  638. f.elementCount = 0;
  639. f.table = NULL;
  640. f.validator = NULL;
  641. f.setDataFn = &defaultProtectedSetFn;
  642. f.getDataFn = &defaultProtectedGetFn;
  643. f.writeDataFn = &defaultProtectedWriteFn;
  644. f.networkMask = 0;
  645. sg_tempFieldList.push_back(f);
  646. }
  647. //------------------------------------------------------------------
  648. bool ConsoleObject::removeField(const char* in_pFieldname)
  649. {
  650. for (U32 i = 0; i < sg_tempFieldList.size(); i++) {
  651. if (dStricmp(in_pFieldname, sg_tempFieldList[i].pFieldname) == 0) {
  652. sg_tempFieldList.erase(i);
  653. return true;
  654. }
  655. }
  656. return false;
  657. }
  658. //--------------------------------------
  659. void ConsoleObject::initPersistFields()
  660. {
  661. docsURL;
  662. }
  663. //--------------------------------------
  664. void ConsoleObject::consoleInit()
  665. {
  666. }
  667. //--------------------------------------
  668. AbstractClassRep* ConsoleObject::getClassRep() const
  669. {
  670. return NULL;
  671. }
  672. bool ConsoleObject::disableFieldSubstitutions(const char* fieldname)
  673. {
  674. StringTableEntry slotname = StringTable->insert(fieldname);
  675. for (U32 i = 0; i < sg_tempFieldList.size(); i++)
  676. {
  677. if (sg_tempFieldList[i].pFieldname == slotname)
  678. {
  679. sg_tempFieldList[i].doNotSubstitute = true;
  680. sg_tempFieldList[i].keepClearSubsOnly = false;
  681. return true;
  682. }
  683. }
  684. return false;
  685. }
  686. bool ConsoleObject::onlyKeepClearSubstitutions(const char* fieldname)
  687. {
  688. StringTableEntry slotname = StringTable->insert(fieldname);
  689. for (U32 i = 0; i < sg_tempFieldList.size(); i++)
  690. {
  691. if (sg_tempFieldList[i].pFieldname == slotname)
  692. {
  693. sg_tempFieldList[i].doNotSubstitute = false;
  694. sg_tempFieldList[i].keepClearSubsOnly = true;
  695. return true;
  696. }
  697. }
  698. return false;
  699. }
  700. String ConsoleObject::_getLogMessage(const char* fmt, va_list args) const
  701. {
  702. String objClass = "UnknownClass";
  703. if(getClassRep())
  704. objClass = getClassRep()->getClassName();
  705. String formattedMessage = String::VToString(fmt, args);
  706. return String::ToString("%s - Object at %x - %s",
  707. objClass.c_str(), this, formattedMessage.c_str());
  708. }
  709. void ConsoleObject::logMessage(const char* fmt, ...) const
  710. {
  711. va_list args;
  712. va_start(args, fmt);
  713. Con::printf(_getLogMessage(fmt, args));
  714. va_end(args);
  715. }
  716. void ConsoleObject::logWarning(const char* fmt, ...) const
  717. {
  718. va_list args;
  719. va_start(args, fmt);
  720. Con::warnf(_getLogMessage(fmt, args));
  721. va_end(args);
  722. }
  723. void ConsoleObject::logError(const char* fmt, ...) const
  724. {
  725. va_list args;
  726. va_start(args, fmt);
  727. Con::errorf(_getLogMessage(fmt, args));
  728. va_end(args);
  729. }
  730. //------------------------------------------------------------------------------
  731. static const char* returnClassList( Vector< AbstractClassRep* >& classes, U32 bufSize )
  732. {
  733. if( !classes.size() )
  734. return "";
  735. dQsort( classes.address(), classes.size(), sizeof( AbstractClassRep* ), ACRCompare );
  736. char* ret = Con::getReturnBuffer( bufSize );
  737. dStrcpy( ret, classes[ 0 ]->getClassName(), bufSize );
  738. for( U32 i = 1; i < classes.size(); i ++ )
  739. {
  740. dStrcat( ret, "\t", bufSize );
  741. dStrcat( ret, classes[ i ]->getClassName(), bufSize );
  742. }
  743. return ret;
  744. }
  745. //------------------------------------------------------------------------------
  746. DefineEngineFunction( isClass, bool, ( const char* identifier ),,
  747. "@brief Returns true if the passed identifier is the name of a declared class.\n\n"
  748. "@ingroup Console")
  749. {
  750. AbstractClassRep* rep = AbstractClassRep::findClassRep( identifier );
  751. return rep != NULL;
  752. }
  753. DefineEngineFunction( isMemberOfClass, bool, ( const char* className, const char* superClassName ),,
  754. "@brief Returns true if the class is derived from the super class.\n\n"
  755. "If either class doesn't exist this returns false.\n"
  756. "@param className The class name.\n"
  757. "@param superClassName The super class to look for.\n"
  758. "@ingroup Console")
  759. {
  760. AbstractClassRep *pRep = AbstractClassRep::findClassRep( className );
  761. while (pRep)
  762. {
  763. if( !dStricmp( pRep->getClassName(), superClassName ) )
  764. return true;
  765. pRep = pRep->getParentClass();
  766. }
  767. return false;
  768. }
  769. DefineEngineFunction( getDescriptionOfClass, const char*, ( const char* className ),,
  770. "@brief Returns the description string for the named class.\n\n"
  771. "@param className The name of the class.\n"
  772. "@return The class description in string format.\n"
  773. "@ingroup Console")
  774. {
  775. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  776. if( rep )
  777. return rep->getDescription();
  778. Con::errorf( "getDescriptionOfClass - no class called '%s'", className );
  779. return "";
  780. }
  781. DefineEngineFunction( getCategoryOfClass, const char*, ( const char* className ),,
  782. "@brief Returns the category of the given class.\n\n"
  783. "@param className The name of the class.\n"
  784. "@ingroup Console")
  785. {
  786. AbstractClassRep* rep = AbstractClassRep::findClassRep( className );
  787. if (rep == NULL)
  788. {
  789. Con::errorf("getCategoryOfClass - no class called '%s'", className);
  790. return "";
  791. }
  792. while (rep && rep->getParentClass())
  793. {
  794. if (dStrcmp(rep->getCategory(), "") != 0)
  795. break;
  796. rep = rep->getParentClass();
  797. }
  798. return rep ? rep->getCategory() : "";
  799. }
  800. DefineEngineFunction( enumerateConsoleClasses, const char*, ( const char* className ), ( "" ),
  801. "@brief Returns a list of classes that derive from the named class.\n\n"
  802. "If the named class is omitted this dumps all the classes.\n"
  803. "@param className The optional base class name.\n"
  804. "@return A tab delimited list of classes.\n"
  805. "@ingroup Editors\n"
  806. "@internal")
  807. {
  808. AbstractClassRep *base = NULL;
  809. if(className && *className)
  810. {
  811. base = AbstractClassRep::findClassRep(className);
  812. if(!base)
  813. return "";
  814. }
  815. Vector<AbstractClassRep*> classes;
  816. U32 bufSize = 0;
  817. for(AbstractClassRep *rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  818. {
  819. if( !base || rep->isClass(base))
  820. {
  821. classes.push_back(rep);
  822. bufSize += dStrlen(rep->getClassName()) + 1;
  823. }
  824. }
  825. return returnClassList( classes, bufSize );
  826. }
  827. DefineEngineFunction( enumerateConsoleClassesByCategory, const char*, ( String category ),,
  828. "@brief Provide a list of classes that belong to the given category.\n\n"
  829. "@param category The category name.\n"
  830. "@return A tab delimited list of classes.\n"
  831. "@ingroup Editors\n"
  832. "@internal")
  833. {
  834. U32 categoryLength = category.length();
  835. U32 bufSize = 0;
  836. Vector< AbstractClassRep* > classes;
  837. for( AbstractClassRep* rep = AbstractClassRep::getClassList(); rep != NULL; rep = rep->getNextClass() )
  838. {
  839. const String& repCategory = rep->getCategory();
  840. if( repCategory.length() >= categoryLength
  841. && ( repCategory.compare( category, categoryLength, String::NoCase ) == 0 )
  842. && ( repCategory[ categoryLength ] == ' ' || repCategory[ categoryLength ] == '\0' ) )
  843. {
  844. classes.push_back( rep );
  845. bufSize += dStrlen( rep->getClassName() ) + 1;
  846. }
  847. }
  848. return returnClassList( classes, bufSize );
  849. }
  850. DefineEngineFunction( dumpNetStats, void, (),,
  851. "@brief Dumps network statistics for each class to the console.\n\n"
  852. "The returned <i>avg</i>, <i>min</i> and <i>max</i> values are in bits sent per update. "
  853. "The <i>num</i> value is the total number of events collected.\n"
  854. "@note This method only works when TORQUE_NET_STATS is defined in torqueConfig.h.\n"
  855. "@ingroup Networking\n" )
  856. {
  857. #ifdef TORQUE_NET_STATS
  858. for (AbstractClassRep * rep = AbstractClassRep::getClassList(); rep; rep = rep->getNextClass())
  859. {
  860. if (rep->mNetStatPack.numEvents || rep->mNetStatUnpack.numEvents || rep->mNetStatWrite.numEvents || rep->mNetStatRead.numEvents)
  861. {
  862. Con::printf("class %s net info",rep->getClassName());
  863. if (rep->mNetStatPack.numEvents)
  864. Con::printf(" packUpdate: avg (%f), min (%i), max (%i), num (%i)",
  865. F32(rep->mNetStatPack.total)/F32(rep->mNetStatPack.numEvents),
  866. rep->mNetStatPack.min,
  867. rep->mNetStatPack.max,
  868. rep->mNetStatPack.numEvents);
  869. if (rep->mNetStatUnpack.numEvents)
  870. Con::printf(" unpackUpdate: avg (%f), min (%i), max (%i), num (%i)",
  871. F32(rep->mNetStatUnpack.total)/F32(rep->mNetStatUnpack.numEvents),
  872. rep->mNetStatUnpack.min,
  873. rep->mNetStatUnpack.max,
  874. rep->mNetStatUnpack.numEvents);
  875. if (rep->mNetStatWrite.numEvents)
  876. Con::printf(" write: avg (%f), min (%i), max (%i), num (%i)",
  877. F32(rep->mNetStatWrite.total)/F32(rep->mNetStatWrite.numEvents),
  878. rep->mNetStatWrite.min,
  879. rep->mNetStatWrite.max,
  880. rep->mNetStatWrite.numEvents);
  881. if (rep->mNetStatRead.numEvents)
  882. Con::printf(" read: avg (%f), min (%i), max (%i), num (%i)",
  883. F32(rep->mNetStatRead.total)/F32(rep->mNetStatRead.numEvents),
  884. rep->mNetStatRead.min,
  885. rep->mNetStatRead.max,
  886. rep->mNetStatRead.numEvents);
  887. S32 sum = 0;
  888. for (S32 i=0; i<32; i++)
  889. sum += rep->mDirtyMaskFrequency[i];
  890. if (sum)
  891. {
  892. Con::printf(" Mask bits:");
  893. for (S32 i=0; i<8; i++)
  894. {
  895. F32 avg0 = rep->mDirtyMaskFrequency[i] ? F32(rep->mDirtyMaskTotal[i])/F32(rep->mDirtyMaskFrequency[i]) : 0.0f;
  896. F32 avg8 = rep->mDirtyMaskFrequency[i+8] ? F32(rep->mDirtyMaskTotal[i+8])/F32(rep->mDirtyMaskFrequency[i+8]) : 0.0f;
  897. F32 avg16 = rep->mDirtyMaskFrequency[i+16] ? F32(rep->mDirtyMaskTotal[i+16])/F32(rep->mDirtyMaskFrequency[i+16]) : 0.0f;
  898. F32 avg24 = rep->mDirtyMaskFrequency[i+24] ? F32(rep->mDirtyMaskTotal[i+24])/F32(rep->mDirtyMaskFrequency[i+24]) : 0.0f;
  899. Con::printf(" %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i (%6.2f) %2i - %4i, (%6.2f)",
  900. i ,rep->mDirtyMaskFrequency[i],avg0,
  901. i+8 ,rep->mDirtyMaskFrequency[i+8],avg8,
  902. i+16,rep->mDirtyMaskFrequency[i+16],avg16,
  903. i+24,rep->mDirtyMaskFrequency[i+24],avg24);
  904. }
  905. }
  906. }
  907. rep->resetNetStats();
  908. }
  909. #endif
  910. }
  911. DefineEngineFunction( sizeof, S32, ( const char *objectOrClass ),,
  912. "@brief Determines the memory consumption of a class or object.\n\n"
  913. "@param objectOrClass The object or class being measured.\n"
  914. "@return Returns the total size of an object in bytes.\n"
  915. "@ingroup Debugging\n")
  916. {
  917. AbstractClassRep *acr = NULL;
  918. SimObject *obj = Sim::findObject(objectOrClass);
  919. if(obj)
  920. acr = obj->getClassRep();
  921. if(!acr)
  922. acr = AbstractClassRep::findClassRep(objectOrClass);
  923. if(acr)
  924. return acr->getSizeof();
  925. if(dStricmp("ConsoleObject", objectOrClass) == 0)
  926. return sizeof(ConsoleObject);
  927. Con::warnf("could not find a class rep for that object or class name.");
  928. return 0;
  929. }
  930. DefineEngineFunction(linkNamespaces, bool, ( String childNSName, String parentNSName ),,
  931. "@brief Links childNS to parentNS.\n\n"
  932. "Links childNS to parentNS, or nothing if parentNS is NULL.\n"
  933. "Will unlink the namespace from previous namespace if a parent already exists.\n"
  934. "@internal\n")
  935. {
  936. StringTableEntry childNSSTE = StringTable->insert(childNSName.c_str());
  937. StringTableEntry parentNSSTE = StringTable->insert(parentNSName.c_str());
  938. Namespace *childNS = Namespace::find(childNSSTE);
  939. Namespace *parentNS = Namespace::find(parentNSSTE);
  940. if (!childNS)
  941. {
  942. return false;
  943. }
  944. Namespace *currentParent = childNS->getParent();
  945. // Link to new NS if applicable
  946. if (currentParent != parentNS)
  947. {
  948. if (currentParent != NULL)
  949. {
  950. if (!childNS->unlinkClass(currentParent))
  951. {
  952. return false;
  953. }
  954. }
  955. if (parentNS != NULL)
  956. {
  957. return childNS->classLinkTo(parentNS);
  958. }
  959. }
  960. return true;
  961. }