tamlAssetReferencedUpdateVisitor.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_REFERENCED_UPDATE_VISITOR_H_
  23. #define _TAML_ASSET_REFERENCED_UPDATE_VISITOR_H_
  24. #ifndef _TAML_XMLPARSER_H_
  25. #include "persistence//taml/tamlXmlParser.h"
  26. #endif
  27. #ifndef _STRINGUNIT_H_
  28. #include "string/stringUnit.h"
  29. #endif
  30. #ifndef _STRINGTABLE_H_
  31. #include "string/stringTable.h"
  32. #endif
  33. #ifndef _ASSET_FIELD_TYPES_H_
  34. #include "assets/assetFieldTypes.h"
  35. #endif
  36. // Debug Profiling.
  37. #include "debug/profiler.h"
  38. //-----------------------------------------------------------------------------
  39. class TamlAssetReferencedUpdateVisitor : public TamlXmlVisitor
  40. {
  41. protected:
  42. virtual bool visit( TiXmlElement* pXmlElement, TamlXmlParser& xmlParser ) { return true; }
  43. virtual bool visit( TiXmlAttribute* pAttribute, TamlXmlParser& xmlParser )
  44. {
  45. // Debug Profiling.
  46. PROFILE_SCOPE(TamlAssetReferencedUpdateVisitor_VisitAttribute);
  47. // Fetch attribute value.
  48. const char* pAttributeValue = pAttribute->Value();
  49. // Fetch attribute value word count.
  50. const U32 valueWordCount = StringUnit::getUnitCount( pAttributeValue, ASSET_ASSIGNMENT_TOKEN );
  51. // Finish if not two words.
  52. if ( valueWordCount != 2 )
  53. return true;
  54. // Skip if this is not an asset signature.
  55. if ( dStricmp( StringUnit::getUnit( pAttributeValue, 0, ASSET_ASSIGNMENT_TOKEN), ASSET_ID_SIGNATURE ) != 0 )
  56. return true;
  57. // Get the asset value.
  58. const char* pAssetValue = StringUnit::getUnit( pAttributeValue, 1, ASSET_ASSIGNMENT_TOKEN );
  59. // Finish if not the asset Id we're looking for.
  60. if ( dStricmp( pAssetValue, mAssetIdFrom ) != 0 )
  61. return true;
  62. // Is the target asset empty?
  63. if ( mAssetIdTo == StringTable->EmptyString )
  64. {
  65. // Yes, so set the attribute as empty.
  66. pAttribute->SetValue( StringTable->EmptyString );
  67. return true;
  68. }
  69. // Format asset.
  70. char assetBuffer[1024];
  71. dSprintf( assetBuffer, sizeof(assetBuffer), "%s%s%s", ASSET_ID_SIGNATURE, ASSET_ASSIGNMENT_TOKEN, mAssetIdTo );
  72. // Assign new value.
  73. pAttribute->SetValue( assetBuffer );
  74. return true;
  75. }
  76. public:
  77. TamlAssetReferencedUpdateVisitor() {}
  78. virtual ~TamlAssetReferencedUpdateVisitor() {}
  79. bool parse( const char* pFilename )
  80. {
  81. TamlXmlParser parser;
  82. return parser.parse( pFilename, *this, true );
  83. }
  84. void setAssetIdFrom( const char* pAssetIdFrom )
  85. {
  86. // Sanity!
  87. AssertFatal( pAssetIdFrom != NULL, "Asset Id from cannot be NULL." );
  88. mAssetIdFrom = StringTable->insert( pAssetIdFrom );
  89. }
  90. StringTableEntry getAssetIdFrom( void ) const { return mAssetIdFrom; }
  91. void setAssetIdTo( const char* pAssetIdTo )
  92. {
  93. // Sanity!
  94. AssertFatal( pAssetIdTo != NULL, "Asset Id to cannot be NULL." );
  95. mAssetIdTo = StringTable->insert( pAssetIdTo );
  96. }
  97. const char* getAssetIdTo( void ) const { return mAssetIdTo; }
  98. private:
  99. StringTableEntry mAssetIdFrom;
  100. StringTableEntry mAssetIdTo;
  101. };
  102. #endif // _TAML_ASSET_REFERENCED_UPDATE_VISITOR_H_