Преглед изворни кода

Improvements to cache clean/generation, moving to AssetDatabase

Josh Engebretson пре 8 година
родитељ
комит
9ce35b9454

+ 75 - 11
Source/ToolCore/Assets/AssetDatabase.cpp

@@ -44,7 +44,8 @@ namespace ToolCore
 
 AssetDatabase::AssetDatabase(Context* context) : Object(context),
     assetScanDepth_(0),
-    assetScanImport_(false)
+    assetScanImport_(false),
+    cacheEnabled_(true)
 {
     SubscribeToEvent(E_LOADFAILED, ATOMIC_HANDLER(AssetDatabase, HandleResourceLoadFailed));
     SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(AssetDatabase, HandleProjectLoaded));
@@ -536,15 +537,10 @@ void AssetDatabase::HandleProjectLoaded(StringHash eventType, VariantMap& eventD
 
     ReadImportConfig();
 
-    FileSystem* fs = GetSubsystem<FileSystem>();
-
-    if (!fs->DirExists(GetCachePath()))
-        fs->CreateDir(GetCachePath());
-
-    ResourceCache* cache = GetSubsystem<ResourceCache>();
-    cache->AddResourceDir(GetCachePath());
-
-    Scan();
+    if (cacheEnabled_)
+    {
+        InitCache();
+    }
 
     SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(AssetDatabase, HandleFileChanged));
 }
@@ -594,7 +590,6 @@ void AssetDatabase::HandleResourceLoadFailed(StringHash eventType, VariantMap& e
     evData[AssetImportError::P_ERROR] = ToString("Asset %s Failed to Load", asset->path_.CString());
     SendEvent(E_ASSETIMPORTERROR, evData);
 
-
 }
 
 void AssetDatabase::HandleFileChanged(StringHash eventType, VariantMap& eventData)
@@ -704,4 +699,73 @@ void AssetDatabase::ReimportAllAssetsInDirectory(const String& directoryPath)
 }
 
 
+bool AssetDatabase::InitCache()
+{
+    FileSystem* fs = GetSubsystem<FileSystem>();
+
+    if (!fs->DirExists(GetCachePath()))
+        fs->CreateDir(GetCachePath());
+
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    cache->AddResourceDir(GetCachePath());
+
+    Scan();
+
+    return true;
+}
+
+bool AssetDatabase::CleanCache()
+{
+    FileSystem* fileSystem = GetSubsystem<FileSystem>();
+
+    String cachePath = GetCachePath();
+
+    if (fileSystem->DirExists(cachePath))
+    {
+        ATOMIC_LOGINFOF("Cleaning cache directory %s", cachePath.CString());
+
+        fileSystem->RemoveDir(cachePath, true);
+
+        if (fileSystem->DirExists(cachePath))
+        {
+            ATOMIC_LOGERRORF("Unable to remove cache directory %s", cachePath.CString());
+            return false;
+        }
+    }
+
+    fileSystem->CreateDir(cachePath);
+
+    if (!fileSystem->DirExists(cachePath))
+    {
+        ATOMIC_LOGERRORF("Unable to create cache directory %s", cachePath.CString());
+        return false;
+    }
+
+    return true;
+
+}
+
+bool AssetDatabase::GenerateCache(bool clean)
+{
+    ATOMIC_LOGINFO("Generating cache... hold on");
+
+    if (clean)
+    {
+        if (!CleanCache())
+            return false;
+    }
+
+    ReimportAllAssets();
+
+    ATOMIC_LOGINFO("Cache generated");
+
+    return true;
+
+}
+
+void AssetDatabase::SetCacheEnabled(bool cacheEnabled)
+{
+    cacheEnabled_ = cacheEnabled;
+}
+
 }

+ 17 - 0
Source/ToolCore/Assets/AssetDatabase.h

@@ -51,6 +51,18 @@ public:
 
     String GetCachePath();
 
