assetBase.h 7.9 KB

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