assetBase.cc 13 KB

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