From c102cd577a2ed5f2be1863f2d786d025d0e7a0f1 Mon Sep 17 00:00:00 2001 From: Xu Date: Wed, 1 Jan 2025 21:57:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8D=E5=86=8D=E4=BD=BF=E7=94=A8=20w?= =?UTF-8?q?il::CreateDirectoryDeepNoThrow=EF=BC=8C=E5=9B=A0=E4=B8=BA?= =?UTF-8?q?=E5=AE=83=E4=B8=8D=E6=94=AF=E6=8C=81=E7=9B=B8=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Magpie.Core/EffectCompiler.cpp | 7 ++--- src/Magpie.Core/Win32Helper.cpp | 41 +++++++++++++++++++++++++++ src/Magpie.Core/include/Win32Helper.h | 14 ++++----- src/Magpie/AppSettings.cpp | 10 +++---- src/Magpie/TouchHelper.cpp | 5 ++-- 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/Magpie.Core/EffectCompiler.cpp b/src/Magpie.Core/EffectCompiler.cpp index 423f2255..f9353849 100644 --- a/src/Magpie.Core/EffectCompiler.cpp +++ b/src/Magpie.Core/EffectCompiler.cpp @@ -1171,8 +1171,6 @@ static uint32_t GeneratePassSource( return 1; } } - - result.push_back('\n'); } //////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1375,9 +1373,8 @@ static uint32_t CompilePasses( std::wstring sourcesPath = sourcesPathName.substr(0, sourcesPathName.find_last_of(L'\\')); if ((flags & EffectCompilerFlags::SaveSources) && !Win32Helper::DirExists(sourcesPath.c_str())) { - HRESULT hr = wil::CreateDirectoryDeepNoThrow(sourcesPath.c_str()); - if (FAILED(hr)) { - Logger::Get().ComError("创建 sources 文件夹失败", hr); + if (!Win32Helper::CreateDir(sourcesPath, true)) { + Logger::Get().Win32Error("创建 sources 文件夹失败"); } } diff --git a/src/Magpie.Core/Win32Helper.cpp b/src/Magpie.Core/Win32Helper.cpp index af8ae9e4..57d88519 100644 --- a/src/Magpie.Core/Win32Helper.cpp +++ b/src/Magpie.Core/Win32Helper.cpp @@ -236,6 +236,47 @@ bool Win32Helper::WriteTextFile(const wchar_t* fileName, std::string_view text) return true; } +bool Win32Helper::FileExists(const wchar_t* fileName) noexcept { + DWORD attrs = GetFileAttributes(fileName); + // 排除文件夹 + return (attrs != INVALID_FILE_ATTRIBUTES) && !(attrs & FILE_ATTRIBUTE_DIRECTORY); +} + +bool Win32Helper::DirExists(const wchar_t* fileName) noexcept { + DWORD attrs = GetFileAttributes(fileName); + return (attrs != INVALID_FILE_ATTRIBUTES) && (attrs & FILE_ATTRIBUTE_DIRECTORY); +} + +bool Win32Helper::CreateDir(const std::wstring& path, bool recursive) noexcept { + assert(!path.empty()); + + if (DirExists(path.c_str())) { + return true; + } + + if (!recursive) { + return CreateDirectory(path.c_str(), nullptr); + } + + size_t searchOffset = 0; + do { + auto segPos = path.find_first_of(L'\\', searchOffset); + if (segPos == std::wstring::npos) { + // 没有分隔符则将整个路径视为文件夹 + segPos = path.size(); + } + + std::wstring subdir = path.substr(0, segPos); + if (!subdir.empty() && !DirExists(subdir.c_str()) && !CreateDirectory(subdir.c_str(), nullptr)) { + return false; + } + + searchOffset = segPos + 1; + } while (searchOffset < path.size()); + + return true; +} + const Win32Helper::OSVersion& Win32Helper::GetOSVersion() noexcept { static OSVersion version = []() -> OSVersion { HMODULE hNtDll = GetModuleHandle(L"ntdll.dll"); diff --git a/src/Magpie.Core/include/Win32Helper.h b/src/Magpie.Core/include/Win32Helper.h index 627f44f3..72d95222 100644 --- a/src/Magpie.Core/include/Win32Helper.h +++ b/src/Magpie.Core/include/Win32Helper.h @@ -34,16 +34,12 @@ struct Win32Helper { static bool WriteTextFile(const wchar_t* fileName, std::string_view text) noexcept; - static bool FileExists(const wchar_t* fileName) noexcept { - DWORD attrs = GetFileAttributes(fileName); - // 排除文件夹 - return (attrs != INVALID_FILE_ATTRIBUTES) && !(attrs & FILE_ATTRIBUTE_DIRECTORY); - } + static bool FileExists(const wchar_t* fileName) noexcept; - static bool DirExists(const wchar_t* fileName) noexcept { - DWORD attrs = GetFileAttributes(fileName); - return (attrs != INVALID_FILE_ATTRIBUTES) && (attrs & FILE_ATTRIBUTE_DIRECTORY); - } + static bool DirExists(const wchar_t* fileName) noexcept; + + // 相比 wil::CreateDirectoryDeepNoThrow 支持相对路径而且更快 + static bool CreateDir(const std::wstring& path, bool recursive = false) noexcept; struct OSVersion : Version { constexpr OSVersion() {} diff --git a/src/Magpie/AppSettings.cpp b/src/Magpie/AppSettings.cpp index a9bbaaa6..06b1f3ec 100644 --- a/src/Magpie/AppSettings.cpp +++ b/src/Magpie/AppSettings.cpp @@ -437,9 +437,8 @@ void AppSettings::_UpdateWindowPlacement() noexcept { } bool AppSettings::_Save(const _AppSettingsData& data) noexcept { - HRESULT hr = wil::CreateDirectoryDeepNoThrow(data._configDir.c_str()); - if (FAILED(hr)) { - Logger::Get().ComError("创建配置文件夹失败", hr); + if (!Win32Helper::CreateDir(data._configDir, true)) { + Logger::Get().Win32Error("创建配置文件夹失败"); return false; } @@ -1067,9 +1066,8 @@ bool AppSettings::_UpdateConfigPath(std::wstring* existingConfigPath) noexcept { } // 确保配置文件夹存在 - HRESULT hr = wil::CreateDirectoryDeepNoThrow(_configDir.c_str()); - if (FAILED(hr)) { - Logger::Get().ComError("创建配置文件夹失败", hr); + if (!Win32Helper::CreateDir(_configDir, true)) { + Logger::Get().Win32Error("创建配置文件夹失败"); return false; } diff --git a/src/Magpie/TouchHelper.cpp b/src/Magpie/TouchHelper.cpp index 4bcebc9f..e89b846d 100644 --- a/src/Magpie/TouchHelper.cpp +++ b/src/Magpie/TouchHelper.cpp @@ -173,9 +173,8 @@ bool TouchHelper::Register() noexcept { } std::wstring magpieDir = StrHelper::Concat(system32Dir.get(), L"\\Magpie"); - hr = wil::CreateDirectoryDeepNoThrow(magpieDir.c_str()); - if (FAILED(hr)) { - Logger::Get().ComError("CreateDirectoryDeepNoThrow 失败", hr); + if (!CreateDirectory(magpieDir.c_str(), nullptr)) { + Logger::Get().Win32Error("CreateDirectory 失败"); return false; }