tamlBinaryReader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. pSimObject->setFilename(mpTaml->getFilePathBuffer());
  121. // Find Taml callbacks.
  122. TamlCallbacks* pCallbacks = dynamic_cast<TamlCallbacks*>( pSimObject );
  123. // Are there any Taml callbacks?
  124. if ( pCallbacks != NULL )
  125. {
  126. // Yes, so call it.
  127. mpTaml->tamlPreRead( pCallbacks );
  128. }
  129. // Parse attributes.
  130. parseAttributes( stream, pSimObject, versionId );
  131. // Does the object require a name?
  132. if ( objectName == StringTable->EmptyString() )
  133. {
  134. // No, so just register anonymously.
  135. pSimObject->registerObject();
  136. }
  137. else
  138. {
  139. // Yes, so register a named object.
  140. pSimObject->registerObject( objectName );
  141. // Was the name assigned?
  142. if ( pSimObject->getName() != objectName )
  143. {
  144. // No, so warn that the name was rejected.
  145. #ifdef TORQUE_DEBUG
  146. 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 );
  147. #else
  148. 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 );
  149. #endif
  150. }
  151. }
  152. // Do we have a reference Id?
  153. if ( tamlRefId != 0 )
  154. {
  155. // Yes, so insert reference.
  156. mObjectReferenceMap.insertUnique( tamlRefId, pSimObject );
  157. }
  158. // Parse custom elements.
  159. TamlCustomNodes customProperties;
  160. // Parse children.
  161. parseChildren( stream, pCallbacks, pSimObject, versionId );
  162. // Parse custom elements.
  163. parseCustomElements( stream, pCallbacks, customProperties, versionId );
  164. // Are there any Taml callbacks?
  165. if ( pCallbacks != NULL )
  166. {
  167. // Yes, so call it.
  168. mpTaml->tamlPostRead( pCallbacks, customProperties );
  169. }
  170. // Return object.
  171. return pSimObject;
  172. }
  173. //-----------------------------------------------------------------------------
  174. void TamlBinaryReader::parseAttributes( Stream& stream, SimObject* pSimObject, const U32 versionId )
  175. {
  176. // Debug Profiling.
  177. PROFILE_SCOPE(TamlBinaryReader_ParseAttributes);
  178. // Sanity!
  179. AssertFatal( pSimObject != NULL, "Taml: Cannot parse attributes on a NULL object." );
  180. // Fetch attribute count.
  181. U32 attributeCount;
  182. stream.read( &attributeCount );
  183. // Finish if no attributes.
  184. if ( attributeCount == 0 )
  185. return;
  186. char valueBuffer[4096];
  187. // Iterate attributes.
  188. for ( U32 index = 0; index < attributeCount; ++index )
  189. {
  190. // Fetch attribute.
  191. StringTableEntry attributeName = stream.readSTString();
  192. stream.readLongString( 4096, valueBuffer );
  193. // We can assume this is a field for now.
  194. pSimObject->setPrefixedDataField(attributeName, NULL, valueBuffer);
  195. }
  196. }
  197. //-----------------------------------------------------------------------------
  198. void TamlBinaryReader::parseChildren( Stream& stream, TamlCallbacks* pCallbacks, SimObject* pSimObject, const U32 versionId )
  199. {
  200. // Debug Profiling.
  201. PROFILE_SCOPE(TamlBinaryReader_ParseChildren);
  202. // Sanity!
  203. AssertFatal( pSimObject != NULL, "Taml: Cannot parse children on a NULL object." );
  204. // Fetch children count.
  205. U32 childrenCount;
  206. stream.read( &childrenCount );
  207. // Finish if no children.
  208. if ( childrenCount == 0 )
  209. return;
  210. // Fetch the Taml children.
  211. TamlChildren* pChildren = dynamic_cast<TamlChildren*>( pSimObject );
  212. // Is this a sim set?
  213. if ( pChildren == NULL )
  214. {
  215. // No, so warn.
  216. Con::warnf("Taml: Child element found under parent but object cannot have children." );
  217. return;
  218. }
  219. // Fetch any container child class specifier.
  220. AbstractClassRep* pContainerChildClass = pSimObject->getClassRep()->getContainerChildClass( true );
  221. // Iterate children.
  222. for ( U32 index = 0; index < childrenCount; ++ index )
  223. {
  224. // Parse child element.
  225. SimObject* pChildSimObject = parseElement( stream, versionId );
  226. // Finish if child failed.
  227. if ( pChildSimObject == NULL )
  228. return;
  229. // Do we have a container child class?
  230. if ( pContainerChildClass != NULL )
  231. {
  232. // Yes, so is the child object the correctly derived type?
  233. if ( !pChildSimObject->getClassRep()->isClass( pContainerChildClass ) )
  234. {
  235. // No, so warn.
  236. Con::warnf("Taml: Child element '%s' found under parent '%s' but object is restricted to children of type '%s'.",
  237. pChildSimObject->getClassName(),
  238. pSimObject->getClassName(),
  239. pContainerChildClass->getClassName() );
  240. // NOTE: We can't delete the object as it may be referenced elsewhere!
  241. pChildSimObject = NULL;
  242. // Skip.
  243. continue;
  244. }
  245. }
  246. // Add child.
  247. pChildren->addTamlChild( pChildSimObject );
  248. // Find Taml callbacks for child.
  249. TamlCallbacks* pChildCallbacks = dynamic_cast<TamlCallbacks*>( pChildSimObject );
  250. // Do we have callbacks on the child?
  251. if ( pChildCallbacks != NULL )
  252. {
  253. // Yes, so perform callback.
  254. mpTaml->tamlAddParent( pChildCallbacks, pSimObject );
  255. }
  256. }
  257. }
  258. //-----------------------------------------------------------------------------
  259. void TamlBinaryReader::parseCustomElements( Stream& stream, TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes, const U32 versionId )
  260. {
  261. // Debug Profiling.
  262. PROFILE_SCOPE(TamlBinaryReader_ParseCustomElement);
  263. // Read custom node count.
  264. U32 customNodeCount;
  265. stream.read( &customNodeCount );
  266. // Finish if no custom nodes.
  267. if ( customNodeCount == 0 )
  268. return;
  269. // Iterate custom nodes.
  270. for ( U32 nodeIndex = 0; nodeIndex < customNodeCount; ++nodeIndex )
  271. {
  272. //Read custom node name.
  273. StringTableEntry nodeName = stream.readSTString();
  274. // Add custom node.
  275. TamlCustomNode* pCustomNode = customNodes.addNode( nodeName );
  276. // Parse the custom node.
  277. parseCustomNode( stream, pCustomNode, versionId );
  278. }
  279. // Do we have callbacks?
  280. if ( pCallbacks == NULL )
  281. {
  282. // No, so warn.
  283. Con::warnf( "Taml: Encountered custom data but object does not support custom data." );
  284. return;
  285. }
  286. // Custom read callback.
  287. mpTaml->tamlCustomRead( pCallbacks, customNodes );
  288. }
  289. //-----------------------------------------------------------------------------
  290. void TamlBinaryReader::parseCustomNode( Stream& stream, TamlCustomNode* pCustomNode, const U32 versionId )
  291. {
  292. // Fetch if a proxy object.
  293. bool isProxyObject;
  294. stream.read( &isProxyObject );
  295. // Is this a proxy object?
  296. if ( isProxyObject )
  297. {
  298. // Yes, so parse proxy object.
  299. SimObject* pProxyObject = parseElement( stream, versionId );
  300. // Add child node.
  301. pCustomNode->addNode( pProxyObject );
  302. return;
  303. }
  304. // No, so read custom node name.
  305. StringTableEntry nodeName = stream.readSTString();
  306. // Add child node.
  307. TamlCustomNode* pChildNode = pCustomNode->addNode( nodeName );
  308. // Read child node text.
  309. char childNodeTextBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
  310. stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, childNodeTextBuffer );
  311. pChildNode->setNodeText( childNodeTextBuffer );
  312. // Read child node count.
  313. U32 childNodeCount;
  314. stream.read( &childNodeCount );
  315. // Do we have any children nodes?
  316. if ( childNodeCount > 0 )
  317. {
  318. // Yes, so parse children nodes.
  319. for( U32 childIndex = 0; childIndex < childNodeCount; ++childIndex )
  320. {
  321. // Parse child node.
  322. parseCustomNode( stream, pChildNode, versionId );
  323. }
  324. }
  325. // Read child field count.
  326. U32 childFieldCount;
  327. stream.read( &childFieldCount );
  328. // Do we have any child fields?
  329. if ( childFieldCount > 0 )
  330. {
  331. // Yes, so parse child fields.
  332. for( U32 childFieldIndex = 0; childFieldIndex < childFieldCount; ++childFieldIndex )
  333. {
  334. // Read field name.
  335. StringTableEntry fieldName = stream.readSTString();
  336. // Read field value.
  337. char valueBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
  338. stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, valueBuffer );
  339. // Add field.
  340. pChildNode->addField( fieldName, valueBuffer );
  341. }
  342. }
  343. }