taml.h 8.4 KB

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