tamlAssetReferencedUpdateVisitor.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_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 _STRINGUNIT_H_
  31. #include "string/stringUnit.h"
  32. #endif
  33. #ifndef _STRINGTABLE_H_
  34. #include "string/stringTable.h"
  35. #endif
  36. #ifndef _ASSET_FIELD_TYPES_H_
  37. #include "assets/assetFieldTypes.h"
  38. #endif
  39. // Debug Profiling.
  40. #include "platform/profiler.h"
  41. //-----------------------------------------------------------------------------
  42. class TamlAssetReferencedUpdateVisitor : public TamlVisitor
  43. {
  44. private:
  45. StringTableEntry mAssetIdFrom;
  46. StringTableEntry mAssetIdTo;
  47. public:
  48. TamlAssetReferencedUpdateVisitor() : mAssetIdFrom(StringTable->EmptyString()), mAssetIdTo(StringTable->EmptyString()) {}
  49. virtual ~TamlAssetReferencedUpdateVisitor() {}
  50. void setAssetIdFrom( const char* pAssetIdFrom )
  51. {
  52. // Sanity!
  53. AssertFatal( pAssetIdFrom != NULL, "Asset Id from cannot be NULL." );
  54. mAssetIdFrom = StringTable->insert( pAssetIdFrom );
  55. }
  56. StringTableEntry getAssetIdFrom( void ) const { return mAssetIdFrom; }
  57. void setAssetIdTo( const char* pAssetIdTo )
  58. {
  59. // Sanity!
  60. AssertFatal( pAssetIdTo != NULL, "Asset Id to cannot be NULL." );
  61. mAssetIdTo = StringTable->insert( pAssetIdTo );
  62. }
  63. const char* getAssetIdTo( void ) const { return mAssetIdTo; }
  64. virtual bool wantsPropertyChanges( void ) { return true; }
  65. virtual bool wantsRootOnly( void ) { return false; }
  66. virtual bool visit( const TamlParser& parser, TamlVisitor::PropertyState& propertyState )
  67. {
  68. // Debug Profiling.
  69. PROFILE_SCOPE(TamlAssetReferencedUpdateVisitor_Visit);
  70. // Fetch the property value.
  71. const char* pPropertyValue = propertyState.getPropertyValue();
  72. // Fetch property value word count.
  73. const U32 valueWordCount = StringUnit::getUnitCount( pPropertyValue, ASSET_ASSIGNMENT_TOKEN );
  74. // Finish if not two words.
  75. if ( valueWordCount != 2 )
  76. return true;
  77. // Finish if this is not an asset signature.
  78. if ( dStricmp( StringUnit::getUnit( pPropertyValue, 0, ASSET_ASSIGNMENT_TOKEN), assetLooseIdSignature ) != 0 )
  79. return true;
  80. // Get the asset value.
  81. const char* pAssetValue = StringUnit::getUnit( pPropertyValue, 1, ASSET_ASSIGNMENT_TOKEN );
  82. // Finish if not the asset Id we're looking for.
  83. if ( dStricmp( pAssetValue, mAssetIdFrom ) != 0 )
  84. return true;
  85. // Is the target asset empty?
  86. if ( mAssetIdTo == StringTable->EmptyString() )
  87. {
  88. // Yes, so update the property as empty.
  89. propertyState.updatePropertyValue(StringTable->EmptyString());
  90. return true;
  91. }
  92. // Format asset.
  93. char assetBuffer[1024];
  94. dSprintf( assetBuffer, sizeof(assetBuffer), "%s%s%s", assetLooseIdSignature, ASSET_ASSIGNMENT_TOKEN, mAssetIdTo );
  95. // Assign new value.
  96. propertyState.updatePropertyValue( assetBuffer );
  97. return true;
  98. }
  99. };
  100. #endif // _TAML_ASSET_REFERENCED_UPDATE_VISITOR_H_