taml.h 8.5 KB

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