Skip to content

Commit

Permalink
Fix #2169 (#2170)
Browse files Browse the repository at this point in the history
* Fix #2169

* Cleanup code
  • Loading branch information
halx99 authored Sep 20, 2024
1 parent 805390e commit f98b776
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 63 deletions.
18 changes: 0 additions & 18 deletions core/platform/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ THE SOFTWARE.

#include "pugixml/pugixml.hpp"

#define DECLARE_GUARD (void)0

#include "base/filesystem.h"

#if defined(_WIN32)
Expand Down Expand Up @@ -535,14 +533,12 @@ bool FileUtils::writeBinaryToFile(const void* data, size_t dataSize, std::string

bool FileUtils::init()
{
DECLARE_GUARD;
_searchPathArray.emplace_back(_defaultResRootPath);
return true;
}

void FileUtils::purgeCachedEntries()
{
DECLARE_GUARD;
_fullPathCache.clear();
_fullPathCacheDir.clear();
}
Expand Down Expand Up @@ -663,9 +659,6 @@ std::string FileUtils::getPathForDirectory(std::string_view dir, std::string_vie

std::string FileUtils::fullPathForFilename(std::string_view filename) const
{

DECLARE_GUARD;

if (filename.empty())
{
return "";
Expand Down Expand Up @@ -714,8 +707,6 @@ std::string FileUtils::fullPathForFilename(std::string_view filename) const

std::string FileUtils::fullPathForDirectory(std::string_view dir) const
{
DECLARE_GUARD;

auto result = std::string();

if (dir.empty())
Expand Down Expand Up @@ -777,31 +768,26 @@ std::string FileUtils::fullPathFromRelativeFile(std::string_view filename, std::

const std::vector<std::string>& FileUtils::getSearchPaths() const
{
DECLARE_GUARD;
return _searchPathArray;
}

const std::vector<std::string>& FileUtils::getOriginalSearchPaths() const
{
DECLARE_GUARD;
return _originalSearchPaths;
}

void FileUtils::setWritablePath(std::string_view writablePath)
{
DECLARE_GUARD;
_writablePath = writablePath;
}

const std::string& FileUtils::getDefaultResourceRootPath() const
{
DECLARE_GUARD;
return _defaultResRootPath;
}

void FileUtils::setDefaultResourceRootPath(std::string_view path)
{
DECLARE_GUARD;
if (_defaultResRootPath != path)
{
_fullPathCache.clear();
Expand All @@ -819,7 +805,6 @@ void FileUtils::setDefaultResourceRootPath(std::string_view path)

void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
{
DECLARE_GUARD;
bool existDefaultRootPath = false;
_originalSearchPaths = searchPaths;

Expand Down Expand Up @@ -857,7 +842,6 @@ void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)

void FileUtils::addSearchPath(std::string_view searchpath, const bool front)
{
DECLARE_GUARD;
std::string path;
if (!isAbsolutePath(searchpath))
path = _defaultResRootPath;
Expand Down Expand Up @@ -957,8 +941,6 @@ bool FileUtils::isDirectoryExist(std::string_view dirPath) const
{
AXASSERT(!dirPath.empty(), "Invalid path");

DECLARE_GUARD;

if (isAbsolutePath(dirPath))
{
return isDirectoryExistInternal(dirPath);
Expand Down
5 changes: 0 additions & 5 deletions core/platform/FileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,6 @@ class AX_DLL FileUtils
virtual std::string getFullPathForFilenameWithinDirectory(std::string_view directory,
std::string_view filename) const;

/**
* mutex used to protect fields.
*/
mutable std::recursive_mutex _mutex;

/**
* The vector contains search paths.
* The lower index of the element in this vector, the higher priority for this search path.
Expand Down
10 changes: 0 additions & 10 deletions core/platform/android/FileUtils-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ THE SOFTWARE.

// #define AX_USE_ANDROID_EXTERNAL_FILES_DIR 1

#define DECLARE_GUARD (void)0 // std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)

namespace ax
{

Expand Down Expand Up @@ -93,8 +91,6 @@ FileUtilsAndroid::~FileUtilsAndroid()

bool FileUtilsAndroid::init()
{
DECLARE_GUARD;

_defaultResRootPath = ASSETS_FOLDER_NAME;

std::string assetsPath(getApkPath());
Expand All @@ -108,9 +104,6 @@ bool FileUtilsAndroid::init()

bool FileUtilsAndroid::isFileExistInternal(std::string_view strFilePath) const
{

DECLARE_GUARD;

if (strFilePath.empty())
{
return false;
Expand Down Expand Up @@ -210,7 +203,6 @@ bool FileUtilsAndroid::isDirectoryExistInternal(std::string_view dirPath) const

bool FileUtilsAndroid::isAbsolutePath(std::string_view strPath) const
{
DECLARE_GUARD;
// 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.axmol.hellocpp/cache/path/path/file.png, or
Expand All @@ -220,7 +212,6 @@ bool FileUtilsAndroid::isAbsolutePath(std::string_view strPath) const

int64_t FileUtilsAndroid::getFileSize(std::string_view filepath) const
{
DECLARE_GUARD;
int64_t size = FileUtils::getFileSize(filepath);
if (size != -1)
{
Expand Down Expand Up @@ -326,7 +317,6 @@ std::string FileUtilsAndroid::getNativeWritableAbsolutePath() const

}

#undef DECLARE_GUARD
#undef ASSETS_FOLDER_NAME_LENGTH
#undef ASSETS_FOLDER_NAME
#undef LOGD
Expand Down
47 changes: 26 additions & 21 deletions core/platform/apple/FileUtils-apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ of this software and associated documentation files (the "Software"), to deal
#include "base/Director.h"
#include "platform/FileUtils.h"

#define DECLARE_GUARD (void)0 // std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)

namespace ax
{

Expand Down Expand Up @@ -100,13 +98,11 @@ of this software and associated documentation files (the "Software"), to deal

std::string FileUtilsApple::getWritablePath() const
{
DECLARE_GUARD;
return getNativeWritableAbsolutePath();
}

std::string FileUtilsApple::getNativeWritableAbsolutePath() const
{
DECLARE_GUARD;
if (_writablePath.length())
{
return _writablePath;
Expand Down Expand Up @@ -228,33 +224,42 @@ static int unlink_cb(const char* fpath, const struct stat* sb, int typeflag, str
std::string FileUtilsApple::getFullPathForFilenameWithinDirectory(std::string_view directory,
std::string_view filename) const
{
auto fullPath = std::string();

// Build full path for the file
if (directory[0] != '/')
{
NSString* path = [pimpl_->getBundle() pathForResource:[[NSString alloc] initWithBytes:filename.data() length:filename.size() encoding:NSUTF8StringEncoding]
std::string ret;

if (filename.empty())
return ret;

if (!directory.empty() && directory[0] == '/') {
size_t pathSize = directory.size() + filename.size();
if (directory.back() != '/')
++pathSize;
ret.reserve(pathSize);
ret += directory;
ret += filename;
}
else
{ // Build full path for the file
NSString* path = nil;
if (!directory.empty()) {
path = [pimpl_->getBundle() pathForResource:[[NSString alloc] initWithBytes:filename.data() length:filename.size() encoding:NSUTF8StringEncoding]
ofType:nil
inDirectory:[[NSString alloc] initWithBytes:directory.data() length:directory.size() encoding:NSUTF8StringEncoding]];
if (path != nil)
{
fullPath = [path UTF8String];
} else {
path = [pimpl_->getBundle() pathForResource:[[NSString alloc] initWithBytes:filename.data() length:filename.size() encoding:NSUTF8StringEncoding]
ofType:nil];
}
}
else
{
fullPath = directory;
fullPath += filename;
if (path != nil)
ret = [path UTF8String];
}

// Check if there's a file at path
BOOL isDir = NO;
if (![s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()] isDirectory:&isDir] || isDir)
if (![s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:ret.c_str()] isDirectory:&isDir] || isDir)
{
fullPath.clear();
ret.clear();
}

return fullPath;
return ret;
}

bool FileUtilsApple::createDirectories(std::string_view path) const
Expand Down
5 changes: 0 additions & 5 deletions core/platform/linux/FileUtils-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ THE SOFTWARE.

using namespace std;

#define DECLARE_GUARD (void)0 // std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)

namespace ax
{

Expand Down Expand Up @@ -73,8 +71,6 @@ FileUtilsLinux::FileUtilsLinux() {}

bool FileUtilsLinux::init()
{
DECLARE_GUARD;

// application path
if (s_exeDir.empty()) {
s_exeDir = _checkPath("/proc/self/exe");
Expand Down Expand Up @@ -123,7 +119,6 @@ bool FileUtilsLinux::init()

string FileUtilsLinux::getWritablePath() const
{
DECLARE_GUARD;
return getNativeWritableAbsolutePath();
}

Expand Down
4 changes: 0 additions & 4 deletions core/platform/win32/FileUtils-win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ THE SOFTWARE.

#include "ntcvt/ntcvt.hpp"

#define DECLARE_GUARD (void)0 // std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)

namespace ax
{

Expand Down Expand Up @@ -164,13 +162,11 @@ int64_t FileUtilsWin32::getFileSize(std::string_view filepath) const

std::string FileUtilsWin32::getWritablePath() const
{
DECLARE_GUARD;
return getNativeWritableAbsolutePath();
}

std::string FileUtilsWin32::getNativeWritableAbsolutePath() const
{
DECLARE_GUARD;
if (_writablePath.length())
{
return _writablePath;
Expand Down

0 comments on commit f98b776

Please sign in to comment.