tamlModuleIdUpdateVisitor.h 4.6 KB

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