From 59f2ae13f873ef4f7c7d86a1d8afca5d4465323c Mon Sep 17 00:00:00 2001
From: James Chen <jianhua.chen@cocos2d-x.org>
Date: Mon, 28 Jan 2013 20:12:48 +0800
Subject: [PATCH] issue #1683: Updating CCFileUtils for Blackberry and
 Marmalade.

---
 cocos2dx/platform/blackberry/CCFileUtils.cpp  | 54 ++++++++++++++-----
 cocos2dx/platform/marmalade/CCFileUtils.cpp   | 54 ++++++++++++++-----
 .../proj.blackberry/proj.blackberry/main.cpp  |  2 -
 samples/Cpp/HelloCpp/proj.blackberry/main.cpp |  1 -
 .../Cpp/SimpleGame/proj.blackberry/main.cpp   |  1 -
 samples/Cpp/TestCpp/proj.blackberry/main.cpp  |  2 -
 .../Cpp/TestCpp/proj.marmalade/TestCPP.mkb    |  4 ++
 samples/Lua/HelloLua/proj.blackberry/main.cpp |  2 -
 samples/Lua/TestLua/proj.blackberry/main.cpp  |  2 -
 template/blackberry/proj.blackberry/main.cpp  |  2 -
 10 files changed, 86 insertions(+), 38 deletions(-)

diff --git a/cocos2dx/platform/blackberry/CCFileUtils.cpp b/cocos2dx/platform/blackberry/CCFileUtils.cpp
index 5017898f6b..ab60062157 100644
--- a/cocos2dx/platform/blackberry/CCFileUtils.cpp
+++ b/cocos2dx/platform/blackberry/CCFileUtils.cpp
@@ -57,7 +57,8 @@ void CCFileUtils::purgeFileUtils()
 
 bool CCFileUtils::init()
 {
-    m_searchPathArray.push_back("");
+    m_strDefaultResRootPath = "app/native/Resources/";
+    m_searchPathArray.push_back(m_strDefaultResRootPath);
     m_searchResolutionsOrderArray.push_back("");
 
     return true;
@@ -71,13 +72,6 @@ void CCFileUtils::purgeCachedEntries()
 
 std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
 {
-    std::string ret = CCApplication::sharedApplication()->getResourceRootPath();;
-
-    if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
-    {
-        ret += "/";
-    }
-
     std::string file = filename;
     std::string file_path = "";
     size_t pos = filename.find_last_of("/");
@@ -101,9 +95,8 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
         path += "/";
     }
     path += file;
-    ret += path;
 
-    return ret;
+    return path;
 }
 
 std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
@@ -140,6 +133,7 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
             {
                 // Adding the full path to cache if the file was found.
                 s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
+                CCLOG("Returning path: %s", fullpath.c_str());
                 return fullpath;
             }
         }
@@ -207,12 +201,46 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz
 
 void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory)
 {
+    if (pszResourceDirectory == NULL) return;
     m_obDirectory = pszResourceDirectory;
-    if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/')
+    std::vector<std::string> searchPaths = this->getSearchPath();;
+    searchPaths.insert(searchPaths.begin(), pszResourceDirectory);
+    this->setSearchPath(searchPaths);
+}
+
+void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
+{
+    bool bExistDefaultRootPath = false;
+
+    m_searchPathArray.clear();
+    for (std::vector<std::string>::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter)
     {
-        m_obDirectory.append("/");
+        std::string strPrefix;
+        std::string path;
+        if ((*iter)[0] != '/')
+        { // Not an absolute path
+            if (iter->find(m_strDefaultResRootPath) != 0)
+            { // The path contains no default resource root path, insert the root path.
+                strPrefix = m_strDefaultResRootPath;
+            }
+        }
+        path = strPrefix+(*iter);
+        if (path.length() > 0 && path[path.length()-1] != '/')
+        {
+            path += "/";
+        }
+        if (!bExistDefaultRootPath && path == m_strDefaultResRootPath)
+        {
+            bExistDefaultRootPath = true;
+        }
+        m_searchPathArray.push_back(path);
+    }
+
+    if (!bExistDefaultRootPath)
+    {
+        CCLOG("Default root path doesn't exist, adding it.");
+        m_searchPathArray.push_back(m_strDefaultResRootPath);
     }
-    m_searchPathArray.insert(m_searchPathArray.begin(), m_obDirectory);
 }
 
 std::string CCFileUtils::getWriteablePath()
