tamlAssetDeclaredVisitor.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_ASSET_DECLARED_VISITOR_H_
  23. #define _TAML_ASSET_DECLARED_VISITOR_H_
  24. #ifndef _TAML_VISITOR_H_
  25. #include "persistence/taml/tamlVisitor.h"
  26. #endif
  27. #ifndef _TAML_PARSER_H_
  28. #include "persistence\/taml/tamlParser.h"
  29. #endif
  30. #ifndef _ASSET_FIELD_TYPES_H_
  31. #include "assets/assetFieldTypes.h"
  32. #endif
  33. #ifndef _ASSET_DEFINITION_H_
  34. #include "assetDefinition.h"
  35. #endif
  36. #ifndef _ASSET_BASE_H_
  37. #include "assetBase.h"
  38. #endif
  39. // Debug Profiling.
  40. #include "platform/profiler.h"
  41. //-----------------------------------------------------------------------------
  42. class TamlAssetDeclaredVisitor : public TamlVisitor
  43. {
  44. public:
  45. typedef StringTableEntry typeAssetId;
  46. typedef Vector<typeAssetId> typeAssetIdVector;
  47. typedef Vector<StringTableEntry> typeLooseFileVector;
  48. private:
  49. AssetDefinition mAssetDefinition;
  50. typeAssetIdVector mAssetDependencies;
  51. typeLooseFileVector mAssetLooseFiles;
  52. public:
  53. TamlAssetDeclaredVisitor() { mAssetDefinition.reset(); }
  54. virtual ~TamlAssetDeclaredVisitor() {}
  55. inline AssetDefinition& getAssetDefinition( void ) { return mAssetDefinition; }
  56. inline typeAssetIdVector& getAssetDependencies( void ) { return mAssetDependencies; }
  57. inline typeLooseFileVector& getAssetLooseFiles( void ) { return mAssetLooseFiles; }
  58. void clear( void ) { mAssetDefinition.reset(); mAssetDependencies.clear(); mAssetLooseFiles.clear(); }
  59. virtual bool wantsPropertyChanges( void ) { return false; }
  60. virtual bool wantsRootOnly( void ) { return false; }
  61. virtual bool visit( const TamlParser& parser, TamlVisitor::PropertyState& propertyState )
  62. {
  63. // Debug Profiling.
  64. PROFILE_SCOPE(TamlAssetDeclaredVisitor_Visit);
  65. // Fetch property name and value.
  66. StringTableEntry propertyName = propertyState.getPropertyName();
  67. const char* pPropertyValue = propertyState.getPropertyValue();
  68. // Is this the root object?
  69. if ( propertyState.isRootObject() )
  70. {
  71. // Yes, so is the asset type set yet?
  72. if ( mAssetDefinition.mAssetType == StringTable->EmptyString() )
  73. {
  74. // No, set set asset type and base file-path.
  75. mAssetDefinition.mAssetType = propertyState.getObjectName();
  76. mAssetDefinition.mAssetBaseFilePath = parser.getParsingFilename();
  77. }
  78. // Asset name?
  79. if ( propertyName == assetNameField )
  80. {
  81. // Yes, so assign it.
  82. mAssetDefinition.mAssetName = StringTable->insert( pPropertyValue );
  83. return true;
  84. }
  85. // Asset description?
  86. else if ( propertyName == assetDescriptionField )
  87. {
  88. // Yes, so assign it.
  89. mAssetDefinition.mAssetDescription = StringTable->insert( pPropertyValue );
  90. return true;
  91. }
  92. // Asset description?
  93. else if ( propertyName == assetCategoryField )
  94. {
  95. // Yes, so assign it.
  96. mAssetDefinition.mAssetCategory = StringTable->insert( pPropertyValue );
  97. return true;
  98. }
  99. // Asset auto-unload?
  100. else if ( propertyName == assetAutoUnloadField )
  101. {
  102. // Yes, so assign it.
  103. mAssetDefinition.mAssetAutoUnload = dAtob( pPropertyValue );
  104. return true;
  105. }
  106. // Asset internal?
  107. else if ( propertyName == assetInternalField )
  108. {
  109. // Yes, so assign it.
  110. mAssetDefinition.mAssetInternal = dAtob( pPropertyValue );
  111. return true;
  112. }
  113. }
  114. // Fetch property word count.
  115. const U32 propertyWordCount = StringUnit::getUnitCount( pPropertyValue, ASSET_ASSIGNMENT_TOKEN );
  116. // Finish if there's not two words.
  117. if ( propertyWordCount != 2 )
  118. return true;
  119. // Fetch the asset signature.
  120. StringTableEntry assetSignature = StringTable->insert( StringUnit::getUnit( pPropertyValue, 0, ASSET_ASSIGNMENT_TOKEN ) );
  121. // Is this an asset Id signature?
  122. if ( assetSignature == assetLooseIdSignature )
  123. {
  124. // Yes, so get asset Id.
  125. typeAssetId assetId = StringTable->insert( StringUnit::getUnit( pPropertyValue, 1, ASSET_ASSIGNMENT_TOKEN ) );
  126. // Finish if the dependency is itself!
  127. if ( mAssetDefinition.mAssetId == assetId )
  128. return true;
  129. // Iterate existing dependencies.
  130. for( typeAssetIdVector::iterator dependencyItr = mAssetDependencies.begin(); dependencyItr != mAssetDependencies.end(); ++dependencyItr )
  131. {
  132. // Finish if asset Id is already a dependency.
  133. if ( *dependencyItr == assetId )
  134. return true;
  135. }
  136. // Insert asset reference.
  137. mAssetDependencies.push_back( assetId );
  138. }
  139. // Is this a loose-file signature?
  140. else if ( assetSignature == assetLooseFileSignature )
  141. {
  142. // Yes, so get loose-file reference.
  143. const char* pAssetLooseFile = StringUnit::getUnit( pPropertyValue, 1, ASSET_ASSIGNMENT_TOKEN );
  144. // Fetch asset path only.
  145. char assetBasePathBuffer[1024];
  146. dSprintf( assetBasePathBuffer, sizeof(assetBasePathBuffer), "%s", mAssetDefinition.mAssetBaseFilePath );
  147. char* pFinalSlash = dStrrchr( assetBasePathBuffer, '/' );
  148. if ( pFinalSlash != NULL ) *pFinalSlash = 0;
  149. // Expand the path in the usual way.
  150. char assetFilePathBuffer[1024];
  151. Con::expandPath( assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetLooseFile, assetBasePathBuffer );
  152. // Insert asset loose-file.
  153. mAssetLooseFiles.push_back( StringTable->insert( assetFilePathBuffer ) );
  154. }
  155. return true;
  156. }
  157. };
  158. #endif // _TAML_ASSET_DECLARED_VISITOR_H_