tamlAssetDeclaredUpdateVisitor.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_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 TamlAssetDeclaredUpdateVisitor : public TamlXmlVisitor
  40. {
  41. protected:
  42. virtual bool visit( TiXmlElement* pXmlElement, TamlXmlParser& xmlParser )
  43. {
  44. // Debug Profiling.
  45. PROFILE_SCOPE(TamlAssetDeclaredUpdateVisitor_VisitElement);
  46. // Finish if this is not the root element.
  47. if ( pXmlElement != pXmlElement->GetDocument()->RootElement() )
  48. return true;
  49. // Fetch asset field names.
  50. StringTableEntry assetNameField = StringTable->insert( ASSET_BASE_ASSETNAME_FIELD );
  51. // Iterate attributes.
  52. for ( TiXmlAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
  53. {
  54. // Insert attribute name.
  55. StringTableEntry attributeName = StringTable->insert( pAttribute->Name() );
  56. // Asset name?
  57. if ( attributeName != assetNameField )
  58. continue;
  59. // Is this the asset Id we're looking for?
  60. if ( dStricmp( pAttribute->Value(), mAssetNameFrom ) != 0 )
  61. {
  62. // No, so warn.
  63. Con::warnf("Cannot rename asset Name '%s' to asset Name '%s' as the declared asset Name was %s",
  64. mAssetNameFrom, mAssetNameTo, pAttribute->Value() );
  65. // Stop processing!
  66. return false;
  67. }
  68. // Assign new value.
  69. pAttribute->SetValue( mAssetNameTo );
  70. // Stop processing!
  71. return false;
  72. }
  73. return true;
  74. }
  75. virtual bool visit( TiXmlAttribute* pAttribute, TamlXmlParser& xmlParser ) { return true; }
  76. public:
  77. TamlAssetDeclaredUpdateVisitor() {}
  78. virtual ~TamlAssetDeclaredUpdateVisitor() {}
  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. // Reset asset Id.
  89. mAssetIdFrom = StringTable->EmptyString;
  90. mAssetNameFrom = StringTable->EmptyString;
  91. // Is asset Id the correct format?
  92. if ( StringUnit::getUnitCount( pAssetIdFrom, ASSET_SCOPE_TOKEN ) != 2 )
  93. {
  94. // No, so warn.
  95. Con::warnf( "TamlAssetDeclaredUpdateVisitor::setAssetIdFrom() - Cannot use asset Id '%s' as it is not the correct format.", pAssetIdFrom );
  96. return;
  97. }
  98. // Set asset Id.
  99. mAssetIdFrom = StringTable->insert( pAssetIdFrom );
  100. mAssetNameFrom = StringTable->insert( StringUnit::getUnit( pAssetIdFrom, 1, ASSET_SCOPE_TOKEN ) );
  101. }
  102. StringTableEntry getAssetIdFrom( void ) const { return mAssetIdFrom; }
  103. void setAssetIdTo( const char* pAssetIdTo )
  104. {
  105. // Sanity!
  106. AssertFatal( pAssetIdTo != NULL, "Asset Id to cannot be NULL." );
  107. // Reset asset Id.
  108. mAssetIdTo = StringTable->EmptyString;
  109. mAssetNameTo = StringTable->EmptyString;
  110. // Is asset Id the correct format?
  111. if ( StringUnit::getUnitCount( pAssetIdTo, ASSET_SCOPE_TOKEN ) != 2 )
  112. {
  113. // No, so warn.
  114. Con::warnf( "TamlAssetDeclaredUpdateVisitor::setAssetIdTo() - Cannot use asset Id '%s' as it is not the correct format.", pAssetIdTo );
  115. return;
  116. }
  117. // Set asset Id.
  118. mAssetIdTo = StringTable->insert( pAssetIdTo );
  119. mAssetNameTo = StringTable->insert( StringUnit::getUnit( pAssetIdTo, 1, ASSET_SCOPE_TOKEN ) );
  120. }
  121. const char* getAssetIdTo( void ) const { return mAssetIdTo; }
  122. private:
  123. StringTableEntry mAssetIdFrom;
  124. StringTableEntry mAssetIdTo;
  125. StringTableEntry mAssetNameFrom;
  126. StringTableEntry mAssetNameTo;
  127. };
  128. #endif // _TAML_ASSET_DECLARED_UPDATE_VISITOR_H_