assetBase.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. extern StringTableEntry assetTypeField;
  43. //#define ASSET_BASE_ASSETNAME_FIELD "AssetName"
  44. //#define ASSET_BASE_ASSETDESCRIPTION_FIELD "AssetDescription"
  45. //#define ASSET_BASE_ASSETCATEGORY_FIELD "AssetCategory"
  46. //#define ASSET_BASE_ASSETINTERNAL_FIELD "AssetInternal"
  47. //#define ASSET_BASE_ASSETPRIVATE_FIELD "AssetPrivate"
  48. //#define ASSET_BASE_AUTOUNLOAD_FIELD "AssetAutoUnload"
  49. //-----------------------------------------------------------------------------
  50. class AssetBase : public SimGroup
  51. {
  52. friend class AssetManager;
  53. typedef SimGroup Parent;
  54. protected:
  55. AssetManager* mpOwningAssetManager;
  56. bool mAssetInitialized;
  57. AssetDefinition* mpAssetDefinition;
  58. U32 mAcquireReferenceCount;
  59. U32 mLoadedState;
  60. public:
  61. enum AssetErrCode
  62. {
  63. Failed,
  64. Ok,
  65. NotLoaded,
  66. Reloading,
  67. BadFileReference,
  68. InvalidFormat,
  69. DependencyNotFound,
  70. FileTooLarge,
  71. UsingFallback,
  72. Extended
  73. };
  74. static const String mErrCodeStrings[AssetErrCode::Extended + 1];
  75. static String getAssetErrstrn(U32 errCode)
  76. {
  77. if (errCode > AssetErrCode::Extended) return "undefined error";
  78. return mErrCodeStrings[errCode];
  79. };
  80. U32 getStatus() { return mLoadedState; };
  81. virtual U32 load() { return NotLoaded; };
  82. AssetBase();
  83. virtual ~AssetBase();
  84. /// Engine.
  85. static void initPersistFields();
  86. void copyTo(SimObject* object) override;
  87. /// Asset configuration.
  88. inline void setAssetName(const char* pAssetName) { if (mpOwningAssetManager == NULL) mpAssetDefinition->mAssetName = StringTable->insert(pAssetName); }
  89. inline StringTableEntry getAssetName(void) const { return mpAssetDefinition ? mpAssetDefinition->mAssetName : StringTable->EmptyString(); }
  90. void setAssetDescription(const char* pAssetDescription);
  91. inline StringTableEntry getAssetDescription(void) const { return mpAssetDefinition ? mpAssetDefinition->mAssetDescription : StringTable->EmptyString(); }
  92. void setAssetCategory(const char* pAssetCategory);
  93. inline StringTableEntry getAssetCategory(void) const { return mpAssetDefinition ? mpAssetDefinition->mAssetCategory : StringTable->EmptyString(); }
  94. void setAssetAutoUnload(const bool autoUnload);
  95. inline bool getAssetAutoUnload(void) const { return mpAssetDefinition->mAssetAutoUnload; }
  96. void setAssetInternal(const bool assetInternal);
  97. inline bool getAssetInternal(void) const { return mpAssetDefinition->mAssetInternal; }
  98. inline bool getAssetPrivate(void) const { return mpAssetDefinition->mAssetPrivate; }
  99. inline StringTableEntry getAssetType(void) const { return mpAssetDefinition ? mpAssetDefinition->mAssetType: StringTable->EmptyString(); }
  100. inline S32 getAcquiredReferenceCount(void) const { return mAcquireReferenceCount; }
  101. inline bool getOwned(void) const { return mpOwningAssetManager != NULL; }
  102. // Asset Id is only available once registered with the asset manager.
  103. inline StringTableEntry getAssetId(void) const { return mpAssetDefinition ? mpAssetDefinition->mAssetId : StringTable->EmptyString(); }
  104. /// Expanding/Collapsing asset paths is only available once registered with the asset manager.
  105. StringTableEntry expandAssetFilePath(const char* pAssetFilePath) const;
  106. StringTableEntry collapseAssetFilePath(const char* pAssetFilePath) const;
  107. virtual bool isAssetValid(void) const
  108. {
  109. return mpOwningAssetManager != nullptr && mAssetInitialized && (mLoadedState == AssetErrCode::Ok || mLoadedState == AssetErrCode::UsingFallback);
  110. }
  111. void refreshAsset(void);
  112. S32 getAssetDependencyFieldCount(const char* pFieldName);
  113. StringTableEntry getAssetDependencyField(const char* pFieldName, S32 index = 0);
  114. void clearAssetDependencyFields(const char* pFieldName);
  115. void addAssetDependencyField(const char* pFieldName, const char* pAssetId);
  116. bool saveAsset();
  117. /// Declare Console Object.
  118. DECLARE_CONOBJECT(AssetBase);
  119. protected:
  120. virtual void initializeAsset(void) {}
  121. virtual void onAssetRefresh(void) {}
  122. virtual void unloadAsset(void) {}
  123. protected:
  124. static bool setAssetName(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetName(data); return false; }
  125. static const char* getAssetName(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetName(); }
  126. static bool writeAssetName(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetName() != StringTable->EmptyString(); }
  127. static bool setAssetDescription(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetDescription(data); return false; }
  128. static const char* getAssetDescription(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetDescription(); }
  129. static bool writeAssetDescription(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetDescription() != StringTable->EmptyString(); }
  130. static bool setAssetCategory(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetCategory(data); return false; }
  131. static const char* getAssetCategory(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetCategory(); }
  132. static bool writeAssetCategory(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetCategory() != StringTable->EmptyString(); }
  133. static bool setAssetAutoUnload(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetAutoUnload(dAtob(data)); return false; }
  134. static const char* getAssetAutoUnload(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetAutoUnload()); }
  135. static bool writeAssetAutoUnload(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetAutoUnload() == false; }
  136. static bool setAssetInternal(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetInternal(dAtob(data)); return false; }
  137. static const char* getAssetInternal(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetInternal()); }
  138. static bool writeAssetInternal(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetInternal() == true; }
  139. static const char* getAssetPrivate(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetPrivate()); }
  140. private:
  141. void acquireAssetReference(void);
  142. bool releaseAssetReference(void);
  143. /// Set asset manager ownership.
  144. void setOwned(AssetManager* pAssetManager, AssetDefinition* pAssetDefinition);
  145. };
  146. //helper macro for stitching string and non string values togeather sans quotes
  147. #define assetText(x,suff) #x#suff
  148. #define macroText(x) #x
  149. #define assetDoc(x,suff) "@brief "#x" "#suff
  150. #endif // _ASSET_BASE_H_