consoleObject.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "platform/platform.h"
  23. #include "console/consoleObject.h"
  24. #include "string/stringTable.h"
  25. #include "algorithm/crc.h"
  26. #include "console/console.h"
  27. #include "console/consoleInternal.h"
  28. #include "console/ConsoleTypeValidators.h"
  29. #include "math/mMath.h"
  30. AbstractClassRep * AbstractClassRep::classLinkList = NULL;
  31. static AbstractClassRep::FieldList sg_tempFieldList;
  32. U32 AbstractClassRep::NetClassCount [NetClassGroupsCount][NetClassTypesCount] = {{0, },};
  33. U32 AbstractClassRep::NetClassBitSize[NetClassGroupsCount][NetClassTypesCount] = {{0, },};
  34. AbstractClassRep ** AbstractClassRep::classTable[NetClassGroupsCount][NetClassTypesCount];
  35. U32 AbstractClassRep::classCRC[NetClassGroupsCount] = {INITIAL_CRC_VALUE, };
  36. bool AbstractClassRep::initialized = false;
  37. //--------------------------------------
  38. const AbstractClassRep::Field *AbstractClassRep::findField(StringTableEntry name) const
  39. {
  40. for(U32 i = 0; i < (U32)mFieldList.size(); i++)
  41. if(mFieldList[i].pFieldname == name)
  42. return &mFieldList[i];
  43. return NULL;
  44. }
  45. //-----------------------------------------------------------------------------
  46. AbstractClassRep* AbstractClassRep::findFieldRoot( StringTableEntry fieldName )
  47. {
  48. // Find the field.
  49. const Field* pField = findField( fieldName );
  50. // Finish if not found.
  51. if ( pField == NULL )
  52. return NULL;
  53. // We're the root if we have no parent.
  54. if ( getParentClass() == NULL )
  55. return this;
  56. // Find the field root via the parent.
  57. AbstractClassRep* pFieldRoot = getParentClass()->findFieldRoot( fieldName );
  58. // We're the root if the parent does not have it else return the field root.
  59. return pFieldRoot == NULL ? this : pFieldRoot;
  60. }
  61. //-----------------------------------------------------------------------------
  62. AbstractClassRep* AbstractClassRep::findContainerChildRoot( AbstractClassRep* pChild )
  63. {
  64. // Fetch container child.
  65. AbstractClassRep* pContainerChildClass = getContainerChildClass( true );
  66. // Finish if not found.
  67. if ( pContainerChildClass == NULL )
  68. return NULL;
  69. // We're the root for the child if we have no parent.
  70. if ( getParentClass() == NULL )
  71. return this;
  72. // Find child in parent.
  73. AbstractClassRep* pParentContainerChildClass = getParentClass()->findContainerChildRoot( pChild );
  74. // We;re the root if the parent does not contain the child else return the container root.
  75. return pParentContainerChildClass == NULL ? this : pParentContainerChildClass;
  76. }
  77. //-----------------------------------------------------------------------------
  78. AbstractClassRep* AbstractClassRep::findClassRep(const char* in_pClassName)
  79. {
  80. AssertFatal(initialized,
  81. "AbstractClassRep::findClassRep() - Tried to find an AbstractClassRep before AbstractClassRep::initialize().");
  82. for (AbstractClassRep *walk = classLinkList; walk; walk = walk->nextClass)
  83. if (dStricmp(walk->getClassName(), in_pClassName) == 0)
  84. return walk;
  85. return NULL;
  86. }
  87. //--------------------------------------
  88. void AbstractClassRep::registerClassRep(AbstractClassRep* in_pRep)
  89. {
  90. AssertFatal(in_pRep != NULL, "AbstractClassRep::registerClassRep was passed a NULL pointer!");
  91. #ifdef TORQUE_DEBUG // assert if this class is already registered.
  92. for(AbstractClassRep *walk = classLinkList; walk; walk = walk->nextClass)
  93. {
  94. AssertFatal(dStricmp(in_pRep->mClassName, walk->mClassName) != 0,
  95. "Duplicate class name registered in AbstractClassRep::registerClassRep()");
  96. }
  97. #endif
  98. in_pRep->nextClass = classLinkList;
  99. classLinkList = in_pRep;
  100. }
  101. //--------------------------------------
  102. ConsoleObject* AbstractClassRep::create(const char* in_pClassName)
  103. {
  104. AssertFatal(initialized,
  105. "AbstractClassRep::create() - Tried to create an object before AbstractClassRep::initialize().");
  106. const AbstractClassRep *rep = AbstractClassRep::findClassRep(in_pClassName);
  107. if(rep)
  108. return rep->create();
  109. AssertWarn(0, avar("Couldn't find class rep for dynamic class: %s", in_pClassName));
  110. return NULL;
  111. }
  112. //--------------------------------------
  113. ConsoleObject* AbstractClassRep::create(const U32 groupId, const U32 typeId, const U32 in_classId)
  114. {
  115. AssertFatal(initialized,
  116. "AbstractClassRep::create() - Tried to create an object before AbstractClassRep::initialize().");
  117. AssertFatal(in_classId < NetClassCount[groupId][typeId],
  118. "AbstractClassRep::create() - Class id out of range.");
  119. AssertFatal(classTable[groupId][typeId][in_classId] != NULL,
  120. "AbstractClassRep::create() - No class with requested ID type.");
  121. // Look up the specified class and create it.
  122. if(classTable[groupId][typeId][in_classId])
  123. return classTable[groupId][typeId][in_classId]->create();
  124. return NULL;
  125. }
  126. //--------------------------------------
  127. S32 QSORT_CALLBACK ACRCompare(const void *aptr, const void *bptr)
  128. {
  129. const AbstractClassRep *a = *((const AbstractClassRep **) aptr);
  130. const AbstractClassRep *b = *((const AbstractClassRep **) bptr);
  131. if(a->mClassType != b->mClassType)
  132. return a->mClassType - b->mClassType;
  133. return dStricmp(a->getClassName(), b->getClassName());
  134. }
  135. void AbstractClassRep::initialize()
  136. {
  137. AssertFatal(!initialized, "Duplicate call to AbstractClassRep::initialize()!");
  138. Vector<AbstractClassRep *> dynamicTable(__FILE__, __LINE__);
  139. AbstractClassRep *walk;
  140. // Initialize namespace references...
  141. for (walk = classLinkList; walk; walk = walk->nextClass)
  142. {
  143. walk->mNamespace = Con::lookupNamespace(StringTable->insert(walk->getClassName()));
  144. walk->mNamespace->mClassRep = walk;
  145. }
  146. // Initialize field lists... (and perform other console registration).
  147. for (walk = classLinkList; walk; walk = walk->nextClass)
  148. {
  149. // sg_tempFieldList is used as a staging area for field lists
  150. // (see addField, addGroup, etc.)
  151. sg_tempFieldList.setSize(0);
  152. walk->init();
  153. // So if we have things in it, copy it over...
  154. if (sg_tempFieldList.size() != 0)
  155. {
  156. if( !walk->mFieldList.size())
  157. walk->mFieldList = sg_tempFieldList;
  158. else
  159. destroyFieldValidators( sg_tempFieldList );
  160. }
  161. // And of course delete it every round.
  162. sg_tempFieldList.clear();
  163. }
  164. // Calculate counts and bit sizes for the various NetClasses.
  165. for (U32 group = 0; group < NetClassGroupsCount; group++)
  166. {
  167. U32 groupMask = 1 << group;
  168. // Specifically, for each NetClass of each NetGroup...
  169. for(U32 type = 0; type < NetClassTypesCount; type++)
  170. {
  171. // Go through all the classes and find matches...
  172. for (walk = classLinkList; walk; walk = walk->nextClass)
  173. {
  174. if(walk->mClassType == type && walk->mClassGroupMask & groupMask)
  175. dynamicTable.push_back(walk);
  176. }
  177. // Set the count for this NetGroup and NetClass
  178. NetClassCount[group][type] = dynamicTable.size();
  179. if(!NetClassCount[group][type])
  180. continue; // If no classes matched, skip to next.
  181. // Sort by type and then by name.
  182. dQsort((void *)dynamicTable.address(), dynamicTable.size(), sizeof(AbstractClassRep *), ACRCompare);
  183. // Allocate storage in the classTable
  184. classTable[group][type] = new AbstractClassRep*[NetClassCount[group][type]];
  185. // Fill this in and assign class ids for this group.
  186. for(U32 i = 0; i < NetClassCount[group][type];i++)
  187. {
  188. classTable[group][type][i] = dynamicTable[i];
  189. dynamicTable[i]->mClassId[group] = i;
  190. }
  191. // And calculate the size of bitfields for this group and type.
  192. NetClassBitSize[group][type] =
  193. getBinLog2(getNextPow2(NetClassCount[group][type] + 1));
  194. dynamicTable.clear();
  195. }
  196. }
  197. // Ok, we're golden!
  198. initialized = true;
  199. }
  200. void AbstractClassRep::destroyFieldValidators( AbstractClassRep::FieldList &mFieldList )
  201. {
  202. for(S32 i = mFieldList.size()-1; i>=0; i-- )
  203. {
  204. ConsoleTypeValidator **p = &mFieldList[i].validator;
  205. if( *p )
  206. {
  207. delete *p;
  208. *p = NULL;
  209. }
  210. }
  211. }
  212. //------------------------------------------------------------------------------
  213. //-------------------------------------- ConsoleObject
  214. char replacebuf[1024];
  215. char* suppressSpaces(const char* in_pname)
  216. {
  217. U32 i = 0;
  218. char chr;
  219. do
  220. {
  221. chr = in_pname[i];
  222. replacebuf[i++] = (chr != 32) ? chr : '_';
  223. } while(chr);
  224. return replacebuf;
  225. }
  226. void ConsoleObject::addGroup(const char* in_pGroupname, const char* in_pGroupDocs)
  227. {
  228. // Remove spaces.
  229. char* pFieldNameBuf = suppressSpaces(in_pGroupname);
  230. // Append group type to fieldname.
  231. dStrcat(pFieldNameBuf, "_begingroup");
  232. // Create Field.
  233. AbstractClassRep::Field f;
  234. f.pFieldname = StringTable->insert(pFieldNameBuf);
  235. f.pGroupname = StringTable->insert(in_pGroupname);
  236. if(in_pGroupDocs)
  237. f.pFieldDocs = StringTable->insert(in_pGroupDocs);
  238. else
  239. f.pFieldDocs = NULL;
  240. f.type = AbstractClassRep::StartGroupFieldType;
  241. f.elementCount = 0;
  242. f.groupExpand = false;
  243. f.validator = NULL;
  244. f.setDataFn = &defaultProtectedSetFn;
  245. f.getDataFn = &defaultProtectedGetFn;
  246. f.writeDataFn = &defaultProtectedWriteFn;
  247. // Add to field list.
  248. sg_tempFieldList.push_back(f);
  249. }
  250. void ConsoleObject::endGroup(const char* in_pGroupname)
  251. {
  252. // Remove spaces.
  253. char* pFieldNameBuf = suppressSpaces(in_pGroupname);
  254. // Append group type to fieldname.
  255. dStrcat(pFieldNameBuf, "_endgroup");
  256. // Create Field.
  257. AbstractClassRep::Field f;
  258. f.pFieldname = StringTable->insert(pFieldNameBuf);
  259. f.pGroupname = StringTable->insert(in_pGroupname);
  260. f.pFieldDocs = NULL;
  261. f.type = AbstractClassRep::EndGroupFieldType;
  262. f.groupExpand = false;
  263. f.validator = NULL;
  264. f.setDataFn = &defaultProtectedSetFn;
  265. f.getDataFn = &defaultProtectedGetFn;
  266. f.writeDataFn = &defaultProtectedWriteFn;
  267. f.elementCount = 0;
  268. // Add to field list.
  269. sg_tempFieldList.push_back(f);
  270. }
  271. void ConsoleObject::addField(const char* in_pFieldname,
  272. const U32 in_fieldType,
  273. const dsize_t in_fieldOffset,
  274. const char* in_pFieldDocs)
  275. {
  276. addField(
  277. in_pFieldname,
  278. in_fieldType,
  279. in_fieldOffset,
  280. &defaultProtectedWriteFn,
  281. 1,
  282. NULL,
  283. in_pFieldDocs);
  284. }
  285. void ConsoleObject::addField(const char* in_pFieldname,
  286. const U32 in_fieldType,
  287. const dsize_t in_fieldOffset,
  288. AbstractClassRep::WriteDataNotify in_writeDataFn,
  289. const char* in_pFieldDocs)
  290. {
  291. addField(
  292. in_pFieldname,
  293. in_fieldType,
  294. in_fieldOffset,
  295. in_writeDataFn,
  296. 1,
  297. NULL,
  298. in_pFieldDocs);
  299. }
  300. void ConsoleObject::addField(const char* in_pFieldname,
  301. const U32 in_fieldType,
  302. const dsize_t in_fieldOffset,
  303. const U32 in_elementCount,
  304. EnumTable *in_table,
  305. const char* in_pFieldDocs)
  306. {
  307. addField(
  308. in_pFieldname,
  309. in_fieldType,
  310. in_fieldOffset,
  311. &defaultProtectedWriteFn,
  312. in_elementCount,
  313. in_table,
  314. in_pFieldDocs);
  315. }
  316. void ConsoleObject::addField(const char* in_pFieldname,
  317. const U32 in_fieldType,
  318. const dsize_t in_fieldOffset,
  319. AbstractClassRep::WriteDataNotify in_writeDataFn,
  320. const U32 in_elementCount,
  321. EnumTable *in_table,
  322. const char* in_pFieldDocs)
  323. {
  324. AbstractClassRep::Field f;
  325. f.pFieldname = StringTable->insert(in_pFieldname);
  326. f.pGroupname = NULL;
  327. if(in_pFieldDocs)
  328. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  329. else
  330. f.pFieldDocs = NULL;
  331. f.type = in_fieldType;
  332. f.offset = in_fieldOffset;
  333. f.elementCount = in_elementCount;
  334. f.table = in_table;
  335. f.validator = NULL;
  336. f.setDataFn = &defaultProtectedSetFn;
  337. f.getDataFn = &defaultProtectedGetFn;
  338. f.writeDataFn = in_writeDataFn;
  339. sg_tempFieldList.push_back(f);
  340. }
  341. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  342. const U32 in_fieldType,
  343. const dsize_t in_fieldOffset,
  344. AbstractClassRep::SetDataNotify in_setDataFn,
  345. AbstractClassRep::GetDataNotify in_getDataFn,
  346. const char* in_pFieldDocs)
  347. {
  348. addProtectedField(
  349. in_pFieldname,
  350. in_fieldType,
  351. in_fieldOffset,
  352. in_setDataFn,
  353. in_getDataFn,
  354. &defaultProtectedWriteFn,
  355. 1,
  356. NULL,
  357. in_pFieldDocs);
  358. }
  359. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  360. const U32 in_fieldType,
  361. const dsize_t in_fieldOffset,
  362. AbstractClassRep::SetDataNotify in_setDataFn,
  363. AbstractClassRep::GetDataNotify in_getDataFn,
  364. AbstractClassRep::WriteDataNotify in_writeDataFn,
  365. const char* in_pFieldDocs)
  366. {
  367. addProtectedField(
  368. in_pFieldname,
  369. in_fieldType,
  370. in_fieldOffset,
  371. in_setDataFn,
  372. in_getDataFn,
  373. in_writeDataFn,
  374. 1,
  375. NULL,
  376. in_pFieldDocs);
  377. }
  378. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  379. const U32 in_fieldType,
  380. const dsize_t in_fieldOffset,
  381. AbstractClassRep::SetDataNotify in_setDataFn,
  382. AbstractClassRep::GetDataNotify in_getDataFn,
  383. const U32 in_elementCount,
  384. EnumTable *in_table,
  385. const char* in_pFieldDocs)
  386. {
  387. addProtectedField(
  388. in_pFieldname,
  389. in_fieldType,
  390. in_fieldOffset,
  391. in_setDataFn,
  392. in_getDataFn,
  393. &defaultProtectedWriteFn,
  394. in_elementCount,
  395. in_table,
  396. in_pFieldDocs);
  397. }
  398. void ConsoleObject::addProtectedField(const char* in_pFieldname,
  399. const U32 in_fieldType,
  400. const dsize_t in_fieldOffset,
  401. AbstractClassRep::SetDataNotify in_setDataFn,
  402. AbstractClassRep::GetDataNotify in_getDataFn,
  403. AbstractClassRep::WriteDataNotify in_writeDataFn,
  404. const U32 in_elementCount,
  405. EnumTable *in_table,
  406. const char* in_pFieldDocs)
  407. {
  408. AbstractClassRep::Field f;
  409. f.pFieldname = StringTable->insert(in_pFieldname);
  410. f.pGroupname = NULL;
  411. if(in_pFieldDocs)
  412. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  413. else
  414. f.pFieldDocs = NULL;
  415. f.type = in_fieldType;
  416. f.offset = in_fieldOffset;
  417. f.elementCount = in_elementCount;
  418. f.table = in_table;
  419. f.validator = NULL;
  420. f.setDataFn = in_setDataFn;
  421. f.getDataFn = in_getDataFn;
  422. f.writeDataFn = in_writeDataFn;
  423. sg_tempFieldList.push_back(f);
  424. }
  425. void ConsoleObject::addFieldV(const char* in_pFieldname,
  426. const U32 in_fieldType,
  427. const dsize_t in_fieldOffset,
  428. ConsoleTypeValidator *v,
  429. const char* in_pFieldDocs)
  430. {
  431. AbstractClassRep::Field f;
  432. f.pFieldname = StringTable->insert(in_pFieldname);
  433. f.pGroupname = NULL;
  434. if(in_pFieldDocs)
  435. f.pFieldDocs = StringTable->insert(in_pFieldDocs);
  436. else
  437. f.pFieldDocs = NULL;
  438. f.type = in_fieldType;
  439. f.offset = in_fieldOffset;
  440. f.elementCount = 1;
  441. f.table = NULL;
  442. f.setDataFn = &defaultProtectedSetFn;
  443. f.getDataFn = &defaultProtectedGetFn;
  444. f.writeDataFn = &defaultProtectedWriteFn;
  445. f.validator = v;
  446. v->fieldIndex = sg_tempFieldList.size();
  447. sg_tempFieldList.push_back(f);
  448. }
  449. void ConsoleObject::addDepricatedField(const char *fieldName)
  450. {
  451. AbstractClassRep::Field f;
  452. f.pFieldname = StringTable->insert(fieldName);
  453. f.pGroupname = NULL;
  454. f.pFieldDocs = NULL;
  455. f.type = AbstractClassRep::DepricatedFieldType;
  456. f.offset = 0;
  457. f.elementCount = 0;
  458. f.table = NULL;
  459. f.validator = NULL;
  460. f.setDataFn = &defaultProtectedSetFn;
  461. f.getDataFn = &defaultProtectedGetFn;
  462. f.writeDataFn = &defaultProtectedWriteFn;
  463. sg_tempFieldList.push_back(f);
  464. }
  465. bool ConsoleObject::removeField(const char* in_pFieldname)
  466. {
  467. for (U32 i = 0; i < (U32)sg_tempFieldList.size(); i++) {
  468. if (dStricmp(in_pFieldname, sg_tempFieldList[i].pFieldname) == 0) {
  469. sg_tempFieldList.erase(i);
  470. return true;
  471. }
  472. }
  473. return false;
  474. }
  475. //--------------------------------------
  476. void ConsoleObject::initPersistFields()
  477. {
  478. }
  479. //--------------------------------------
  480. void ConsoleObject::consoleInit()
  481. {
  482. }
  483. ConsoleObject::~ConsoleObject()
  484. {
  485. }
  486. //--------------------------------------
  487. AbstractClassRep* ConsoleObject::getClassRep() const
  488. {
  489. return NULL;
  490. }