From bcd691ad9f60bf4b53ab757a1d1cd7818dcf29eb Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 24 Aug 2012 17:55:29 +0800 Subject: [PATCH] use absolute path in CCFileUtils --- cocos2dx/platform/linux/CCFileUtils.cpp | 27 +++++++++++++++++--- samples/HelloCpp/proj.linux/main.cpp | 21 +++++++++++++++- samples/TestCpp/proj.linux/main.cpp | 29 +++++++++++++++++++--- samples/TestJavascript/proj.linux/main.cpp | 25 ++++++++++++++++--- 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/cocos2dx/platform/linux/CCFileUtils.cpp b/cocos2dx/platform/linux/CCFileUtils.cpp index 9a28715d2c..389b667bbc 100644 --- a/cocos2dx/platform/linux/CCFileUtils.cpp +++ b/cocos2dx/platform/linux/CCFileUtils.cpp @@ -12,6 +12,9 @@ #include "CCApplication.h" #include "cocoa/CCString.h" #include +#include +#include +#include using namespace std; @@ -45,13 +48,17 @@ void CCFileUtils::purgeCachedEntries() const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) { + if (pszRelativePath && pszRelativePath[0] == '/') + { + return pszRelativePath; + } + const char* pszRootPath = CCApplication::sharedApplication()->getResourceRootPath(); CCString* pRet = CCString::create(pszRootPath); - const char* resDir = CCFileUtils::sharedFileUtils()->getResourceDirectory(); - if (resDir != NULL) + if (m_obDirectory.size() > 0) { - pRet->m_sString += resDir; + pRet->m_sString += m_obDirectory.c_str(); } if (pszRelativePath != NULL) @@ -59,6 +66,20 @@ const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) pRet->m_sString += pszRelativePath; } + // check if file or path exist + struct stat sts; + if (stat(pRet->getCString(), &sts) == -1 && errno == ENOENT) + { + // find from "Resources/" + pRet->m_sString.clear(); + pRet->m_sString = CCApplication::sharedApplication()->getResourceRootPath(); + pRet->m_sString += pszRelativePath; + if (stat(pRet->getCString(), &sts) == -1 && errno == ENOENT) + { + return pszRelativePath; + } + } + return pRet->getCString(); } diff --git a/samples/HelloCpp/proj.linux/main.cpp b/samples/HelloCpp/proj.linux/main.cpp index af006d3292..4e1fbc8fb4 100644 --- a/samples/HelloCpp/proj.linux/main.cpp +++ b/samples/HelloCpp/proj.linux/main.cpp @@ -4,12 +4,31 @@ #include "cocos2d.h" #include "CCEGLView.h" +#include +#include +#include +#include + USING_NS_CC; + +// 500 is enough? +#define MAXPATHLEN 500 + int main(int argc, char **argv) { + // get application path + int length; + char fullpath[MAXPATHLEN]; + length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); + fullpath[length] = '\0'; + + std::string resourcePath = fullpath; + resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); + resourcePath += "/../Resources/"; + // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath("../Resources/"); + CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setFrameSize(960, 640); return CCApplication::sharedApplication()->run(); diff --git a/samples/TestCpp/proj.linux/main.cpp b/samples/TestCpp/proj.linux/main.cpp index a8dcf70b12..72efb90db0 100644 --- a/samples/TestCpp/proj.linux/main.cpp +++ b/samples/TestCpp/proj.linux/main.cpp @@ -1,14 +1,35 @@ #include "main.h" -#include "cocos2d.h" + #include "../Classes/AppDelegate.h" +#include "cocos2d.h" +#include "CCEGLView.h" + +#include +#include +#include +#include USING_NS_CC; -int main(int argc, char **argv) { + +// 500 is enough? +#define MAXPATHLEN 500 + +int main(int argc, char **argv) +{ + // get application path + int length; + char fullpath[MAXPATHLEN]; + length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); + fullpath[length] = '\0'; + + std::string resourcePath = fullpath; + resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); + resourcePath += "/../Resources/"; + // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath("../Resources/"); + CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setFrameSize(480, 320); - return CCApplication::sharedApplication()->run(); } diff --git a/samples/TestJavascript/proj.linux/main.cpp b/samples/TestJavascript/proj.linux/main.cpp index db9f8b5424..72efb90db0 100644 --- a/samples/TestJavascript/proj.linux/main.cpp +++ b/samples/TestJavascript/proj.linux/main.cpp @@ -4,12 +4,31 @@ #include "cocos2d.h" #include "CCEGLView.h" -USING_NS_CC; -int main(int argc, char **argv) { +#include +#include +#include +#include +USING_NS_CC; + +// 500 is enough? +#define MAXPATHLEN 500 + +int main(int argc, char **argv) +{ + // get application path + int length; + char fullpath[MAXPATHLEN]; + length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); + fullpath[length] = '\0'; + + std::string resourcePath = fullpath; + resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); + resourcePath += "/../Resources/"; + // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath("../Resources/"); + CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setFrameSize(480, 320); return CCApplication::sharedApplication()->run();