diff --git a/Engine/source/assets/assetManager.cpp b/Engine/source/assets/assetManager.cpp index f58239ad6..066fc3808 100644 --- a/Engine/source/assets/assetManager.cpp +++ b/Engine/source/assets/assetManager.cpp @@ -964,6 +964,148 @@ bool AssetManager::renameReferencedAsset( const char* pAssetIdFrom, const char* return true; } +bool AssetManager::compileAllAssets(const bool compressed, const bool includeUnloaded) +{ + // Debug Profiling. + PROFILE_SCOPE(AssetManager_CompileAllAssets); + + // Info. + if (mEchoInfo) + { + Con::printSeparator(); + Con::printf("Asset Manager: Started compiling ALL assets."); + } + + Vector assetsToRelease; + + // Are we including unloaded assets? + if (includeUnloaded) + { + // Yes, so prepare a list of assets to release and load them. + for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr) + { + // Fetch asset Id. + typeAssetId assetId = assetItr->key; + + // Skip if asset is loaded. + if (assetItr->value->mpAssetBase != NULL) + continue; + + // Note asset as needing a release. + assetsToRelease.push_back(assetId); + + // Acquire the asset. + acquireAsset(assetId); + } + } + + bool oldCompressed = mTaml.getBinaryCompression(); + mTaml.setBinaryCompression(compressed); + bool success = false; + // Refresh the current loaded assets. + // NOTE: This will result in some assets being refreshed more than once due to asset dependencies. + for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr) + { + // Skip private assets. + if (assetItr->value->mAssetPrivate) + continue; + + // Refresh asset if it's loaded. + success = compileAsset(assetItr->key); + if (!success) + { + break; + } + } + + mTaml.setBinaryCompression(oldCompressed); + + // Are we including unloaded assets? + if (includeUnloaded) + { + // Yes, so release the assets we loaded. + for (Vector::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr) + { + releaseAsset(*assetItr); + } + } + + // Info. + if (mEchoInfo) + { + Con::printSeparator(); + Con::printf("Asset Manager: Finished compiling ALL assets."); + } + + return success; +} + +bool AssetManager::compileModuleAssets(ModuleDefinition* pModuleDefinition) +{ + // Sanity! + AssertFatal(pModuleDefinition != NULL, "Cannot remove declared assets using a NULL module definition"); + + // Fetch module assets. + ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets(); + + // Remove all module assets. + while (moduleAssets.size() > 0) + { + // Fetch asset definition. + AssetDefinition* pAssetDefinition = *moduleAssets.begin(); + bool success = compileAsset(pAssetDefinition->mAssetId); + if (!success) + return false; + } + + return true; +} + +bool AssetManager::compileAsset(const char* pAssetId) +{ + // Sanity! + AssertFatal(pAssetId != NULL, "Cannot compile NULL asset"); + + AssetDefinition* pAssetDefinition = findAsset(pAssetId); + + // Does the asset exist? + if (pAssetDefinition == NULL) + { + Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not exist", pAssetId); + return false; + } + + // Info. + if (mEchoInfo) + { + Con::printSeparator(); + Con::printf("Asset Manager::compileAsset Started compiling Asset Id '%s'...", pAssetId); + } + + AssetBase* pAssetBase = pAssetDefinition->mpAssetBase; + if (pAssetBase != NULL) + { + Torque::Path binaryPath = pAssetDefinition->mAssetBaseFilePath; + binaryPath.setExtension(mTaml.getAutoFormatBinaryExtension()); + // Save asset. + mTaml.write(pAssetBase, binaryPath.getFullPath().c_str()); + } + else + { + Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not have an assetBase", pAssetId); + return false; + } + + // Info. + if (mEchoInfo) + { + Con::printSeparator(); + Con::printf("Asset Manager: Finished refreshing Asset Id '%s'.", pAssetId); + } + + return false; +} + //----------------------------------------------------------------------------- bool AssetManager::releaseAsset( const char* pAssetId ) diff --git a/Engine/source/assets/assetManager.h b/Engine/source/assets/assetManager.h index f87ac10ad..60afe08c9 100644 --- a/Engine/source/assets/assetManager.h +++ b/Engine/source/assets/assetManager.h @@ -151,6 +151,28 @@ public: bool isReferencedAsset( const char* pAssetId ); bool renameReferencedAsset( const char* pAssetIdFrom, const char* pAssetIdTo ); + /// + /// Compile all assets. + /// + /// Do we want binary compression? + /// Do we want to include unloaded assets? + /// True if successful, otherwise false. + bool compileAllAssets(const bool compressed = false, const bool includeUnloaded = false); + + /// + /// Compile all assets for a module. + /// + /// The module definition to compile. + /// + bool compileModuleAssets(ModuleDefinition* pModuleDefinition); + + /// + /// Compile a single asset to binary. + /// + /// The asset id to be compiled. + /// True if successful, otherwise false. + bool compileAsset(const char* pAssetId); + /// Public asset acquisition. template T* acquireAsset( const char* pAssetId ) { @@ -404,4 +426,4 @@ private: extern AssetManager AssetDatabase; -#endif // _ASSET_MANAGER_H_ \ No newline at end of file +#endif // _ASSET_MANAGER_H_ diff --git a/Engine/source/assets/assetManager_ScriptBinding.h b/Engine/source/assets/assetManager_ScriptBinding.h index 806ec2aeb..aa8c60d3c 100644 --- a/Engine/source/assets/assetManager_ScriptBinding.h +++ b/Engine/source/assets/assetManager_ScriptBinding.h @@ -1,4 +1,4 @@ -//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- // Copyright (c) 2013 GarageGames, LLC // // Permission is hereby granted, free of charge, to any person obtaining a copy @@ -20,11 +20,12 @@ // IN THE SOFTWARE. //----------------------------------------------------------------------------- #include "console/engineAPI.h" -#include "assetBase.h" -#include "assetManager.h" +#include "assets/assetBase.h" +#include "assets/assetManager.h" #include "module/moduleDefinition.h" #include "console/sim.h" + DefineEngineMethod(AssetManager, compileReferencedAssets, bool, (const char* moduleDefinition), (""), "Compile the referenced assets determined by the specified module definition.\n" "@param moduleDefinition The module definition specifies the asset manifest.\n" @@ -853,3 +854,40 @@ DefineEngineMethod(AssetManager, dumpDeclaredAssets, void, (), , { return object->dumpDeclaredAssets(); } + +//----------------------------------------------------------------------------- + +DefineEngineMethod(AssetManager, compileAllAssets, bool, (bool compressed, bool includeUnloaded),(false, true), + "Compile all assets.\n" + "@return true on success.\n") +{ + return object->compileAllAssets(compressed, includeUnloaded); +} + +DefineEngineMethod(AssetManager, compileModuleAssets, bool, (const char* moduleDefinition), (""), + "Compile all assets for a module.\n" + "@return true on success.\n") +{ + // Fetch module definition. + ModuleDefinition* pModuleDefinition; + Sim::findObject(moduleDefinition, pModuleDefinition); + + // Did we find the module definition? + if (pModuleDefinition == NULL) + { + // No, so warn. + Con::warnf("AssetManager::removeDeclaredAssets() - Could not find the module definition '%s'.", moduleDefinition); + return false; + } + + // Remove declared assets. + return object->compileModuleAssets(pModuleDefinition); +} + +DefineEngineMethod(AssetManager, compileAsset, bool, (const char* assetId), (""), + "Compile a single asset.\n" + "@return true on success.\n") +{ + return object->compileAsset(assetId); +} +