assetBase.cpp 14 KB

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