tamlAssetDeclaredUpdateVisitor.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_UPDATE_VISITOR_H_
  23. #define _TAML_ASSET_DECLARED_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 _ASSET_FIELD_TYPES_H_
  31. #include "assets/assetFieldTypes.h"
  32. #endif
  33. // Debug Profiling.
  34. #include "platform/profiler.h"
  35. //-----------------------------------------------------------------------------
  36. class TamlAssetDeclaredUpdateVisitor : public TamlVisitor
  37. {
  38. private:
  39. StringTableEntry mAssetIdFrom;
  40. StringTableEntry mAssetIdTo;
  41. StringTableEntry mAssetNameFrom;
  42. StringTableEntry mAssetNameTo;
  43. public:
  44. TamlAssetDeclaredUpdateVisitor() {}
  45. virtual ~TamlAssetDeclaredUpdateVisitor() {}
  46. void setAssetIdFrom( const char* pAssetIdFrom )
  47. {
  48. // Sanity!
  49. AssertFatal( pAssetIdFrom != NULL, "Asset Id from cannot be NULL." );
  50. // Reset asset Id.
  51. mAssetIdFrom = StringTable->EmptyString();
  52. mAssetNameFrom = StringTable->EmptyString();
  53. // Is asset Id the correct format?
  54. if ( StringUnit::getUnitCount( pAssetIdFrom, ASSET_SCOPE_TOKEN ) != 2 )
  55. {
  56. // No, so warn.
  57. Con::warnf( "TamlAssetDeclaredUpdateVisitor::setAssetIdFrom() - Cannot use asset Id '%s' as it is not the correct format.", pAssetIdFrom );
  58. return;
  59. }
  60. // Set asset Id.
  61. mAssetIdFrom = StringTable->insert( pAssetIdFrom );
  62. mAssetNameFrom = StringTable->insert( StringUnit::getUnit( pAssetIdFrom, 1, ASSET_SCOPE_TOKEN ) );
  63. }
  64. StringTableEntry getAssetIdFrom( void ) const { return mAssetIdFrom; }
  65. void setAssetIdTo( const char* pAssetIdTo )
  66. {
  67. // Sanity!
  68. AssertFatal( pAssetIdTo != NULL, "Asset Id to cannot be NULL." );
  69. // Reset asset Id.
  70. mAssetIdTo = StringTable->EmptyString();
  71. mAssetNameTo = StringTable->EmptyString();
  72. // Is asset Id the correct format?
  73. if ( StringUnit::getUnitCount( pAssetIdTo, ASSET_SCOPE_TOKEN ) != 2 )
  74. {
  75. // No, so warn.
  76. Con::warnf( "TamlAssetDeclaredUpdateVisitor::setAssetIdTo() - Cannot use asset Id '%s' as it is not the correct format.", pAssetIdTo );
  77. return;
  78. }
  79. // Set asset Id.
  80. mAssetIdTo = StringTable->insert( pAssetIdTo );
  81. mAssetNameTo = StringTable->insert( StringUnit::getUnit( pAssetIdTo, 1, ASSET_SCOPE_TOKEN ) );
  82. }
  83. const char* getAssetIdTo( void ) const { return mAssetIdTo; }
  84. virtual bool wantsPropertyChanges( void ) { return true; }
  85. virtual bool wantsRootOnly( void ) { return true; }
  86. virtual bool visit( const TamlParser& parser, TamlVisitor::PropertyState& propertyState )
  87. {
  88. // Debug Profiling.
  89. PROFILE_SCOPE(TamlAssetDeclaredUpdateVisitor_Visit);
  90. // Finish if not the asset name field.
  91. if ( propertyState.getPropertyName() != assetNameField )
  92. return true;
  93. // Is this the asset Id we're looking for?
  94. if ( dStricmp( propertyState.getPropertyValue(), mAssetNameFrom ) != 0 )
  95. {
  96. // No, so warn.
  97. Con::warnf("Cannot rename asset Name '%s' to asset Name '%s' as the declared asset Name was %s",
  98. mAssetNameFrom, mAssetNameTo, propertyState.getPropertyValue() );
  99. // Stop processing!
  100. return false;
  101. }
  102. // Assign new value.
  103. propertyState.updatePropertyValue( mAssetNameTo );
  104. // Stop processing!
  105. return false;
  106. }
  107. };
  108. #endif // _TAML_ASSET_DECLARED_UPDATE_VISITOR_H_