assetBase.cpp 15 KB

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