assetBase.cpp

Engine/source/assets/assetBase.cpp

More...

Public Functions

Detailed Description

Public Variables

StringTableEntry assetAutoUnloadField 
StringTableEntry assetCategoryField 
StringTableEntry assetDescriptionField 
StringTableEntry assetInternalField 
StringTableEntry assetNameField 
StringTableEntry assetPrivateField 

Public Functions

IMPLEMENT_CONOBJECT(AssetBase )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2013 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _ASSET_BASE_H_
 25#include "assetBase.h"
 26#endif
 27
 28#ifndef _ASSET_MANAGER_H_
 29#include "assetManager.h"
 30#endif
 31
 32#ifndef _CONSOLETYPES_H_
 33#include "console/consoleTypes.h"
 34#endif
 35
 36// Script bindings.
 37#include "assetBase_ScriptBinding.h"
 38
 39// Debug Profiling.
 40#include "platform/profiler.h"
 41
 42//-----------------------------------------------------------------------------
 43
 44IMPLEMENT_CONOBJECT(AssetBase);
 45
 46//-----------------------------------------------------------------------------
 47
 48StringTableEntry assetNameField = StringTable->insert("AssetName");
 49StringTableEntry assetDescriptionField = StringTable->insert("AssetDescription");
 50StringTableEntry assetCategoryField = StringTable->insert("AssetCategory");
 51StringTableEntry assetAutoUnloadField = StringTable->insert("AssetAutoUnload");
 52StringTableEntry assetInternalField = StringTable->insert("AssetInternal");
 53StringTableEntry assetPrivateField = StringTable->insert("AssetPrivate");
 54
 55//-----------------------------------------------------------------------------
 56const String AssetBase::mErrCodeStrings[] =
 57{
 58   "Failed",
 59   "Ok",
 60   "NotLoaded",
 61   "BadFileReference",
 62   "InvalidFormat",
 63   "DependencyNotFound",
 64   "FileTooLarge",
 65   "UsingFallback",
 66   "UnKnown"
 67};
 68
 69AssetBase::AssetBase() :
 70mpOwningAssetManager(NULL),
 71mAcquireReferenceCount(0),
 72mAssetInitialized(false)
 73{
 74   // Generate an asset definition.
 75   mpAssetDefinition = new AssetDefinition();
 76
 77   mInternalName = StringTable->EmptyString();
 78   mClassName = StringTable->EmptyString();
 79   mSuperClassName = StringTable->EmptyString();
 80}
 81
 82//-----------------------------------------------------------------------------
 83
 84AssetBase::~AssetBase()
 85{
 86   // If the asset manager does not own the asset then we own the
 87   // asset definition so delete it.
 88   if (!getOwned())
 89      SAFE_DELETE(mpAssetDefinition);
 90}
 91
 92//-----------------------------------------------------------------------------
 93
 94void AssetBase::initPersistFields()
 95{
 96   // Call parent.
 97   Parent::initPersistFields();
 98
 99   // Asset configuration.
100   addProtectedField(assetNameField, TypeString, 0, &setAssetName, &getAssetName, &writeAssetName, "The name of the asset.  The is not a unique identification like an asset Id.");
101   addProtectedField(assetDescriptionField, TypeString, 0, &setAssetDescription, &getAssetDescription, &writeAssetDescription, "The simple description of the asset contents.");
102   addProtectedField(assetCategoryField, TypeString, 0, &setAssetCategory, &getAssetCategory, &writeAssetCategory, "An arbitrary category that can be used to categorized assets.");
103   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.");
104   addProtectedField(assetInternalField, TypeBool, 0, &setAssetInternal, &getAssetInternal, &writeAssetInternal, "Whether the asset is used internally only or not.");
105   addProtectedField(assetPrivateField, TypeBool, 0, &defaultProtectedNotSetFn, &getAssetPrivate, &defaultProtectedNotWriteFn, "Whether the asset is private or not.");
106}
107
108//------------------------------------------------------------------------------
109
110void AssetBase::copyTo(SimObject* object)
111{
112   // Call to parent.
113   Parent::copyTo(object);
114
115   // Cast to asset.
116   AssetBase* pAsset = static_cast<AssetBase*>(object);
117
118   // Sanity!
119   AssertFatal(pAsset != NULL, "AssetBase::copyTo() - Object is not the correct type.");
120
121   // Copy state.
122   pAsset->setAssetName(getAssetName());
123   pAsset->setAssetDescription(getAssetDescription());
124   pAsset->setAssetCategory(getAssetCategory());
125   pAsset->setAssetAutoUnload(getAssetAutoUnload());
126   pAsset->setAssetInternal(getAssetInternal());
127}
128
129//-----------------------------------------------------------------------------
130
131void AssetBase::setAssetDescription(const char* pAssetDescription)
132{
133   // Fetch asset description.
134   StringTableEntry assetDescription = StringTable->insert(pAssetDescription);
135
136   // Ignore no change.
137   if (mpAssetDefinition->mAssetDescription == assetDescription)
138      return;
139
140   // Update.
141   mpAssetDefinition->mAssetDescription = assetDescription;
142
143   // Refresh the asset.
144   refreshAsset();
145}
146
147//-----------------------------------------------------------------------------
148
149void AssetBase::setAssetCategory(const char* pAssetCategory)
150{
151   // Fetch asset category.
152   StringTableEntry assetCategory = StringTable->insert(pAssetCategory);
153
154   // Ignore no change.
155   if (mpAssetDefinition->mAssetCategory == assetCategory)
156      return;
157
158   // Update.
159   mpAssetDefinition->mAssetCategory = assetCategory;
160
161   // Refresh the asset.
162   refreshAsset();
163}
164
165//-----------------------------------------------------------------------------
166
167void AssetBase::setAssetAutoUnload(const bool autoUnload)
168{
169   // Ignore no change.
170   if (mpAssetDefinition->mAssetAutoUnload == autoUnload)
171      return;
172
173   // Update.
174   mpAssetDefinition->mAssetAutoUnload = autoUnload;
175
176   // Refresh the asset.
177   refreshAsset();
178}
179
180//-----------------------------------------------------------------------------
181
182void AssetBase::setAssetInternal(const bool assetInternal)
183{
184   // Ignore no change,
185   if (mpAssetDefinition->mAssetInternal == assetInternal)
186      return;
187
188   // Update.
189   mpAssetDefinition->mAssetInternal = assetInternal;
190
191   // Refresh the asset.
192   refreshAsset();
193}
194
195//-----------------------------------------------------------------------------
196
197StringTableEntry AssetBase::expandAssetFilePath(const char* pAssetFilePath) const
198{
199   // Debug Profiling.
200   PROFILE_SCOPE(AssetBase_ExpandAssetFilePath);
201
202   // Sanity!
203   AssertFatal(pAssetFilePath != NULL, "Cannot expand a NULL asset path.");
204
205   // Fetch asset file-path length.
206   const U32 assetFilePathLength = dStrlen(pAssetFilePath);
207
208   // Are there any characters in the path?
209   if (assetFilePathLength == 0)
210   {
211      // No, so return empty.
212      return StringTable->EmptyString();
213   }
214
215   // Fetch the asset base-path hint.
216   StringTableEntry assetBasePathHint;
217   if (getOwned() && !getAssetPrivate())
218   {
219      assetBasePathHint = mpOwningAssetManager->getAssetPath(getAssetId());
220   }
221   else
222   {
223      assetBasePathHint = NULL;
224   }
225
226   // Expand the path with the asset base-path hint.
227   char assetFilePathBuffer[1024];
228   Con::expandPath(assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath, assetBasePathHint);
229   return StringTable->insert(assetFilePathBuffer);
230}
231
232//-----------------------------------------------------------------------------
233
234StringTableEntry AssetBase::collapseAssetFilePath(const char* pAssetFilePath) const
235{
236   // Debug Profiling.
237   PROFILE_SCOPE(AssetBase_CollapseAssetFilePath);
238
239   // Sanity!
240   AssertFatal(pAssetFilePath != NULL, "Cannot collapse a NULL asset path.");
241
242   // Fetch asset file-path length.
243   const U32 assetFilePathLength = dStrlen(pAssetFilePath);
244
245   // Are there any characters in the path?
246   if (assetFilePathLength == 0)
247   {
248      // No, so return empty.
249      return StringTable->EmptyString();
250   }
251
252   char assetFilePathBuffer[1024];
253
254   // Is the asset not owned or private?
255   if (!getOwned() || getAssetPrivate())
256   {
257      // Yes, so we can only collapse the path using the platform layer.
258      Con::collapsePath(assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath);
259      return StringTable->insert(assetFilePathBuffer);
260   }
261
262   // Fetch asset base-path.
263   StringTableEntry assetBasePath = mpOwningAssetManager->getAssetPath(getAssetId());
264
265   // Is the asset file-path location within the asset base-path?
266   if (Con::isBasePath(pAssetFilePath, assetBasePath))
267   {
268      // Yes, so fetch path relative to the asset base-path.
269      StringTableEntry relativePath = Platform::makeRelativePathName(pAssetFilePath, assetBasePath);
270
271      // Format the collapsed path.
272      dSprintf(assetFilePathBuffer, sizeof(assetFilePathBuffer), "%s", relativePath);
273   }
274   else
275   {
276      // No, so we can collapse the path using the platform layer.
277      Con::collapsePath(assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath);
278   }
279
280   return StringTable->insert(assetFilePathBuffer);
281}
282
283//-----------------------------------------------------------------------------
284
285void AssetBase::refreshAsset(void)
286{
287   // Debug Profiling.
288   PROFILE_SCOPE(AssetBase_RefreshAsset);
289
290   // Finish if asset is not owned or is not initialized.
291   if (mpOwningAssetManager == NULL || !mAssetInitialized)
292      return;
293
294   // Yes, so refresh the asset via the asset manager.
295   mpOwningAssetManager->refreshAsset(getAssetId());
296}
297
298//-----------------------------------------------------------------------------
299
300S32 AssetBase::getAssetDependencyFieldCount(const char* pFieldName)
301{
302   S32 matchedFieldCount = 0;
303   SimFieldDictionary* fieldDictionary = getFieldDictionary();
304   for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
305   {
306      SimFieldDictionary::Entry* entry = *itr;
307
308      if (String(entry->slotName).startsWith(pFieldName))
309      {
310         matchedFieldCount++;
311      }
312   }
313
314   return matchedFieldCount;
315}
316
317//-----------------------------------------------------------------------------
318
319void AssetBase::clearAssetDependencyFields(const char* pFieldName)
320{
321   SimFieldDictionary* fieldDictionary = getFieldDictionary();
322   for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
323   {
324      SimFieldDictionary::Entry* entry = *itr;
325
326      if (String(entry->slotName).startsWith(pFieldName))
327      {
328         setDataField(entry->slotName, NULL, "");
329      }
330   }
331}
332
333//-----------------------------------------------------------------------------
334
335void AssetBase::addAssetDependencyField(const char* pFieldName, const char* pAssetId)
336{
337   U32 existingFieldCount = getAssetDependencyFieldCount(pFieldName);
338
339   //we have a match!
340   char depSlotName[50];
341   dSprintf(depSlotName, sizeof(depSlotName), "%s%d", pFieldName, existingFieldCount);
342
343   char depValue[255];
344   dSprintf(depValue, sizeof(depValue), "@Asset=%s", pAssetId);
345
346   setDataField(StringTable->insert(depSlotName), NULL, StringTable->insert(depValue));
347}
348
349//-----------------------------------------------------------------------------
350bool AssetBase::saveAsset()
351{
352   // Set the format mode.
353   Taml taml;
354
355   // Yes, so set it.
356   taml.setFormatMode(Taml::getFormatModeEnum("xml"));
357
358   // Turn-off auto-formatting.
359   taml.setAutoFormat(false);
360
361   // Read object.
362   bool success = taml.write(this, AssetDatabase.getAssetFilePath(getAssetId()));
363
364   if (!success)
365      return false;
366
367   return true;
368}
369
370//-----------------------------------------------------------------------------
371
372void AssetBase::acquireAssetReference(void)
373{
374   // Acquired the acquired reference count.
375   if (mpOwningAssetManager != NULL)
376      mpOwningAssetManager->acquireAcquiredReferenceCount();
377
378   mAcquireReferenceCount++;
379}
380
381//-----------------------------------------------------------------------------
382
383bool AssetBase::releaseAssetReference(void)
384{
385   // Are there any acquisition references?
386   if (mAcquireReferenceCount == 0)
387   {
388      // Return "unload" unless auto unload is off.
389      return mpAssetDefinition->mAssetAutoUnload;
390   }
391
392   // Release the acquired reference count.
393   if (mpOwningAssetManager != NULL)
394      mpOwningAssetManager->releaseAcquiredReferenceCount();
395
396   // Release reference.
397   mAcquireReferenceCount--;
398
399   // Are there any acquisition references?
400   if (mAcquireReferenceCount == 0)
401   {
402      // No, so return "unload" unless auto unload is off.
403      return mpAssetDefinition->mAssetAutoUnload;
404   }
405
406   // Return "don't unload".
407   return false;
408}
409
410//-----------------------------------------------------------------------------
411
412void AssetBase::setOwned(AssetManager* pAssetManager, AssetDefinition* pAssetDefinition)
413{
414   // Debug Profiling.
415   PROFILE_SCOPE(AssetBase_setOwned);
416
417   // Sanity!
418   AssertFatal(pAssetManager != NULL, "Cannot set asset ownership with NULL asset manager.");
419   AssertFatal(mpOwningAssetManager == NULL, "Cannot set asset ownership if it is already owned.");
420   AssertFatal(pAssetDefinition != NULL, "Cannot set asset ownership with a NULL asset definition.");
421   AssertFatal(mpAssetDefinition != NULL, "Asset ownership assigned but has a NULL asset definition.");
422   AssertFatal(mpAssetDefinition->mAssetName == pAssetDefinition->mAssetName, "Asset ownership differs by asset name.");
423   AssertFatal(mpAssetDefinition->mAssetDescription == pAssetDefinition->mAssetDescription, "Asset ownership differs by asset description.");
424   AssertFatal(mpAssetDefinition->mAssetCategory == pAssetDefinition->mAssetCategory, "Asset ownership differs by asset category.");
425   AssertFatal(mpAssetDefinition->mAssetAutoUnload == pAssetDefinition->mAssetAutoUnload, "Asset ownership differs by asset auto-unload flag.");
426   AssertFatal(mpAssetDefinition->mAssetInternal == pAssetDefinition->mAssetInternal, "Asset ownership differs by asset internal flag.");
427
428   // Transfer asset definition ownership state.
429   delete mpAssetDefinition;
430   mpAssetDefinition = pAssetDefinition;
431
432   // Flag as owned.
433   // NOTE: This must be done prior to initializing the asset so any initialization can assume ownership.
434   mpOwningAssetManager = pAssetManager;
435
436   // Initialize the asset.
437   initializeAsset();
438
439   // Flag asset as initialized.
440   mAssetInitialized = true;
441}
442