2013-02-01 11:20:46 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-01-31 08:51:43 +08:00
|
|
|
|
2014-09-10 08:41:49 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-01-31 08:51:43 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/android/CCFileUtils-android.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCCommon.h"
|
2015-11-27 17:00:33 +08:00
|
|
|
#include "platform/android/jni/JniHelper.h"
|
2016-05-04 09:21:35 +08:00
|
|
|
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
2017-08-21 23:28:59 +08:00
|
|
|
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxEngineDataManager.h"
|
2013-03-08 05:56:42 +08:00
|
|
|
#include "android/asset_manager.h"
|
|
|
|
#include "android/asset_manager_jni.h"
|
2016-05-04 09:21:35 +08:00
|
|
|
#include "base/ZipUtils.h"
|
2017-06-05 13:37:50 +08:00
|
|
|
|
2013-02-27 08:32:00 +08:00
|
|
|
#include <stdlib.h>
|
2015-07-10 14:37:42 +08:00
|
|
|
#include <sys/stat.h>
|
2013-02-27 08:32:00 +08:00
|
|
|
|
2014-09-10 07:50:02 +08:00
|
|
|
#define LOG_TAG "CCFileUtils-android.cpp"
|
2013-03-08 05:56:07 +08:00
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
|
|
|
|
2016-07-01 15:42:02 +08:00
|
|
|
#define ASSETS_FOLDER_NAME "assets/"
|
|
|
|
#define ASSETS_FOLDER_NAME_LENGTH 7
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2013-07-03 09:35:46 +08:00
|
|
|
NS_CC_BEGIN
|
2013-03-08 05:56:42 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
AAssetManager* FileUtilsAndroid::assetmanager = nullptr;
|
2016-05-04 09:21:35 +08:00
|
|
|
ZipFile* FileUtilsAndroid::obbfile = nullptr;
|
2013-12-18 14:58:17 +08:00
|
|
|
|
2013-07-03 09:35:46 +08:00
|
|
|
void FileUtilsAndroid::setassetmanager(AAssetManager* a) {
|
2013-12-18 14:58:17 +08:00
|
|
|
if (nullptr == a) {
|
|
|
|
LOGD("setassetmanager : received unexpected nullptr parameter");
|
2013-07-03 09:35:46 +08:00
|
|
|
return;
|
2013-03-08 05:56:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-03 09:35:46 +08:00
|
|
|
cocos2d::FileUtilsAndroid::assetmanager = a;
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-07-12 12:03:39 +08:00
|
|
|
FileUtils* FileUtils::getInstance()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 14:58:17 +08:00
|
|
|
if (s_sharedFileUtils == nullptr)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_sharedFileUtils = new FileUtilsAndroid();
|
2016-05-04 09:21:35 +08:00
|
|
|
if (!s_sharedFileUtils->init())
|
2013-07-23 20:44:08 +08:00
|
|
|
{
|
|
|
|
delete s_sharedFileUtils;
|
2013-12-18 14:58:17 +08:00
|
|
|
s_sharedFileUtils = nullptr;
|
2013-07-23 20:44:08 +08:00
|
|
|
CCLOG("ERROR: Could not init CCFileUtilsAndroid");
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtilsAndroid::FileUtilsAndroid()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtilsAndroid::~FileUtilsAndroid()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2016-05-04 09:21:35 +08:00
|
|
|
if (obbfile)
|
|
|
|
{
|
|
|
|
delete obbfile;
|
|
|
|
obbfile = nullptr;
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsAndroid::init()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2016-07-01 15:42:02 +08:00
|
|
|
_defaultResRootPath = ASSETS_FOLDER_NAME;
|
2016-05-04 09:21:35 +08:00
|
|
|
|
|
|
|
std::string assetsPath(getApkPath());
|
|
|
|
if (assetsPath.find("/obb/") != std::string::npos)
|
|
|
|
{
|
|
|
|
obbfile = new ZipFile(assetsPath);
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
return FileUtils::init();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2014-09-29 19:25:00 +08:00
|
|
|
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<std::string> 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;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-09-29 19:25:00 +08:00
|
|
|
if (change)
|
|
|
|
{
|
|
|
|
newFileName.clear();
|
2014-09-30 01:07:19 +08:00
|
|
|
for (auto &s : v)
|
2014-09-29 19:25:00 +08:00
|
|
|
{
|
|
|
|
newFileName.append(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newFileName;
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-04-02 15:35:09 +08:00
|
|
|
if (strFilePath.empty())
|
2013-04-25 21:51:13 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
bool bFound = false;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
// Check whether file exists in apk.
|
2013-02-01 22:19:58 +08:00
|
|
|
if (strFilePath[0] != '/')
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-02-27 06:45:14 +08:00
|
|
|
const char* s = strFilePath.c_str();
|
2013-02-01 22:19:58 +08:00
|
|
|
|
2013-06-26 16:42:11 +08:00
|
|
|
// Found "assets/" at the beginning of the path and we don't want it
|
2016-05-04 09:21:35 +08:00
|
|
|
if (strFilePath.find(_defaultResRootPath) == 0) s += _defaultResRootPath.length();
|
|
|
|
|
|
|
|
if (obbfile && obbfile->fileExists(s))
|
|
|
|
{
|
|
|
|
bFound = true;
|
|
|
|
}
|
|
|
|
else if (FileUtilsAndroid::assetmanager)
|
|
|
|
{
|
2013-07-03 09:35:46 +08:00
|
|
|
AAsset* aa = AAssetManager_open(FileUtilsAndroid::assetmanager, s, AASSET_MODE_UNKNOWN);
|
2013-02-27 06:45:14 +08:00
|
|
|
if (aa)
|
|
|
|
{
|
|
|
|
bFound = true;
|
|
|
|
AAsset_close(aa);
|
|
|
|
} else {
|
|
|
|
// CCLOG("[AssetManager] ... in APK %s, found = false!", strFilePath.c_str());
|
|
|
|
}
|
|
|
|
}
|
2013-02-01 22:19:58 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
else
|
|
|
|
{
|
2013-02-01 22:19:58 +08:00
|
|
|
FILE *fp = fopen(strFilePath.c_str(), "r");
|
2016-05-04 09:21:35 +08:00
|
|
|
if (fp)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
bFound = true;
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bFound;
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:37:42 +08:00
|
|
|
bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) const
|
|
|
|
{
|
|
|
|
if (dirPath.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* s = dirPath.c_str();
|
2016-07-01 15:42:02 +08:00
|
|
|
|
2015-07-10 14:37:42 +08:00
|
|
|
// find absolute path in flash memory
|
|
|
|
if (s[0] == '/')
|
|
|
|
{
|
|
|
|
CCLOG("find in flash memory dirPath(%s)", s);
|
|
|
|
struct stat st;
|
|
|
|
if (stat(s, &st) == 0)
|
|
|
|
{
|
|
|
|
return S_ISDIR(st.st_mode);
|
|
|
|
}
|
|
|
|
}
|
2016-07-01 15:42:02 +08:00
|
|
|
else
|
2015-07-10 14:37:42 +08:00
|
|
|
{
|
2016-07-01 15:42:02 +08:00
|
|
|
// find it in apk's assets dir
|
|
|
|
// Found "assets/" at the beginning of the path and we don't want it
|
|
|
|
CCLOG("find in apk dirPath(%s)", s);
|
|
|
|
if (dirPath.find(ASSETS_FOLDER_NAME) == 0)
|
2015-07-10 14:37:42 +08:00
|
|
|
{
|
2016-07-01 15:42:02 +08:00
|
|
|
s += ASSETS_FOLDER_NAME_LENGTH;
|
|
|
|
}
|
|
|
|
if (FileUtilsAndroid::assetmanager)
|
|
|
|
{
|
|
|
|
AAssetDir* aa = AAssetManager_openDir(FileUtilsAndroid::assetmanager, s);
|
|
|
|
if (aa && AAssetDir_getNextFileName(aa))
|
|
|
|
{
|
|
|
|
AAssetDir_close(aa);
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-10 14:37:42 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-01 15:42:02 +08:00
|
|
|
|
2015-07-10 14:37:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
bool FileUtilsAndroid::isAbsolutePath(const std::string& strPath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
// On Android, there are two situations for full path.
|
|
|
|
// 1) Files in APK, e.g. assets/path/path/file.png
|
|
|
|
// 2) Files not in APK, e.g. /data/data/org.cocos2dx.hellocpp/cache/path/path/file.png, or /sdcard/path/path/file.png.
|
|
|
|
// So these two situations need to be checked on Android.
|
2013-06-15 14:03:30 +08:00
|
|
|
if (strPath[0] == '/' || strPath.find(_defaultResRootPath) == 0)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:16:21 +08:00
|
|
|
long FileUtilsAndroid::getFileSize(const std::string& filepath)
|
|
|
|
{
|
|
|
|
long size = FileUtils::getFileSize(filepath);
|
|
|
|
if (size != -1) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileUtilsAndroid::assetmanager)
|
|
|
|
{
|
|
|
|
string relativePath = filepath;
|
|
|
|
if (filepath.find(_defaultResRootPath) == 0)
|
|
|
|
{
|
|
|
|
relativePath = filepath.substr(_defaultResRootPath.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
AAsset* asset = AAssetManager_open(FileUtilsAndroid::assetmanager, relativePath.data(), AASSET_MODE_UNKNOWN);
|
|
|
|
if (asset)
|
|
|
|
{
|
|
|
|
size = AAsset_getLength(asset);
|
|
|
|
AAsset_close(asset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
FileUtils::Status FileUtilsAndroid::getContents(const std::string& filename, ResizableBuffer* buffer)
|
2013-12-18 14:58:17 +08:00
|
|
|
{
|
2017-08-21 23:28:59 +08:00
|
|
|
EngineDataManager::onBeforeReadFile();
|
2017-06-05 13:37:50 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
static const std::string apkprefix("assets/");
|
2013-12-18 14:58:17 +08:00
|
|
|
if (filename.empty())
|
2016-04-26 13:37:22 +08:00
|
|
|
return FileUtils::Status::NotExists;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
string fullPath = fullPathForFilename(filename);
|
2015-01-13 17:30:07 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
if (fullPath[0] == '/')
|
|
|
|
return FileUtils::getContents(fullPath, buffer);
|
2013-12-18 14:58:17 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
string relativePath = string();
|
|
|
|
size_t position = fullPath.find(apkprefix);
|
|
|
|
if (0 == position) {
|
|
|
|
// "assets/" is at the beginning of the path and we don't want it
|
|
|
|
relativePath += fullPath.substr(apkprefix.size());
|
|
|
|
} else {
|
|
|
|
relativePath = fullPath;
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
2016-05-04 09:21:35 +08:00
|
|
|
|
|
|
|
if (obbfile)
|
|
|
|
{
|
|
|
|
if (obbfile->getFileData(relativePath, buffer))
|
|
|
|
return FileUtils::Status::OK;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
if (nullptr == assetmanager) {
|
|
|
|
LOGD("... FileUtilsAndroid::assetmanager is nullptr");
|
|
|
|
return FileUtils::Status::NotInitialized;
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
AAsset* asset = AAssetManager_open(assetmanager, relativePath.data(), AASSET_MODE_UNKNOWN);
|
|
|
|
if (nullptr == asset) {
|
|
|
|
LOGD("asset is nullptr");
|
|
|
|
return FileUtils::Status::OpenFailed;
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
auto size = AAsset_getLength(asset);
|
|
|
|
buffer->resize(size);
|
2014-04-10 11:07:00 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
int readsize = AAsset_read(asset, buffer->buffer(), size);
|
|
|
|
AAsset_close(asset);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
if (readsize < size) {
|
|
|
|
if (readsize >= 0)
|
|
|
|
buffer->resize(readsize);
|
2016-06-25 11:58:55 +08:00
|
|
|
return FileUtils::Status::ReadFailed;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2016-01-18 17:58:04 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
return FileUtils::Status::OK;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
string FileUtilsAndroid::getWritablePath() const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
// Fix for Nexus 10 (Android 4.2 multi-user environment)
|
|
|
|
// the path is retrieved through Java Context.getCacheDir() method
|
|
|
|
string dir("");
|
2018-09-10 15:06:19 +08:00
|
|
|
string tmp = JniHelper::callStaticStringMethod("org.cocos2dx.lib.Cocos2dxHelper", "getCocos2dxWritablePath");
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-03-08 16:00:14 +08:00
|
|
|
if (tmp.length() > 0)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
dir.append(tmp).append("/");
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2014-01-31 08:51:43 +08:00
|
|
|
|
|
|
|
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|