tamlModuleIdUpdateVisitor.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_MODULE_ID_UPDATE_VISITOR_H_
  23. #define _TAML_MODULE_ID_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. //-----------------------------------------------------------------------------
  34. class TamlModuleIdUpdateVisitor : public TamlVisitor
  35. {
  36. private:
  37. StringTableEntry mModuleIdFrom;
  38. StringTableEntry mModuleIdTo;
  39. U32 mModuleIdFromLength;
  40. U32 mModuleIdToLength;
  41. public:
  42. TamlModuleIdUpdateVisitor() :
  43. mModuleIdFrom( StringTable->EmptyString ),
  44. mModuleIdTo( StringTable->EmptyString ),
  45. mModuleIdFromLength( 0 ),
  46. mModuleIdToLength( 0 )
  47. {}
  48. virtual ~TamlModuleIdUpdateVisitor() {}
  49. virtual bool wantsPropertyChanges( void ) { return true; }
  50. virtual bool wantsRootOnly( void ) { return true; }
  51. virtual bool visit( const TamlParser& parser, TamlVisitor::PropertyState& propertyState )
  52. {
  53. // Debug Profiling.
  54. PROFILE_SCOPE(TamlModuleIdUpdateVisitor_Visit);
  55. // Fetch property value.
  56. const char* pPropertyValue = propertyState.getPropertyValue();
  57. // Fetch value length.
  58. const U32 valueLenth = dStrlen(pPropertyValue);
  59. char newAttributeValueBuffer[1024];
  60. // Is this an expando?
  61. if ( *pPropertyValue == '^' )
  62. {
  63. // Yes, so finish if it's not the correct length.
  64. if ( valueLenth < mModuleIdFromLength+1 )
  65. return true;
  66. // Is this the module Id?
  67. if ( dStrnicmp( pPropertyValue+1, mModuleIdFrom, mModuleIdFromLength ) == 0 )
  68. {
  69. // Yes, so format a new value.
  70. dSprintf( newAttributeValueBuffer, sizeof(newAttributeValueBuffer), "^%s%s",
  71. mModuleIdTo, pPropertyValue+1+mModuleIdFromLength );
  72. // Assign new value.
  73. propertyState.updatePropertyValue( newAttributeValueBuffer );
  74. }
  75. return true;
  76. }
  77. // Does the field start with the module Id?
  78. if ( dStrnicmp( pPropertyValue, mModuleIdFrom, mModuleIdFromLength ) == 0 )
  79. {
  80. // Yes, so format a new value.
  81. dSprintf( newAttributeValueBuffer, sizeof(newAttributeValueBuffer), "%s%s",
  82. mModuleIdTo, pPropertyValue+mModuleIdFromLength );
  83. // Assign new value.
  84. propertyState.updatePropertyValue( newAttributeValueBuffer );
  85. }
  86. return true;
  87. }
  88. void setModuleIdFrom( const char* pModuleIdFrom )
  89. {
  90. // Sanity!
  91. AssertFatal( pModuleIdFrom != NULL, "Module Id from cannot be NULL." );
  92. // Set module Id.
  93. mModuleIdFrom = StringTable->insert( pModuleIdFrom );
  94. mModuleIdFromLength = dStrlen(mModuleIdFrom);
  95. }
  96. StringTableEntry getModuleIdFrom( void ) const { return mModuleIdFrom; }
  97. void setModuleIdTo( const char* pModuleIdTo )
  98. {
  99. // Sanity!
  100. AssertFatal( pModuleIdTo != NULL, "Module Id to cannot be NULL." );
  101. // Set module Id.
  102. mModuleIdTo = StringTable->insert( pModuleIdTo );
  103. mModuleIdToLength = dStrlen(mModuleIdTo);
  104. }
  105. const char* getModuleIdTo( void ) const { return mModuleIdTo; }
  106. };
  107. #endif // _TAML_MODULE_ID_UPDATE_VISITOR_H_