assetBase.cpp 15 KB

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