tamlBinaryReader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/tamlBinaryReader.h"
  23. #ifndef _ZIPSUBSTREAM_H_
  24. #include "core/util/zip/zipSubStream.h"
  25. #endif
  26. // Debug Profiling.
  27. #include "platform/profiler.h"
  28. //-----------------------------------------------------------------------------
  29. SimObject* TamlBinaryReader::read( FileStream& stream )
  30. {
  31. // Debug Profiling.
  32. PROFILE_SCOPE(TamlBinaryReader_Read);
  33. // Read Taml signature.
  34. StringTableEntry tamlSignature = stream.readSTString();
  35. // Is the signature correct?
  36. if ( tamlSignature != StringTable->insert( TAML_SIGNATURE ) )
  37. {
  38. // Warn.
  39. Con::warnf("Taml: Cannot read binary file as signature is incorrect '%s'.", tamlSignature );
  40. return NULL;
  41. }
  42. // Read version Id.
  43. U32 versionId;
  44. stream.read( &versionId );
  45. // Read compressed flag.
  46. bool compressed;
  47. stream.read( &compressed );
  48. SimObject* pSimObject = NULL;
  49. // Is the stream compressed?
  50. if ( compressed )
  51. {
  52. // Yes, so attach zip stream.
  53. ZipSubRStream zipStream;
  54. zipStream.attachStream( &stream );
  55. // Parse element.
  56. pSimObject = parseElement( zipStream, versionId );
  57. // Detach zip stream.
  58. zipStream.detachStream();
  59. }
  60. else
  61. {
  62. // No, so parse element.
  63. pSimObject = parseElement( stream, versionId );
  64. }
  65. return pSimObject;
  66. }
  67. //-----------------------------------------------------------------------------
  68. void TamlBinaryReader::resetParse( void )
  69. {
  70. // Debug Profiling.
  71. PROFILE_SCOPE(TamlBinaryReader_ResetParse);
  72. // Clear object reference map.
  73. mObjectReferenceMap.clear();
  74. }
  75. //-----------------------------------------------------------------------------
  76. SimObject* TamlBinaryReader::parseElement( Stream& stream, const U32 versionId )
  77. {
  78. // Debug Profiling.
  79. PROFILE_SCOPE(TamlBinaryReader_ParseElement);
  80. SimObject* pSimObject = NULL;
  81. #ifdef TORQUE_DEBUG
  82. // Format the type location.
  83. char typeLocationBuffer[64];
  84. dSprintf( typeLocationBuffer, sizeof(typeLocationBuffer), "Taml [format='binary' offset=%u]", stream.getPosition() );
  85. #endif
  86. // Fetch element name.
  87. StringTableEntry typeName = stream.readSTString();
  88. // Fetch object name.
  89. StringTableEntry objectName = stream.readSTString();
  90. // Read references.
  91. U32 tamlRefId;
  92. U32 tamlRefToId;
  93. stream.read( &tamlRefId );
  94. stream.read( &tamlRefToId );
  95. // Do we have a reference to Id?
  96. if ( tamlRefToId != 0 )
  97. {
  98. // Yes, so fetch reference.
  99. typeObjectReferenceHash::Iterator referenceItr = mObjectReferenceMap.find( tamlRefToId );
  100. // Did we find the reference?
  101. if ( referenceItr == mObjectReferenceMap.end() )
  102. {
  103. // No, so warn.
  104. Con::warnf( "Taml: Could not find a reference Id of '%d'", tamlRefToId );
  105. return NULL;
  106. }
  107. // Return object.
  108. return referenceItr->value;
  109. }
  110. #ifdef TORQUE_DEBUG
  111. // Create type.
  112. pSimObject = Taml::createType( typeName, mpTaml, typeLocationBuffer );
  113. #else
  114. // Create type.
  115. pSimObject = Taml::createType( typeName, mpTaml );
  116. #endif
  117. // Finish if we couldn't create the type.
  118. if ( pSimObject == NULL )
  119. return NULL;
  120. // Find Taml callbacks.
  121. TamlCallbacks* pCallbacks = dynamic_cast<TamlCallbacks*>( pSimObject );
  122. // Are there any Taml callbacks?
  123. if ( pCallbacks != NULL )
  124. {
  125. // Yes, so call it.
  126. mpTaml->tamlPreRead( pCallbacks );
  127. }
  128. // Parse attributes.
  129. parseAttributes( stream, pSimObject, versionId );
  130. // Does the object require a name?
  131. if ( objectName == StringTable->EmptyString() )
  132. {
  133. // No, so just register anonymously.
  134. pSimObject->registerObject();
  135. }
  136. else
  137. {
  138. // Yes, so register a named object.
  139. pSimObject->registerObject( objectName );
  140. // Was the name assigned?
  141. if ( pSimObject->getName() != objectName )
  142. {
  143. // No, so warn that the name was rejected.
  144. #ifdef TORQUE_DEBUG
  145. Con::warnf( "Taml::parseElement() - Registered an instance of type '%s' but a request to name it '%s' was rejected. This is typically because an object of that name already exists. '%s'", typeName, objectName, typeLocationBuffer );
  146. #else
  147. Con::warnf( "Taml::parseElement() - Registered an instance of type '%s' but a request to name it '%s' was rejected. This is typically because an object of that name already exists.", typeName, objectName );
  148. #endif
  149. }
  150. }
  151. // Do we have a reference Id?
  152. if ( tamlRefId != 0 )
  153. {
  154. // Yes, so insert reference.
  155. mObjectReferenceMap.insertUnique( tamlRefId, pSimObject );
  156. }
  157. // Parse custom elements.
  158. TamlCustomNodes customProperties;
  159. // Parse children.
  160. parseChildren( stream, pCallbacks, pSimObject, versionId );
  161. // Parse custom elements.
  162. parseCustomElements( stream, pCallbacks, customProperties, versionId );
  163. // Are there any Taml callbacks?
  164. if ( pCallbacks != NULL )
  165. {
  166. // Yes, so call it.
  167. mpTaml->tamlPostRead( pCallbacks, customProperties );
  168. }
  169. // Return object.
  170. return pSimObject;
  171. }
  172. //-----------------------------------------------------------------------------
  173. void TamlBinaryReader::parseAttributes( Stream& stream, SimObject* pSimObject, const U32 versionId )
  174. {
  175. // Debug Profiling.
  176. PROFILE_SCOPE(TamlBinaryReader_ParseAttributes);
  177. // Sanity!
  178. AssertFatal( pSimObject != NULL, "Taml: Cannot parse attributes on a NULL object." );
  179. // Fetch attribute count.
  180. U32 attributeCount;
  181. stream.read( &attributeCount );
  182. // Finish if no attributes.
  183. if ( attributeCount == 0 )
  184. return;
  185. char valueBuffer[4096];
  186. // Iterate attributes.
  187. for ( U32 index = 0; index < attributeCount; ++index )
  188. {
  189. // Fetch attribute.
  190. StringTableEntry attributeName = stream.readSTString();
  191. stream.readLongString( 4096, valueBuffer );
  192. // We can assume this is a field for now.
  193. pSimObject->setPrefixedDataField(attributeName, NULL, valueBuffer);
  194. }
  195. }
  196. //-----------------------------------------------------------------------------
  197. void TamlBinaryReader::parseChildren( Stream& stream, TamlCallbacks* pCallbacks, SimObject* pSimObject, const U32 versionId )
  198. {
  199. // Debug Profiling.
  200. PROFILE_SCOPE(TamlBinaryReader_ParseChildren);
  201. // Sanity!
  202. AssertFatal( pSimObject != NULL, "Taml: Cannot parse children on a NULL object." );
  203. // Fetch children count.
  204. U32 childrenCount;
  205. stream.read( &childrenCount );
  206. // Finish if no children.
  207. if ( childrenCount == 0 )
  208. return;
  209. // Fetch the Taml children.
  210. TamlChildren* pChildren = dynamic_cast<TamlChildren*>( pSimObject );
  211. // Is this a sim set?
  212. if ( pChildren == NULL )
  213. {
  214. // No, so warn.
  215. Con::warnf("Taml: Child element found under parent but object cannot have children." );
  216. return;
  217. }
  218. // Fetch any container child class specifier.
  219. AbstractClassRep* pContainerChildClass = pSimObject->getClassRep()->getContainerChildClass( true );
  220. // Iterate children.
  221. for ( U32 index = 0; index < childrenCount; ++ index )
  222. {
  223. // Parse child element.
  224. SimObject* pChildSimObject = parseElement( stream, versionId );
  225. // Finish if child failed.
  226. if ( pChildSimObject == NULL )
  227. return;
  228. // Do we have a container child class?
  229. if ( pContainerChildClass != NULL )
  230. {
  231. // Yes, so is the child object the correctly derived type?
  232. if ( !pChildSimObject->getClassRep()->isClass( pContainerChildClass ) )
  233. {
  234. // No, so warn.
  235. Con::warnf("Taml: Child element '%s' found under parent '%s' but object is restricted to children of type '%s'.",
  236. pChildSimObject->getClassName(),
  237. pSimObject->getClassName(),
  238. pContainerChildClass->getClassName() );
  239. // NOTE: We can't delete the object as it may be referenced elsewhere!
  240. pChildSimObject = NULL;
  241. // Skip.
  242. continue;
  243. }
  244. }
  245. // Add child.
  246. pChildren->addTamlChild( pChildSimObject );
  247. // Find Taml callbacks for child.
  248. TamlCallbacks* pChildCallbacks = dynamic_cast<TamlCallbacks*>( pChildSimObject );
  249. // Do we have callbacks on the child?
  250. if ( pChildCallbacks != NULL )
  251. {
  252. // Yes, so perform callback.
  253. mpTaml->tamlAddParent( pChildCallbacks, pSimObject );
  254. }
  255. }
  256. }
  257. //-----------------------------------------------------------------------------
  258. void TamlBinaryReader::parseCustomElements( Stream& stream, TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes, const U32 versionId )
  259. {
  260. // Debug Profiling.
  261. PROFILE_SCOPE(TamlBinaryReader_ParseCustomElement);
  262. // Read custom node count.
  263. U32 customNodeCount;
  264. stream.read( &customNodeCount );
  265. // Finish if no custom nodes.
  266. if ( customNodeCount == 0 )
  267. return;
  268. // Iterate custom nodes.
  269. for ( U32 nodeIndex = 0; nodeIndex < customNodeCount; ++nodeIndex )
  270. {
  271. //Read custom node name.
  272. StringTableEntry nodeName = stream.readSTString();
  273. // Add custom node.
  274. TamlCustomNode* pCustomNode = customNodes.addNode( nodeName );
  275. // Parse the custom node.
  276. parseCustomNode( stream, pCustomNode, versionId );
  277. }
  278. // Do we have callbacks?
  279. if ( pCallbacks == NULL )
  280. {
  281. // No, so warn.
  282. Con::warnf( "Taml: Encountered custom data but object does not support custom data." );
  283. return;
  284. }
  285. // Custom read callback.
  286. mpTaml->tamlCustomRead( pCallbacks, customNodes );
  287. }
  288. //-----------------------------------------------------------------------------
  289. void TamlBinaryReader::parseCustomNode( Stream& stream, TamlCustomNode* pCustomNode, const U32 versionId )
  290. {
  291. // Fetch if a proxy object.
  292. bool isProxyObject;
  293. stream.read( &isProxyObject );
  294. // Is this a proxy object?
  295. if ( isProxyObject )
  296. {
  297. // Yes, so parse proxy object.
  298. SimObject* pProxyObject = parseElement( stream, versionId );
  299. // Add child node.
  300. pCustomNode->addNode( pProxyObject );
  301. return;
  302. }
  303. // No, so read custom node name.
  304. StringTableEntry nodeName = stream.readSTString();
  305. // Add child node.
  306. TamlCustomNode* pChildNode = pCustomNode->addNode( nodeName );
  307. // Read child node text.
  308. char childNodeTextBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
  309. stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, childNodeTextBuffer );
  310. pChildNode->setNodeText( childNodeTextBuffer );
  311. // Read child node count.
  312. U32 childNodeCount;
  313. stream.read( &childNodeCount );
  314. // Do we have any children nodes?
  315. if ( childNodeCount > 0 )
  316. {
  317. // Yes, so parse children nodes.
  318. for( U32 childIndex = 0; childIndex < childNodeCount; ++childIndex )
  319. {
  320. // Parse child node.
  321. parseCustomNode( stream, pChildNode, versionId );
  322. }
  323. }
  324. // Read child field count.
  325. U32 childFieldCount;
  326. stream.read( &childFieldCount );
  327. // Do we have any child fields?
  328. if ( childFieldCount > 0 )
  329. {
  330. // Yes, so parse child fields.
  331. for( U32 childFieldIndex = 0; childFieldIndex < childFieldCount; ++childFieldIndex )
  332. {
  333. // Read field name.
  334. StringTableEntry fieldName = stream.readSTString();
  335. // Read field value.
  336. char valueBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
  337. stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, valueBuffer );
  338. // Add field.
  339. pChildNode->addField( fieldName, valueBuffer );
  340. }
  341. }
  342. }