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