axmol/core/platform/win32/FileUtils-win32.cpp

363 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2019-11-23 20:27:39 +08:00
https://axmol.dev/
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.
****************************************************************************/
#include "platform/win32/FileUtils-win32.h"
#include "platform/Common.h"
2019-11-23 20:27:39 +08:00
#include <Shlobj.h>
#include <cstdlib>
#include <regex>
#include <sstream>
2021-12-25 10:04:45 +08:00
#include <sys/types.h>
#include <sys/stat.h>
2019-11-23 20:27:39 +08:00
2021-11-26 17:19:50 +08:00
#include "ntcvt/ntcvt.hpp"
2021-12-25 10:04:45 +08:00
#define DECLARE_GUARD (void)0 // std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
2022-04-28 12:00:32 +08:00
#define AX_MAX_PATH 512
2019-11-23 20:27:39 +08:00
// D:\aaa\bbb\ccc\ddd\abc.txt --> D:/aaa/bbb/ccc/ddd/abc.txt
2022-04-28 11:43:59 +08:00
static std::string convertPathFormatToUnixStyle(const std::string_view& path)
2019-11-23 20:27:39 +08:00
{
std::string ret{path};
2022-04-28 11:43:59 +08:00
std::replace(ret.begin(), ret.end(), '\\', '/');
2019-11-23 20:27:39 +08:00
return ret;
}
2022-04-28 11:43:59 +08:00
static std::string convertPathFormatToWinStyle(const std::string_view& path)
{
std::string ret{path};
2022-04-28 11:43:59 +08:00
std::replace(ret.begin(), ret.end(), '/', '\\');
return ret;
}
static std::string _checkWorkingPath()
2019-11-23 20:27:39 +08:00
{
WCHAR utf16Path[AX_MAX_PATH + 2] = {0};
size_t nNum = GetCurrentDirectoryW(AX_MAX_PATH, utf16Path);
utf16Path[nNum++] = '\\';
auto workingDir = ntcvt::from_chars(std::wstring_view{utf16Path, static_cast<size_t>(nNum)});
std::replace(workingDir.begin(), workingDir.end(), '\\', '/');
return workingDir;
}
static std::string _checkExePath()
{
WCHAR utf16Path[AX_MAX_PATH + 1] = {0};
size_t nNum = GetModuleFileNameW(NULL, utf16Path, AX_MAX_PATH);
std::wstring_view u16pathsv{utf16Path, nNum};
u16pathsv.remove_suffix(u16pathsv.length() - u16pathsv.find_last_of('\\') - 1);
auto u8path = ntcvt::from_chars(u16pathsv);
std::replace(u8path.begin(), u8path.end(), '\\', '/');
return u8path;
2019-11-23 20:27:39 +08:00
}
FileUtils* FileUtils::getInstance()
{
if (s_sharedFileUtils == nullptr)
{
s_sharedFileUtils = new FileUtilsWin32();
2021-12-25 10:04:45 +08:00
if (!s_sharedFileUtils->init())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
delete s_sharedFileUtils;
s_sharedFileUtils = nullptr;
AXLOGE("ERROR: Could not init FileUtilsWin32");
2019-11-23 20:27:39 +08:00
}
}
return s_sharedFileUtils;
}
2021-12-25 10:04:45 +08:00
FileUtilsWin32::FileUtilsWin32() {}
2019-11-23 20:27:39 +08:00
bool FileUtilsWin32::init()
{
if(s_exeDir.empty()) {
s_exeDir = _checkExePath();
}
auto workingDir = _checkWorkingPath();
bool startedFromSelfLocation = workingDir == s_exeDir;
if (!startedFromSelfLocation || !isDirectoryExistInternal(AX_CONTENT_DIR))
_defaultResRootPath = std::move(workingDir);
2022-04-28 11:43:59 +08:00
else
2022-04-28 12:00:32 +08:00
{
_defaultResRootPath.reserve(s_exeDir.size() + AX_CONTENT_DIR_LEN);
_defaultResRootPath.append(s_exeDir).append(AX_CONTENT_DIR, AX_CONTENT_DIR_LEN);
2022-04-28 12:00:32 +08:00
}
2022-04-28 11:43:59 +08:00
bool bRet = FileUtils::init();
2022-04-28 11:43:59 +08:00
// make sure any path relative to exe dir can be found when app working directory location not exe path
if (!startedFromSelfLocation)
2022-04-28 12:00:32 +08:00
addSearchPath(s_exeDir);
return bRet;
2019-11-23 20:27:39 +08:00
}
bool FileUtilsWin32::isDirectoryExistInternal(std::string_view dirPath) const
2019-11-23 20:27:39 +08:00
{
uint32_t fAttrib = GetFileAttributesW(ntcvt::from_chars(dirPath).c_str());
2021-12-25 10:04:45 +08:00
return (fAttrib != INVALID_FILE_ATTRIBUTES && (fAttrib & FILE_ATTRIBUTE_DIRECTORY));
2019-11-23 20:27:39 +08:00
}
bool FileUtilsWin32::isFileExistInternal(std::string_view strFilePath) const
2019-11-23 20:27:39 +08:00
{
2022-04-28 11:43:59 +08:00
DWORD attr = GetFileAttributesW(ntcvt::from_chars(strFilePath).c_str());
return (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY));
2019-11-23 20:27:39 +08:00
}
std::string FileUtilsWin32::getPathForFilename(std::string_view filename,
std::string_view searchPath) const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
std::string unixFileName = convertPathFormatToUnixStyle(filename);
std::string unixSearchPath = convertPathFormatToUnixStyle(searchPath);
2019-11-23 20:27:39 +08:00
2022-11-01 06:47:36 +08:00
return FileUtils::getPathForFilename(unixFileName, unixSearchPath);
2019-11-23 20:27:39 +08:00
}
std::string FileUtilsWin32::getFullPathForFilenameWithinDirectory(std::string_view strDirectory,
std::string_view strFilename) const
2019-11-23 20:27:39 +08:00
{
std::string unixDirectory = convertPathFormatToUnixStyle(strDirectory);
2021-12-25 10:04:45 +08:00
std::string unixFilename = convertPathFormatToUnixStyle(strFilename);
2019-11-23 20:27:39 +08:00
return FileUtils::getFullPathForFilenameWithinDirectory(unixDirectory, unixFilename);
}
int64_t FileUtilsWin32::getFileSize(std::string_view filepath) const
2019-11-23 20:27:39 +08:00
{
if (filepath.empty())
return -1;
2021-12-25 10:04:45 +08:00
WIN32_FILE_ATTRIBUTE_DATA attrs = {0};
if (GetFileAttributesExW(ntcvt::from_chars(filepath).c_str(), GetFileExInfoStandard, &attrs) &&
!(attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
2021-12-25 10:04:45 +08:00
return static_cast<long long>(attrs.nFileSizeHigh) << 32 | static_cast<unsigned long long>(attrs.nFileSizeLow);
return -1;
2019-11-23 20:27:39 +08:00
}
2021-07-04 21:21:29 +08:00
std::string FileUtilsWin32::getWritablePath() const
{
DECLARE_GUARD;
return getNativeWritableAbsolutePath();
}
std::string FileUtilsWin32::getNativeWritableAbsolutePath() const
2019-11-23 20:27:39 +08:00
{
DECLARE_GUARD;
if (_writablePath.length())
{
return _writablePath;
}
// Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe
2022-04-28 12:00:32 +08:00
WCHAR full_path[AX_MAX_PATH + 1] = {0};
::GetModuleFileNameW(nullptr, full_path, AX_MAX_PATH + 1);
2019-11-23 20:27:39 +08:00
// Debug app uses executable directory; Non-debug app uses local app data directory
2021-12-25 10:04:45 +08:00
//#ifndef _DEBUG
2019-11-23 20:27:39 +08:00
// Get filename of executable only, e.g. MyGame.exe
WCHAR* base_name = wcsrchr(full_path, '\\');
2021-07-04 21:21:29 +08:00
std::wstring retPath;
if (base_name)
2019-11-23 20:27:39 +08:00
{
2022-04-28 12:00:32 +08:00
WCHAR app_data_path[AX_MAX_PATH + 1];
2019-11-23 20:27:39 +08:00
// Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, app_data_path)))
2019-11-23 20:27:39 +08:00
{
2021-07-04 21:21:29 +08:00
std::wstring ret(app_data_path);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// Adding executable filename, e.g. C:\Documents and Settings\username\Local Settings\Application
// Data\MyGame.exe
2019-11-23 20:27:39 +08:00
ret += base_name;
// Remove ".exe" extension, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame
ret = ret.substr(0, ret.rfind(L"."));
ret += L"\\";
// Create directory
if (SUCCEEDED(SHCreateDirectoryExW(nullptr, ret.c_str(), nullptr)))
2019-11-23 20:27:39 +08:00
{
retPath = ret;
}
}
}
if (retPath.empty())
2021-12-25 10:04:45 +08:00
//#endif // not defined _DEBUG
2019-11-23 20:27:39 +08:00
{
// If fetching of local app data directory fails, use the executable one
retPath = full_path;
// remove xxx.exe
retPath = retPath.substr(0, retPath.rfind(L"\\") + 1);
}
return convertPathFormatToUnixStyle(ntcvt::from_chars(retPath));
2019-11-23 20:27:39 +08:00
}
bool FileUtilsWin32::renameFile(std::string_view oldfullpath, std::string_view newfullpath) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!oldfullpath.empty(), "Invalid path");
AXASSERT(!newfullpath.empty(), "Invalid path");
2019-11-23 20:27:39 +08:00
std::wstring _wNew = ntcvt::from_chars(newfullpath);
std::wstring _wOld = ntcvt::from_chars(oldfullpath);
2019-11-23 20:27:39 +08:00
if (FileUtils::getInstance()->isFileExist(newfullpath))
{
if (!DeleteFile(_wNew.c_str()))
{
AXLOGE("Fail to delete file {} !Error code is 0x{:x}", newfullpath, GetLastError());
2019-11-23 20:27:39 +08:00
}
}
if (MoveFileW(_wOld.c_str(), _wNew.c_str()))
2019-11-23 20:27:39 +08:00
{
return true;
}
else
{
AXLOGE("Fail to rename file {} to {} !Error code is 0x{:x}", oldfullpath, newfullpath,
2021-12-25 10:04:45 +08:00
GetLastError());
2019-11-23 20:27:39 +08:00
return false;
}
}
bool FileUtilsWin32::renameFile(std::string_view path, std::string_view oldname, std::string_view name) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!path.empty(), "Invalid path");
std::string oldPath{path};
oldPath += oldname;
std::string newPath{path};
newPath += name;
2019-11-23 20:27:39 +08:00
std::regex pat("\\/");
std::string _old = std::regex_replace(oldPath, pat, "\\");
std::string _new = std::regex_replace(newPath, pat, "\\");
return renameFile(_old, _new);
}
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
bool FileUtilsWin32::createDirectories(std::string_view dirPath) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!dirPath.empty(), "Invalid path");
2019-11-23 20:27:39 +08:00
if (isDirectoryExist(dirPath))
return true;
std::wstring path = ntcvt::from_chars(dirPath);
2019-11-23 20:27:39 +08:00
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
bool fail = false;
if ((GetFileAttributesW(path.c_str())) == INVALID_FILE_ATTRIBUTES)
2019-11-23 20:27:39 +08:00
{
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
axstd::splitpath_cb(
&path.front(), [](wchar_t* ptr) { return *ptr != '\0'; },
[&dirPath, &fail](const wchar_t* subpath) {
auto attribs = GetFileAttributesW(subpath);
if (attribs == INVALID_FILE_ATTRIBUTES)
2019-11-23 20:27:39 +08:00
{
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
BOOL ret = CreateDirectoryW(subpath, NULL);
2019-11-23 20:27:39 +08:00
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
fail = true;
AXLOGE("Fail create directory {} !Error code is 0x{:x}", dirPath, GetLastError());
2019-11-23 20:27:39 +08:00
}
}
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
else
fail = !(attribs & FILE_ATTRIBUTE_DIRECTORY);
return fail;
});
2019-11-23 20:27:39 +08:00
}
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
return !fail;
2019-11-23 20:27:39 +08:00
}
bool FileUtilsWin32::removeFile(std::string_view filepath) const
2019-11-23 20:27:39 +08:00
{
std::string win32path = convertPathFormatToWinStyle(filepath);
2019-11-23 20:27:39 +08:00
if (DeleteFileW(ntcvt::from_chars(win32path).c_str()))
2019-11-23 20:27:39 +08:00
{
return true;
}
else
{
AXLOGE("Fail remove file {} !Error code is 0x{:x}", filepath, GetLastError());
2019-11-23 20:27:39 +08:00
return false;
}
}
bool FileUtilsWin32::removeDirectory(std::string_view dirPath) const
2019-11-23 20:27:39 +08:00
{
std::string dirPathCopy{dirPath};
2020-08-18 14:29:09 +08:00
if (!dirPath.empty() && dirPath[dirPath.length() - 1] != '/' && dirPath[dirPath.length() - 1] != '\\')
2019-11-23 20:27:39 +08:00
{
dirPathCopy.push_back('/');
2019-11-23 20:27:39 +08:00
}
std::wstring wpath = ntcvt::from_chars(dirPathCopy);
2019-11-23 20:27:39 +08:00
std::wstring files = wpath + L"*.*";
WIN32_FIND_DATA wfd;
2021-12-25 10:04:45 +08:00
HANDLE search = FindFirstFileEx(files.c_str(), FindExInfoStandard, &wfd, FindExSearchNameMatch, NULL, 0);
bool ret = true;
2019-11-23 20:27:39 +08:00
if (search != INVALID_HANDLE_VALUE)
{
BOOL find = true;
while (find)
{
// Need check string . and .. for delete folders and files begin name.
std::wstring fileName = wfd.cFileName;
if (fileName != L"." && fileName != L"..")
{
std::wstring temp = wpath + wfd.cFileName;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
temp += '/';
ret = ret && this->removeDirectory(ntcvt::from_chars(temp));
2019-11-23 20:27:39 +08:00
}
else
{
SetFileAttributesW(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
ret = ret && DeleteFileW(temp.c_str());
2019-11-23 20:27:39 +08:00
}
}
find = FindNextFile(search, &wfd);
}
FindClose(search);
}
if (ret && RemoveDirectoryW(wpath.c_str()))
2019-11-23 20:27:39 +08:00
{
return true;
}
return false;
}
}