tamlJSONParser.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/json/tamlJSONParser.h"
  23. #include "persistence/taml/tamlVisitor.h"
  24. #include "console/console.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/frameAllocator.h"
  27. // Debug Profiling.
  28. #include "platform/profiler.h"
  29. //-----------------------------------------------------------------------------
  30. bool TamlJSONParser::accept( const char* pFilename, TamlVisitor& visitor )
  31. {
  32. // Debug Profiling.
  33. PROFILE_SCOPE(TamlJSONParser_Accept);
  34. // Sanity!
  35. AssertFatal( pFilename != NULL, "Cannot parse a NULL filename." );
  36. // Expand the file-path.
  37. char filenameBuffer[1024];
  38. Con::expandToolScriptFilename( filenameBuffer, sizeof(filenameBuffer), pFilename );
  39. FileStream stream;
  40. // File open for read?
  41. if ( !stream.open( filenameBuffer, Torque::FS::File::Write ) )
  42. {
  43. // No, so warn.
  44. Con::warnf("TamlJSONParser::parse() - Could not open filename '%s' for parse.", filenameBuffer );
  45. return false;
  46. }
  47. // Read JSON file.
  48. const U32 streamSize = stream.getStreamSize();
  49. FrameTemp<char> jsonText( streamSize + 1 );
  50. if ( !stream.read( streamSize, jsonText ) )
  51. {
  52. // Warn!
  53. Con::warnf("TamlJSONParser::parse() - Could not load Taml JSON file from stream.");
  54. return false;
  55. }
  56. // Create JSON document.
  57. rapidjson::Document inputDocument;
  58. inputDocument.Parse<0>( jsonText );
  59. // Close the stream.
  60. stream.close();
  61. // Check the document is valid.
  62. if ( inputDocument.GetType() != rapidjson::kObjectType )
  63. {
  64. // Warn!
  65. Con::warnf("TamlJSONParser::parse() - Load Taml JSON file from stream but was invalid.");
  66. return false;
  67. }
  68. // Set parsing filename.
  69. setParsingFilename( filenameBuffer );
  70. // Flag document as not dirty.
  71. mDocumentDirty = false;
  72. // Fetch the root.
  73. rapidjson::Value::MemberIterator rootItr = inputDocument.MemberBegin();
  74. // Parse root value.
  75. parseType( rootItr, visitor, true );
  76. // Reset parsing filename.
  77. setParsingFilename( StringTable->EmptyString() );
  78. // Finish if the document is not dirty.
  79. if ( !mDocumentDirty )
  80. return true;
  81. // Open for write?
  82. if ( !stream.open( filenameBuffer, Torque::FS::File::Write ) )
  83. {
  84. // No, so warn.
  85. Con::warnf("TamlJSONParser::parse() - Could not open filename '%s' for write.", filenameBuffer );
  86. return false;
  87. }
  88. // Create output document.
  89. rapidjson::Document outputDocument;
  90. outputDocument.AddMember( rootItr->name, rootItr->value, outputDocument.GetAllocator() );
  91. // Write document to stream.
  92. rapidjson::PrettyWriter<FileStream> jsonStreamWriter( stream );
  93. outputDocument.Accept( jsonStreamWriter );
  94. // Close the stream.
  95. stream.close();
  96. return true;
  97. }
  98. //-----------------------------------------------------------------------------
  99. inline bool TamlJSONParser::parseType( rapidjson::Value::MemberIterator& memberItr, TamlVisitor& visitor, const bool isRoot )
  100. {
  101. // Debug Profiling.
  102. PROFILE_SCOPE(TamlJSONParser_ParseType);
  103. // Fetch name and value.
  104. const rapidjson::Value& typeName = memberItr->name;
  105. rapidjson::Value& typeValue = memberItr->value;
  106. // Create a visitor property state.
  107. TamlVisitor::PropertyState propertyState;
  108. propertyState.setObjectName( typeName.GetString(), isRoot );
  109. // Parse field members.
  110. for( rapidjson::Value::MemberIterator fieldMemberItr = typeValue.MemberBegin(); fieldMemberItr != typeValue.MemberEnd(); ++fieldMemberItr )
  111. {
  112. // Fetch value.
  113. const rapidjson::Value& fieldName = fieldMemberItr->name;
  114. rapidjson::Value& fieldValue = fieldMemberItr->value;
  115. // Skip if not a field.
  116. if ( fieldValue.IsObject() )
  117. continue;
  118. char valueBuffer[4096];
  119. if ( !parseStringValue( valueBuffer, sizeof(valueBuffer), fieldValue, fieldName.GetString() ) )
  120. {
  121. // Warn.
  122. Con::warnf( "TamlJSONParser::parseTyoe() - Could not interpret value for field '%s'", fieldName.GetString() );
  123. continue;
  124. }
  125. // Configure property state.
  126. propertyState.setProperty( fieldName.GetString(), valueBuffer );
  127. // Visit this attribute.
  128. const bool visitStatus = visitor.visit( *this, propertyState );
  129. // Was the property value changed?
  130. if ( propertyState.getPropertyValueDirty() )
  131. {
  132. // Yes, so update the attribute.
  133. fieldValue.SetString(rapidjson::GenericStringRef<UTF8>(propertyState.getPropertyValue()) );
  134. // Flag the document as dirty.
  135. mDocumentDirty = true;
  136. }
  137. // Finish if requested.
  138. if ( !visitStatus )
  139. return false;
  140. }
  141. // Finish if only the root is needed.
  142. if ( visitor.wantsRootOnly() )
  143. return false;
  144. // Parse children and custom node members.
  145. for( rapidjson::Value::MemberIterator objectMemberItr = typeValue.MemberBegin(); objectMemberItr != typeValue.MemberEnd(); ++objectMemberItr )
  146. {
  147. // Fetch name and value.
  148. const rapidjson::Value& objectValue = objectMemberItr->value;
  149. // Skip if not an object.
  150. if ( !objectValue.IsObject() )
  151. continue;
  152. // Parse the type.
  153. if ( !parseType( objectMemberItr, visitor, false ) )
  154. return false;
  155. }
  156. return true;
  157. }
  158. //-----------------------------------------------------------------------------
  159. inline bool TamlJSONParser::parseStringValue( char* pBuffer, const S32 bufferSize, const rapidjson::Value& value, const char* pName )
  160. {
  161. // Debug Profiling.
  162. PROFILE_SCOPE(TamlJSONParser_ParseStringValue);
  163. // Handle field value appropriately.
  164. if ( value.IsString() )
  165. {
  166. dSprintf( pBuffer, bufferSize, "%s", value.GetString() );
  167. return true;
  168. }
  169. if ( value.IsNumber() )
  170. {
  171. if ( value.IsInt() )
  172. {
  173. dSprintf( pBuffer, bufferSize, "%d", value.GetInt() );
  174. return true;
  175. }
  176. if ( value.IsUint() )
  177. {
  178. dSprintf( pBuffer, bufferSize, "%d", value.GetUint() );
  179. return true;
  180. }
  181. if ( value.IsInt64() )
  182. {
  183. dSprintf( pBuffer, bufferSize, "%d", value.GetInt64() );
  184. return true;
  185. }
  186. if ( value.IsUint64() )
  187. {
  188. dSprintf( pBuffer, bufferSize, "%d", value.GetUint64() );
  189. return true;
  190. }
  191. if ( value.IsDouble() )
  192. {
  193. dSprintf( pBuffer, bufferSize, "%f", value.GetDouble() );
  194. return true;
  195. }
  196. }
  197. if ( value.IsBool() )
  198. {
  199. dSprintf( pBuffer, bufferSize, "%d", value.GetBool() );
  200. return true;
  201. }
  202. // Failed to get value type.
  203. Con::warnf( "Taml: Encountered a field '%s' but its value is an unknown type.", pName );
  204. return false;
  205. }