consoleObject.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. #ifndef _CONSOLEOBJECT_H_
  27. #define _CONSOLEOBJECT_H_
  28. #ifndef _TVECTOR_H_
  29. #include "core/util/tVector.h"
  30. #endif
  31. #ifndef _STRINGTABLE_H_
  32. #include "core/stringTable.h"
  33. #endif
  34. #ifndef _STRINGFUNCTIONS_H_
  35. #include "core/strings/stringFunctions.h"
  36. #endif
  37. #ifndef _BITSET_H_
  38. #include "core/bitSet.h"
  39. #endif
  40. #ifndef _DYNAMIC_CONSOLETYPES_H_
  41. #include "console/dynamicTypes.h"
  42. #endif
  43. #ifndef _ENGINEOBJECT_H_
  44. #include "console/engineObject.h"
  45. #endif
  46. #ifndef _ENGINEFUNCTIONS_H_
  47. #include "console/engineFunctions.h"
  48. #endif
  49. #ifndef _SIMOBJECTREF_H_
  50. #include "console/simObjectRef.h"
  51. #endif
  52. #ifndef TINYXML_INCLUDED
  53. #include "tinyxml2.h"
  54. #endif
  55. #ifndef _CONSOLFUNCTIONS_H_
  56. #include "console/consoleFunctions.h"
  57. #endif
  58. /// @file
  59. /// Legacy console object system.
  60. /// @ingroup console_system Console System
  61. /// @{
  62. class Namespace;
  63. class ConsoleObject;
  64. enum NetClassTypes
  65. {
  66. NetClassTypeObject = 0,
  67. NetClassTypeDataBlock,
  68. NetClassTypeEvent,
  69. NetClassTypesCount,
  70. };
  71. enum NetClassGroups
  72. {
  73. NetClassGroupGame = 0,
  74. NetClassGroupCommunity,
  75. NetClassGroup3,
  76. NetClassGroup4,
  77. NetClassGroupsCount,
  78. };
  79. enum NetClassMasks
  80. {
  81. NetClassGroupGameMask = BIT(NetClassGroupGame),
  82. NetClassGroupCommunityMask = BIT(NetClassGroupCommunity),
  83. };
  84. enum NetDirection
  85. {
  86. NetEventDirAny,
  87. NetEventDirServerToClient,
  88. NetEventDirClientToServer,
  89. };
  90. class SimObject;
  91. class TypeValidator;
  92. class ConsoleClassObject;
  93. DECLARE_SCOPE( ConsoleAPI );
  94. //=============================================================================
  95. // AbstractClassRep.
  96. //=============================================================================
  97. /// Core functionality for class manipulation.
  98. ///
  99. /// @section AbstractClassRep_intro Introduction (or, Why AbstractClassRep?)
  100. ///
  101. /// Many of Torque's subsystems, especially network, console, and sim,
  102. /// require the ability to programatically instantiate classes. For instance,
  103. /// when objects are ghosted, the networking layer needs to be able to create
  104. /// an instance of the object on the client. When the console scripting
  105. /// language runtime encounters the "new" keyword, it has to be able to fill
  106. /// that request.
  107. ///
  108. /// Since standard C++ doesn't provide a function to create a new instance of
  109. /// an arbitrary class at runtime, one must be created. This is what
  110. /// AbstractClassRep and ConcreteClassRep are all about. They allow the registration
  111. /// and instantiation of arbitrary classes at runtime.
  112. ///
  113. /// In addition, ACR keeps track of the fields (registered via addField() and co.) of
  114. /// a class, allowing programmatic access of class fields.
  115. ///
  116. /// @see ConsoleObject
  117. ///
  118. /// @note In general, you will only access the functionality implemented in this class via
  119. /// ConsoleObject::create(). Most of the time, you will only ever need to use this part
  120. /// part of the engine indirectly - ie, you will use the networking system or the console,
  121. /// or ConsoleObject, and they will indirectly use this code. <b>The following discussion
  122. /// is really only relevant for advanced engine users.</b>
  123. ///
  124. /// @section AbstractClassRep_netstuff NetClasses and Class IDs
  125. ///
  126. /// Torque supports a notion of group, type, and direction for objects passed over
  127. /// the network. Class IDs are assigned sequentially per-group, per-type, so that, for instance,
  128. /// the IDs assigned to Datablocks are seperate from the IDs assigned to NetObjects or NetEvents.
  129. /// This can translate into significant bandwidth savings (especially since the size of the fields
  130. /// for transmitting these bits are determined at run-time based on the number of IDs given out.
  131. ///
  132. /// @section AbstractClassRep_details AbstractClassRep Internals
  133. ///
  134. /// Much like ConsoleConstructor, ACR does some preparatory work at runtime before execution
  135. /// is passed to main(). In actual fact, this preparatory work is done by the ConcreteClassRep
  136. /// template. Let's examine this more closely.
  137. ///
  138. /// If we examine ConsoleObject, we see that two macros must be used in the definition of a
  139. /// properly integrated objects. From the ConsoleObject example:
  140. ///
  141. /// @code
  142. /// // This is from inside the class definition...
  143. /// DECLARE_CONOBJECT(TorqueObject);
  144. ///
  145. /// // And this is from outside the class definition...
  146. /// IMPLEMENT_CONOBJECT(TorqueObject);
  147. /// @endcode
  148. ///
  149. /// What do these things actually do?
  150. ///
  151. /// Not all that much, in fact. They expand to code something like this:
  152. ///
  153. /// @code
  154. /// // This is from inside the class definition...
  155. /// static ConcreteClassRep<TorqueObject> dynClassRep;
  156. /// static AbstractClassRep* getParentStaticClassRep();
  157. /// static AbstractClassRep* getStaticClassRep();
  158. /// virtual AbstractClassRep* getClassRep() const;
  159. /// @endcode
  160. ///
  161. /// @code
  162. /// // And this is from outside the class definition...
  163. /// AbstractClassRep* TorqueObject::getClassRep() const { return &TorqueObject::dynClassRep; }
  164. /// AbstractClassRep* TorqueObject::getStaticClassRep() { return &dynClassRep; }
  165. /// AbstractClassRep* TorqueObject::getParentStaticClassRep() { return Parent::getStaticClassRep(); }
  166. /// ConcreteClassRep<TorqueObject> TorqueObject::dynClassRep("TorqueObject", 0, -1, 0);
  167. /// @endcode
  168. ///
  169. /// As you can see, getClassRep(), getStaticClassRep(), and getParentStaticClassRep() are just
  170. /// accessors to allow access to various ConcreteClassRep instances. This is where the Parent
  171. /// typedef comes into play as well - it lets getParentStaticClassRep() get the right
  172. /// class rep.
  173. ///
  174. /// In addition, dynClassRep is declared as a member of TorqueObject, and defined later
  175. /// on. Much like ConsoleConstructor, ConcreteClassReps add themselves to a global linked
  176. /// list in their constructor.
  177. ///
  178. /// Then, when AbstractClassRep::initialize() is called, from Con::init(), we iterate through
  179. /// the list and perform the following tasks:
  180. /// - Sets up a Namespace for each class.
  181. /// - Call the init() method on each ConcreteClassRep. This method:
  182. /// - Links namespaces between parent and child classes, using Con::classLinkNamespaces.
  183. /// - Calls initPersistFields() and consoleInit().
  184. /// - As a result of calling initPersistFields, the field list for the class is populated.
  185. /// - Assigns network IDs for classes based on their NetGroup membership. Determines
  186. /// bit allocations for network ID fields.
  187. ///
  188. /// @nosubgrouping
  189. class AbstractClassRep : public ConsoleBaseType
  190. {
  191. friend class ConsoleObject;
  192. public:
  193. typedef ConsoleBaseType Parent;
  194. /// Allows the writing of a custom TAML schema.
  195. typedef void(*WriteCustomTamlSchema)(const AbstractClassRep* pClassRep, tinyxml2::XMLElement* pParentElement);
  196. /// @name 'Tructors
  197. /// @{
  198. ///
  199. /// @param conIdPtr Pointer to the static S32 console ID.
  200. /// @param conTypeName Console type name.
  201. AbstractClassRep( S32* conIdPtr, const char* typeName )
  202. : Parent( sizeof( void* ), conIdPtr, typeName )
  203. {
  204. VECTOR_SET_ASSOCIATION( mFieldList );
  205. mCategory = StringTable->EmptyString();
  206. mClassGroupMask = 0;
  207. std::fill_n(mClassId, NetClassGroupsCount, -1);
  208. mClassName = StringTable->EmptyString();
  209. mClassSizeof = 0;
  210. mClassType = 0;
  211. mDescription = StringTable->EmptyString();
  212. #ifdef TORQUE_NET_STATS
  213. dMemset(mDirtyMaskFrequency, 0, sizeof(mDirtyMaskFrequency));
  214. dMemset(mDirtyMaskTotal, 0, sizeof(mDirtyMaskTotal));
  215. #endif
  216. mDynamicGroupExpand = false;
  217. mNamespace = NULL;
  218. mNetEventDir = 0;
  219. nextClass = NULL;
  220. parentClass = NULL;
  221. mIsRenderEnabled = true;
  222. mIsSelectionEnabled = true;
  223. }
  224. /// @}
  225. /// @name Representation Interface
  226. /// @{
  227. //TODO: move over to EngineTypeNetInfo
  228. S32 mClassGroupMask; ///< Mask indicating in which NetGroups this object belongs.
  229. S32 mClassType; ///< Stores the NetClass of this class.
  230. S32 mNetEventDir; ///< Stores the NetDirection of this class.
  231. S32 mClassId[ NetClassGroupsCount ]; ///< Stores the IDs assigned to this class for each group.
  232. S32 mClassSizeof; ///< Size of instances in bytes.
  233. //TODO: move over to EngineTypeNetInfo
  234. #ifdef TORQUE_NET_STATS
  235. struct NetStatInstance
  236. {
  237. U32 numEvents;
  238. U32 total;
  239. S32 min;
  240. S32 max;
  241. void reset()
  242. {
  243. numEvents = 0;
  244. total = 0;
  245. min = S32_MAX;
  246. max = S32_MIN;
  247. }
  248. void update(U32 amount)
  249. {
  250. numEvents++;
  251. total += amount;
  252. min = getMin((S32)amount, min);
  253. max = getMax((S32)amount, max);
  254. }
  255. NetStatInstance()
  256. {
  257. reset();
  258. }
  259. };
  260. NetStatInstance mNetStatPack;
  261. NetStatInstance mNetStatUnpack;
  262. NetStatInstance mNetStatWrite;
  263. NetStatInstance mNetStatRead;
  264. U32 mDirtyMaskFrequency[32];
  265. U32 mDirtyMaskTotal[32];
  266. void resetNetStats()
  267. {
  268. mNetStatPack.reset();
  269. mNetStatUnpack.reset();
  270. mNetStatWrite.reset();
  271. mNetStatRead.reset();
  272. for(S32 i=0; i<32; i++)
  273. {
  274. mDirtyMaskFrequency[i] = 0;
  275. mDirtyMaskTotal[i] = 0;
  276. }
  277. }
  278. void updateNetStatPack(U32 dirtyMask, U32 length)
  279. {
  280. mNetStatPack.update(length);
  281. for(S32 i=0; i<32; i++)
  282. if(BIT(i) & dirtyMask)
  283. {
  284. mDirtyMaskFrequency[i]++;
  285. mDirtyMaskTotal[i] += length;
  286. }
  287. }
  288. void updateNetStatUnpack(U32 length)
  289. {
  290. mNetStatUnpack.update(length);
  291. }
  292. void updateNetStatWriteData(U32 length)
  293. {
  294. mNetStatWrite.update(length);
  295. }
  296. void updateNetStatReadData(U32 length)
  297. {
  298. mNetStatRead.update(length);
  299. }
  300. #endif
  301. S32 getClassId (U32 netClassGroup) const { return mClassId[ netClassGroup ]; }
  302. static U32 getClassCRC (U32 netClassGroup) { return classCRC[ netClassGroup ]; }
  303. AbstractClassRep* getCommonParent( const AbstractClassRep *otherClass ) const;
  304. /// Return the name of this class.
  305. StringTableEntry getClassName() const { return mClassName; }
  306. /// Return the namespace that contains the methods of this class.
  307. Namespace* getNameSpace() const { return mNamespace; }
  308. /// Return the AbstractClassRep of the class that this class is derived from.
  309. AbstractClassRep* getParentClass() const { return parentClass; }
  310. virtual AbstractClassRep* getContainerChildClass(const bool recurse) = 0;
  311. virtual WriteCustomTamlSchema getCustomTamlSchema(void) = 0;
  312. /// Return the size of instances of this class in bytes.
  313. S32 getSizeof() const { return mClassSizeof; }
  314. /// Return the next class in the global class list link chain.
  315. AbstractClassRep* getNextClass() const { return nextClass; }
  316. /// Return the head of the global class list.
  317. static AbstractClassRep* getClassList() { return classLinkList; }
  318. /// Helper class to see if we are a given class, or a subclass thereof by
  319. /// comparing AbstractClassRep pointers.
  320. bool isSubclassOf( const AbstractClassRep* klass ) const
  321. {
  322. const AbstractClassRep *walk = this;
  323. // Walk up parents, checking for equivalence.
  324. while ( walk )
  325. {
  326. if ( walk == klass )
  327. return true;
  328. walk = walk->parentClass;
  329. };
  330. return false;
  331. }
  332. /// Helper class to see if we are a given class, or a subclass thereof by
  333. /// comparing the class name strings.
  334. bool isSubclassOf( const char *klass ) const
  335. {
  336. klass = StringTable->insert( klass );
  337. // Walk up parents, checking for equivalence.
  338. const AbstractClassRep *walk = this;
  339. while ( walk )
  340. {
  341. if ( walk->mClassName == klass )
  342. return true;
  343. walk = walk->parentClass;
  344. };
  345. return false;
  346. }
  347. /// @deprecated Use isSubclassOf.
  348. bool isClass( const AbstractClassRep* acr ) const
  349. {
  350. return isSubclassOf( acr );
  351. }
  352. virtual ConsoleObject* create () const = 0;
  353. AbstractClassRep* findFieldRoot(StringTableEntry fieldName);
  354. protected:
  355. virtual void init();
  356. const char * mClassName;
  357. AbstractClassRep * nextClass;
  358. AbstractClassRep * parentClass;
  359. Namespace * mNamespace;
  360. /// @}
  361. public:
  362. bool mIsRenderEnabled;
  363. bool mIsSelectionEnabled;
  364. bool isRenderEnabled() const { return mIsRenderEnabled; }
  365. bool isSelectionEnabled() const { return mIsSelectionEnabled; }
  366. /// @name Categories
  367. /// @{
  368. protected:
  369. const char* mCategory;
  370. const char* mDescription;
  371. public:
  372. /// Return the space separated category path for the class.
  373. const char* getCategory() const { return mCategory; }
  374. /// Return a short description string suitable for displaying in tooltips.
  375. const char* getDescription() const { return mDescription; }
  376. /// @}
  377. /// @name Fields
  378. /// @{
  379. public:
  380. /// This is a function pointer typedef to support get/set callbacks for fields
  381. typedef bool (*SetDataNotify)( void *obj, const char *array, const char *data );
  382. typedef const char *(*GetDataNotify)( void *obj, const char *data );
  383. /// This is a function pointer typedef to support optional writing for fields.
  384. typedef bool(*WriteDataNotify)(void* obj, StringTableEntry pFieldName);
  385. /// These are special field type values used to mark
  386. /// groups and arrays in the field list.
  387. /// @see Field::type
  388. /// @see addArray, endArray
  389. /// @see addGroup, endGroup
  390. /// @see addGroup, endGroup
  391. /// @see addDeprecatedField
  392. enum ACRFieldTypes : U32
  393. {
  394. /// The first custom field type... all fields
  395. /// types greater or equal to this one are not
  396. /// console data types.
  397. ARCFirstCustomField = 0xFFFFFFFB,
  398. /// Marks the start of a fixed size array of fields.
  399. /// @see addArray
  400. StartArrayFieldType = 0xFFFFFFFB,
  401. /// Marks the end of a fixed size array of fields.
  402. /// @see endArray
  403. EndArrayFieldType = 0xFFFFFFFC,
  404. /// Marks the beginning of a group of fields.
  405. /// @see addGroup
  406. StartGroupFieldType = 0xFFFFFFFD,
  407. /// Marks the beginning of a group of fields.
  408. /// @see endGroup
  409. EndGroupFieldType = 0xFFFFFFFE,
  410. /// Marks a field that is depreciated and no
  411. /// longer stores a value.
  412. /// @see addDeprecatedField
  413. DeprecatedFieldType = 0xFFFFFFFF
  414. };
  415. enum FieldFlags
  416. {
  417. FIELD_HideInInspectors = BIT( 0 ), ///< Do not show the field in inspectors.
  418. FIELD_ComponentInspectors = BIT(1), ///< Custom fields used by components. They are likely to be non-standard size/configuration, so
  419. ///< They are handled specially
  420. FIELD_CustomInspectors = BIT(2), ///< Display as a button in inspectors.
  421. FIELD_SpecialtyArrayField = BIT(3)
  422. };
  423. struct Field
  424. {
  425. Field()
  426. : pFieldname( NULL ),
  427. pGroupname( NULL ),
  428. pFieldDocs( NULL ),
  429. groupExpand( false ),
  430. type( 0 ),
  431. offset( 0 ),
  432. elementCount( 0 ),
  433. table( NULL ),
  434. validator( NULL ),
  435. setDataFn( NULL ),
  436. getDataFn( NULL ),
  437. writeDataFn(NULL),
  438. networkMask(0)
  439. {
  440. doNotSubstitute = keepClearSubsOnly = false;
  441. }
  442. StringTableEntry pFieldname; ///< Name of the field.
  443. const char* pGroupname; ///< Optionally filled field containing the group name.
  444. ///
  445. /// This is filled when type is StartField or EndField
  446. const char* pFieldDocs; ///< Documentation about this field; see consoleDoc.cc.
  447. bool groupExpand; ///< Flag to track expanded/not state of this group in the editor.
  448. U32 type; ///< A data type ID or one of the special custom fields. @see ACRFieldTypes
  449. U32 offset; ///< Memory offset from beginning of class for this field.
  450. S32 elementCount; ///< Number of elements, if this is an array.
  451. const EnumTable * table; ///< If this is an enum, this points to the table defining it.
  452. BitSet32 flag; ///< Stores various flags
  453. TypeValidator *validator; ///< Validator, if any.
  454. SetDataNotify setDataFn; ///< Set data notify Fn
  455. GetDataNotify getDataFn; ///< Get data notify Fn
  456. WriteDataNotify writeDataFn; ///< Function to determine whether data should be written or not.
  457. bool doNotSubstitute;
  458. bool keepClearSubsOnly;
  459. U32 networkMask;
  460. };
  461. typedef Vector<Field> FieldList;
  462. FieldList mFieldList;
  463. bool mDynamicGroupExpand;
  464. const Field* findField( StringTableEntry fieldName ) const;
  465. /// @}
  466. /// @name Console Type Interface
  467. /// @{
  468. void* getNativeVariable() override { return new ( AbstractClassRep* ); } // Any pointer-sized allocation will do.
  469. void deleteNativeVariable( void* var ) override { delete reinterpret_cast< AbstractClassRep** >( var ); }
  470. /// @}
  471. /// @name Abstract Class Database
  472. /// @{
  473. protected:
  474. static AbstractClassRep ** classTable[NetClassGroupsCount][NetClassTypesCount];
  475. static AbstractClassRep * classLinkList;
  476. static U32 classCRC[NetClassGroupsCount];
  477. static bool initialized;
  478. static ConsoleObject* create(const char* in_pClassName);
  479. static ConsoleObject* create(const U32 groupId, const U32 typeId, const U32 in_classId);
  480. public:
  481. static U32 NetClassCount [NetClassGroupsCount][NetClassTypesCount];
  482. static U32 NetClassBitSize[NetClassGroupsCount][NetClassTypesCount];
  483. static void registerClassRep(AbstractClassRep*);
  484. static AbstractClassRep* findClassRep(const char* in_pClassName);
  485. static AbstractClassRep* findClassRep( U32 groupId, U32 typeId, U32 classId );
  486. static void removeClassRep(AbstractClassRep*); // This should not be used lightly
  487. static void initialize(); // Called from Con::init once on startup
  488. static void shutdown();
  489. /// @}
  490. };
  491. extern AbstractClassRep::FieldList sg_tempFieldList;
  492. //=============================================================================
  493. // ConcreteClassRep.
  494. //=============================================================================
  495. /// Helper class for AbstractClassRep.
  496. ///
  497. /// @see AbtractClassRep
  498. /// @see ConsoleObject
  499. template< class T >
  500. class ConcreteAbstractClassRep : public AbstractClassRep
  501. {
  502. public:
  503. AbstractClassRep* getContainerChildClass(const bool recurse) override
  504. {
  505. // Fetch container children type.
  506. AbstractClassRep* pChildren = T::getContainerChildStaticClassRep();
  507. if (!recurse || pChildren != NULL)
  508. return pChildren;
  509. // Fetch parent type.
  510. AbstractClassRep* pParent = T::getParentStaticClassRep();
  511. if (pParent == NULL)
  512. return NULL;
  513. // Get parent container children.
  514. return pParent->getContainerChildClass(recurse);
  515. }
  516. WriteCustomTamlSchema getCustomTamlSchema(void) override
  517. {
  518. return T::getStaticWriteCustomTamlSchema();
  519. }
  520. static EnginePropertyTable _smPropertyTable;
  521. static EnginePropertyTable& smPropertyTable;
  522. ConcreteAbstractClassRep(const char* name,
  523. const char* conTypeName,
  524. S32* conTypeIdPtr,
  525. S32 netClassGroupMask,
  526. S32 netClassType,
  527. S32 netEventDir,
  528. AbstractClassRep* parent,
  529. const char* (*parentDesc)())
  530. : AbstractClassRep(conTypeIdPtr, conTypeName)
  531. {
  532. mClassName = StringTable->insert(name);
  533. mCategory = T::__category();
  534. mTypeInfo = _MAPTYPE< T >();
  535. if (mTypeInfo)
  536. const_cast< EngineTypeInfo* >(mTypeInfo)->mPropertyTable = &smPropertyTable;
  537. if (&T::__description != parentDesc)
  538. mDescription = T::__description();
  539. // Clean up mClassId
  540. for (U32 i = 0; i < NetClassGroupsCount; i++)
  541. mClassId[i] = -1;
  542. // Set properties for this ACR
  543. mClassType = netClassType;
  544. mClassGroupMask = netClassGroupMask;
  545. mNetEventDir = netEventDir;
  546. parentClass = parent;
  547. mClassSizeof = sizeof(T);
  548. // Finally, register ourselves.
  549. registerClassRep(this);
  550. };
  551. /// Wrap constructor.
  552. ConsoleObject* create() const override { return NULL; }
  553. /// Perform class specific initialization tasks.
  554. ///
  555. /// Link namespaces, call initPersistFields() and consoleInit().
  556. void init() override
  557. {
  558. // Get handle to our parent class, if any, and ourselves (we are our parent's child).
  559. AbstractClassRep *parent = T::getParentStaticClassRep();
  560. AbstractClassRep *child = T::getStaticClassRep();
  561. // If we got reps, then link those namespaces! (To get proper inheritance.)
  562. if (parent && child)
  563. Con::classLinkNamespaces(parent->getNameSpace(), child->getNameSpace());
  564. // Finally, do any class specific initialization...
  565. T::initPersistFields();
  566. T::consoleInit();
  567. EnginePropertyTable::Property* props = new EnginePropertyTable::Property[sg_tempFieldList.size() + 1];
  568. for (int i = 0; i < sg_tempFieldList.size(); ++i)
  569. {
  570. EnginePropertyTable::Property prop;
  571. prop.mDocString = sg_tempFieldList[i].pFieldDocs;
  572. prop.mName = sg_tempFieldList[i].pFieldname;
  573. prop.mNumElements = sg_tempFieldList[i].elementCount;
  574. prop.mFlags = 0;
  575. if (sg_tempFieldList[i].type == StartGroupFieldType)
  576. prop.mFlags |= EnginePropertyGroupBegin;
  577. if (sg_tempFieldList[i].type == EndGroupFieldType)
  578. prop.mFlags |= EnginePropertyGroupEnd;
  579. prop.mType = sg_tempFieldList[i].type;
  580. props[i] = prop;
  581. }
  582. _smPropertyTable = EnginePropertyTable(sg_tempFieldList.size(), props);
  583. smPropertyTable = _smPropertyTable;
  584. const_cast<EngineTypeInfo*>(mTypeInfo)->mPropertyTable = &_smPropertyTable;
  585. // After we hand it off, immediately delete if safe:
  586. delete[] props;
  587. // Let the base finish up.
  588. AbstractClassRep::init();
  589. }
  590. /// @name Console Type Interface
  591. /// @{
  592. void setData(void* dptr, S32 argc, const char** argv, const EnumTable* tbl, BitSet32 flag) override
  593. {
  594. if (argc == 1)
  595. {
  596. T** obj = (T**)dptr;
  597. *obj = dynamic_cast< T* >(T::__findObject(argv[0]));
  598. }
  599. else
  600. Con::errorf("Cannot set multiple args to a single ConsoleObject*.");
  601. }
  602. const char* getData(void* dptr, const EnumTable* tbl, BitSet32 flag) override
  603. {
  604. T** obj = (T**)dptr;
  605. return Con::getReturnBuffer(T::__getObjectId(*obj));
  606. }
  607. const char* getTypeClassName() override { return mClassName; }
  608. const bool isDatablock() override { return T::__smIsDatablock; };
  609. /// @}
  610. };
  611. template< class T >
  612. class ConcreteClassRep : public ConcreteAbstractClassRep<T>
  613. {
  614. public:
  615. ConcreteClassRep(const char* name,
  616. const char* conTypeName,
  617. S32* conTypeIdPtr,
  618. S32 netClassGroupMask,
  619. S32 netClassType,
  620. S32 netEventDir,
  621. AbstractClassRep* parent,
  622. const char* (*parentDesc)())
  623. : ConcreteAbstractClassRep<T>(name, conTypeName, conTypeIdPtr, netClassGroupMask, netClassType, netEventDir, parent, parentDesc)
  624. {
  625. }
  626. /// Wrap constructor.
  627. ConsoleObject* create() const override { return new T; }
  628. };
  629. template< typename T > EnginePropertyTable ConcreteAbstractClassRep< T >::_smPropertyTable(0, NULL);
  630. template< typename T > EnginePropertyTable& ConcreteAbstractClassRep< T >::smPropertyTable = ConcreteAbstractClassRep< T >::_smPropertyTable;
  631. //------------------------------------------------------------------------------
  632. // Forward declaration of this function so it can be used in the class
  633. const char *defaultProtectedGetFn( void *obj, const char *data );
  634. bool defaultProtectedWriteFn(void* obj, StringTableEntry pFieldName);
  635. //=============================================================================
  636. // ConsoleObject.
  637. //=============================================================================
  638. /// Interface class to the console.
  639. ///
  640. /// @section ConsoleObject_basics The Basics
  641. ///
  642. /// Any object which you want to work with the console system should derive from this,
  643. /// and access functionality through the static interface.
  644. ///
  645. /// This class is always used with the DECLARE_CONOBJECT and IMPLEMENT_* macros.
  646. ///
  647. /// @code
  648. /// // A very basic example object. It will do nothing!
  649. /// class TorqueObject : public ConsoleObject {
  650. /// // Must provide a Parent typedef so the console system knows what we inherit from.
  651. /// typedef ConsoleObject Parent;
  652. ///
  653. /// // This does a lot of menial declaration for you.
  654. /// DECLARE_CONOBJECT(TorqueObject);
  655. ///
  656. /// // This is for us to register our fields in.
  657. /// static void initPersistFields();
  658. ///
  659. /// // A sample field.
  660. /// S8 mSample;
  661. /// }
  662. /// @endcode
  663. ///
  664. /// @code
  665. /// // And the accordant implementation...
  666. /// IMPLEMENT_CONOBJECT(TorqueObject);
  667. ///
  668. /// void TorqueObject::initPersistFields()
  669. /// {
  670. /// // If you want to inherit any fields from the parent (you do), do this:
  671. /// Parent::initPersistFields();
  672. ///
  673. /// // Pass the field, the type, the offset, and a usage string.
  674. /// addField("sample", TypeS8, Offset(mSample, TorqueObject), "A test field.");
  675. /// }
  676. /// @endcode
  677. ///
  678. /// That's all you need to do to get a class registered with the console system. At this point,
  679. /// you can instantiate it via script, tie methods to it using ConsoleMethod, register fields,
  680. /// and so forth. You can also register any global variables related to the class by creating
  681. /// a consoleInit() method.
  682. ///
  683. /// You will need to use different IMPLEMENT_ macros in different cases; for instance, if you
  684. /// are making a NetObject (for ghosting), a DataBlock, or a NetEvent.
  685. ///
  686. /// @see AbstractClassRep for gory implementation details.
  687. /// @nosubgrouping
  688. class ConsoleObject : public EngineObject
  689. {
  690. DECLARE_ABSTRACT_CLASS( ConsoleObject, EngineObject );
  691. protected:
  692. /// @deprecated This is disallowed.
  693. ConsoleObject(const ConsoleObject&) { mDocsClick = false; };
  694. public:
  695. /// <summary>
  696. /// Only used for interfacing with the editor's inspector docsURL button
  697. /// </summary>
  698. bool mDocsClick;
  699. ConsoleObject() { mDocsClick = false; }
  700. /// Get a reference to a field by name.
  701. const AbstractClassRep::Field *findField(StringTableEntry fieldName) const;
  702. /// Gets the ClassRep.
  703. virtual AbstractClassRep* getClassRep() const;
  704. #define DECLARE_ABSTRACT_CONOBJECT( className ) \
  705. DECLARE_ABSTRACT_CLASS( className, Parent ); \
  706. static S32 _smTypeId; \
  707. static ConcreteAbstractClassRep< className > dynClassRep; \
  708. static AbstractClassRep* getParentStaticClassRep(); \
  709. static AbstractClassRep* getStaticClassRep(); \
  710. static SimObjectRefConsoleBaseType< className > ptrRefType; \
  711. virtual AbstractClassRep* getClassRep() const
  712. /// Set the value of a field.
  713. bool setField(const char *fieldName, const char *value);
  714. public:
  715. /// @name Object Creation
  716. /// @{
  717. static ConsoleObject* create(const char* in_pClassName);
  718. static ConsoleObject* create(const U32 groupId, const U32 typeId, const U32 in_classId);
  719. /// @}
  720. public:
  721. /// Get the classname from a class tag.
  722. static const char* lookupClassName(const U32 in_classTag) { return ""; };
  723. /// @name Fields
  724. /// @{
  725. /// Mark the beginning of a group of fields.
  726. ///
  727. /// This is used in the consoleDoc system.
  728. /// @see console_autodoc
  729. static void addGroup(const char* in_pGroupname, const char* in_pGroupDocs = NULL);
  730. /// Mark the end of a group of fields.
  731. ///
  732. /// This is used in the consoleDoc system.
  733. /// @see console_autodoc
  734. static void endGroup(const char* in_pGroupname);
  735. /// Marks the start of a fixed size array of fields.
  736. /// @see console_autodoc
  737. static void addArray( const char *arrayName, S32 count );
  738. /// Marks the end of an array of fields.
  739. /// @see console_autodoc
  740. static void endArray( const char *arrayName );
  741. /// Register a complex field.
  742. ///
  743. /// @param in_pFieldname Name of the field.
  744. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  745. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  746. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  747. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  748. static void addField(const char* in_pFieldname,
  749. const U32 in_fieldType,
  750. const dsize_t in_fieldOffset,
  751. const U32 in_elementCount = 1,
  752. const char* in_pFieldDocs = NULL,
  753. U32 flags = 0 );
  754. static void addField(const char* in_pFieldname,
  755. const U32 in_fieldType,
  756. const dsize_t in_fieldOffset,
  757. AbstractClassRep::WriteDataNotify in_writeDataFn,
  758. const U32 in_elementCount = 1,
  759. const char* in_pFieldDocs = NULL,
  760. U32 flags = 0);
  761. /// Register a simple field.
  762. ///
  763. /// @param in_pFieldname Name of the field.
  764. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  765. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  766. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  767. static void addField(const char* in_pFieldname,
  768. const U32 in_fieldType,
  769. const dsize_t in_fieldOffset,
  770. const char* in_pFieldDocs,
  771. U32 flags = 0 );
  772. static void addField(const char* in_pFieldname,
  773. const U32 in_fieldType,
  774. const dsize_t in_fieldOffset,
  775. AbstractClassRep::WriteDataNotify in_writeDataFn,
  776. const char* in_pFieldDocs,
  777. U32 flags = 0);
  778. /// Register a validated field.
  779. ///
  780. /// A validated field is just like a normal field except that you can't
  781. /// have it be an array, and that you give it a pointer to a TypeValidator
  782. /// subclass, which is then used to validate any value placed in it. Invalid
  783. /// values are ignored and an error is printed to the console.
  784. ///
  785. /// @see addField
  786. /// @see typeValidators.h
  787. static void addFieldV(const char* in_pFieldname,
  788. const U32 in_fieldType,
  789. const dsize_t in_fieldOffset,
  790. TypeValidator *v,
  791. const char * in_pFieldDocs = NULL);
  792. static void addFieldV(const char* in_pFieldname,
  793. const U32 in_fieldType,
  794. const dsize_t in_fieldOffset,
  795. TypeValidator* v,
  796. const U32 in_elementCount,
  797. const char* in_pFieldDocs = NULL);
  798. /// Register a complex protected field.
  799. ///
  800. /// @param in_pFieldname Name of the field.
  801. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  802. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  803. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  804. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  805. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  806. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  807. static void addProtectedField(const char* in_pFieldname,
  808. const U32 in_fieldType,
  809. const dsize_t in_fieldOffset,
  810. AbstractClassRep::SetDataNotify in_setDataFn,
  811. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  812. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  813. const U32 in_elementCount = 1,
  814. const char* in_pFieldDocs = NULL,
  815. U32 flags = 0);
  816. static void addProtectedField(const char* in_pFieldname,
  817. const U32 in_fieldType,
  818. const dsize_t in_fieldOffset,
  819. AbstractClassRep::SetDataNotify in_setDataFn,
  820. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  821. const U32 in_elementCount = 1,
  822. const char* in_pFieldDocs = NULL,
  823. U32 flags = 0);
  824. /// Register a simple protected field.
  825. ///
  826. /// @param in_pFieldname Name of the field.
  827. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  828. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  829. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  830. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  831. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  832. static void addProtectedField(const char* in_pFieldname,
  833. const U32 in_fieldType,
  834. const dsize_t in_fieldOffset,
  835. AbstractClassRep::SetDataNotify in_setDataFn,
  836. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  837. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  838. const char* in_pFieldDocs = NULL,
  839. U32 flags = 0);
  840. static void addProtectedField(const char* in_pFieldname,
  841. const U32 in_fieldType,
  842. const dsize_t in_fieldOffset,
  843. AbstractClassRep::SetDataNotify in_setDataFn,
  844. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  845. const char* in_pFieldDocs = NULL,
  846. U32 flags = 0);
  847. static void addProtectedFieldV(const char* in_pFieldname,
  848. const U32 in_fieldType,
  849. const dsize_t in_fieldOffset,
  850. AbstractClassRep::SetDataNotify in_setDataFn,
  851. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  852. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  853. TypeValidator* v = NULL,
  854. const U32 in_elementCount = 1,
  855. const char* in_pFieldDocs = NULL,
  856. U32 flags = 0);
  857. static void addProtectedFieldV(const char* in_pFieldname,
  858. const U32 in_fieldType,
  859. const dsize_t in_fieldOffset,
  860. AbstractClassRep::SetDataNotify in_setDataFn,
  861. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  862. TypeValidator* v = NULL,
  863. const U32 in_elementCount = 1,
  864. const char* in_pFieldDocs = NULL,
  865. U32 flags = 0);
  866. static void addProtectedFieldV(const char* in_pFieldname,
  867. const U32 in_fieldType,
  868. const dsize_t in_fieldOffset,
  869. AbstractClassRep::SetDataNotify in_setDataFn,
  870. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  871. TypeValidator* v = NULL,
  872. const char* in_pFieldDocs = NULL,
  873. U32 flags = 0);
  874. /// Add a deprecated field.
  875. ///
  876. /// A deprecated field will always be undefined, even if you assign a value to it. This
  877. /// is useful when you need to make sure that a field is not being used anymore.
  878. static void addDeprecatedField(const char *fieldName);
  879. /// Remove a field.
  880. ///
  881. /// Sometimes, you just have to remove a field!
  882. /// @returns True on success.
  883. static bool removeField(const char* in_pFieldname);
  884. /// @}
  885. /// @name Logging
  886. /// @{
  887. /// Overload this in subclasses to change the message formatting.
  888. /// @param fmt A printf style format string.
  889. /// @param args A va_list containing the args passed ot a log function.
  890. /// @note It is suggested that you use String::VToString.
  891. virtual String _getLogMessage(const char* fmt, va_list args) const;
  892. /// @}
  893. public:
  894. /// @name Logging
  895. /// These functions will try to print out a message along the lines
  896. /// of "ObjectClass - ObjectName(ObjectId) - formatted message"
  897. /// @{
  898. /// Logs with Con::printf.
  899. void logMessage(const char* fmt, ...) const;
  900. /// Logs with Con::warnf.
  901. void logWarning(const char* fmt, ...) const;
  902. /// Logs with Con::errorf.
  903. void logError(const char* fmt, ...) const;
  904. /// @}
  905. /// Register dynamic fields in a subclass of ConsoleObject.
  906. ///
  907. /// @see addField(), addFieldV(), addDeprecatedField(), addGroup(), endGroup()
  908. static void initPersistFields();
  909. /// Register global constant variables and do other one-time initialization tasks in
  910. /// a subclass of ConsoleObject.
  911. ///
  912. /// @deprecated You should use ConsoleMethod and ConsoleFunction, not this, to
  913. /// register methods or commands.
  914. /// @see console
  915. static void consoleInit();
  916. /// @name Field List
  917. /// @{
  918. /// Get a list of all the fields. This information cannot be modified.
  919. const AbstractClassRep::FieldList& getFieldList() const;
  920. /// Get a list of all the fields, set up so we can modify them.
  921. ///
  922. /// @note This is a bad trick to pull if you aren't very careful,
  923. /// since you can blast field data!
  924. AbstractClassRep::FieldList& getModifiableFieldList();
  925. /// Get a handle to a boolean telling us if we expanded the dynamic group.
  926. ///
  927. /// @see GuiInspector::Inspect()
  928. bool& getDynamicGroupExpand();
  929. /// @}
  930. /// @name ConsoleObject Implementation
  931. ///
  932. /// These functions are implemented in every subclass of
  933. /// ConsoleObject by an IMPLEMENT_CONOBJECT or IMPLEMENT_CO_* macro.
  934. /// @{
  935. /// Get the abstract class information for this class.
  936. static AbstractClassRep *getStaticClassRep() { return NULL; }
  937. /// Get the abstract class information for this class's superclass.
  938. static AbstractClassRep *getParentStaticClassRep() { return NULL; }
  939. /// Get our network-layer class id.
  940. ///
  941. /// @param netClassGroup The net class for which we want our ID.
  942. /// @see
  943. S32 getClassId(U32 netClassGroup) const;
  944. /// Get our compiler and platform independent class name.
  945. ///
  946. /// @note This name can be used to instantiate another instance using create()
  947. StringTableEntry getClassName() const;
  948. /// @}
  949. static const char* __category() { return ""; }
  950. static const char* __description() { return ""; }
  951. /// Subclasses of ConsoleObjects that are datablocks should redefine this static member variable
  952. /// and set it to true.
  953. static const bool __smIsDatablock = false;
  954. /// @name Object IDs and lookup.
  955. /// For a subclass hierarchy based on ConsoleObject to become functional for use as a console object type,
  956. /// the hierarchy must implement a naming scheme and indexing function for looking up objects by name.
  957. /// @{
  958. static ConsoleObject* __findObject( const char* ) { return NULL; }
  959. static const char* __getObjectId( ConsoleObject* ) { return ""; }
  960. protected:
  961. static bool disableFieldSubstitutions(const char* in_pFieldname);
  962. static bool onlyKeepClearSubstitutions(const char* in_pFieldname);
  963. };
  964. #define addNamedField(fieldName,type,className) addField(#fieldName, type, Offset(fieldName,className))
  965. #define addNamedFieldV(fieldName,type,className, validator) addFieldV(#fieldName, type, Offset(fieldName,className), validator)
  966. //------------------------------------------------------------------------------
  967. //-------------------------------------- Inlines
  968. //
  969. inline S32 ConsoleObject::getClassId(U32 netClassGroup) const
  970. {
  971. AssertFatal(getClassRep() != NULL,"Cannot get tag from non-declared dynamic class!");
  972. return getClassRep()->getClassId(netClassGroup);
  973. }
  974. inline StringTableEntry ConsoleObject::getClassName() const
  975. {
  976. AssertFatal(getClassRep() != NULL,
  977. "Cannot get tag from non-declared dynamic class");
  978. return getClassRep()->getClassName();
  979. }
  980. inline const AbstractClassRep::Field * ConsoleObject::findField(StringTableEntry name) const
  981. {
  982. AssertFatal(getClassRep() != NULL,
  983. avar("Cannot get field '%s' from non-declared dynamic class.", name));
  984. return getClassRep()->findField(name);
  985. }
  986. inline bool ConsoleObject::setField(const char *fieldName, const char *value)
  987. {
  988. //sanity check
  989. if ((! fieldName) || (! fieldName[0]) || (! value))
  990. return false;
  991. if (! getClassRep())
  992. return false;
  993. const AbstractClassRep::Field *myField = getClassRep()->findField(StringTable->insert(fieldName));
  994. if (! myField)
  995. return false;
  996. Con::setData(
  997. myField->type,
  998. (void *) (((const char *)(this)) + myField->offset),
  999. 0,
  1000. 1,
  1001. &value,
  1002. myField->table,
  1003. myField->flag);
  1004. return true;
  1005. }
  1006. inline ConsoleObject* ConsoleObject::create(const char* in_pClassName)
  1007. {
  1008. return AbstractClassRep::create(in_pClassName);
  1009. }
  1010. inline ConsoleObject* ConsoleObject::create(const U32 groupId, const U32 typeId, const U32 in_classId)
  1011. {
  1012. return AbstractClassRep::create(groupId, typeId, in_classId);
  1013. }
  1014. inline const AbstractClassRep::FieldList& ConsoleObject::getFieldList() const
  1015. {
  1016. return getClassRep()->mFieldList;
  1017. }
  1018. inline AbstractClassRep::FieldList& ConsoleObject::getModifiableFieldList()
  1019. {
  1020. return getClassRep()->mFieldList;
  1021. }
  1022. inline bool& ConsoleObject::getDynamicGroupExpand()
  1023. {
  1024. return getClassRep()->mDynamicGroupExpand;
  1025. }
  1026. /// @name ConsoleObject Macros
  1027. /// @{
  1028. #define DECLARE_CONOBJECT( className ) \
  1029. DECLARE_CLASS( className, Parent ); \
  1030. static S32 _smTypeId; \
  1031. static ConcreteClassRep< className > dynClassRep; \
  1032. static AbstractClassRep* getParentStaticClassRep(); \
  1033. static AbstractClassRep* getStaticClassRep(); \
  1034. static SimObjectRefConsoleBaseType< className > ptrRefType; \
  1035. static AbstractClassRep::WriteCustomTamlSchema getStaticWriteCustomTamlSchema(); \
  1036. static AbstractClassRep* getContainerChildStaticClassRep(); \
  1037. AbstractClassRep* getClassRep() const override
  1038. #define DECLARE_CATEGORY( string ) \
  1039. static const char* __category() { return string; }
  1040. #define DECLARE_DESCRIPTION( string ) \
  1041. static const char* __description() { return string; }
  1042. #define IMPLEMENT_CONOBJECT( className ) \
  1043. IMPLEMENT_CLASS( className, NULL ) \
  1044. END_IMPLEMENT_CLASS; \
  1045. S32 className::_smTypeId; \
  1046. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1047. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1048. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1049. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1050. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1051. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1052. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1053. #define IMPLEMENT_CONOBJECT_CHILDREN( className ) \
  1054. IMPLEMENT_CLASS( className, NULL ) \
  1055. END_IMPLEMENT_CLASS; \
  1056. S32 className::_smTypeId; \
  1057. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1058. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1059. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1060. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1061. AbstractClassRep* className::getContainerChildStaticClassRep() { return Children::getStaticClassRep(); } \
  1062. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1063. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1064. #define IMPLEMENT_CONOBJECT_SCHEMA( className, schema ) \
  1065. IMPLEMENT_CLASS( className, NULL ) \
  1066. END_IMPLEMENT_CLASS; \
  1067. S32 className::_smTypeId; \
  1068. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1069. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1070. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1071. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1072. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1073. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return schema; } \
  1074. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1075. #define IMPLEMENT_CONOBJECT_CHILDREN_SCHEMA( className, schema ) \
  1076. IMPLEMENT_CLASS( className, NULL ) \
  1077. END_IMPLEMENT_CLASS; \
  1078. S32 className::_smTypeId; \
  1079. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1080. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1081. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1082. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1083. AbstractClassRep* className::getContainerChildStaticClassRep() { return Children::getStaticClassRep(); } \
  1084. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return schema; } \
  1085. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1086. #define IMPLEMENT_ABSTRACT_CONOBJECT( className ) \
  1087. IMPLEMENT_NONINSTANTIABLE_CLASS( className, NULL ) \
  1088. END_IMPLEMENT_CLASS; \
  1089. S32 className::_smTypeId; \
  1090. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1091. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1092. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1093. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1094. ConcreteAbstractClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1095. #define IMPLEMENT_CO_NETOBJECT_V1( className ) \
  1096. IMPLEMENT_CLASS( className, NULL ) \
  1097. END_IMPLEMENT_CLASS; \
  1098. S32 className::_smTypeId; \
  1099. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1100. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1101. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1102. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1103. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1104. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1105. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, NetClassGroupGameMask, NetClassTypeObject, 0, className::getParentStaticClassRep(), &Parent::__description )
  1106. #define IMPLEMENT_CO_DATABLOCK_V1( className ) \
  1107. IMPLEMENT_CLASS( className, NULL ) \
  1108. END_IMPLEMENT_CLASS; \
  1109. S32 className::_smTypeId; \
  1110. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1111. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1112. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1113. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1114. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1115. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1116. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId, NetClassGroupGameMask, NetClassTypeDataBlock, 0, className::getParentStaticClassRep(), &Parent::__description )
  1117. // Support for adding properties to classes CONOBJECT style.
  1118. #define PROPERTY_TABLE( className ) \
  1119. namespace { namespace _ ## className { \
  1120. extern EnginePropertyTable _propTable; \
  1121. } } \
  1122. template<> EnginePropertyTable& \
  1123. ConcreteClassRep< className >::smPropertyTable = _ ## className::_propTable; \
  1124. namespace { namespace _ ## className { \
  1125. EnginePropertyTable::Property _props[] = {
  1126. #define END_PROPERTY_TABLE \
  1127. { NULL } \
  1128. }; \
  1129. EnginePropertyTable _propTable( sizeof( _props ) / sizeof( _props[ 0 ] ) - 1, _props ); \
  1130. } }
  1131. /// @}
  1132. //------------------------------------------------------------------------------
  1133. // Protected field default get/set functions
  1134. //
  1135. // The reason for these functions is that it will save one branch per console
  1136. // data request and script functions will still execute at the same speed as
  1137. // before the modifications to allow protected static fields. These will just
  1138. // inline and the code should be roughly the same size, and just as fast as
  1139. // before the modifications. -pw
  1140. inline bool defaultProtectedSetFn( void *object, const char *index, const char *data )
  1141. {
  1142. return true;
  1143. }
  1144. inline bool defaultProtectedSetNotEmptyFn( void *object, const char *index, const char *data )
  1145. {
  1146. return data && data[0];
  1147. }
  1148. inline const char *defaultProtectedGetFn( void *obj, const char *data )
  1149. {
  1150. return data;
  1151. }
  1152. inline const char *emptyStringProtectedGetFn( void *obj, const char *data )
  1153. {
  1154. return "";
  1155. }
  1156. inline bool defaultProtectedWriteFn(void* obj, StringTableEntry pFieldName)
  1157. {
  1158. return true;
  1159. }
  1160. inline bool defaultProtectedNotSetFn(void* obj, const char *array, const char* data)
  1161. {
  1162. return false;
  1163. }
  1164. inline bool defaultProtectedNotWriteFn(void* obj, StringTableEntry pFieldName)
  1165. {
  1166. return false;
  1167. }
  1168. /// @}
  1169. #endif //_CONSOLEOBJECT_H_