taml.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 _SIMBASE_H_
  37. #include "sim/simBase.h"
  38. #endif
  39. #ifndef _HASHTABLE_H
  40. #include "collection/hashTable.h"
  41. #endif
  42. #ifndef _FILESTREAM_H_
  43. #include "io/fileStream.h"
  44. #endif
  45. //-----------------------------------------------------------------------------
  46. extern StringTableEntry tamlRefIdName;
  47. extern StringTableEntry tamlRefToIdName;
  48. extern StringTableEntry tamlNamedObjectName;
  49. //-----------------------------------------------------------------------------
  50. #define TAML_SIGNATURE "Taml"
  51. #define TAML_SCHEMA_VARIABLE "$pref::T2D::TAMLSchema"
  52. //-----------------------------------------------------------------------------
  53. class TamlXmlWriter;
  54. class TamlXmlReader;
  55. class TamlBinaryWriter;
  56. class TamlBinaryReader;
  57. //-----------------------------------------------------------------------------
  58. class Taml : public SimObject
  59. {
  60. friend class TamlXmlWriter;
  61. friend class TamlXmlReader;
  62. friend class TamlBinaryWriter;
  63. friend class TamlBinaryReader;
  64. public:
  65. enum TamlFormatMode
  66. {
  67. InvalidFormat = 0,
  68. XmlFormat,
  69. BinaryFormat
  70. };
  71. private:
  72. typedef SimObject Parent;
  73. typedef Vector<TamlWriteNode*> typeNodeVector;
  74. typedef HashMap<SimObjectId, TamlWriteNode*> typeCompiledHash;
  75. typeNodeVector mCompiledNodes;
  76. typeCompiledHash mCompiledObjects;
  77. U32 mMasterNodeId;
  78. TamlFormatMode mFormatMode;
  79. bool mBinaryCompression;
  80. bool mAutoFormat;
  81. StringTableEntry mAutoFormatXmlExtension;
  82. StringTableEntry mAutoFormatBinaryExtension;
  83. bool mWriteDefaults;
  84. char mFilePathBuffer[1024];
  85. bool mProgenitorUpdate;
  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. static SimObject* createType( StringTableEntry typeName, const Taml* pTaml, const char* pProgenitorSuffix = NULL );
  108. /// Taml callbacks.
  109. inline void tamlPreWrite( TamlCallbacks* pCallbacks ) { pCallbacks->onTamlPreWrite(); }
  110. inline void tamlPostWrite( TamlCallbacks* pCallbacks ) { pCallbacks->onTamlPostWrite(); }
  111. inline void tamlPreRead( TamlCallbacks* pCallbacks ) { pCallbacks->onTamlPreRead(); }
  112. inline void tamlPostRead( TamlCallbacks* pCallbacks, const TamlCustomNodes& customNodes ) { pCallbacks->onTamlPostRead( customNodes ); }
  113. inline void tamlAddParent( TamlCallbacks* pCallbacks, SimObject* pParentObject ) { pCallbacks->onTamlAddParent( pParentObject ); }
  114. inline void tamlCustomWrite( TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes ) { pCallbacks->onTamlCustomWrite( customNodes ); }
  115. inline void tamlCustomRead( TamlCallbacks* pCallbacks, const TamlCustomNodes& customNodes ) { pCallbacks->onTamlCustomRead( customNodes ); }
  116. public:
  117. Taml();
  118. virtual ~Taml() {}
  119. virtual bool onAdd() { if ( !Parent::onAdd() ) return false; resetCompilation(); return true; }
  120. virtual void onRemove() { resetCompilation(); Parent::onRemove(); }
  121. static void initPersistFields();
  122. /// Format mode.
  123. inline void setFormatMode( const TamlFormatMode formatMode ) { mFormatMode = formatMode != Taml::InvalidFormat ? formatMode : Taml::XmlFormat; }
  124. inline TamlFormatMode getFormatMode( void ) const { return mFormatMode; }
  125. /// Auto-Format mode.
  126. inline void setAutoFormat( const bool autoFormat ) { mAutoFormat = autoFormat; }
  127. inline bool getAutoFormat( void ) const { return mAutoFormat; }
  128. /// Write defaults.
  129. inline void setWriteDefaults( const bool writeDefaults ) { mWriteDefaults = writeDefaults; }
  130. inline bool getWriteDefaults( void ) const { return mWriteDefaults; }
  131. inline void setProgenitorUpdate( const bool progenitorUpdate ) { mProgenitorUpdate = progenitorUpdate; }
  132. inline bool getProgenitorUpdate( void ) const { return mProgenitorUpdate; }
  133. // Auto-format extensions.
  134. inline void setAutoFormatXmlExtension( const char* pExtension ) { mAutoFormatXmlExtension = StringTable->insert( pExtension ); }
  135. inline StringTableEntry getAutoFormatXmlExtension( void ) const { return mAutoFormatXmlExtension; }
  136. inline void setAutoFormatBinaryExtension( const char* pExtension ) { mAutoFormatBinaryExtension = StringTable->insert( pExtension ); }
  137. inline StringTableEntry getAutoFormatBinaryExtension( void ) const { return mAutoFormatBinaryExtension; }
  138. /// Compression.
  139. inline void setBinaryCompression( const bool compressed ) { mBinaryCompression = compressed; }
  140. inline bool getBinaryCompression( void ) const { return mBinaryCompression; }
  141. TamlFormatMode getFileAutoFormatMode( const char* pFilename );
  142. const char* getFilePathBuffer( void ) const { return mFilePathBuffer; }
  143. /// Write.
  144. bool write( SimObject* pSimObject, const char* pFilename );
  145. /// Read.
  146. template<typename T> inline T* read( const char* pFilename )
  147. {
  148. SimObject* pSimObject = read( pFilename );
  149. if ( pSimObject == NULL )
  150. return NULL;
  151. T* pObj = dynamic_cast<T*>( pSimObject );
  152. if ( pObj != NULL )
  153. return pObj;
  154. pSimObject->deleteObject();
  155. return NULL;
  156. }
  157. SimObject* read( const char* pFilename );
  158. static TamlFormatMode getFormatModeEnum( const char* label );
  159. static const char* getFormatModeDescription( const TamlFormatMode formatMode );
  160. /// Schema generation.
  161. static bool generateTamlSchema();
  162. /// Write a unrestricted custom Taml schema.
  163. static void WriteUnrestrictedCustomTamlSchema( const char* pCustomNodeName, const AbstractClassRep* pClassRep, TiXmlElement* pParentElement );
  164. /// Declare Console Object.
  165. DECLARE_CONOBJECT( Taml );
  166. };
  167. #endif // _TAML_H_