assetBase.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include "assetBase.h"
  24. #endif
  25. #ifndef _ASSET_MANAGER_H_
  26. #include "assetManager.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. // Script bindings.
  32. #include "assetBase_ScriptBinding.h"
  33. // Debug Profiling.
  34. #include "debug/profiler.h"
  35. //-----------------------------------------------------------------------------
  36. IMPLEMENT_CONOBJECT( AssetBase );
  37. //-----------------------------------------------------------------------------
  38. AssetBase::AssetBase() :
  39. mAcquireReferenceCount( 0 ),
  40. mpOwningAssetManager( NULL ),
  41. mAssetInitialized( false )
  42. {
  43. // Generate an asset definition.
  44. mpAssetDefinition = new AssetDefinition();
  45. }
  46. //-----------------------------------------------------------------------------
  47. AssetBase::~AssetBase()
  48. {
  49. // If the asset manager does not own the asset then we own the
  50. // asset definition so delete it.
  51. if ( !getOwned() )
  52. delete mpAssetDefinition;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void AssetBase::initPersistFields()
  56. {
  57. // Call parent.
  58. Parent::initPersistFields();
  59. // Asset configuration.
  60. addProtectedField( ASSET_BASE_ASSETNAME_FIELD, TypeString, 0, &setAssetName, &getAssetName, &writeAssetName, "The name of the asset. The is not a unique identification like an asset Id." );
  61. addProtectedField( ASSET_BASE_ASSETDESCRIPTION_FIELD, TypeString, 0, &setAssetDescription, &getAssetDescription, &writeAssetDescription, "The simple description of the asset contents." );
  62. addProtectedField( ASSET_BASE_CATEGORY_FIELD, TypeString, 0, &setAssetCategory, &getAssetCategory, &writeAssetCategory, "An arbitrary category that can be used to categorized assets." );
  63. addProtectedField( ASSET_BASE_AUTOUNLOAD_FIELD, TypeBool, 0, &setAssetAutoUnload, &getAssetAutoUnload, &writeAssetAutoUnload, "Whether the asset is automatically unloaded when an asset is released and has no other acquisitions or not." );
  64. addProtectedField( ASSET_BASE_ASSETINTERNAL_FIELD, TypeBool, 0, &setAssetInternal, &getAssetInternal, &writeAssetInternal, "Whether the asset is used internally only or not." );
  65. addProtectedField( ASSET_BASE_ASSETPRIVATE_FIELD, TypeBool, 0, &defaultProtectedNotSetFn, &getAssetPrivate, &defaultProtectedNotWriteFn, "Whether the asset is private or not." );
  66. }
  67. //------------------------------------------------------------------------------
  68. void AssetBase::copyTo(SimObject* object)
  69. {
  70. // Call to parent.
  71. Parent::copyTo(object);
  72. // Cast to asset.
  73. AssetBase* pAsset = static_cast<AssetBase*>(object);
  74. // Sanity!
  75. AssertFatal(pAsset != NULL, "AssetBase::copyTo() - Object is not the correct type.");
  76. // Copy state.
  77. pAsset->setAssetName( getAssetName() );
  78. pAsset->setAssetDescription( getAssetDescription() );
  79. pAsset->setAssetCategory( getAssetCategory() );
  80. pAsset->setAssetAutoUnload( getAssetAutoUnload() );
  81. pAsset->setAssetInternal( getAssetInternal() );
  82. }
  83. //-----------------------------------------------------------------------------
  84. void AssetBase::setAssetDescription( const char* pAssetDescription )
  85. {
  86. // Fetch asset description.
  87. StringTableEntry assetDescription = StringTable->insert(pAssetDescription);
  88. // Ignore no change.
  89. if ( mpAssetDefinition->mAssetDescription == assetDescription )
  90. return;
  91. // Update.
  92. mpAssetDefinition->mAssetDescription = assetDescription;
  93. // Refresh the asset.
  94. refreshAsset();
  95. }
  96. //-----------------------------------------------------------------------------
  97. void AssetBase::setAssetCategory( const char* pAssetCategory )
  98. {
  99. // Fetch asset category.
  100. StringTableEntry assetCategory = StringTable->insert(pAssetCategory);
  101. // Ignore no change.
  102. if ( mpAssetDefinition->mAssetCategory == assetCategory )
  103. return;
  104. // Update.
  105. mpAssetDefinition->mAssetCategory = assetCategory;
  106. // Refresh the asset.
  107. refreshAsset();
  108. }
  109. //-----------------------------------------------------------------------------
  110. void AssetBase::setAssetAutoUnload( const bool autoUnload )
  111. {
  112. // Ignore no change.
  113. if ( mpAssetDefinition->mAssetAutoUnload == autoUnload )
  114. return;
  115. // Update.
  116. mpAssetDefinition->mAssetAutoUnload = autoUnload;
  117. // Refresh the asset.
  118. refreshAsset();
  119. }
  120. //-----------------------------------------------------------------------------
  121. void AssetBase::setAssetInternal( const bool assetInternal )
  122. {
  123. // Ignore no change,
  124. if ( mpAssetDefinition->mAssetInternal == assetInternal )
  125. return;
  126. // Update.
  127. mpAssetDefinition->mAssetInternal = assetInternal;
  128. // Refresh the asset.
  129. refreshAsset();
  130. }
  131. //-----------------------------------------------------------------------------
  132. StringTableEntry AssetBase::expandAssetFilePath( const char* pAssetFilePath ) const
  133. {
  134. // Debug Profiling.
  135. PROFILE_SCOPE(AssetBase_ExpandAssetFilePath);
  136. // Sanity!
  137. AssertFatal( pAssetFilePath != NULL, "Cannot expand a NULL asset path." );
  138. // Fetch asset file-path length.
  139. const U32 assetFilePathLength = dStrlen(pAssetFilePath);
  140. // Are there any characters in the path?
  141. if ( assetFilePathLength == 0 )
  142. {
  143. // No, so return empty.
  144. return StringTable->EmptyString;
  145. }
  146. // Fetch the asset base-path hint.
  147. StringTableEntry assetBasePathHint;
  148. if ( getOwned() && !getAssetPrivate() )
  149. {
  150. assetBasePathHint = mpOwningAssetManager->getAssetPath( getAssetId() );
  151. }
  152. else
  153. {
  154. assetBasePathHint = NULL;
  155. }
  156. // Expand the path with the asset base-path hint.
  157. char assetFilePathBuffer[1024];
  158. Con::expandPath( assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath, assetBasePathHint );
  159. return StringTable->insert( assetFilePathBuffer );
  160. }
  161. //-----------------------------------------------------------------------------
  162. StringTableEntry AssetBase::collapseAssetFilePath( const char* pAssetFilePath ) const
  163. {
  164. // Debug Profiling.
  165. PROFILE_SCOPE(AssetBase_CollapseAssetFilePath);
  166. // Sanity!
  167. AssertFatal( pAssetFilePath != NULL, "Cannot collapse a NULL asset path." );
  168. // Fetch asset file-path length.
  169. const U32 assetFilePathLength = dStrlen(pAssetFilePath);
  170. // Are there any characters in the path?
  171. if ( assetFilePathLength == 0 )
  172. {
  173. // No, so return empty.
  174. return StringTable->EmptyString;
  175. }
  176. char assetFilePathBuffer[1024];
  177. // Is the asset not owned or private?
  178. if ( !getOwned() || getAssetPrivate() )
  179. {
  180. // Yes, so we can only collapse the path using the platform layer.
  181. Con::collapsePath( assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath );
  182. return StringTable->insert( assetFilePathBuffer );
  183. }
  184. // Fetch asset base-path.
  185. StringTableEntry assetBasePath = mpOwningAssetManager->getAssetPath( getAssetId() );
  186. // Is the asset file-path location within the asset base-path?
  187. if ( Con::isBasePath( pAssetFilePath, assetBasePath ) )
  188. {
  189. // Yes, so fetch path relative to the asset base-path.
  190. StringTableEntry relativePath = Platform::makeRelativePathName( pAssetFilePath, assetBasePath );
  191. // Format the collapsed path.
  192. dSprintf( assetFilePathBuffer, sizeof(assetFilePathBuffer), "%s", relativePath );
  193. }
  194. else
  195. {
  196. // No, so we can collapse the path using the platform layer.
  197. Con::collapsePath( assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath );
  198. }
  199. return StringTable->insert( assetFilePathBuffer );
  200. }
  201. //-----------------------------------------------------------------------------
  202. void AssetBase::refreshAsset( void )
  203. {
  204. // Debug Profiling.
  205. PROFILE_SCOPE(AssetBase_RefreshAsset);
  206. // Finish if asset is not owned or is not initialized.
  207. if ( mpOwningAssetManager == NULL || !mAssetInitialized )
  208. return;
  209. // Yes, so refresh the asset via the asset manager.
  210. mpOwningAssetManager->refreshAsset( getAssetId() );
  211. }
  212. //-----------------------------------------------------------------------------
  213. void AssetBase::acquireAssetReference( void )
  214. {
  215. // Acquired the acquired reference count.
  216. if ( mpOwningAssetManager != NULL )
  217. mpOwningAssetManager->acquireAcquiredReferenceCount();
  218. mAcquireReferenceCount++;
  219. }
  220. //-----------------------------------------------------------------------------
  221. bool AssetBase::releaseAssetReference( void )
  222. {
  223. // Are there any acquisition references?
  224. if ( mAcquireReferenceCount == 0 )
  225. {
  226. // Return "unload" unless auto unload is off.
  227. return mpAssetDefinition->mAssetAutoUnload;
  228. }
  229. // Release the acquired reference count.
  230. if ( mpOwningAssetManager != NULL )
  231. mpOwningAssetManager->releaseAcquiredReferenceCount();
  232. // Release reference.
  233. mAcquireReferenceCount--;
  234. // Are there any acquisition references?
  235. if ( mAcquireReferenceCount == 0 )
  236. {
  237. // No, so return "unload" unless auto unload is off.
  238. return mpAssetDefinition->mAssetAutoUnload;
  239. }
  240. // Return "don't unload".
  241. return false;
  242. }
  243. //-----------------------------------------------------------------------------
  244. void AssetBase::setOwned( AssetManager* pAssetManager, AssetDefinition* pAssetDefinition )
  245. {
  246. // Debug Profiling.
  247. PROFILE_SCOPE(AssetBase_setOwned);
  248. // Sanity!
  249. AssertFatal( pAssetManager != NULL, "Cannot set asset ownership with NULL asset manager." );
  250. AssertFatal( mpOwningAssetManager == NULL, "Cannot set asset ownership if it is already owned." );
  251. AssertFatal( pAssetDefinition != NULL, "Cannot set asset ownership with a NULL asset definition." );
  252. AssertFatal( mpAssetDefinition != NULL, "Asset ownership assigned but has a NULL asset definition." );
  253. AssertFatal( mpAssetDefinition->mAssetName == pAssetDefinition->mAssetName, "Asset ownership differs by asset name." );
  254. AssertFatal( mpAssetDefinition->mAssetDescription == pAssetDefinition->mAssetDescription, "Asset ownership differs by asset description." );
  255. AssertFatal( mpAssetDefinition->mAssetCategory == pAssetDefinition->mAssetCategory, "Asset ownership differs by asset category." );
  256. AssertFatal( mpAssetDefinition->mAssetAutoUnload == pAssetDefinition->mAssetAutoUnload, "Asset ownership differs by asset auto-unload flag." );
  257. AssertFatal( mpAssetDefinition->mAssetInternal == pAssetDefinition->mAssetInternal, "Asset ownership differs by asset internal flag." );
  258. // Transfer asset definition ownership state.
  259. delete mpAssetDefinition;
  260. mpAssetDefinition = pAssetDefinition;
  261. // Flag as owned.
  262. // NOTE: This must be done prior to initializing the asset so any initialization can assume ownership.
  263. mpOwningAssetManager = pAssetManager;
  264. // Initialize the asset.
  265. initializeAsset();
  266. // Flag asset as initialized.
  267. mAssetInitialized = true;
  268. }