assetBase.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "platform/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. mpOwningAssetManager(NULL),
  47. mAcquireReferenceCount(0),
  48. mAssetInitialized(false)
  49. {
  50. // Generate an asset definition.
  51. mpAssetDefinition = new AssetDefinition();
  52. mInternalName = StringTable->EmptyString();
  53. mClassName = StringTable->EmptyString();
  54. mSuperClassName = StringTable->EmptyString();
  55. }
  56. //-----------------------------------------------------------------------------
  57. AssetBase::~AssetBase()
  58. {
  59. // If the asset manager does not own the asset then we own the
  60. // asset definition so delete it.
  61. if (!getOwned())
  62. SAFE_DELETE(mpAssetDefinition);
  63. }
  64. //-----------------------------------------------------------------------------
  65. void AssetBase::initPersistFields()
  66. {
  67. // Call parent.
  68. Parent::initPersistFields();
  69. // Asset configuration.
  70. addProtectedField(assetNameField, TypeString, 0, &setAssetName, &getAssetName, &writeAssetName, "The name of the asset. The is not a unique identification like an asset Id.");
  71. addProtectedField(assetDescriptionField, TypeString, 0, &setAssetDescription, &getAssetDescription, &writeAssetDescription, "The simple description of the asset contents.");
  72. addProtectedField(assetCategoryField, TypeString, 0, &setAssetCategory, &getAssetCategory, &writeAssetCategory, "An arbitrary category that can be used to categorized assets.");
  73. 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.");
  74. addProtectedField(assetInternalField, TypeBool, 0, &setAssetInternal, &getAssetInternal, &writeAssetInternal, "Whether the asset is used internally only or not.");
  75. addProtectedField(assetPrivateField, TypeBool, 0, &defaultProtectedNotSetFn, &getAssetPrivate, &defaultProtectedNotWriteFn, "Whether the asset is private or not.");
  76. }
  77. //------------------------------------------------------------------------------
  78. void AssetBase::copyTo(SimObject* object)
  79. {
  80. // Call to parent.
  81. Parent::copyTo(object);
  82. // Cast to asset.
  83. AssetBase* pAsset = static_cast<AssetBase*>(object);
  84. // Sanity!
  85. AssertFatal(pAsset != NULL, "AssetBase::copyTo() - Object is not the correct type.");
  86. // Copy state.
  87. pAsset->setAssetName(getAssetName());
  88. pAsset->setAssetDescription(getAssetDescription());
  89. pAsset->setAssetCategory(getAssetCategory());
  90. pAsset->setAssetAutoUnload(getAssetAutoUnload());
  91. pAsset->setAssetInternal(getAssetInternal());
  92. }
  93. //-----------------------------------------------------------------------------
  94. void AssetBase::setAssetDescription(const char* pAssetDescription)
  95. {
  96. // Fetch asset description.
  97. StringTableEntry assetDescription = StringTable->insert(pAssetDescription);
  98. // Ignore no change.
  99. if (mpAssetDefinition->mAssetDescription == assetDescription)
  100. return;
  101. // Update.
  102. mpAssetDefinition->mAssetDescription = assetDescription;
  103. // Refresh the asset.
  104. refreshAsset();
  105. }
  106. //-----------------------------------------------------------------------------
  107. void AssetBase::setAssetCategory(const char* pAssetCategory)
  108. {
  109. // Fetch asset category.
  110. StringTableEntry assetCategory = StringTable->insert(pAssetCategory);
  111. // Ignore no change.
  112. if (mpAssetDefinition->mAssetCategory == assetCategory)
  113. return;
  114. // Update.
  115. mpAssetDefinition->mAssetCategory = assetCategory;
  116. // Refresh the asset.
  117. refreshAsset();
  118. }
  119. //-----------------------------------------------------------------------------
  120. void AssetBase::setAssetAutoUnload(const bool autoUnload)
  121. {
  122. // Ignore no change.
  123. if (mpAssetDefinition->mAssetAutoUnload == autoUnload)
  124. return;
  125. // Update.
  126. mpAssetDefinition->mAssetAutoUnload = autoUnload;
  127. // Refresh the asset.
  128. refreshAsset();
  129. }
  130. //-----------------------------------------------------------------------------
  131. void AssetBase::setAssetInternal(const bool assetInternal)
  132. {
  133. // Ignore no change,
  134. if (mpAssetDefinition->mAssetInternal == assetInternal)
  135. return;
  136. // Update.
  137. mpAssetDefinition->mAssetInternal = assetInternal;
  138. // Refresh the asset.
  139. refreshAsset();
  140. }
  141. //-----------------------------------------------------------------------------
  142. StringTableEntry AssetBase::expandAssetFilePath(const char* pAssetFilePath) const
  143. {
  144. // Debug Profiling.
  145. PROFILE_SCOPE(AssetBase_ExpandAssetFilePath);
  146. // Sanity!
  147. AssertFatal(pAssetFilePath != NULL, "Cannot expand a NULL asset path.");
  148. // Fetch asset file-path length.
  149. const U32 assetFilePathLength = dStrlen(pAssetFilePath);
  150. // Are there any characters in the path?
  151. if (assetFilePathLength == 0)
  152. {
  153. // No, so return empty.
  154. return StringTable->EmptyString();
  155. }
  156. // Fetch the asset base-path hint.
  157. StringTableEntry assetBasePathHint;
  158. if (getOwned() && !getAssetPrivate())
  159. {
  160. assetBasePathHint = mpOwningAssetManager->getAssetPath(getAssetId());
  161. }
  162. else
  163. {
  164. assetBasePathHint = NULL;
  165. }
  166. // Expand the path with the asset base-path hint.
  167. char assetFilePathBuffer[1024];
  168. Con::expandPath(assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath, assetBasePathHint);
  169. return StringTable->insert(assetFilePathBuffer);
  170. }
  171. //-----------------------------------------------------------------------------
  172. StringTableEntry AssetBase::collapseAssetFilePath(const char* pAssetFilePath) const
  173. {
  174. // Debug Profiling.
  175. PROFILE_SCOPE(AssetBase_CollapseAssetFilePath);
  176. // Sanity!
  177. AssertFatal(pAssetFilePath != NULL, "Cannot collapse a NULL asset path.");
  178. // Fetch asset file-path length.
  179. const U32 assetFilePathLength = dStrlen(pAssetFilePath);
  180. // Are there any characters in the path?
  181. if (assetFilePathLength == 0)
  182. {
  183. // No, so return empty.
  184. return StringTable->EmptyString();
  185. }
  186. char assetFilePathBuffer[1024];
  187. // Is the asset not owned or private?
  188. if (!getOwned() || getAssetPrivate())
  189. {
  190. // Yes, so we can only collapse the path using the platform layer.
  191. Con::collapsePath(assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath);
  192. return StringTable->insert(assetFilePathBuffer);
  193. }
  194. // Fetch asset base-path.
  195. StringTableEntry assetBasePath = mpOwningAssetManager->getAssetPath(getAssetId());
  196. // Is the asset file-path location within the asset base-path?
  197. if (Con::isBasePath(pAssetFilePath, assetBasePath))
  198. {
  199. // Yes, so fetch path relative to the asset base-path.
  200. StringTableEntry relativePath = Platform::makeRelativePathName(pAssetFilePath, assetBasePath);
  201. // Format the collapsed path.
  202. dSprintf(assetFilePathBuffer, sizeof(assetFilePathBuffer), "%s", relativePath);
  203. }
  204. else
  205. {
  206. // No, so we can collapse the path using the platform layer.
  207. Con::collapsePath(assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath);
  208. }
  209. return StringTable->insert(assetFilePathBuffer);
  210. }
  211. //-----------------------------------------------------------------------------
  212. void AssetBase::refreshAsset(void)
  213. {
  214. // Debug Profiling.
  215. PROFILE_SCOPE(AssetBase_RefreshAsset);
  216. // Finish if asset is not owned or is not initialized.
  217. if (mpOwningAssetManager == NULL || !mAssetInitialized)
  218. return;
  219. // Yes, so refresh the asset via the asset manager.
  220. mpOwningAssetManager->refreshAsset(getAssetId());
  221. }
  222. //-----------------------------------------------------------------------------
  223. S32 AssetBase::getAssetDependencyFieldCount(const char* pFieldName)
  224. {
  225. S32 matchedFieldCount = 0;
  226. SimFieldDictionary* fieldDictionary = getFieldDictionary();
  227. for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
  228. {
  229. SimFieldDictionary::Entry* entry = *itr;
  230. if (String(entry->slotName).startsWith(pFieldName))
  231. {
  232. matchedFieldCount++;
  233. }
  234. }
  235. return matchedFieldCount;
  236. }
  237. //-----------------------------------------------------------------------------
  238. void AssetBase::clearAssetDependencyFields(const char* pFieldName)
  239. {
  240. SimFieldDictionary* fieldDictionary = getFieldDictionary();
  241. for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
  242. {
  243. SimFieldDictionary::Entry* entry = *itr;
  244. if (String(entry->slotName).startsWith(pFieldName))
  245. {
  246. setDataField(entry->slotName, NULL, "");
  247. }
  248. }
  249. }
  250. //-----------------------------------------------------------------------------
  251. void AssetBase::addAssetDependencyField(const char* pFieldName, const char* pAssetId)
  252. {
  253. U32 existingFieldCount = getAssetDependencyFieldCount(pFieldName);
  254. //we have a match!
  255. char depSlotName[50];
  256. dSprintf(depSlotName, sizeof(depSlotName), "%s%d", pFieldName, existingFieldCount);
  257. char depValue[255];
  258. dSprintf(depValue, sizeof(depValue), "@Asset=%s", pAssetId);
  259. setDataField(StringTable->insert(depSlotName), NULL, StringTable->insert(depValue));
  260. }
  261. //-----------------------------------------------------------------------------
  262. bool AssetBase::saveAsset()
  263. {
  264. // Set the format mode.
  265. Taml taml;
  266. // Yes, so set it.
  267. taml.setFormatMode(Taml::getFormatModeEnum("xml"));
  268. // Turn-off auto-formatting.
  269. taml.setAutoFormat(false);
  270. // Read object.
  271. bool success = taml.write(this, AssetDatabase.getAssetFilePath(getAssetId()));
  272. if (!success)
  273. return false;
  274. return true;
  275. }
  276. //-----------------------------------------------------------------------------
  277. void AssetBase::acquireAssetReference(void)
  278. {
  279. // Acquired the acquired reference count.
  280. if (mpOwningAssetManager != NULL)
  281. mpOwningAssetManager->acquireAcquiredReferenceCount();
  282. mAcquireReferenceCount++;
  283. }
  284. //-----------------------------------------------------------------------------
  285. bool AssetBase::releaseAssetReference(void)
  286. {
  287. // Are there any acquisition references?
  288. if (mAcquireReferenceCount == 0)
  289. {
  290. // Return "unload" unless auto unload is off.
  291. return mpAssetDefinition->mAssetAutoUnload;
  292. }
  293. // Release the acquired reference count.
  294. if (mpOwningAssetManager != NULL)
  295. mpOwningAssetManager->releaseAcquiredReferenceCount();
  296. // Release reference.
  297. mAcquireReferenceCount--;
  298. // Are there any acquisition references?
  299. if (mAcquireReferenceCount == 0)
  300. {
  301. // No, so return "unload" unless auto unload is off.
  302. return mpAssetDefinition->mAssetAutoUnload;
  303. }
  304. // Return "don't unload".
  305. return false;
  306. }
  307. //-----------------------------------------------------------------------------
  308. void AssetBase::setOwned(AssetManager* pAssetManager, AssetDefinition* pAssetDefinition)
  309. {
  310. // Debug Profiling.
  311. PROFILE_SCOPE(AssetBase_setOwned);
  312. // Sanity!
  313. AssertFatal(pAssetManager != NULL, "Cannot set asset ownership with NULL asset manager.");
  314. AssertFatal(mpOwningAssetManager == NULL, "Cannot set asset ownership if it is already owned.");
  315. AssertFatal(pAssetDefinition != NULL, "Cannot set asset ownership with a NULL asset definition.");
  316. AssertFatal(mpAssetDefinition != NULL, "Asset ownership assigned but has a NULL asset definition.");
  317. AssertFatal(mpAssetDefinition->mAssetName == pAssetDefinition->mAssetName, "Asset ownership differs by asset name.");
  318. AssertFatal(mpAssetDefinition->mAssetDescription == pAssetDefinition->mAssetDescription, "Asset ownership differs by asset description.");
  319. AssertFatal(mpAssetDefinition->mAssetCategory == pAssetDefinition->mAssetCategory, "Asset ownership differs by asset category.");
  320. AssertFatal(mpAssetDefinition->mAssetAutoUnload == pAssetDefinition->mAssetAutoUnload, "Asset ownership differs by asset auto-unload flag.");
  321. AssertFatal(mpAssetDefinition->mAssetInternal == pAssetDefinition->mAssetInternal, "Asset ownership differs by asset internal flag.");
  322. // Transfer asset definition ownership state.
  323. delete mpAssetDefinition;
  324. mpAssetDefinition = pAssetDefinition;
  325. // Flag as owned.
  326. // NOTE: This must be done prior to initializing the asset so any initialization can assume ownership.
  327. mpOwningAssetManager = pAssetManager;
  328. // Initialize the asset.
  329. initializeAsset();
  330. // Flag asset as initialized.
  331. mAssetInitialized = true;
  332. }