axmolengine / axmol

Axmol Engine – A Multi-platform Engine for Desktop, XBOX (UWP) and Mobile games. (A fork of Cocos2d-x-4.0)
https://axmol.dev
MIT License
929 stars 205 forks source link

Improve FileUtils removeDirectory implementation for android #2208

Open aryamansingh2008 opened 1 month ago

aryamansingh2008 commented 1 month ago

How much do you want sponsor somebody to solve this feature: $0

Is your feature request related to a problem? Please describe. The FileUtils::removeDirectory method makes a system call to delete the directory. This invokes a separate process and comes with command line interpretation of the command, which makes this slow.

Describe the solution you'd like Can we use std::filesystem::remove_all instead to achieve this? To my knowledge, it is cross platform and should perform better than making a system call.

Code Snippet

bool FileUtils::removeDirectory(std::string_view path) const { # if !defined(AX_TARGET_OS_TVOS)

# if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID) if (nftw(path.data(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS) == -1) return false; else return true; # else std::string command = "rm -r \""s; // Path may include space. command.append(path).append("\"", 1); if (system(command.c_str()) >= 0) return true; else return false; # endif // (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID)

# else return false; # endif // !defined(AX_TARGET_OS_TVOS) }

Suggested Improvement

bool FileUtils::removeDirectory(std::string_view path) const { # if !defined(AX_TARGET_OS_TVOS)

# if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID) if (nftw(path.data(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS) == -1) return false; else return true; # else std::error_code ec; std::filesystem::remove_all(path, ec); return !ec; # endif // (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID)

# else return false; # endif // !defined(AX_TARGET_OS_TVOS) }