Browse Source

Moved the logic for determining whether to use executable directory or the current working directory into FileSystem::GetProgramDir().

Lasse Öörni 12 years ago
parent
commit
f74f4dd6f8

+ 0 - 13
Source/Engine/Engine/Engine.cpp

@@ -181,7 +181,6 @@ bool Engine::Initialize(const VariantMap& parameters)
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     FileSystem* fileSystem = GetSubsystem<FileSystem>();
     String exePath = fileSystem->GetProgramDir();
-    String cwdPath = fileSystem->GetCurrentDir();
     
     Vector<String> resourcePaths = GetParameter(parameters, "ResourcePaths", "CoreData;Data").GetString().Split(';');
     Vector<String> resourcePackages = GetParameter(parameters, "ResourcePackages").GetString().Split(';');
@@ -192,10 +191,6 @@ bool Engine::Initialize(const VariantMap& parameters)
         bool success = false;
         
         String packageName = exePath + resourcePaths[i] + ".pak";
-        // Try cwd-relative path if not found next to executable
-        if (!fileSystem->FileExists(packageName))
-            packageName = cwdPath + resourcePaths[i] + ".pak";
-        
         if (fileSystem->FileExists(packageName))
         {
             SharedPtr<PackageFile> package(new PackageFile(context_));
@@ -209,10 +204,6 @@ bool Engine::Initialize(const VariantMap& parameters)
         if (!success)
         {
             String pathName = exePath + resourcePaths[i];
-            // Try cwd-relative path if not found next to executable
-            if (!fileSystem->DirExists(pathName))
-                pathName = cwdPath + resourcePaths[i];
-            
             if (fileSystem->DirExists(pathName))
                 success = cache->AddResourceDir(pathName);
         }
@@ -230,10 +221,6 @@ bool Engine::Initialize(const VariantMap& parameters)
         bool success = false;
         
         String packageName = exePath + resourcePackages[i];
-        // Try cwd-relative path if not found next to executable
-        if (!fileSystem->FileExists(packageName))
-            packageName = cwdPath + resourcePackages[i];
-        
         if (fileSystem->FileExists(packageName))
         {
             SharedPtr<PackageFile> package(new PackageFile(context_));

+ 22 - 16
Source/Engine/IO/FileSystem.cpp

@@ -140,11 +140,7 @@ int FileSystem::SystemRun(const String& fileName, const Vector<String>& argument
         // Add .exe extension if no extension defined
         if (GetExtension(fixedFileName).Empty())
             fixedFileName += ".exe";
-
-        // If executable is not found, try cwd-relative path as a fallback
-        if (!FileExists(fixedFileName))
-            fixedFileName = GetNativePath(GetCurrentDir() + GetFileNameAndExtension(fixedFileName));
-
+        
         String commandLine = "\"" + fixedFileName + "\"";
         for (unsigned i = 0; i < arguments.Size(); ++i)
             commandLine += " " + arguments[i];
@@ -170,10 +166,6 @@ int FileSystem::SystemRun(const String& fileName, const Vector<String>& argument
 
         return exitCode;
         #else
-        // If executable is not found, try cwd-relative path as a fallback
-        if (!FileExists(fixedFileName))
-            fixedFileName = GetNativePath(GetCurrentDir() + GetFileNameAndExtension(fixedFileName));
-        
         pid_t pid = fork();
         if (!pid)
         {
@@ -445,33 +437,47 @@ void FileSystem::ScanDir(Vector<String>& result, const String& pathName, const S
 
 String FileSystem::GetProgramDir() const
 {
+    // Return cached value if possible
+    if (!programDir_.Empty())
+        return programDir_;
+    
     #if defined(ANDROID)
     // This is an internal directory specifier pointing to the assets in the .apk
     // Files from this directory will be opened using special handling
-    return "/apk/";
+    programDir_ = "/apk/";
+    return programDir_;
     #elif defined(IOS)
-    return AddTrailingSlash(SDL_IOS_GetResourceDir());
+    programDir_ = AddTrailingSlash(SDL_IOS_GetResourceDir());
+    return programDir_;
     #elif defined(WIN32)
     wchar_t exeName[MAX_PATH];
     exeName[0] = 0;
     GetModuleFileNameW(0, exeName, MAX_PATH);
-    return GetPath(String(exeName));
+    programDir_ = GetPath(String(exeName));
     #elif defined(__APPLE__)
     char exeName[MAX_PATH];
     memset(exeName, 0, MAX_PATH);
     unsigned size = MAX_PATH;
     _NSGetExecutablePath(exeName, &size);
-    return GetPath(String(exeName));
+    programDir_ = GetPath(String(exeName));
     #elif defined(__linux__)
     char exeName[MAX_PATH];
     memset(exeName, 0, MAX_PATH);
     pid_t pid = getpid();
     String link = "/proc/" + String(pid) + "/exe";
     readlink(link.CString(), exeName, MAX_PATH);
-    return GetPath(String(exeName));
-    #else
-    return String();
+    programDir_ = GetPath(String(exeName));
     #endif
+    
+    // If the executable directory does not contain CoreData & Data directories, but the current working directory does, use the
+    // current working directory instead
+    /// \todo Should not rely on such fixed convention
+    String currentDir = GetCurrentDir();
+    if (!DirExists(programDir_ + "CoreData") && !DirExists(programDir_ + "Data") && (DirExists(currentDir + "CoreData") ||
+        DirExists(currentDir + "Data")))
+        programDir_ = currentDir;
+    
+    return programDir_;
 }
 
 String FileSystem::GetUserDocumentsDir() const

+ 3 - 1
Source/Engine/IO/FileSystem.h

@@ -79,7 +79,7 @@ public:
     bool DirExists(const String& pathName) const;
     /// Scan a directory for specified files.
     void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const;
-    /// Return the program's directory.
+    /// Return the program's directory. If it does not contain the Urho3D default CoreData and Data directories, and the current working directory does, return the working directory instead.
     String GetProgramDir() const;
     /// Return the user documents directory.
     String GetUserDocumentsDir() const;
@@ -90,6 +90,8 @@ private:
     
     /// Allowed directories.
     HashSet<String> allowedPaths_;
+    /// Cached program directory.
+    mutable String programDir_;
 };
 
 /// Split a full path to path, filename and extension. The extension will be converted to lowercase.

+ 1 - 2
Source/Samples/06_SkeletalAnimation/SkeletalAnimation.cpp

@@ -95,7 +95,6 @@ void SkeletalAnimation::CreateScene()
     // Create a Zone component for ambient lighting & fog color control
     Node* zoneNode = scene_->CreateChild("Zone");
     Zone* zone = zoneNode->CreateComponent<Zone>();
-    // Set same volume as the Octree, set a close bluish fog and some ambient light
     zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
     zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
     zone->SetFogColor(Color(0.5f, 0.5f, 0.7f));
@@ -129,7 +128,7 @@ void SkeletalAnimation::CreateScene()
         // but we need to update the model's position manually in any case
         Animation* walkAnimation = cache->GetResource<Animation>("Models/Jack_Walk.ani");
         AnimationState* state = modelObject->AddAnimationState(walkAnimation);
-        // Enable looping and full blending weight
+        // Enable full blending weight and looping
         state->SetWeight(1.0f);
         state->SetLooped(true);
     }