tamlBinaryWriter.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include "persistence/taml/binary/tamlBinaryWriter.h"
  23. #ifndef _ZIPSUBSTREAM_H_
  24. #include "core/util/zip/zipSubStream.h"
  25. #endif
  26. // Debug Profiling.
  27. #include "platform/profiler.h"
  28. //-----------------------------------------------------------------------------
  29. bool TamlBinaryWriter::write( FileStream& stream, const TamlWriteNode* pTamlWriteNode, const bool compressed )
  30. {
  31. // Debug Profiling.
  32. PROFILE_SCOPE(TamlBinaryWriter_Write);
  33. // Write Taml signature.
  34. stream.writeString( StringTable->insert( TAML_SIGNATURE ) );
  35. // Write version Id.
  36. stream.write( mVersionId );
  37. // Write compressed flag.
  38. stream.write( compressed );
  39. // Are we compressed?
  40. if ( compressed )
  41. {
  42. // yes, so attach zip stream.
  43. ZipSubWStream zipStream;
  44. zipStream.attachStream( &stream );
  45. // Write element.
  46. writeElement( zipStream, pTamlWriteNode );
  47. // Detach zip stream.
  48. zipStream.detachStream();
  49. }
  50. else
  51. {
  52. // No, so write element.
  53. writeElement( stream, pTamlWriteNode );
  54. }
  55. return true;
  56. }
  57. //-----------------------------------------------------------------------------
  58. void TamlBinaryWriter::writeElement( Stream& stream, const TamlWriteNode* pTamlWriteNode )
  59. {
  60. // Debug Profiling.
  61. PROFILE_SCOPE(TamlBinaryWriter_WriteElement);
  62. // Fetch object.
  63. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  64. // Fetch element name.
  65. const char* pElementName = pSimObject->getClassName();
  66. // Write element name.
  67. stream.writeString( pElementName );
  68. // Fetch object name.
  69. const char* pObjectName = pTamlWriteNode->mpObjectName;
  70. // Write object name.
  71. stream.writeString( pObjectName != NULL ? pObjectName : StringTable->EmptyString() );
  72. // Fetch reference Id.
  73. const U32 tamlRefId = pTamlWriteNode->mRefId;
  74. // Write reference Id.
  75. stream.write( tamlRefId );
  76. // Do we have a reference to node?
  77. if ( pTamlWriteNode->mRefToNode != NULL )
  78. {
  79. // Yes, so fetch reference to Id.
  80. const U32 tamlRefToId = pTamlWriteNode->mRefToNode->mRefId;
  81. // Sanity!
  82. AssertFatal( tamlRefToId != 0, "Taml: Invalid reference to Id." );
  83. // Write reference to Id.
  84. stream.write( tamlRefToId );
  85. // Finished.
  86. return;
  87. }
  88. // No, so write no reference to Id.
  89. stream.write( 0 );
  90. // Write attributes.
  91. writeAttributes( stream, pTamlWriteNode );
  92. // Write children.
  93. writeChildren( stream, pTamlWriteNode );
  94. // Write custom elements.
  95. writeCustomElements( stream, pTamlWriteNode );
  96. }
  97. //-----------------------------------------------------------------------------
  98. void TamlBinaryWriter::writeAttributes( Stream& stream, const TamlWriteNode* pTamlWriteNode )
  99. {
  100. // Debug Profiling.
  101. PROFILE_SCOPE(TamlBinaryWriter_WriteAttributes);
  102. // Fetch fields.
  103. const Vector<TamlWriteNode::FieldValuePair*>& fields = pTamlWriteNode->mFields;
  104. // Write placeholder attribute count.
  105. stream.write( (U32)fields.size() );
  106. // Finish if no fields.
  107. if ( fields.size() == 0 )
  108. return;
  109. // Iterate fields.
  110. for( Vector<TamlWriteNode::FieldValuePair*>::const_iterator itr = fields.begin(); itr != fields.end(); ++itr )
  111. {
  112. // Fetch field/value pair.
  113. TamlWriteNode::FieldValuePair* pFieldValue = (*itr);
  114. // Write attribute.
  115. stream.writeString( pFieldValue->mName );
  116. stream.writeLongString( 4096, pFieldValue->mpValue );
  117. }
  118. }
  119. void TamlBinaryWriter::writeChildren( Stream& stream, const TamlWriteNode* pTamlWriteNode )
  120. {
  121. // Debug Profiling.
  122. PROFILE_SCOPE(TamlBinaryWriter_WriteChildren);
  123. // Fetch children.
  124. Vector<TamlWriteNode*>* pChildren = pTamlWriteNode->mChildren;
  125. // Do we have any children?
  126. if ( pChildren == NULL )
  127. {
  128. // No, so write no children.
  129. stream.write( (U32)0 );
  130. return;
  131. }
  132. // Write children count.
  133. stream.write( (U32)pChildren->size() );
  134. // Iterate children.
  135. for( Vector<TamlWriteNode*>::iterator itr = pChildren->begin(); itr != pChildren->end(); ++itr )
  136. {
  137. // Write child.
  138. writeElement( stream, (*itr) );
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. void TamlBinaryWriter::writeCustomElements( Stream& stream, const TamlWriteNode* pTamlWriteNode )
  143. {
  144. // Debug Profiling.
  145. PROFILE_SCOPE(TamlBinaryWriter_WriteCustomElements);
  146. // Fetch custom nodes.
  147. const TamlCustomNodes& customNodes = pTamlWriteNode->mCustomNodes;
  148. // Fetch custom nodes.
  149. const TamlCustomNodeVector& nodes = customNodes.getNodes();
  150. // Write custom node count.
  151. stream.write( (U32)nodes.size() );
  152. // Finish if there are no nodes.
  153. if ( nodes.size() == 0 )
  154. return;
  155. // Iterate custom nodes.
  156. for( TamlCustomNodeVector::const_iterator customNodesItr = nodes.begin(); customNodesItr != nodes.end(); ++customNodesItr )
  157. {
  158. // Fetch the custom node.
  159. TamlCustomNode* pCustomNode = *customNodesItr;
  160. // Write custom node name.
  161. stream.writeString( pCustomNode->getNodeName() );
  162. // Fetch node children.
  163. const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
  164. // Iterate children nodes.
  165. for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
  166. {
  167. // Fetch child node.
  168. const TamlCustomNode* pChildNode = *childNodeItr;
  169. // Write the custom node.
  170. writeCustomNode( stream, pChildNode );
  171. }
  172. }
  173. }
  174. //-----------------------------------------------------------------------------
  175. void TamlBinaryWriter::writeCustomNode( Stream& stream, const TamlCustomNode* pCustomNode )
  176. {
  177. // Is the node a proxy object?
  178. if ( pCustomNode->isProxyObject() )
  179. {
  180. // Yes, so flag as proxy object.
  181. stream.write( true );
  182. // Write the element.
  183. writeElement( stream, pCustomNode->getProxyWriteNode() );
  184. return;
  185. }
  186. // No, so flag as custom node.
  187. stream.write( false );
  188. // Write custom node name.
  189. stream.writeString( pCustomNode->getNodeName() );
  190. // Write custom node text.
  191. stream.writeLongString(MAX_TAML_NODE_FIELDVALUE_LENGTH, pCustomNode->getNodeTextField().getFieldValue());
  192. // Fetch node children.
  193. const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
  194. // Fetch child node count.
  195. const U32 childNodeCount = (U32)nodeChildren.size();
  196. // Write custom node count.
  197. stream.write( childNodeCount );
  198. // Do we have any children nodes.
  199. if ( childNodeCount > 0 )
  200. {
  201. // Yes, so iterate children nodes.
  202. for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
  203. {
  204. // Fetch child node.
  205. const TamlCustomNode* pChildNode = *childNodeItr;
  206. // Write the custom node.
  207. writeCustomNode( stream, pChildNode );
  208. }
  209. }
  210. // Fetch fields.
  211. const TamlCustomFieldVector& fields = pCustomNode->getFields();
  212. // Fetch child field count.
  213. const U32 childFieldCount = (U32)fields.size();
  214. // Write custom field count.
  215. stream.write( childFieldCount );
  216. // Do we have any child fields?
  217. if ( childFieldCount > 0 )
  218. {
  219. // Yes, so iterate fields.
  220. for ( TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr )
  221. {
  222. // Fetch node field.
  223. const TamlCustomField* pField = *fieldItr;
  224. // Write the node field.
  225. stream.writeString( pField->getFieldName() );
  226. stream.writeLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, pField->getFieldValue() );
  227. }
  228. }
  229. }