Skip to content

Commit

Permalink
fix: 不再使用 wil::CreateDirectoryDeepNoThrow,因为它不支持相对路径
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Jan 1, 2025
1 parent c6ef833 commit c102cd5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
7 changes: 2 additions & 5 deletions src/Magpie.Core/EffectCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,8 +1171,6 @@ static uint32_t GeneratePassSource(
return 1;
}
}

result.push_back('\n');
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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 文件夹失败");
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/Magpie.Core/Win32Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
14 changes: 5 additions & 9 deletions src/Magpie.Core/include/Win32Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
10 changes: 4 additions & 6 deletions src/Magpie/AppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Magpie/TouchHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit c102cd5

Please sign in to comment.