consoleObject.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. #ifndef _CONSOLEOBJECT_H_
  23. #define _CONSOLEOBJECT_H_
  24. #ifndef _STRINGTABLE_H_
  25. #include "string/stringTable.h"
  26. #endif
  27. #ifndef _PLATFORM_H_
  28. #include "platform/platform.h"
  29. #endif
  30. #ifndef _VECTOR_H_
  31. #include "collection/vector.h"
  32. #endif
  33. #ifndef _BITSET_H_
  34. #include "collection/bitSet.h"
  35. #endif
  36. #ifndef _CONSOLE_H_
  37. #include "console/console.h"
  38. #endif
  39. class Namespace;
  40. class ConsoleObject;
  41. enum NetClassTypes {
  42. NetClassTypeObject = 0,
  43. NetClassTypeDataBlock,
  44. NetClassTypeEvent,
  45. NetClassTypesCount,
  46. };
  47. enum NetClassGroups {
  48. NetClassGroupGame = 0,
  49. NetClassGroupCommunity,
  50. NetClassGroup3,
  51. NetClassGroup4,
  52. NetClassGroupsCount,
  53. };
  54. enum NetClassMasks {
  55. NetClassGroupGameMask = BIT(NetClassGroupGame),
  56. NetClassGroupCommunityMask = BIT(NetClassGroupCommunity),
  57. };
  58. enum NetDirection
  59. {
  60. NetEventDirAny,
  61. NetEventDirServerToClient,
  62. NetEventDirClientToServer,
  63. };
  64. class SimObject;
  65. class ConsoleTypeValidator;
  66. /// Core functionality for class manipulation.
  67. ///
  68. /// @section AbstractClassRep_intro Introduction (or, Why AbstractClassRep?)
  69. ///
  70. /// Many of Torque's subsystems, especially network, console, and sim,
  71. /// require the ability to programatically instantiate classes. For instance,
  72. /// when objects are ghosted, the networking layer needs to be able to create
  73. /// an instance of the object on the client. When the console scripting
  74. /// language runtime encounters the "new" keyword, it has to be able to fill
  75. /// that request.
  76. ///
  77. /// Since standard C++ doesn't provide a function to create a new instance of
  78. /// an arbitrary class at runtime, one must be created. This is what
  79. /// AbstractClassRep and ConcreteClassRep are all about. They allow the registration
  80. /// and instantiation of arbitrary classes at runtime.
  81. ///
  82. /// In addition, ACR keeps track of the fields (registered via addField() and co.) of
  83. /// a class, allowing programmatic access of class fields.
  84. ///
  85. /// @see ConsoleObject
  86. ///
  87. /// @note In general, you will only access the functionality implemented in this class via
  88. /// ConsoleObject::create(). Most of the time, you will only ever need to use this part
  89. /// part of the engine indirectly - ie, you will use the networking system or the console,
  90. /// or ConsoleObject, and they will indirectly use this code. <b>The following discussion
  91. /// is really only relevant for advanced engine users.</b>
  92. ///
  93. /// @section AbstractClassRep_netstuff NetClasses and Class IDs
  94. ///
  95. /// Torque supports a notion of group, type, and direction for objects passed over
  96. /// the network. Class IDs are assigned sequentially per-group, per-type, so that, for instance,
  97. /// the IDs assigned to Datablocks are seperate from the IDs assigned to NetObjects or NetEvents.
  98. /// This can translate into significant bandwidth savings (especially since the size of the fields
  99. /// for transmitting these bits are determined at run-time based on the number of IDs given out.
  100. ///
  101. /// @section AbstractClassRep_details AbstractClassRep Internals
  102. ///
  103. /// Much like ConsoleConstructor, ACR does some preparatory work at runtime before execution
  104. /// is passed to main(). In actual fact, this preparatory work is done by the ConcreteClassRep
  105. /// template. Let's examine this more closely.
  106. ///
  107. /// If we examine ConsoleObject, we see that two macros must be used in the definition of a
  108. /// properly integrated objects. From the ConsoleObject example:
  109. ///
  110. /// @code
  111. /// // This is from inside the class definition...
  112. /// DECLARE_CONOBJECT(TorqueObject);
  113. ///
  114. /// // And this is from outside the class definition...
  115. /// IMPLEMENT_CONOBJECT(TorqueObject);
  116. /// @endcode
  117. ///
  118. /// What do these things actually do?
  119. ///
  120. /// Not all that much, in fact. They expand to code something like this:
  121. ///
  122. /// @code
  123. /// // This is from inside the class definition...
  124. /// static ConcreteClassRep<TorqueObject> dynClassRep;
  125. /// static AbstractClassRep* getParentStaticClassRep();
  126. /// static AbstractClassRep* getStaticClassRep();
  127. /// virtual AbstractClassRep* getClassRep() const;
  128. /// @endcode
  129. ///
  130. /// @code
  131. /// // And this is from outside the class definition...
  132. /// AbstractClassRep* TorqueObject::getClassRep() const { return &TorqueObject::dynClassRep; }
  133. /// AbstractClassRep* TorqueObject::getStaticClassRep() { return &dynClassRep; }
  134. /// AbstractClassRep* TorqueObject::getParentStaticClassRep() { return Parent::getStaticClassRep(); }
  135. /// ConcreteClassRep<TorqueObject> TorqueObject::dynClassRep("TorqueObject", 0, -1, 0);
  136. /// @endcode
  137. ///
  138. /// As you can see, getClassRep(), getStaticClassRep(), and getParentStaticClassRep() are just
  139. /// accessors to allow access to various ConcreteClassRep instances. This is where the Parent
  140. /// typedef comes into play as well - it lets getParentStaticClassRep() get the right
  141. /// class rep.
  142. ///
  143. /// In addition, dynClassRep is declared as a member of TorqueObject, and defined later
  144. /// on. Much like ConsoleConstructor, ConcreteClassReps add themselves to a global linked
  145. /// list in their constructor.
  146. ///
  147. /// Then, when AbstractClassRep::initialize() is called, from Con::init(), we iterate through
  148. /// the list and perform the following tasks:
  149. /// - Sets up a Namespace for each class.
  150. /// - Call the init() method on each ConcreteClassRep. This method:
  151. /// - Links namespaces between parent and child classes, using Con::classLinkNamespaces.
  152. /// - Calls initPersistFields() and consoleInit().
  153. /// - As a result of calling initPersistFields, the field list for the class is populated.
  154. /// - Assigns network IDs for classes based on their NetGroup membership. Determines
  155. /// bit allocations for network ID fields.
  156. ///
  157. /// @nosubgrouping
  158. class AbstractClassRep
  159. {
  160. friend class ConsoleObject;
  161. public:
  162. /// @name 'Tructors
  163. /// @{
  164. AbstractClassRep()
  165. {
  166. VECTOR_SET_ASSOCIATION(mFieldList);
  167. parentClass = NULL;
  168. }
  169. virtual ~AbstractClassRep() { }
  170. /// @}
  171. /// @name Representation Interface
  172. /// @{
  173. S32 mClassGroupMask; ///< Mask indicating in which NetGroups this object belongs.
  174. S32 mClassType; ///< Stores the NetClass of this class.
  175. S32 mNetEventDir; ///< Stores the NetDirection of this class.
  176. S32 mClassId[NetClassGroupsCount]; ///< Stores the IDs assigned to this class for each group.
  177. S32 getClassId (U32 netClassGroup) const;
  178. static U32 getClassCRC (U32 netClassGroup);
  179. const char* getClassName() const;
  180. static AbstractClassRep* getClassList();
  181. Namespace* getNameSpace();
  182. AbstractClassRep* getNextClass();
  183. AbstractClassRep* getParentClass();
  184. /// Helper class to see if we are a given class, or a subclass thereof.
  185. bool isClass(AbstractClassRep *acr)
  186. {
  187. AbstractClassRep *walk = this;
  188. // Walk up parents, checking for equivalence.
  189. while(walk)
  190. {
  191. if(walk == acr)
  192. return true;
  193. walk = walk->parentClass;
  194. };
  195. return false;
  196. }
  197. virtual ConsoleObject* create () const = 0;
  198. protected:
  199. virtual void init() const = 0;
  200. const char * mClassName;
  201. AbstractClassRep * nextClass;
  202. AbstractClassRep * parentClass;
  203. Namespace * mNamespace;
  204. /// @}
  205. /// @name Fields
  206. /// @{
  207. public:
  208. /// This is a function pointer typedef to support get/set callbacks for fields
  209. typedef bool (*SetDataNotify)( void *obj, const char *data );
  210. typedef const char *(*GetDataNotify)( void *obj, const char *data );
  211. /// This is a function pointer typedef to support optional writing for fields.
  212. typedef bool (*WriteDataNotify)( void* obj, const char* pFieldName );
  213. enum ACRFieldTypes
  214. {
  215. StartGroupFieldType = 0xFFFFFFFD,
  216. EndGroupFieldType = 0xFFFFFFFE,
  217. DepricatedFieldType = 0xFFFFFFFF
  218. };
  219. struct Field {
  220. const char* pFieldname; ///< Name of the field.
  221. const char* pGroupname; ///< Optionally filled field containing the group name.
  222. ///
  223. /// This is filled when type is StartField or EndField
  224. const char* pFieldDocs; ///< Documentation about this field; see consoleDoc.cc.
  225. bool groupExpand; ///< Flag to track expanded/not state of this group in the editor.
  226. U32 type; ///< A type ID. @see ACRFieldTypes
  227. U32 offset; ///< Memory offset from beginning of class for this field.
  228. S32 elementCount; ///< Number of elements, if this is an array.
  229. EnumTable * table; ///< If this is an enum, this points to the table defining it.
  230. BitSet32 flag; ///< Stores various flags
  231. ConsoleTypeValidator *validator; ///< Validator, if any.
  232. SetDataNotify setDataFn; ///< Set data notify Fn
  233. GetDataNotify getDataFn; ///< Get data notify Fn
  234. WriteDataNotify writeDataFn; ///< Function to determine whether data should be written or not.
  235. };
  236. typedef Vector<Field> FieldList;
  237. FieldList mFieldList;
  238. bool mDynamicGroupExpand;
  239. const Field *findField(StringTableEntry fieldName) const;
  240. /// @}
  241. /// @name Abstract Class Database
  242. /// @{
  243. protected:
  244. static AbstractClassRep ** classTable[NetClassGroupsCount][NetClassTypesCount];
  245. static AbstractClassRep * classLinkList;
  246. static U32 classCRC[NetClassGroupsCount];
  247. static bool initialized;
  248. static ConsoleObject* create(const char* in_pClassName);
  249. static ConsoleObject* create(const U32 groupId, const U32 typeId, const U32 in_classId);
  250. public:
  251. static U32 NetClassCount [NetClassGroupsCount][NetClassTypesCount];
  252. static U32 NetClassBitSize[NetClassGroupsCount][NetClassTypesCount];
  253. static void registerClassRep(AbstractClassRep*);
  254. static AbstractClassRep* findClassRep(const char* in_pClassName);
  255. static void initialize(); // Called from Con::init once on startup
  256. static void destroyFieldValidators(AbstractClassRep::FieldList &mFieldList);
  257. /// @}
  258. };
  259. inline AbstractClassRep *AbstractClassRep::getClassList()
  260. {
  261. return classLinkList;
  262. }
  263. inline U32 AbstractClassRep::getClassCRC(U32 group)
  264. {
  265. return classCRC[group];
  266. }
  267. inline AbstractClassRep *AbstractClassRep::getNextClass()
  268. {
  269. return nextClass;
  270. }
  271. inline AbstractClassRep *AbstractClassRep::getParentClass()
  272. {
  273. return parentClass;
  274. }
  275. inline S32 AbstractClassRep::getClassId(U32 group) const
  276. {
  277. return mClassId[group];
  278. }
  279. inline const char* AbstractClassRep::getClassName() const
  280. {
  281. return mClassName;
  282. }
  283. inline Namespace *AbstractClassRep::getNameSpace()
  284. {
  285. return mNamespace;
  286. }
  287. //------------------------------------------------------------------------------
  288. //-------------------------------------- ConcreteClassRep
  289. //
  290. /// Helper class for AbstractClassRep.
  291. ///
  292. /// @see AbtractClassRep
  293. /// @see ConsoleObject
  294. template <class T>
  295. class ConcreteClassRep : public AbstractClassRep
  296. {
  297. public:
  298. ConcreteClassRep(const char *name, S32 netClassGroupMask, S32 netClassType, S32 netEventDir, AbstractClassRep *parent)
  299. {
  300. // name is a static compiler string so no need to worry about copying or deleting
  301. mClassName = name;
  302. // Clean up mClassId
  303. for(U32 i = 0; i < NetClassGroupsCount; i++)
  304. mClassId[i] = -1;
  305. // Set properties for this ACR
  306. mClassType = netClassType;
  307. mClassGroupMask = netClassGroupMask;
  308. mNetEventDir = netEventDir;
  309. parentClass = parent;
  310. // Finally, register ourselves.
  311. registerClassRep(this);
  312. };
  313. /// Perform class specific initialization tasks.
  314. ///
  315. /// Link namespaces, call initPersistFields() and consoleInit().
  316. void init() const
  317. {
  318. // Get handle to our parent class, if any, and ourselves (we are our parent's child).
  319. AbstractClassRep *parent = T::getParentStaticClassRep();
  320. AbstractClassRep *child = T::getStaticClassRep ();
  321. // If we got reps, then link those namespaces! (To get proper inheritance.)
  322. if(parent && child)
  323. Con::classLinkNamespaces(parent->getNameSpace(), child->getNameSpace());
  324. // Finally, do any class specific initialization...
  325. T::initPersistFields();
  326. T::consoleInit();
  327. }
  328. /// Wrap constructor.
  329. ConsoleObject* create() const { return new T; }
  330. };
  331. //------------------------------------------------------------------------------
  332. // Forward declarations so they can be used in the class
  333. const char *defaultProtectedGetFn( void *obj, const char *data );
  334. bool defaultProtectedWriteFn( void* obj, StringTableEntry pFieldName );
  335. /// Interface class to the console.
  336. ///
  337. /// @section ConsoleObject_basics The Basics
  338. ///
  339. /// Any object which you want to work with the console system should derive from this,
  340. /// and access functionality through the static interface.
  341. ///
  342. /// This class is always used with the DECLARE_CONOBJECT and IMPLEMENT_* macros.
  343. ///
  344. /// @code
  345. /// // A very basic example object. It will do nothing!
  346. /// class TorqueObject : public ConsoleObject {
  347. /// // Must provide a Parent typedef so the console system knows what we inherit from.
  348. /// typedef ConsoleObject Parent;
  349. ///
  350. /// // This does a lot of menial declaration for you.
  351. /// DECLARE_CONOBJECT(TorqueObject);
  352. ///
  353. /// // This is for us to register our fields in.
  354. /// static void initPersistFields();
  355. ///
  356. /// // A sample field.
  357. /// S8 mSample;
  358. /// }
  359. /// @endcode
  360. ///
  361. /// @code
  362. /// // And the accordant implementation...
  363. /// IMPLEMENT_CONOBJECT(TorqueObject);
  364. ///
  365. /// void TorqueObject::initPersistFields()
  366. /// {
  367. /// // If you want to inherit any fields from the parent (you do), do this:
  368. /// Parent::initPersistFields();
  369. ///
  370. /// // Pass the field, the type, the offset, and a usage string.
  371. /// addField("sample", TypeS8, Offset(mSample, TorqueObject), "A test field.");
  372. /// }
  373. /// @endcode
  374. ///
  375. /// That's all you need to do to get a class registered with the console system. At this point,
  376. /// you can instantiate it via script, tie methods to it using ConsoleMethod, register fields,
  377. /// and so forth. You can also register any global variables related to the class by creating
  378. /// a consoleInit() method.
  379. ///
  380. /// You will need to use different IMPLEMENT_ macros in different cases; for instance, if you
  381. /// are making a NetObject (for ghosting), a DataBlock, or a NetEvent.
  382. ///
  383. /// @see AbstractClassRep for gory implementation details.
  384. /// @nosubgrouping
  385. class ConsoleObject
  386. {
  387. protected:
  388. /// @deprecated This is disallowed.
  389. ConsoleObject() { /* disallowed */ }
  390. /// @deprecated This is disallowed.
  391. ConsoleObject(const ConsoleObject&);
  392. public:
  393. /// Get a reference to a field by name.
  394. const AbstractClassRep::Field* findField(StringTableEntry fieldName) const;
  395. /// Gets the ClassRep.
  396. virtual AbstractClassRep* getClassRep() const;
  397. /// Set the value of a field.
  398. bool setField(const char *fieldName, const char *value);
  399. virtual ~ConsoleObject();
  400. public:
  401. /// @name Object Creation
  402. /// @{
  403. static ConsoleObject* create(const char* in_pClassName);
  404. static ConsoleObject* create(const U32 groupId, const U32 typeId, const U32 in_classId);
  405. /// @}
  406. public:
  407. /// Get the classname from a class tag.
  408. static const char* lookupClassName(const U32 in_classTag);
  409. protected:
  410. /// @name Fields
  411. /// @{
  412. /// Mark the beginning of a group of fields.
  413. ///
  414. /// This is used in the consoleDoc system.
  415. /// @see console_autodoc
  416. static void addGroup(const char* in_pGroupname, const char* in_pGroupDocs = NULL);
  417. /// Mark the end of a group of fields.
  418. ///
  419. /// This is used in the consoleDoc system.
  420. /// @see console_autodoc
  421. static void endGroup(const char* in_pGroupname);
  422. /// Register a complex field.
  423. ///
  424. /// @param in_pFieldname Name of the field.
  425. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  426. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  427. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  428. /// @param in_table An EnumTable, if this is an enumerated field.
  429. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  430. static void addField(const char* in_pFieldname,
  431. const U32 in_fieldType,
  432. const dsize_t in_fieldOffset,
  433. const U32 in_elementCount = 1,
  434. EnumTable * in_table = NULL,
  435. const char* in_pFieldDocs = NULL);
  436. /// Register a complex field with a write notify.
  437. ///
  438. /// @param in_pFieldname Name of the field.
  439. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  440. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  441. /// @param in_writeDataFn This method will return whether the field should be written or not.
  442. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  443. /// @param in_table An EnumTable, if this is an enumerated field.
  444. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  445. static void addField(const char* in_pFieldname,
  446. const U32 in_fieldType,
  447. const dsize_t in_fieldOffset,
  448. AbstractClassRep::WriteDataNotify in_writeDataFn,
  449. const U32 in_elementCount = 1,
  450. EnumTable * in_table = NULL,
  451. const char* in_pFieldDocs = NULL);
  452. /// Register a simple field.
  453. ///
  454. /// @param in_pFieldname Name of the field.
  455. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  456. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  457. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  458. static void addField(const char* in_pFieldname,
  459. const U32 in_fieldType,
  460. const dsize_t in_fieldOffset,
  461. const char* in_pFieldDocs);
  462. /// Register a simple field with a write notify.
  463. ///
  464. /// @param in_pFieldname Name of the field.
  465. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  466. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  467. /// @param in_writeDataFn This method will return whether the field should be written or not.
  468. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  469. static void addField(const char* in_pFieldname,
  470. const U32 in_fieldType,
  471. const dsize_t in_fieldOffset,
  472. AbstractClassRep::WriteDataNotify in_writeDataFn,
  473. const char* in_pFieldDocs );
  474. /// Register a validated field.
  475. ///
  476. /// A validated field is just like a normal field except that you can't
  477. /// have it be an array, and that you give it a pointer to a ConsoleTypeValidator
  478. /// subclass, which is then used to validate any value placed in it. Invalid
  479. /// values are ignored and an error is printed to the console.
  480. ///
  481. /// @see addField
  482. /// @see typeValidators.h
  483. static void addFieldV(const char* in_pFieldname,
  484. const U32 in_fieldType,
  485. const dsize_t in_fieldOffset,
  486. ConsoleTypeValidator *v,
  487. const char * in_pFieldDocs = NULL);
  488. /// Register a complex protected field.
  489. ///
  490. /// @param in_pFieldname Name of the field.
  491. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  492. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  493. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  494. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  495. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  496. /// @param in_table An EnumTable, if this is an enumerated field.
  497. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  498. static void addProtectedField(const char* in_pFieldname,
  499. const U32 in_fieldType,
  500. const dsize_t in_fieldOffset,
  501. AbstractClassRep::SetDataNotify in_setDataFn,
  502. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  503. const U32 in_elementCount = 1,
  504. EnumTable * in_table = NULL,
  505. const char* in_pFieldDocs = NULL);
  506. /// Register a complex protected field.
  507. ///
  508. /// @param in_pFieldname Name of the field.
  509. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  510. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  511. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  512. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  513. /// @param in_writeDataFn This method will return whether the field should be written or not.
  514. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  515. /// @param in_table An EnumTable, if this is an enumerated field.
  516. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  517. static void addProtectedField(const char* in_pFieldname,
  518. const U32 in_fieldType,
  519. const dsize_t in_fieldOffset,
  520. AbstractClassRep::SetDataNotify in_setDataFn,
  521. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  522. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  523. const U32 in_elementCount = 1,
  524. EnumTable * in_table = NULL,
  525. const char* in_pFieldDocs = NULL);
  526. /// Register a simple protected field.
  527. ///
  528. /// @param in_pFieldname Name of the field.
  529. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  530. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  531. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  532. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  533. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  534. static void addProtectedField(const char* in_pFieldname,
  535. const U32 in_fieldType,
  536. const dsize_t in_fieldOffset,
  537. AbstractClassRep::SetDataNotify in_setDataFn,
  538. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  539. const char* in_pFieldDocs = NULL);
  540. /// Register a simple protected field.
  541. ///
  542. /// @param in_pFieldname Name of the field.
  543. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  544. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  545. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  546. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  547. /// @param in_writeDataFn This method will return whether the field should be written or not.
  548. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  549. static void addProtectedField(const char* in_pFieldname,
  550. const U32 in_fieldType,
  551. const dsize_t in_fieldOffset,
  552. AbstractClassRep::SetDataNotify in_setDataFn,
  553. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  554. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  555. const char* in_pFieldDocs = NULL);
  556. /// Add a deprecated field.
  557. ///
  558. /// A deprecated field will always be undefined, even if you assign a value to it. This
  559. /// is useful when you need to make sure that a field is not being used anymore.
  560. static void addDepricatedField(const char *fieldName);
  561. /// Remove a field.
  562. ///
  563. /// Sometimes, you just have to remove a field!
  564. /// @returns True on success.
  565. static bool removeField(const char* in_pFieldname);
  566. /// @}
  567. public:
  568. /// Register dynamic fields in a subclass of ConsoleObject.
  569. ///
  570. /// @see addField(), addFieldV(), addDepricatedField(), addGroup(), endGroup()
  571. static void initPersistFields();
  572. /// Register global constant variables and do other one-time initialization tasks in
  573. /// a subclass of ConsoleObject.
  574. ///
  575. /// @deprecated You should use ConsoleMethod and ConsoleFunction, not this, to
  576. /// register methods or commands.
  577. /// @see console
  578. static void consoleInit();
  579. /// @name Field List
  580. /// @{
  581. /// Get a list of all the fields. This information cannot be modified.
  582. const AbstractClassRep::FieldList& getFieldList() const;
  583. /// Get a list of all the fields, set up so we can modify them.
  584. ///
  585. /// @note This is a bad trick to pull if you aren't very careful,
  586. /// since you can blast field data!
  587. AbstractClassRep::FieldList& getModifiableFieldList();
  588. /// Get a handle to a boolean telling us if we expanded the dynamic group.
  589. ///
  590. /// @see GuiInspector::Inspect()
  591. bool& getDynamicGroupExpand();
  592. /// @}
  593. /// @name ConsoleObject Implementation
  594. ///
  595. /// These functions are implemented in every subclass of
  596. /// ConsoleObject by an IMPLEMENT_CONOBJECT or IMPLEMENT_CO_* macro.
  597. /// @{
  598. /// Get the abstract class information for this class.
  599. static AbstractClassRep *getStaticClassRep() { return NULL; }
  600. /// Get the abstract class information for this class's superclass.
  601. static AbstractClassRep *getParentStaticClassRep() { return NULL; }
  602. /// Get our network-layer class id.
  603. ///
  604. /// @param netClassGroup The net class for which we want our ID.
  605. /// @see
  606. S32 getClassId(U32 netClassGroup) const;
  607. /// Get our compiler and platform independent class name.
  608. ///
  609. /// @note This name can be used to instantiate another instance using create()
  610. const char *getClassName() const;
  611. /// @}
  612. };
  613. // Deprecated? -pw
  614. #define addNamedField(fieldName,type,className) addField(#fieldName, type, Offset(fieldName,className))
  615. #define addNamedFieldV(fieldName,type,className, validator) addFieldV(#fieldName, type, Offset(fieldName,className), validator)
  616. //------------------------------------------------------------------------------
  617. //-------------------------------------- Inlines
  618. //
  619. inline S32 ConsoleObject::getClassId(U32 netClassGroup) const
  620. {
  621. AssertFatal(getClassRep() != NULL,"Cannot get tag from non-declared dynamic class!");
  622. return getClassRep()->getClassId(netClassGroup);
  623. }
  624. inline const char * ConsoleObject::getClassName() const
  625. {
  626. AssertFatal(getClassRep() != NULL,
  627. "Cannot get tag from non-declared dynamic class");
  628. return getClassRep()->getClassName();
  629. }
  630. inline const AbstractClassRep::Field * ConsoleObject::findField(StringTableEntry name) const
  631. {
  632. AssertFatal(getClassRep() != NULL,
  633. avar("Cannot get field '%s' from non-declared dynamic class.", name));
  634. return getClassRep()->findField(name);
  635. }
  636. inline bool ConsoleObject::setField(const char *fieldName, const char *value)
  637. {
  638. //sanity check
  639. if ((! fieldName) || (! fieldName[0]) || (! value))
  640. return false;
  641. if (! getClassRep())
  642. return false;
  643. const AbstractClassRep::Field *myField = getClassRep()->findField(StringTable->insert(fieldName));
  644. if (! myField)
  645. return false;
  646. Con::setData(
  647. myField->type,
  648. (void *) (((const char *)(this)) + myField->offset),
  649. 0,
  650. 1,
  651. &value,
  652. myField->table,
  653. myField->flag);
  654. return true;
  655. }
  656. inline ConsoleObject* ConsoleObject::create(const char* in_pClassName)
  657. {
  658. return AbstractClassRep::create(in_pClassName);
  659. }
  660. inline ConsoleObject* ConsoleObject::create(const U32 groupId, const U32 typeId, const U32 in_classId)
  661. {
  662. return AbstractClassRep::create(groupId, typeId, in_classId);
  663. }
  664. inline const AbstractClassRep::FieldList& ConsoleObject::getFieldList() const
  665. {
  666. return getClassRep()->mFieldList;
  667. }
  668. inline AbstractClassRep::FieldList& ConsoleObject::getModifiableFieldList()
  669. {
  670. return getClassRep()->mFieldList;
  671. }
  672. inline bool& ConsoleObject::getDynamicGroupExpand()
  673. {
  674. return getClassRep()->mDynamicGroupExpand;
  675. }
  676. /// @name ConsoleObject Macros
  677. /// @{
  678. #define DECLARE_CONOBJECT(className) \
  679. static ConcreteClassRep<className> dynClassRep; \
  680. static AbstractClassRep* getParentStaticClassRep(); \
  681. static AbstractClassRep* getStaticClassRep(); \
  682. virtual AbstractClassRep* getClassRep() const
  683. #define IMPLEMENT_CONOBJECT(className) \
  684. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  685. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  686. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  687. ConcreteClassRep<className> className::dynClassRep(#className, 0, -1, 0, className::getParentStaticClassRep())
  688. #define IMPLEMENT_CO_NETOBJECT_V1(className) \
  689. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  690. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  691. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  692. ConcreteClassRep<className> className::dynClassRep(#className, NetClassGroupGameMask, NetClassTypeObject, 0, className::getParentStaticClassRep())
  693. #define IMPLEMENT_CO_DATABLOCK_V1(className) \
  694. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  695. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  696. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  697. ConcreteClassRep<className> className::dynClassRep(#className, NetClassGroupGameMask, NetClassTypeDataBlock, 0, className::getParentStaticClassRep())
  698. /// @}
  699. //------------------------------------------------------------------------------
  700. inline bool defaultProtectedSetFn( void *obj, const char *data )
  701. {
  702. return true;
  703. }
  704. inline const char *defaultProtectedGetFn( void *obj, const char *data )
  705. {
  706. return data;
  707. }
  708. inline bool defaultProtectedWriteFn( void* obj, StringTableEntry pFieldName )
  709. {
  710. return true;
  711. }
  712. inline bool defaultProtectedNotSetFn(void* obj, const char* data)
  713. {
  714. return false;
  715. }
  716. inline bool defaultProtectedNotWriteFn( void* obj, StringTableEntry pFieldName )
  717. {
  718. return false;
  719. }
  720. #endif //_CONSOLEOBJECT_H_