assetBase.h 9.1 KB

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