Browse Source

Sanitate resource dir similarly when adding and removing it to ensure removing works properly.

Lasse Öörni 12 years ago
parent
commit
2e01ca5e67

+ 5 - 1
Source/Engine/LuaScript/pkgs/Resource/ResourceCache.pkg

@@ -4,7 +4,7 @@ class ResourceCache
 {    
     void ReleaseAllResources(bool force = false);
     bool ReloadResource(Resource* resource);
-    
+
     void SetMemoryBudget(ShortStringHash type, unsigned budget);
     void SetMemoryBudget(const String type, unsigned budget);
     
@@ -23,6 +23,10 @@ class ResourceCache
     bool GetAutoReloadResources() const;
     bool GetSearchPackagesFirst() const;
     
+    String GetPreferredResourceDir(const String path) const;
+    String SanitateResourceName(const String name) const;
+    String SanitateResourceDirName(const String name) const;
+
     tolua_readonly tolua_property__get_set unsigned totalMemoryUse;
     tolua_readonly tolua_property__get_set bool autoReloadResources;
     tolua_readonly tolua_property__get_set bool searchPackagesFirst;

+ 17 - 9
Source/Engine/Resource/ResourceCache.cpp

@@ -81,12 +81,7 @@ bool ResourceCache::AddResourceDir(const String& pathName, unsigned int priority
     }
     
     // Convert path to absolute
-    String fixedPath = AddTrailingSlash(pathName);
-    if (!IsAbsolutePath(fixedPath))
-        fixedPath = fileSystem->GetCurrentDir() + fixedPath;
-    
-    // Sanitate away /./ construct
-    fixedPath.Replace("/./", "/");
+    String fixedPath = SanitateResourceDirName(pathName);
     
     // Check that the same path does not already exist
     for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
@@ -149,12 +144,13 @@ bool ResourceCache::AddManualResource(Resource* resource)
     return true;
 }
 
-void ResourceCache::RemoveResourceDir(const String& path)
+void ResourceCache::RemoveResourceDir(const String& pathName)
 {
-    String fixedPath = AddTrailingSlash(path);
+    String fixedPath = SanitateResourceDirName(pathName);
+    
     for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
     {
-        if (!resourceDirs_[i].Compare(path, false))
+        if (!resourceDirs_[i].Compare(fixedPath, false))
         {
             resourceDirs_.Erase(i);
             if (fileWatchers_.Size() > i)
@@ -600,6 +596,18 @@ String ResourceCache::SanitateResourceName(const String& nameIn) const
     return name.Trimmed();
 }
 
+String ResourceCache::SanitateResourceDirName(const String& nameIn) const
+{
+    String fixedPath = AddTrailingSlash(nameIn);
+    if (!IsAbsolutePath(fixedPath))
+        fixedPath = GetSubsystem<FileSystem>()->GetCurrentDir() + fixedPath;
+    
+    // Sanitate away /./ construct
+    fixedPath.Replace("/./", "/");
+    
+    return fixedPath.Trimmed();
+}
+
 void ResourceCache::StoreResourceDependency(Resource* resource, const String& dependency)
 {
     // If resource reloading is not on, do not create the dependency data structure (saves memory)

+ 2 - 0
Source/Engine/Resource/ResourceCache.h

@@ -134,6 +134,8 @@ public:
     String GetPreferredResourceDir(const String& path) const;
     /// Remove unsupported constructs from the resource name to prevent ambiguity, and normalize absolute filename to resource path relative if possible.
     String SanitateResourceName(const String& name) const;
+    /// Remove unnecessary constructs from a resource directory name and ensure it to be an absolute path.
+    String SanitateResourceDirName(const String& name) const;
     /// Store a dependency for a resource. If a dependency file changes, the resource will be reloaded.
     void StoreResourceDependency(Resource* resource, const String& dependency);
     /// Reset dependencies for a resource.

+ 1 - 0
Source/Engine/Script/ResourceAPI.cpp

@@ -110,6 +110,7 @@ static void RegisterResourceCache(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ResourceCache", "File@ GetFile(const String&in)", asFUNCTION(ResourceCacheGetFile), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "String GetPreferredResourceDir(const String&in) const", asMETHOD(ResourceCache, GetPreferredResourceDir), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "String SanitateResourceName(const String&in) const", asMETHOD(ResourceCache, SanitateResourceName), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ResourceCache", "String SanitateResourceDirName(const String&in) const", asMETHOD(ResourceCache, SanitateResourceDirName), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "String GetResourceFileName(const String&in) const", asMETHOD(ResourceCache, GetResourceFileName), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetResource(const String&in, const String&in)", asFUNCTION(ResourceCacheGetResource), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetResource(ShortStringHash, const String&in)", asMETHODPR(ResourceCache, GetResource, (ShortStringHash, const String&), Resource*), asCALL_THISCALL);