From 240b922271e4b52cb394e85d2cc47f6a589edd23 Mon Sep 17 00:00:00 2001 From: lite3 Date: Mon, 29 Sep 2014 19:25:00 +0800 Subject: [PATCH 1/3] remove '../' at path on android. because AAssetManager_open do not support '../' in path. --- .../platform/android/CCFileUtils-android.cpp | 52 +++++++++++++++++++ cocos/platform/android/CCFileUtils-android.h | 2 + 2 files changed, 54 insertions(+) diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index 6045c1f371..2e3fc7abfe 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -81,6 +81,58 @@ bool FileUtilsAndroid::init() return FileUtils::init(); } +std::string FileUtilsAndroid::getNewFilename(const std::string &filename) const +{ + std::string newFileName = FileUtils::getNewFilename(filename); + // ../xxx do not fix this path + auto pos = newFileName.find("../"); + if (pos == std::string::npos || pos == 0) + { + return newFileName; + } + + std::vector v(3); + v.resize(0); + auto change = false; + size_t size = newFileName.size(); + size_t idx = 0; + bool noexit = true; + while (noexit) + { + pos = newFileName.find('/', idx); + std::string tmp; + if (pos == std::string::npos) + { + tmp = newFileName.substr(idx, size - idx); + noexit = false; + }else + { + tmp = newFileName.substr(idx, pos - idx + 1); + } + auto t = v.size(); + if (t > 0 && v[t-1].compare("../") != 0 && + (tmp.compare("../") == 0 || tmp.compare("..") == 0)) + { + v.pop_back(); + change = true; + }else + { + v.push_back(tmp); + } + idx = pos + 1; + } + + if (change) + { + newFileName.clear(); + for (auto s : v) + { + newFileName.append(s); + } + } + return newFileName; +} + bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const { if (strFilePath.empty()) diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h index d2415da2f7..cf590ffebe 100644 --- a/cocos/platform/android/CCFileUtils-android.h +++ b/cocos/platform/android/CCFileUtils-android.h @@ -61,6 +61,8 @@ public: /* override funtions */ bool init(); + std::string getNewFilename(const std::string &filename) const override; + /** @deprecated Please use FileUtils::getDataFromFile or FileUtils::getStringFromFile instead. */ CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t * size) override; From 21081b9e1adbf22ee54e95ada2fd099017ab6605 Mon Sep 17 00:00:00 2001 From: lite3 Date: Tue, 30 Sep 2014 01:07:19 +0800 Subject: [PATCH 2/3] use reference --- cocos/platform/android/CCFileUtils-android.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index 2e3fc7abfe..aec74df4c4 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -125,7 +125,7 @@ std::string FileUtilsAndroid::getNewFilename(const std::string &filename) const if (change) { newFileName.clear(); - for (auto s : v) + for (auto &s : v) { newFileName.append(s); } From e1814d04808d88ec6d29dd2dc9704da12497a0b7 Mon Sep 17 00:00:00 2001 From: lite3 Date: Sat, 11 Oct 2014 11:20:14 +0800 Subject: [PATCH 3/3] add virtual in FileUtilsAndroid::getNewFilename --- cocos/platform/android/CCFileUtils-android.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h index cf590ffebe..7f65d4aa8e 100644 --- a/cocos/platform/android/CCFileUtils-android.h +++ b/cocos/platform/android/CCFileUtils-android.h @@ -61,7 +61,7 @@ public: /* override funtions */ bool init(); - std::string getNewFilename(const std::string &filename) const override; + virtual std::string getNewFilename(const std::string &filename) const override; /** @deprecated Please use FileUtils::getDataFromFile or FileUtils::getStringFromFile instead. */ CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t * size) override;