2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#include "platform/apple/CCFileUtils-apple.h"
|
|
|
|
|
|
|
|
#include <ftw.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
#define DECLARE_GUARD (void)0 // std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
struct FileUtilsApple::IMPL
|
|
|
|
{
|
|
|
|
IMPL(NSBundle* bundle) : bundle_([NSBundle mainBundle]) {}
|
|
|
|
void setBundle(NSBundle* bundle) { bundle_ = bundle; }
|
|
|
|
NSBundle* getBundle() const { return bundle_; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
NSBundle* bundle_;
|
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtilsApple::FileUtilsApple() : pimpl_(new IMPL([NSBundle mainBundle])) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FileUtilsApple::~FileUtilsApple() = default;
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_FILEUTILS_APPLE_ENABLE_OBJC
|
2021-12-31 12:12:40 +08:00
|
|
|
void FileUtilsApple::setBundle(NSBundle* bundle)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
pimpl_->setBundle(bundle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma mark - FileUtils
|
|
|
|
|
|
|
|
static NSFileManager* s_fileManager = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
FileUtils* FileUtils::getInstance()
|
|
|
|
{
|
|
|
|
if (s_sharedFileUtils == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
s_sharedFileUtils = new FileUtilsApple();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!s_sharedFileUtils->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
delete s_sharedFileUtils;
|
|
|
|
s_sharedFileUtils = nullptr;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("ERROR: Could not init CCFileUtilsApple");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtilsApple::getWritablePath() const
|
2021-04-22 23:00:00 +08:00
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
return getNativeWritableAbsolutePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtilsApple::getNativeWritableAbsolutePath() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
DECLARE_GUARD;
|
|
|
|
if (_writablePath.length())
|
|
|
|
{
|
|
|
|
return _writablePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// save to document folder
|
2021-12-31 12:12:40 +08:00
|
|
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
|
|
NSString* documentsDirectory = [paths objectAtIndex:0];
|
|
|
|
std::string strRet = [documentsDirectory UTF8String];
|
2019-11-23 20:27:39 +08:00
|
|
|
strRet.append("/");
|
|
|
|
return strRet;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtilsApple::isFileExistInternal(std::string_view filePath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (filePath.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (filePath[0] != '/')
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
std::string file;
|
|
|
|
size_t pos = filePath.find_last_of("/");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
file = filePath.substr(pos + 1);
|
|
|
|
path = filePath.substr(0, pos + 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file = filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:file.c_str()]
|
2021-12-31 12:12:40 +08:00
|
|
|
ofType:nil
|
|
|
|
inDirectory:[NSString stringWithUTF8String:path.c_str()]];
|
|
|
|
if (fullpath != nil)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Search path is an absolute path.
|
2021-12-31 12:12:40 +08:00
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:filePath.data()]])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static int unlink_cb(const char* fpath, const struct stat* sb, int typeflag, struct FTW* ftwbuf)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto ret = remove(fpath);
|
|
|
|
if (ret)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
log("Fail to remove: %s ", fpath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtilsApple::removeDirectory(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (path.empty())
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("Fail to remove directory, path is empty!");
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (nftw(path.data(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS))
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtilsApple::getPathForDirectory(std::string_view dir,
|
|
|
|
std::string_view resolutionDiretory,
|
|
|
|
std::string_view searchPath) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string path{searchPath};
|
|
|
|
path.append(resolutionDiretory).append(dir);
|
|
|
|
|
|
|
|
if (!path.empty() && path[path.length() - 1] == '/')
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
path.erase(path.end() - 1);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
if (path[0] == '/')
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
BOOL isDir = false;
|
2021-12-31 12:12:40 +08:00
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:dir.data()] isDirectory:&isDir])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return isDir ? path : "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
NSString* fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:path.c_str()]
|
2019-11-23 20:27:39 +08:00
|
|
|
ofType:nil];
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fullpath != nil)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return [fullpath UTF8String];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string FileUtilsApple::getFullPathForFilenameWithinDirectory(std::string_view directory,
|
|
|
|
std::string_view filename) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (directory[0] != '/')
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
NSString* fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:filename.data()]
|
|
|
|
ofType:nil
|
|
|
|
inDirectory:[NSString stringWithUTF8String:directory.data()]];
|
|
|
|
if (fullpath != nil)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return [fullpath UTF8String];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fullPath{directory};
|
|
|
|
fullPath += filename;
|
2019-11-23 20:27:39 +08:00
|
|
|
// Search path is an absolute path.
|
2021-12-31 12:12:40 +08:00
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()]])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool FileUtilsApple::createDirectory(std::string_view path) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!path.empty(), "Invalid path");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isDirectoryExist(path))
|
|
|
|
return true;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NSError* error;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
bool result = [s_fileManager createDirectoryAtPath:[NSString stringWithUTF8String:path.data()]
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
attributes:nil
|
|
|
|
error:&error];
|
|
|
|
|
|
|
|
if (!result && error != nil)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("Fail to create directory \"%s\": %s", path.data(), [error.localizedDescription UTF8String]);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|