consoleObject.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. FIELD_DontWriteToFile = BIT(4),
  423. };
  424. struct Field
  425. {
  426. Field()
  427. : pFieldname( NULL ),
  428. pGroupname( NULL ),
  429. pFieldDocs( NULL ),
  430. groupExpand( false ),
  431. type( 0 ),
  432. offset( 0 ),
  433. elementCount( 0 ),
  434. table( NULL ),
  435. validator( NULL ),
  436. setDataFn( NULL ),
  437. getDataFn( NULL ),
  438. writeDataFn(NULL),
  439. networkMask(0)
  440. {
  441. doNotSubstitute = keepClearSubsOnly = false;
  442. }
  443. StringTableEntry pFieldname; ///< Name of the field.
  444. const char* pGroupname; ///< Optionally filled field containing the group name.
  445. ///
  446. /// This is filled when type is StartField or EndField
  447. const char* pFieldDocs; ///< Documentation about this field; see consoleDoc.cc.
  448. bool groupExpand; ///< Flag to track expanded/not state of this group in the editor.
  449. U32 type; ///< A data type ID or one of the special custom fields. @see ACRFieldTypes
  450. U32 offset; ///< Memory offset from beginning of class for this field.
  451. S32 elementCount; ///< Number of elements, if this is an array.
  452. const EnumTable * table; ///< If this is an enum, this points to the table defining it.
  453. BitSet32 flag; ///< Stores various flags
  454. TypeValidator *validator; ///< Validator, if any.
  455. SetDataNotify setDataFn; ///< Set data notify Fn
  456. GetDataNotify getDataFn; ///< Get data notify Fn
  457. WriteDataNotify writeDataFn; ///< Function to determine whether data should be written or not.
  458. bool doNotSubstitute;
  459. bool keepClearSubsOnly;
  460. U32 networkMask;
  461. };
  462. typedef Vector<Field> FieldList;
  463. FieldList mFieldList;
  464. bool mDynamicGroupExpand;
  465. const Field* findField( StringTableEntry fieldName ) const;
  466. /// @}
  467. /// @name Console Type Interface
  468. /// @{
  469. void* getNativeVariable() override { return new ( AbstractClassRep* ); } // Any pointer-sized allocation will do.
  470. void deleteNativeVariable( void* var ) override { delete reinterpret_cast< AbstractClassRep** >( var ); }
  471. /// @}
  472. /// @name Abstract Class Database
  473. /// @{
  474. protected:
  475. static AbstractClassRep ** classTable[NetClassGroupsCount][NetClassTypesCount];
  476. static AbstractClassRep * classLinkList;
  477. static U32 classCRC[NetClassGroupsCount];
  478. static bool initialized;
  479. static ConsoleObject* create(const char* in_pClassName);
  480. static ConsoleObject* create(const U32 groupId, const U32 typeId, const U32 in_classId);
  481. public:
  482. static U32 NetClassCount [NetClassGroupsCount][NetClassTypesCount];
  483. static U32 NetClassBitSize[NetClassGroupsCount][NetClassTypesCount];
  484. static void registerClassRep(AbstractClassRep*);
  485. static AbstractClassRep* findClassRep(const char* in_pClassName);
  486. static AbstractClassRep* findClassRep( U32 groupId, U32 typeId, U32 classId );
  487. static void removeClassRep(AbstractClassRep*); // This should not be used lightly
  488. static void initialize(); // Called from Con::init once on startup
  489. static void shutdown();
  490. /// @}
  491. };
  492. extern AbstractClassRep::FieldList sg_tempFieldList;
  493. //=============================================================================
  494. // ConcreteClassRep.
  495. //=============================================================================
  496. /// Helper class for AbstractClassRep.
  497. ///
  498. /// @see AbtractClassRep
  499. /// @see ConsoleObject
  500. template< class T >
  501. class ConcreteAbstractClassRep : public AbstractClassRep
  502. {
  503. public:
  504. AbstractClassRep* getContainerChildClass(const bool recurse) override
  505. {
  506. // Fetch container children type.
  507. AbstractClassRep* pChildren = T::getContainerChildStaticClassRep();
  508. if (!recurse || pChildren != NULL)
  509. return pChildren;
  510. // Fetch parent type.
  511. AbstractClassRep* pParent = T::getParentStaticClassRep();
  512. if (pParent == NULL)
  513. return NULL;
  514. // Get parent container children.
  515. return pParent->getContainerChildClass(recurse);
  516. }
  517. WriteCustomTamlSchema getCustomTamlSchema(void) override
  518. {
  519. return T::getStaticWriteCustomTamlSchema();
  520. }
  521. static EnginePropertyTable _smPropertyTable;
  522. static EnginePropertyTable& smPropertyTable;
  523. ConcreteAbstractClassRep(const char* name,
  524. const char* conTypeName,
  525. S32* conTypeIdPtr,
  526. S32 netClassGroupMask,
  527. S32 netClassType,
  528. S32 netEventDir,
  529. AbstractClassRep* parent,
  530. const char* (*parentDesc)())
  531. : AbstractClassRep(conTypeIdPtr, conTypeName)
  532. {
  533. mClassName = StringTable->insert(name);
  534. mCategory = T::__category();
  535. mTypeInfo = _MAPTYPE< T >();
  536. if (mTypeInfo)
  537. const_cast< EngineTypeInfo* >(mTypeInfo)->mPropertyTable = &smPropertyTable;
  538. if (&T::__description != parentDesc)
  539. mDescription = T::__description();
  540. // Clean up mClassId
  541. for (U32 i = 0; i < NetClassGroupsCount; i++)
  542. mClassId[i] = -1;
  543. // Set properties for this ACR
  544. mClassType = netClassType;
  545. mClassGroupMask = netClassGroupMask;
  546. mNetEventDir = netEventDir;
  547. parentClass = parent;
  548. mClassSizeof = sizeof(T);
  549. // Finally, register ourselves.
  550. registerClassRep(this);
  551. };
  552. /// Wrap constructor.
  553. ConsoleObject* create() const override { return NULL; }
  554. /// Perform class specific initialization tasks.
  555. ///
  556. /// Link namespaces, call initPersistFields() and consoleInit().
  557. void init() override
  558. {
  559. // Get handle to our parent class, if any, and ourselves (we are our parent's child).
  560. AbstractClassRep *parent = T::getParentStaticClassRep();
  561. AbstractClassRep *child = T::getStaticClassRep();
  562. // If we got reps, then link those namespaces! (To get proper inheritance.)
  563. if (parent && child)
  564. Con::classLinkNamespaces(parent->getNameSpace(), child->getNameSpace());
  565. // Finally, do any class specific initialization...
  566. T::initPersistFields();
  567. T::consoleInit();
  568. EnginePropertyTable::Property* props = new EnginePropertyTable::Property[sg_tempFieldList.size() + 1];
  569. for (int i = 0; i < sg_tempFieldList.size(); ++i)
  570. {
  571. EnginePropertyTable::Property prop;
  572. prop.mDocString = sg_tempFieldList[i].pFieldDocs;
  573. prop.mName = sg_tempFieldList[i].pFieldname;
  574. prop.mNumElements = sg_tempFieldList[i].elementCount;
  575. prop.mFlags = 0;
  576. if (sg_tempFieldList[i].type == StartGroupFieldType)
  577. prop.mFlags |= EnginePropertyGroupBegin;
  578. if (sg_tempFieldList[i].type == EndGroupFieldType)
  579. prop.mFlags |= EnginePropertyGroupEnd;
  580. prop.mType = sg_tempFieldList[i].type;
  581. props[i] = prop;
  582. }
  583. _smPropertyTable = EnginePropertyTable(sg_tempFieldList.size(), props);
  584. smPropertyTable = _smPropertyTable;
  585. const_cast<EngineTypeInfo*>(mTypeInfo)->mPropertyTable = &_smPropertyTable;
  586. // After we hand it off, immediately delete if safe:
  587. delete[] props;
  588. // Let the base finish up.
  589. AbstractClassRep::init();
  590. }
  591. /// @name Console Type Interface
  592. /// @{
  593. void setData(void* dptr, S32 argc, const char** argv, const EnumTable* tbl, BitSet32 flag) override
  594. {
  595. if (argc == 1)
  596. {
  597. T** obj = (T**)dptr;
  598. *obj = dynamic_cast< T* >(T::__findObject(argv[0]));
  599. }
  600. else
  601. Con::errorf("Cannot set multiple args to a single ConsoleObject*.");
  602. }
  603. const char* getData(void* dptr, const EnumTable* tbl, BitSet32 flag) override
  604. {
  605. T** obj = (T**)dptr;
  606. return Con::getReturnBuffer(T::__getObjectId(*obj));
  607. }
  608. const char* getTypeClassName() override { return mClassName; }
  609. const bool isDatablock() override { return T::__smIsDatablock; };
  610. /// @}
  611. };
  612. template< class T >
  613. class ConcreteClassRep : public ConcreteAbstractClassRep<T>
  614. {
  615. public:
  616. ConcreteClassRep(const char* name,
  617. const char* conTypeName,
  618. S32* conTypeIdPtr,
  619. S32 netClassGroupMask,
  620. S32 netClassType,
  621. S32 netEventDir,
  622. AbstractClassRep* parent,
  623. const char* (*parentDesc)())
  624. : ConcreteAbstractClassRep<T>(name, conTypeName, conTypeIdPtr, netClassGroupMask, netClassType, netEventDir, parent, parentDesc)
  625. {
  626. }
  627. /// Wrap constructor.
  628. ConsoleObject* create() const override { return new T; }
  629. };
  630. template< typename T > EnginePropertyTable ConcreteAbstractClassRep< T >::_smPropertyTable(0, NULL);
  631. template< typename T > EnginePropertyTable& ConcreteAbstractClassRep< T >::smPropertyTable = ConcreteAbstractClassRep< T >::_smPropertyTable;
  632. //------------------------------------------------------------------------------
  633. // Forward declaration of this function so it can be used in the class
  634. const char *defaultProtectedGetFn( void *obj, const char *data );
  635. bool defaultProtectedWriteFn(void* obj, StringTableEntry pFieldName);
  636. //=============================================================================
  637. // ConsoleObject.
  638. //=============================================================================
  639. /// Interface class to the console.
  640. ///
  641. /// @section ConsoleObject_basics The Basics
  642. ///
  643. /// Any object which you want to work with the console system should derive from this,
  644. /// and access functionality through the static interface.
  645. ///
  646. /// This class is always used with the DECLARE_CONOBJECT and IMPLEMENT_* macros.
  647. ///
  648. /// @code
  649. /// // A very basic example object. It will do nothing!
  650. /// class TorqueObject : public ConsoleObject {
  651. /// // Must provide a Parent typedef so the console system knows what we inherit from.
  652. /// typedef ConsoleObject Parent;
  653. ///
  654. /// // This does a lot of menial declaration for you.
  655. /// DECLARE_CONOBJECT(TorqueObject);
  656. ///
  657. /// // This is for us to register our fields in.
  658. /// static void initPersistFields();
  659. ///
  660. /// // A sample field.
  661. /// S8 mSample;
  662. /// }
  663. /// @endcode
  664. ///
  665. /// @code
  666. /// // And the accordant implementation...
  667. /// IMPLEMENT_CONOBJECT(TorqueObject);
  668. ///
  669. /// void TorqueObject::initPersistFields()
  670. /// {
  671. /// // If you want to inherit any fields from the parent (you do), do this:
  672. /// Parent::initPersistFields();
  673. ///
  674. /// // Pass the field, the type, the offset, and a usage string.
  675. /// addField("sample", TypeS8, Offset(mSample, TorqueObject), "A test field.");
  676. /// }
  677. /// @endcode
  678. ///
  679. /// That's all you need to do to get a class registered with the console system. At this point,
  680. /// you can instantiate it via script, tie methods to it using ConsoleMethod, register fields,
  681. /// and so forth. You can also register any global variables related to the class by creating
  682. /// a consoleInit() method.
  683. ///
  684. /// You will need to use different IMPLEMENT_ macros in different cases; for instance, if you
  685. /// are making a NetObject (for ghosting), a DataBlock, or a NetEvent.
  686. ///
  687. /// @see AbstractClassRep for gory implementation details.
  688. /// @nosubgrouping
  689. class ConsoleObject : public EngineObject
  690. {
  691. DECLARE_ABSTRACT_CLASS( ConsoleObject, EngineObject );
  692. protected:
  693. /// @deprecated This is disallowed.
  694. ConsoleObject(const ConsoleObject&) { mDocsClick = false; };
  695. public:
  696. /// <summary>
  697. /// Only used for interfacing with the editor's inspector docsURL button
  698. /// </summary>
  699. bool mDocsClick;
  700. ConsoleObject() { mDocsClick = false; }
  701. /// Get a reference to a field by name.
  702. const AbstractClassRep::Field *findField(StringTableEntry fieldName) const;
  703. /// Gets the ClassRep.
  704. virtual AbstractClassRep* getClassRep() const;
  705. #define DECLARE_ABSTRACT_CONOBJECT( className ) \
  706. DECLARE_ABSTRACT_CLASS( className, Parent ); \
  707. static S32 _smTypeId; \
  708. static ConcreteAbstractClassRep< className > dynClassRep; \
  709. static AbstractClassRep* getParentStaticClassRep(); \
  710. static AbstractClassRep* getStaticClassRep(); \
  711. static SimObjectRefConsoleBaseType< className > ptrRefType; \
  712. virtual AbstractClassRep* getClassRep() const
  713. /// Set the value of a field.
  714. bool setField(const char *fieldName, const char *value);
  715. public:
  716. /// @name Object Creation
  717. /// @{
  718. static ConsoleObject* create(const char* in_pClassName);
  719. static ConsoleObject* create(const U32 groupId, const U32 typeId, const U32 in_classId);
  720. /// @}
  721. public:
  722. /// Get the classname from a class tag.
  723. static const char* lookupClassName(const U32 in_classTag) { return ""; };
  724. /// @name Fields
  725. /// @{
  726. /// Mark the beginning of a group of fields.
  727. ///
  728. /// This is used in the consoleDoc system.
  729. /// @see console_autodoc
  730. static void addGroup(const char* in_pGroupname, const char* in_pGroupDocs = NULL);
  731. /// Mark the end of a group of fields.
  732. ///
  733. /// This is used in the consoleDoc system.
  734. /// @see console_autodoc
  735. static void endGroup(const char* in_pGroupname);
  736. /// Marks the start of a fixed size array of fields.
  737. /// @see console_autodoc
  738. static void addArray( const char *arrayName, S32 count );
  739. /// Marks the end of an array of fields.
  740. /// @see console_autodoc
  741. static void endArray( const char *arrayName );
  742. /// Register a complex field.
  743. ///
  744. /// @param in_pFieldname Name of the field.
  745. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  746. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  747. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  748. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  749. static void addField(const char* in_pFieldname,
  750. const U32 in_fieldType,
  751. const dsize_t in_fieldOffset,
  752. const U32 in_elementCount = 1,
  753. const char* in_pFieldDocs = NULL,
  754. U32 flags = 0 );
  755. static void addField(const char* in_pFieldname,
  756. const U32 in_fieldType,
  757. const dsize_t in_fieldOffset,
  758. AbstractClassRep::WriteDataNotify in_writeDataFn,
  759. const U32 in_elementCount = 1,
  760. const char* in_pFieldDocs = NULL,
  761. U32 flags = 0);
  762. /// Register a simple field.
  763. ///
  764. /// @param in_pFieldname Name of the field.
  765. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  766. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  767. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  768. static void addField(const char* in_pFieldname,
  769. const U32 in_fieldType,
  770. const dsize_t in_fieldOffset,
  771. const char* in_pFieldDocs,
  772. U32 flags = 0 );
  773. static void addField(const char* in_pFieldname,
  774. const U32 in_fieldType,
  775. const dsize_t in_fieldOffset,
  776. AbstractClassRep::WriteDataNotify in_writeDataFn,
  777. const char* in_pFieldDocs,
  778. U32 flags = 0);
  779. /// Register a validated field.
  780. ///
  781. /// A validated field is just like a normal field except that you can't
  782. /// have it be an array, and that you give it a pointer to a TypeValidator
  783. /// subclass, which is then used to validate any value placed in it. Invalid
  784. /// values are ignored and an error is printed to the console.
  785. ///
  786. /// @see addField
  787. /// @see typeValidators.h
  788. static void addFieldV(const char* in_pFieldname,
  789. const U32 in_fieldType,
  790. const dsize_t in_fieldOffset,
  791. TypeValidator *v,
  792. const char * in_pFieldDocs = NULL);
  793. static void addFieldV(const char* in_pFieldname,
  794. const U32 in_fieldType,
  795. const dsize_t in_fieldOffset,
  796. TypeValidator* v,
  797. const U32 in_elementCount,
  798. const char* in_pFieldDocs = NULL);
  799. /// Register a complex protected field.
  800. ///
  801. /// @param in_pFieldname Name of the field.
  802. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  803. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  804. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  805. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  806. /// @param in_elementCount Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.
  807. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  808. static void addProtectedField(const char* in_pFieldname,
  809. const U32 in_fieldType,
  810. const dsize_t in_fieldOffset,
  811. AbstractClassRep::SetDataNotify in_setDataFn,
  812. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  813. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  814. const U32 in_elementCount = 1,
  815. const char* in_pFieldDocs = NULL,
  816. U32 flags = 0);
  817. static void addProtectedField(const char* in_pFieldname,
  818. const U32 in_fieldType,
  819. const dsize_t in_fieldOffset,
  820. AbstractClassRep::SetDataNotify in_setDataFn,
  821. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  822. const U32 in_elementCount = 1,
  823. const char* in_pFieldDocs = NULL,
  824. U32 flags = 0);
  825. /// Register a simple protected field.
  826. ///
  827. /// @param in_pFieldname Name of the field.
  828. /// @param in_fieldType Type of the field. @see ConsoleDynamicTypes
  829. /// @param in_fieldOffset Offset to the field from the start of the class; calculated using the Offset() macro.
  830. /// @param in_setDataFn When this field gets set, it will call the callback provided. @see console_protected
  831. /// @param in_getDataFn When this field is accessed for it's data, it will return the value of this function
  832. /// @param in_pFieldDocs Usage string for this field. @see console_autodoc
  833. static void addProtectedField(const char* in_pFieldname,
  834. const U32 in_fieldType,
  835. const dsize_t in_fieldOffset,
  836. AbstractClassRep::SetDataNotify in_setDataFn,
  837. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  838. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  839. const char* in_pFieldDocs = NULL,
  840. U32 flags = 0);
  841. static void addProtectedField(const char* in_pFieldname,
  842. const U32 in_fieldType,
  843. const dsize_t in_fieldOffset,
  844. AbstractClassRep::SetDataNotify in_setDataFn,
  845. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  846. const char* in_pFieldDocs = NULL,
  847. U32 flags = 0);
  848. static void addProtectedFieldV(const char* in_pFieldname,
  849. const U32 in_fieldType,
  850. const dsize_t in_fieldOffset,
  851. AbstractClassRep::SetDataNotify in_setDataFn,
  852. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  853. AbstractClassRep::WriteDataNotify in_writeDataFn = &defaultProtectedWriteFn,
  854. TypeValidator* v = NULL,
  855. const U32 in_elementCount = 1,
  856. const char* in_pFieldDocs = NULL,
  857. U32 flags = 0);
  858. static void addProtectedFieldV(const char* in_pFieldname,
  859. const U32 in_fieldType,
  860. const dsize_t in_fieldOffset,
  861. AbstractClassRep::SetDataNotify in_setDataFn,
  862. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  863. TypeValidator* v = NULL,
  864. const U32 in_elementCount = 1,
  865. const char* in_pFieldDocs = NULL,
  866. U32 flags = 0);
  867. static void addProtectedFieldV(const char* in_pFieldname,
  868. const U32 in_fieldType,
  869. const dsize_t in_fieldOffset,
  870. AbstractClassRep::SetDataNotify in_setDataFn,
  871. AbstractClassRep::GetDataNotify in_getDataFn = &defaultProtectedGetFn,
  872. TypeValidator* v = NULL,
  873. const char* in_pFieldDocs = NULL,
  874. U32 flags = 0);
  875. /// Add a deprecated field.
  876. ///
  877. /// A deprecated field will always be undefined, even if you assign a value to it. This
  878. /// is useful when you need to make sure that a field is not being used anymore.
  879. static void addDeprecatedField(const char *fieldName);
  880. /// Remove a field.
  881. ///
  882. /// Sometimes, you just have to remove a field!
  883. /// @returns True on success.
  884. static bool removeField(const char* in_pFieldname);
  885. /// @}
  886. /// @name Logging
  887. /// @{
  888. /// Overload this in subclasses to change the message formatting.
  889. /// @param fmt A printf style format string.
  890. /// @param args A va_list containing the args passed ot a log function.
  891. /// @note It is suggested that you use String::VToString.
  892. virtual String _getLogMessage(const char* fmt, va_list args) const;
  893. /// @}
  894. public:
  895. /// @name Logging
  896. /// These functions will try to print out a message along the lines
  897. /// of "ObjectClass - ObjectName(ObjectId) - formatted message"
  898. /// @{
  899. /// Logs with Con::printf.
  900. void logMessage(const char* fmt, ...) const;
  901. /// Logs with Con::warnf.
  902. void logWarning(const char* fmt, ...) const;
  903. /// Logs with Con::errorf.
  904. void logError(const char* fmt, ...) const;
  905. /// @}
  906. /// Register dynamic fields in a subclass of ConsoleObject.
  907. ///
  908. /// @see addField(), addFieldV(), addDeprecatedField(), addGroup(), endGroup()
  909. static void initPersistFields();
  910. /// Register global constant variables and do other one-time initialization tasks in
  911. /// a subclass of ConsoleObject.
  912. ///
  913. /// @deprecated You should use ConsoleMethod and ConsoleFunction, not this, to
  914. /// register methods or commands.
  915. /// @see console
  916. static void consoleInit();
  917. /// @name Field List
  918. /// @{
  919. /// Get a list of all the fields. This information cannot be modified.
  920. const AbstractClassRep::FieldList& getFieldList() const;
  921. /// Get a list of all the fields, set up so we can modify them.
  922. ///
  923. /// @note This is a bad trick to pull if you aren't very careful,
  924. /// since you can blast field data!
  925. AbstractClassRep::FieldList& getModifiableFieldList();
  926. /// Get a handle to a boolean telling us if we expanded the dynamic group.
  927. ///
  928. /// @see GuiInspector::Inspect()
  929. bool& getDynamicGroupExpand();
  930. /// @}
  931. /// @name ConsoleObject Implementation
  932. ///
  933. /// These functions are implemented in every subclass of
  934. /// ConsoleObject by an IMPLEMENT_CONOBJECT or IMPLEMENT_CO_* macro.
  935. /// @{
  936. /// Get the abstract class information for this class.
  937. static AbstractClassRep *getStaticClassRep() { return NULL; }
  938. /// Get the abstract class information for this class's superclass.
  939. static AbstractClassRep *getParentStaticClassRep() { return NULL; }
  940. /// Get our network-layer class id.
  941. ///
  942. /// @param netClassGroup The net class for which we want our ID.
  943. /// @see
  944. S32 getClassId(U32 netClassGroup) const;
  945. /// Get our compiler and platform independent class name.
  946. ///
  947. /// @note This name can be used to instantiate another instance using create()
  948. StringTableEntry getClassName() const;
  949. /// @}
  950. static const char* __category() { return ""; }
  951. static const char* __description() { return ""; }
  952. /// Subclasses of ConsoleObjects that are datablocks should redefine this static member variable
  953. /// and set it to true.
  954. static const bool __smIsDatablock = false;
  955. /// @name Object IDs and lookup.
  956. /// For a subclass hierarchy based on ConsoleObject to become functional for use as a console object type,
  957. /// the hierarchy must implement a naming scheme and indexing function for looking up objects by name.
  958. /// @{
  959. static ConsoleObject* __findObject( const char* ) { return NULL; }
  960. static const char* __getObjectId( ConsoleObject* ) { return ""; }
  961. protected:
  962. static bool disableFieldSubstitutions(const char* in_pFieldname);
  963. static bool onlyKeepClearSubstitutions(const char* in_pFieldname);
  964. };
  965. #define addNamedField(fieldName,type,className) addField(#fieldName, type, Offset(fieldName,className))
  966. #define addNamedFieldV(fieldName,type,className, validator) addFieldV(#fieldName, type, Offset(fieldName,className), validator)
  967. //------------------------------------------------------------------------------
  968. //-------------------------------------- Inlines
  969. //
  970. inline S32 ConsoleObject::getClassId(U32 netClassGroup) const
  971. {
  972. AssertFatal(getClassRep() != NULL,"Cannot get tag from non-declared dynamic class!");
  973. return getClassRep()->getClassId(netClassGroup);
  974. }
  975. inline StringTableEntry ConsoleObject::getClassName() const
  976. {
  977. AssertFatal(getClassRep() != NULL,
  978. "Cannot get tag from non-declared dynamic class");
  979. return getClassRep()->getClassName();
  980. }
  981. inline const AbstractClassRep::Field * ConsoleObject::findField(StringTableEntry name) const
  982. {
  983. AssertFatal(getClassRep() != NULL,
  984. avar("Cannot get field '%s' from non-declared dynamic class.", name));
  985. return getClassRep()->findField(name);
  986. }
  987. inline bool ConsoleObject::setField(const char *fieldName, const char *value)
  988. {
  989. //sanity check
  990. if ((! fieldName) || (! fieldName[0]) || (! value))
  991. return false;
  992. if (! getClassRep())
  993. return false;
  994. const AbstractClassRep::Field *myField = getClassRep()->findField(StringTable->insert(fieldName));
  995. if (! myField)
  996. return false;
  997. Con::setData(
  998. myField->type,
  999. (void *) (((const char *)(this)) + myField->offset),
  1000. 0,
  1001. 1,
  1002. &value,
  1003. myField->table,
  1004. myField->flag);
  1005. return true;
  1006. }
  1007. inline ConsoleObject* ConsoleObject::create(const char* in_pClassName)
  1008. {
  1009. return AbstractClassRep::create(in_pClassName);
  1010. }
  1011. inline ConsoleObject* ConsoleObject::create(const U32 groupId, const U32 typeId, const U32 in_classId)
  1012. {
  1013. return AbstractClassRep::create(groupId, typeId, in_classId);
  1014. }
  1015. inline const AbstractClassRep::FieldList& ConsoleObject::getFieldList() const
  1016. {
  1017. return getClassRep()->mFieldList;
  1018. }
  1019. inline AbstractClassRep::FieldList& ConsoleObject::getModifiableFieldList()
  1020. {
  1021. return getClassRep()->mFieldList;
  1022. }
  1023. inline bool& ConsoleObject::getDynamicGroupExpand()
  1024. {
  1025. return getClassRep()->mDynamicGroupExpand;
  1026. }
  1027. /// @name ConsoleObject Macros
  1028. /// @{
  1029. #define DECLARE_CONOBJECT( className ) \
  1030. DECLARE_CLASS( className, Parent ); \
  1031. static S32 _smTypeId; \
  1032. static ConcreteClassRep< className > dynClassRep; \
  1033. static AbstractClassRep* getParentStaticClassRep(); \
  1034. static AbstractClassRep* getStaticClassRep(); \
  1035. static SimObjectRefConsoleBaseType< className > ptrRefType; \
  1036. static AbstractClassRep::WriteCustomTamlSchema getStaticWriteCustomTamlSchema(); \
  1037. static AbstractClassRep* getContainerChildStaticClassRep(); \
  1038. AbstractClassRep* getClassRep() const override
  1039. #define DECLARE_CATEGORY( string ) \
  1040. static const char* __category() { return string; }
  1041. #define DECLARE_DESCRIPTION( string ) \
  1042. static const char* __description() { return string; }
  1043. #define IMPLEMENT_CONOBJECT( className ) \
  1044. IMPLEMENT_CLASS( className, NULL ) \
  1045. END_IMPLEMENT_CLASS; \
  1046. S32 className::_smTypeId; \
  1047. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1048. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1049. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1050. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1051. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1052. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1053. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1054. #define IMPLEMENT_CONOBJECT_CHILDREN( className ) \
  1055. IMPLEMENT_CLASS( className, NULL ) \
  1056. END_IMPLEMENT_CLASS; \
  1057. S32 className::_smTypeId; \
  1058. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1059. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1060. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1061. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1062. AbstractClassRep* className::getContainerChildStaticClassRep() { return Children::getStaticClassRep(); } \
  1063. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1064. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1065. #define IMPLEMENT_CONOBJECT_SCHEMA( className, schema ) \
  1066. IMPLEMENT_CLASS( className, NULL ) \
  1067. END_IMPLEMENT_CLASS; \
  1068. S32 className::_smTypeId; \
  1069. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1070. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1071. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1072. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1073. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1074. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return schema; } \
  1075. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1076. #define IMPLEMENT_CONOBJECT_CHILDREN_SCHEMA( className, schema ) \
  1077. IMPLEMENT_CLASS( className, NULL ) \
  1078. END_IMPLEMENT_CLASS; \
  1079. S32 className::_smTypeId; \
  1080. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1081. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1082. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1083. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1084. AbstractClassRep* className::getContainerChildStaticClassRep() { return Children::getStaticClassRep(); } \
  1085. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return schema; } \
  1086. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1087. #define IMPLEMENT_ABSTRACT_CONOBJECT( className ) \
  1088. IMPLEMENT_NONINSTANTIABLE_CLASS( className, NULL ) \
  1089. END_IMPLEMENT_CLASS; \
  1090. S32 className::_smTypeId; \
  1091. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1092. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1093. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1094. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1095. ConcreteAbstractClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, 0, -1, 0, className::getParentStaticClassRep(), &Parent::__description )
  1096. #define IMPLEMENT_CO_NETOBJECT_V1( className ) \
  1097. IMPLEMENT_CLASS( className, NULL ) \
  1098. END_IMPLEMENT_CLASS; \
  1099. S32 className::_smTypeId; \
  1100. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1101. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1102. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1103. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1104. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1105. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1106. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, NetClassGroupGameMask, NetClassTypeObject, 0, className::getParentStaticClassRep(), &Parent::__description )
  1107. #define IMPLEMENT_CO_DATABLOCK_V1( className ) \
  1108. IMPLEMENT_CLASS( className, NULL ) \
  1109. END_IMPLEMENT_CLASS; \
  1110. S32 className::_smTypeId; \
  1111. SimObjectRefConsoleBaseType< className > className::ptrRefType( "Type" #className "Ref" ); \
  1112. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  1113. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  1114. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  1115. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  1116. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  1117. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId, NetClassGroupGameMask, NetClassTypeDataBlock, 0, className::getParentStaticClassRep(), &Parent::__description )
  1118. // Support for adding properties to classes CONOBJECT style.
  1119. #define PROPERTY_TABLE( className ) \
  1120. namespace { namespace _ ## className { \
  1121. extern EnginePropertyTable _propTable; \
  1122. } } \
  1123. template<> EnginePropertyTable& \
  1124. ConcreteClassRep< className >::smPropertyTable = _ ## className::_propTable; \
  1125. namespace { namespace _ ## className { \
  1126. EnginePropertyTable::Property _props[] = {
  1127. #define END_PROPERTY_TABLE \
  1128. { NULL } \
  1129. }; \
  1130. EnginePropertyTable _propTable( sizeof( _props ) / sizeof( _props[ 0 ] ) - 1, _props ); \
  1131. } }
  1132. /// @}
  1133. //------------------------------------------------------------------------------
  1134. // Protected field default get/set functions
  1135. //
  1136. // The reason for these functions is that it will save one branch per console
  1137. // data request and script functions will still execute at the same speed as
  1138. // before the modifications to allow protected static fields. These will just
  1139. // inline and the code should be roughly the same size, and just as fast as
  1140. // before the modifications. -pw
  1141. inline bool defaultProtectedSetFn( void *object, const char *index, const char *data )
  1142. {
  1143. return true;
  1144. }
  1145. inline bool defaultProtectedSetNotEmptyFn( void *object, const char *index, const char *data )
  1146. {
  1147. return data && data[0];
  1148. }
  1149. inline const char *defaultProtectedGetFn( void *obj, const char *data )
  1150. {
  1151. return data;
  1152. }
  1153. inline const char *emptyStringProtectedGetFn( void *obj, const char *data )
  1154. {
  1155. return "";
  1156. }
  1157. inline bool defaultProtectedWriteFn(void* obj, StringTableEntry pFieldName)
  1158. {
  1159. return true;
  1160. }
  1161. inline bool defaultProtectedNotSetFn(void* obj, const char *array, const char* data)
  1162. {
  1163. return false;
  1164. }
  1165. inline bool defaultProtectedNotWriteFn(void* obj, StringTableEntry pFieldName)
  1166. {
  1167. return false;
  1168. }
  1169. /// @}
  1170. #endif //_CONSOLEOBJECT_H_