+    /// Get whether the asset cache is enabled
+    bool GetCacheEnabled() const { return cacheEnabled_; }
+
+    /// Set whether the asset cache is enabled 
+    void SetCacheEnabled(bool cacheEnabled);
+
+    /// Cleans the asset Cache folder by removing and recreating it
+    bool CleanCache();
+
+    /// Regenerates the asset cache, clean removes the Cache folder before generating
+    bool GenerateCache(bool clean = true);
+
     void DeleteAsset(Asset* asset);
 
     void Scan();
@@ -85,6 +97,9 @@ private:
     bool ImportDirtyAssets();
     void PreloadAssets();
 
+    // internal method that initializes project asset cache
+    bool InitCache();
+
     // Update mapping of asset paths to cache file representations, by type
     void UpdateAssetCacheMap();
 
@@ -102,6 +117,8 @@ private:
     // Whether any asset was imported during scan
     bool assetScanImport_;
 
+    bool cacheEnabled_;
+
 };
 
 }

+ 8 - 82
Source/ToolCore/Command/CacheCmd.cpp

@@ -40,7 +40,8 @@ CacheCmd::CacheCmd(Context* context) : Command(context),
     cleanCache_(false),
     generateCache_(false)
 {
-
+    // We disable the AssetDatabase cache, as will be cleaning, regenerating, etc
+    GetSubsystem<AssetDatabase>()->SetCacheEnabled(false);
 }
 
 CacheCmd::~CacheCmd()
@@ -83,92 +84,17 @@ bool CacheCmd::ParseInternal(const Vector<String>& arguments, unsigned startInde
     return true;
 }
 
-
-bool CacheCmd::CreateCacheDirectory() const
-{
-    FileSystem* fileSystem = GetSubsystem<FileSystem>();
-
-    if (fileSystem->DirExists(cachePath_))
-        return true;
-
-    fileSystem->CreateDir(cachePath_);
-
-    if (!fileSystem->DirExists(cachePath_))
-    {
-        ATOMIC_LOGERRORF("Unable to create cache directory %s", cachePath_.CString());
-        return false;
-    }
-
-    return true;
-}
-
-bool CacheCmd::RemoveCacheDirectory() const
-{
-    FileSystem* fileSystem = GetSubsystem<FileSystem>();
-
-    if (!fileSystem->DirExists(cachePath_))
-        return true;
-
-    fileSystem->RemoveDir(cachePath_, true);
-
-    if (fileSystem->DirExists(cachePath_))
-    {
-        ATOMIC_LOGERRORF("Unable to remove cache directory %s", cachePath_.CString());
-        return false;
-    }
-
-    return true;
-}
-
-bool CacheCmd::CleanCache() const
-{
-    ATOMIC_LOGINFO("Cleaning Cache...");
-
-    if (!RemoveCacheDirectory())
-        return false;
-
-    if (!CreateCacheDirectory())
-        return false;
-
-   return true;
-}
-
-bool CacheCmd::GenerateCache() const
-{
-    ATOMIC_LOGINFO("Generating Cache... hold on");
-
-    AssetDatabase* database = GetSubsystem<AssetDatabase>();
-    database->ReimportAllAssets();
-
-    ATOMIC_LOGINFO("Cache Generated");
-
-    return true;
-}
-
 void CacheCmd::Run()
 {
-    ToolSystem* tsystem = GetSubsystem<ToolSystem>();
-    ToolEnvironment* env = GetSubsystem<ToolCore::ToolEnvironment>();
-    Project* project = tsystem->GetProject();
-
-    cachePath_ = AddTrailingSlash(project->GetProjectPath()) + "Cache";
-
-    if (cleanCache_)
+    AssetDatabase* database = GetSubsystem<AssetDatabase>();
+    
+    if (generateCache_)
     {
-        if (!CleanCache())
-        {
-            Error("Unable to clean cache");
-            return;
-        }
+        database->GenerateCache(cleanCache_);
     }
-
-    if (generateCache_)
+    else if (cleanCache_)
     {
-        if (!GenerateCache())
-        {
-            Error("Unable to generate cache");
-            return;
-        }
+        database->CleanCache();
     }
 
     Finished();