taml.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 _TAML_H_
  23. #define _TAML_H_
  24. #ifndef _TAML_CALLBACKS_H_
  25. #include "persistence/taml/tamlCallbacks.h"
  26. #endif
  27. #ifndef _TAML_CUSTOM_H_
  28. #include "persistence/taml/tamlCustom.h"
  29. #endif
  30. #ifndef _TAML_CHILDREN_H_
  31. #include "persistence/taml/tamlChildren.h"
  32. #endif
  33. #ifndef _TAML_WRITE_NODE_H_
  34. #include "persistence/taml/tamlWriteNode.h"
  35. #endif
  36. #ifndef _TAML_VISITOR_H_
  37. #include "persistence/taml/tamlVisitor.h"
  38. #endif
  39. #ifndef _SIMBASE_H_
  40. #include "console/simBase.h"
  41. #endif
  42. #ifndef _TDICTIONARY_H_
  43. #include "core/util/tDictionary.h"
  44. #endif
  45. #ifndef _FILESTREAM_H_
  46. #include "core/stream/fileStream.h"
  47. #endif
  48. //-----------------------------------------------------------------------------
  49. extern StringTableEntry tamlRefIdName;
  50. extern StringTableEntry tamlRefToIdName;
  51. extern StringTableEntry tamlNamedObjectName;
  52. //-----------------------------------------------------------------------------
  53. #define TAML_SIGNATURE "Taml"
  54. #define TAML_SCHEMA_VARIABLE "$pref::T2D::TAMLSchema"
  55. #define TAML_JSON_STRICT_VARIBLE "$pref::T2D::JSONStrict"
  56. //-----------------------------------------------------------------------------
  57. /// @ingroup tamlGroup
  58. /// @see tamlGroup
  59. class Taml : public SimObject
  60. {
  61. public:
  62. enum TamlFormatMode
  63. {
  64. InvalidFormat = 0,
  65. XmlFormat,
  66. BinaryFormat,
  67. JSONFormat,
  68. };
  69. private:
  70. typedef SimObject Parent;
  71. typedef Vector<TamlWriteNode*> typeNodeVector;
  72. typedef HashTable<SimObjectId, TamlWriteNode*> typeCompiledHash;
  73. typeNodeVector mCompiledNodes;
  74. typeCompiledHash mCompiledObjects;
  75. U32 mMasterNodeId;
  76. TamlFormatMode mFormatMode;
  77. StringTableEntry mAutoFormatXmlExtension;
  78. StringTableEntry mAutoFormatBinaryExtension;
  79. StringTableEntry mAutoFormatJSONExtension;
  80. bool mJSONStrict;
  81. bool mBinaryCompression;
  82. bool mAutoFormat;
  83. bool mWriteDefaults;
  84. bool mProgenitorUpdate;
  85. char mFilePathBuffer[1024];
  86. private:
  87. void resetCompilation( void );
  88. TamlWriteNode* compileObject( SimObject* pSimObject, const bool forceId = false );
  89. void compileStaticFields( TamlWriteNode* pTamlWriteNode );
  90. void compileDynamicFields( TamlWriteNode* pTamlWriteNode );
  91. void compileChildren( TamlWriteNode* pTamlWriteNode );
  92. void compileCustomState( TamlWriteNode* pTamlWriteNode );
  93. void compileCustomNodeState( TamlCustomNode* pCustomNode );
  94. bool write( FileStream& stream, SimObject* pSimObject, const TamlFormatMode formatMode );
  95. SimObject* read( FileStream& stream, const TamlFormatMode formatMode );
  96. template<typename T> inline T* read( FileStream& stream, const TamlFormatMode formatMode )
  97. {
  98. SimObject* pSimObject = read( stream, formatMode );
  99. if ( pSimObject == NULL )
  100. return NULL;
  101. T* pObj = dynamic_cast<T*>( pSimObject );
  102. if ( pObj != NULL )
  103. return pObj;
  104. pSimObject->deleteObject();
  105. return NULL;
  106. }
  107. public:
  108. Taml();
  109. virtual ~Taml() {}
  110. virtual bool onAdd();
  111. virtual void onRemove();
  112. static void initPersistFields();
  113. /// Format mode.
  114. inline void setFormatMode( const TamlFormatMode formatMode ) { mFormatMode = formatMode != Taml::InvalidFormat ? formatMode : Taml::XmlFormat; }
  115. inline TamlFormatMode getFormatMode( void ) const { return mFormatMode; }
  116. /// Auto-Format mode.
  117. inline void setAutoFormat( const bool autoFormat ) { mAutoFormat = autoFormat; }
  118. inline bool getAutoFormat( void ) const { return mAutoFormat; }
  119. /// Write defaults.
  120. inline void setWriteDefaults( const bool writeDefaults ) { mWriteDefaults = writeDefaults; }
  121. inline bool getWriteDefaults( void ) const { return mWriteDefaults; }
  122. /// Progenitor.
  123. inline void setProgenitorUpdate( const bool progenitorUpdate ) { mProgenitorUpdate = progenitorUpdate; }
  124. inline bool getProgenitorUpdate( void ) const { return mProgenitorUpdate; }
  125. /// Auto-format extensions.
  126. inline void setAutoFormatXmlExtension( const char* pExtension ) { mAutoFormatXmlExtension = StringTable->insert( pExtension ); }
  127. inline StringTableEntry getAutoFormatXmlExtension( void ) const { return mAutoFormatXmlExtension; }
  128. inline void setAutoFormatBinaryExtension( const char* pExtension ) { mAutoFormatBinaryExtension = StringTable->insert( pExtension ); }
  129. inline StringTableEntry getAutoFormatBinaryExtension( void ) const { return mAutoFormatBinaryExtension; }
  130. /// Compression.
  131. inline void setBinaryCompression( const bool compressed ) { mBinaryCompression = compressed; }
  132. inline bool getBinaryCompression( void ) const { return mBinaryCompression; }
  133. /// JSON Strict RFC4627 mode.
  134. inline void setJSONStrict( const bool jsonStrict ) { mJSONStrict = jsonStrict; }
  135. inline bool getJSONStrict( void ) const { return mJSONStrict; }
  136. TamlFormatMode getFileAutoFormatMode( const char* pFilename );
  137. const char* getFilePathBuffer( void ) const { return mFilePathBuffer; }
  138. /// Write.
  139. bool write( SimObject* pSimObject, const char* pFilename );
  140. /// Read.
  141. template<typename T> inline T* read( const char* pFilename )
  142. {
  143. SimObject* pSimObject = read( pFilename );
  144. if ( pSimObject == NULL )
  145. return NULL;
  146. T* pObj = dynamic_cast<T*>( pSimObject );
  147. if ( pObj != NULL )
  148. return pObj;
  149. pSimObject->deleteObject();
  150. return NULL;
  151. }
  152. SimObject* read( const char* pFilename );
  153. /// Parse.
  154. bool parse( const char* pFilename, TamlVisitor& visitor );
  155. /// Create type.
  156. static SimObject* createType( StringTableEntry typeName, const Taml* pTaml, const char* pProgenitorSuffix = NULL );
  157. /// Schema generation.
  158. static bool generateTamlSchema();
  159. /// Write a unrestricted custom Taml schema.
  160. static void WriteUnrestrictedCustomTamlSchema( const char* pCustomNodeName, const AbstractClassRep* pClassRep, tinyxml2::XMLElement* pParentElement );
  161. /// Get format mode info.
  162. static TamlFormatMode getFormatModeEnum( const char* label );
  163. static const char* getFormatModeDescription( const TamlFormatMode formatMode );
  164. /// Taml callbacks.
  165. inline void tamlPreWrite( TamlCallbacks* pCallbacks ) { pCallbacks->onTamlPreWrite(); }
  166. inline void tamlPostWrite( TamlCallbacks* pCallbacks ) { pCallbacks->onTamlPostWrite(); }
  167. inline void tamlPreRead( TamlCallbacks* pCallbacks ) { pCallbacks->onTamlPreRead(); }
  168. inline void tamlPostRead( TamlCallbacks* pCallbacks, const TamlCustomNodes& customNodes ) { pCallbacks->onTamlPostRead( customNodes ); }
  169. inline void tamlAddParent( TamlCallbacks* pCallbacks, SimObject* pParentObject ) { pCallbacks->onTamlAddParent( pParentObject ); }
  170. inline void tamlCustomWrite( TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes ) { pCallbacks->onTamlCustomWrite( customNodes ); }
  171. inline void tamlCustomRead( TamlCallbacks* pCallbacks, const TamlCustomNodes& customNodes ) { pCallbacks->onTamlCustomRead( customNodes ); }
  172. /// Declare Console Object.
  173. DECLARE_CONOBJECT( Taml );
  174. };
  175. #endif // _TAML_H_