diff --git a/cocos2dx/platform/marmalade/CCFileUtils.cpp b/cocos2dx/platform/marmalade/CCFileUtils.cpp
index 738f5fa2ca..b38e64e452 100644
--- a/cocos2dx/platform/marmalade/CCFileUtils.cpp
+++ b/cocos2dx/platform/marmalade/CCFileUtils.cpp
@@ -52,7 +52,8 @@ CCFileUtils* CCFileUtils::sharedFileUtils()
 
 bool CCFileUtils::init()
 {
-    m_searchPathArray.push_back("");
+    m_strDefaultResRootPath = "";
+    m_searchPathArray.push_back(m_strDefaultResRootPath);
     m_searchResolutionsOrderArray.push_back("");
     
     return true;
@@ -76,13 +77,6 @@ void CCFileUtils::purgeCachedEntries()
 
 std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
 {
-    std::string ret = "";
-
-    if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
-    {
-        ret += "/";
-    }
-    
     std::string file = filename;
     std::string file_path = "";
     size_t pos = filename.find_last_of("/");
@@ -106,9 +100,8 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
         path += "/";
     }
     path += file;
-    ret += path;
     
-    return ret;
+    return path;
 }
 
 std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
@@ -119,6 +112,7 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
     // Return directly if it's an absolute path.
     if (pszFileName[0] == '/')
     {
+        CCLOG("Return absolute path( %s ) directly.", pszFileName);
         return pszFileName;
     }
     
@@ -198,12 +192,46 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz
 
 void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory)
 {
+    if (pszResourceDirectory == NULL) return;
     m_obDirectory = pszResourceDirectory;
-    if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/')
+    std::vector<std::string> searchPaths = this->getSearchPath();;
+    searchPaths.insert(searchPaths.begin(), pszResourceDirectory);
+    this->setSearchPath(searchPaths);
+}
+
+void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
+{
+    bool bExistDefaultRootPath = false;
+    
+    m_searchPathArray.clear();
+    for (std::vector<std::string>::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter)
     {
-        m_obDirectory.append("/");
+        std::string strPrefix;
+        std::string path;
+        if ((*iter)[0] != '/')
+        { // Not an absolute path
+            if (iter->find(m_strDefaultResRootPath) != 0)
+            { // The path contains no default resource root path, insert the root path.
+                strPrefix = m_strDefaultResRootPath;
+            }
+        }
+        path = strPrefix+(*iter);
+        if (path.length() > 0 && path[path.length()-1] != '/')
+        {
+            path += "/";
+        }
+        if (!bExistDefaultRootPath && path == m_strDefaultResRootPath)
+        {
+            bExistDefaultRootPath = true;
+        }
+        m_searchPathArray.push_back(path);
+    }
+    
+    if (!bExistDefaultRootPath)
+    {
+        CCLOG("Default root path doesn't exist, adding it.");
+        m_searchPathArray.push_back(m_strDefaultResRootPath);
     }
-    m_searchPathArray.insert(m_searchPathArray.begin(), m_obDirectory);
 }
 
 std::string CCFileUtils::getWriteablePath()
diff --git a/external/Box2D/proj.blackberry/proj.blackberry/main.cpp b/external/Box2D/proj.blackberry/proj.blackberry/main.cpp
index ac090debb8..ec42c08823 100644
--- a/external/Box2D/proj.blackberry/proj.blackberry/main.cpp
+++ b/external/Box2D/proj.blackberry/proj.blackberry/main.cpp
@@ -23,8 +23,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication().setResourceRootPath("app/native/Resources/");
-
     CCEGLView& eglView = CCEGLView::sharedOpenGLView();
     eglView.setSize(width, height);
 
