assetBase.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 _ASSET_BASE_H_
  23. #define _ASSET_BASE_H_
  24. #ifndef _ASSET_DEFINITION_H_
  25. #include "assetDefinition.h"
  26. #endif
  27. #ifndef _STRINGUNIT_H_
  28. #include "string/stringUnit.h"
  29. #endif
  30. #ifndef _ASSET_FIELD_TYPES_H_
  31. #include "assets/assetFieldTypes.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. class AssetManager;
  35. //-----------------------------------------------------------------------------
  36. #define ASSET_BASE_ASSETNAME_FIELD "AssetName"
  37. #define ASSET_BASE_ASSETDESCRIPTION_FIELD "AssetDescription"
  38. #define ASSET_BASE_CATEGORY_FIELD "AssetCategory"
  39. #define ASSET_BASE_ASSETINTERNAL_FIELD "AssetInternal"
  40. #define ASSET_BASE_ASSETPRIVATE_FIELD "AssetPrivate"
  41. #define ASSET_BASE_AUTOUNLOAD_FIELD "AssetAutoUnload"
  42. //-----------------------------------------------------------------------------
  43. class AssetBase : public SimObject
  44. {
  45. friend class AssetManager;
  46. typedef SimObject Parent;
  47. AssetManager* mpOwningAssetManager;
  48. bool mAssetInitialized;
  49. AssetDefinition* mpAssetDefinition;
  50. U32 mAcquireReferenceCount;
  51. public:
  52. AssetBase();
  53. virtual ~AssetBase();
  54. /// Engine.
  55. static void initPersistFields();
  56. virtual void copyTo(SimObject* object);
  57. /// Asset configuration.
  58. inline void setAssetName( const char* pAssetName ) { if ( mpOwningAssetManager == NULL ) mpAssetDefinition->mAssetName = StringTable->insert(pAssetName); }
  59. inline StringTableEntry getAssetName( void ) const { return mpAssetDefinition->mAssetName; }
  60. void setAssetDescription( const char* pAssetDescription );
  61. inline StringTableEntry getAssetDescription( void ) const { return mpAssetDefinition->mAssetDescription; }
  62. void setAssetCategory( const char* pAssetCategory );
  63. inline StringTableEntry getAssetCategory( void ) const { return mpAssetDefinition->mAssetCategory; }
  64. void setAssetAutoUnload( const bool autoUnload );
  65. inline bool getAssetAutoUnload( void ) const { return mpAssetDefinition->mAssetAutoUnload; }
  66. void setAssetInternal( const bool assetInternal );
  67. inline bool getAssetInternal( void ) const { return mpAssetDefinition->mAssetInternal; }
  68. inline bool getAssetPrivate( void ) const { return mpAssetDefinition->mAssetPrivate; }
  69. inline StringTableEntry getAssetType( void ) const { return mpAssetDefinition->mAssetType; }
  70. inline S32 getAcquiredReferenceCount( void ) const { return mAcquireReferenceCount; }
  71. inline bool getOwned( void ) const { return mpOwningAssetManager != NULL; }
  72. // Asset Id is only available once registered with the asset manager.
  73. inline StringTableEntry getAssetId( void ) const { return mpAssetDefinition->mAssetId; }
  74. /// Expanding/Collapsing asset paths is only available once registered with the asset manager.
  75. StringTableEntry expandAssetFilePath( const char* pAssetFilePath ) const;
  76. StringTableEntry collapseAssetFilePath( const char* pAssetFilePath ) const;
  77. virtual bool isAssetValid( void ) const { return true; }
  78. void refreshAsset( void );
  79. /// Declare Console Object.
  80. DECLARE_CONOBJECT( AssetBase );
  81. protected:
  82. virtual void initializeAsset( void ) {}
  83. virtual void onAssetRefresh( void ) {}
  84. protected:
  85. static bool setAssetName(void* obj, const char* data) { static_cast<AssetBase*>(obj)->setAssetName( data ); return false; }
  86. static const char* getAssetName(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetName(); }
  87. static bool writeAssetName( void* obj, StringTableEntry pFieldName ) { return static_cast<AssetBase*>(obj)->getAssetName() != StringTable->EmptyString; }
  88. static bool setAssetDescription(void* obj, const char* data) { static_cast<AssetBase*>(obj)->setAssetDescription( data ); return false; }
  89. static const char* getAssetDescription(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetDescription(); }
  90. static bool writeAssetDescription( void* obj, StringTableEntry pFieldName ) { return static_cast<AssetBase*>(obj)->getAssetDescription() != StringTable->EmptyString; }
  91. static bool setAssetCategory(void* obj, const char* data) { static_cast<AssetBase*>(obj)->setAssetCategory( data ); return false; }
  92. static const char* getAssetCategory(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetCategory(); }
  93. static bool writeAssetCategory( void* obj, StringTableEntry pFieldName ) { return static_cast<AssetBase*>(obj)->getAssetCategory() != StringTable->EmptyString; }
  94. static bool setAssetAutoUnload(void* obj, const char* data) { static_cast<AssetBase*>(obj)->setAssetAutoUnload( dAtob(data) ); return false; }
  95. static const char* getAssetAutoUnload(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetAutoUnload()); }
  96. static bool writeAssetAutoUnload( void* obj, StringTableEntry pFieldName ) { return static_cast<AssetBase*>(obj)->getAssetAutoUnload() == false; }
  97. static bool setAssetInternal(void* obj, const char* data) { static_cast<AssetBase*>(obj)->setAssetInternal( dAtob(data) ); return false; }
  98. static const char* getAssetInternal(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetInternal()); }
  99. static bool writeAssetInternal( void* obj, StringTableEntry pFieldName ) { return static_cast<AssetBase*>(obj)->getAssetInternal() == true; }
  100. static const char* getAssetPrivate(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetPrivate()); }
  101. private:
  102. void acquireAssetReference( void );
  103. bool releaseAssetReference( void );
  104. /// Set asset manager ownership.
  105. void setOwned( AssetManager* pAssetManager, AssetDefinition* pAssetDefinition );
  106. };
  107. #endif // _ASSET_BASE_H_