diff --git a/samples/Cpp/HelloCpp/proj.blackberry/main.cpp b/samples/Cpp/HelloCpp/proj.blackberry/main.cpp
index af00341f86..fbf37685df 100644
--- a/samples/Cpp/HelloCpp/proj.blackberry/main.cpp
+++ b/samples/Cpp/HelloCpp/proj.blackberry/main.cpp
@@ -25,7 +25,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication()->setResourceRootPath("app/native/Resources/");
 
     CCEGLView* eglView = CCEGLView::sharedOpenGLView();
     eglView->setFrameSize(width, height);
diff --git a/samples/Cpp/SimpleGame/proj.blackberry/main.cpp b/samples/Cpp/SimpleGame/proj.blackberry/main.cpp
index af00341f86..fbf37685df 100644
--- a/samples/Cpp/SimpleGame/proj.blackberry/main.cpp
+++ b/samples/Cpp/SimpleGame/proj.blackberry/main.cpp
@@ -25,7 +25,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication()->setResourceRootPath("app/native/Resources/");
 
     CCEGLView* eglView = CCEGLView::sharedOpenGLView();
     eglView->setFrameSize(width, height);
diff --git a/samples/Cpp/TestCpp/proj.blackberry/main.cpp b/samples/Cpp/TestCpp/proj.blackberry/main.cpp
index 4791cea650..589b9bfdd8 100644
--- a/samples/Cpp/TestCpp/proj.blackberry/main.cpp
+++ b/samples/Cpp/TestCpp/proj.blackberry/main.cpp
@@ -23,8 +23,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication()->setResourceRootPath("app/native/Resources/");
-
     CCEGLView* eglView = CCEGLView::sharedOpenGLView();
     eglView->setFrameSize(width, height);
 
diff --git a/samples/Cpp/TestCpp/proj.marmalade/TestCPP.mkb b/samples/Cpp/TestCpp/proj.marmalade/TestCPP.mkb
index 68011a34d4..02576e9663 100644
--- a/samples/Cpp/TestCpp/proj.marmalade/TestCPP.mkb
+++ b/samples/Cpp/TestCpp/proj.marmalade/TestCPP.mkb
@@ -465,6 +465,10 @@ files
 	ZwoptexTest.cpp
 	ZwoptexTest.h
 
+	[FileUtilsTest]
+	(../Classes/FileUtilsTest)
+	FileUtilsTest.cpp
+	FileUtilsTest.h
 }
 
 
diff --git a/samples/Lua/HelloLua/proj.blackberry/main.cpp b/samples/Lua/HelloLua/proj.blackberry/main.cpp
index af00341f86..1c2b01d787 100644
--- a/samples/Lua/HelloLua/proj.blackberry/main.cpp
+++ b/samples/Lua/HelloLua/proj.blackberry/main.cpp
@@ -25,8 +25,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication()->setResourceRootPath("app/native/Resources/");
-
     CCEGLView* eglView = CCEGLView::sharedOpenGLView();
     eglView->setFrameSize(width, height);
 
diff --git a/samples/Lua/TestLua/proj.blackberry/main.cpp b/samples/Lua/TestLua/proj.blackberry/main.cpp
index af00341f86..1c2b01d787 100644
--- a/samples/Lua/TestLua/proj.blackberry/main.cpp
+++ b/samples/Lua/TestLua/proj.blackberry/main.cpp
@@ -25,8 +25,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication()->setResourceRootPath("app/native/Resources/");
-
     CCEGLView* eglView = CCEGLView::sharedOpenGLView();
     eglView->setFrameSize(width, height);
 
diff --git a/template/blackberry/proj.blackberry/main.cpp b/template/blackberry/proj.blackberry/main.cpp
index af00341f86..1c2b01d787 100644
--- a/template/blackberry/proj.blackberry/main.cpp
+++ b/template/blackberry/proj.blackberry/main.cpp
@@ -25,8 +25,6 @@ int main(int argc, char **argv)
 		height = 600;
 	}
 
-	CCApplication::sharedApplication()->setResourceRootPath("app/native/Resources/");
-
     CCEGLView* eglView = CCEGLView::sharedOpenGLView();
     eglView->setFrameSize(width, height);