Skip to content

Commit

Permalink
refactor: 简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Apr 16, 2024
1 parent 17705ea commit feb70ac
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Common.Post.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<PreprocessorDefinitions>_WINDOWS;WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;WINRT_NO_MODULE_LOCK;NOGDICAPMASKS;NOICONS;NOATOM;NOCLIPBOARD;NODRAWTEXT;NOMEMMGR;NOMETAFILE;NOMINMAX;NOOPENFILE;NOSCROLL;NOSERVICE;NOSOUND;NOTEXTMETRIC;NOCOMM;NOKANJI;NOHELP;NOPROFILER;NODEFERWINDOWPOS;NOMCX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINDOWS;WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;WINRT_NO_MODULE_LOCK;WIL_SUPPRESS_EXCEPTIONS;NOGDICAPMASKS;NOICONS;NOATOM;NOCLIPBOARD;NODRAWTEXT;NOMEMMGR;NOMETAFILE;NOMINMAX;NOOPENFILE;NOSCROLL;NOSERVICE;NOSOUND;NOTEXTMETRIC;NOCOMM;NOKANJI;NOHELP;NOPROFILER;NODEFERWINDOWPOS;NOMCX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(CommitId)'!=''">MAGPIE_COMMIT_ID=$(CommitId);%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(MajorVersion)'!='' And '$(MinorVersion)'!='' And '$(PatchVersion)'!='' And '$(VersionTag)'!=''">MAGPIE_VERSION_MAJOR=$(MajorVersion);MAGPIE_VERSION_MINOR=$(MinorVersion);MAGPIE_VERSION_PATCH=$(PatchVersion);MAGPIE_VERSION_TAG=$(VersionTag);%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
Expand Down
8 changes: 4 additions & 4 deletions src/Magpie/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

// 将当前目录设为程序所在目录
static void SetWorkingDir() noexcept {
std::wstring exePath = Win32Utils::GetExePath();
std::wstring path = Win32Utils::GetExePath();

FAIL_FAST_IF_FAILED(PathCchRemoveFileSpec(
exePath.data(),
exePath.size() + 1
path.data(),
path.size() + 1
));

FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(exePath.c_str()));
FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(path.c_str()));
}

static void IncreaseTimerResolution() noexcept {
Expand Down
6 changes: 4 additions & 2 deletions src/Shared/CommonPch.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#include <span>

// WIL
// string_maker<std::wstring> 需要启用异常
#define WIL_ENABLE_EXCEPTIONS
#include <wil/stl.h>
#undef WIL_ENABLE_EXCEPTIONS
#include <wil/resource.h>
#include <wil/cppwinrt.h> // 应在 C++/WinRT 前包含
#include <wil/win32_helpers.h>
#include <wil/stl.h>
#include <wil/filesystem.h>

// C++/WinRT
Expand Down
11 changes: 11 additions & 0 deletions src/Updater/Updater.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>Pathcch.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand All @@ -59,7 +60,17 @@
<ItemGroup>
<Manifest Include="app.manifest" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.240122.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.240122.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.240122.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.240122.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
</Target>
</Project>
6 changes: 6 additions & 0 deletions src/Updater/Updater.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@
<ItemGroup>
<Manifest Include="app.manifest" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Natvis Include="$(MSBuildThisFileDirectory)..\..\natvis\wil.natvis" />
</ItemGroup>
</Project>
48 changes: 21 additions & 27 deletions src/Updater/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,32 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include "pch.h"
#include <shellapi.h>
#include "Version.h"
#include "PackageFiles.h"
#include <filesystem>
#include <shellapi.h>
#include "Utils.h"

// 将当前目录设为程序所在目录
static void SetCurDir() noexcept {
wchar_t curDir[MAX_PATH] = { 0 };
GetModuleFileName(NULL, curDir, MAX_PATH);
static void SetWorkingDir() noexcept {
std::wstring path;
FAIL_FAST_IF_FAILED(wil::GetModuleFileNameW(NULL, path));

for (int i = (int)Utils::StrLen(curDir) - 1; i >= 0; --i) {
if (curDir[i] == L'\\' || curDir[i] == L'/') {
break;
} else {
curDir[i] = L'\0';
}
}
FAIL_FAST_IF_FAILED(PathCchRemoveFileSpec(
path.data(),
path.size() + 1
));

SetCurrentDirectory(curDir);
FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(path.c_str()));
}

static bool WaitForMagpieToExit() noexcept {
static constexpr const wchar_t* SINGLE_INSTANCE_MUTEX_NAME = L"{4C416227-4A30-4A2F-8F23-8701544DD7D6}";

HANDLE hSingleInstanceMutex = CreateMutex(nullptr, FALSE, SINGLE_INSTANCE_MUTEX_NAME);
if (hSingleInstanceMutex) {
WaitForSingleObject(hSingleInstanceMutex, 10000);
CloseHandle(hSingleInstanceMutex);
{
wil::unique_mutex_nothrow hSingleInstanceMutex;
if (hSingleInstanceMutex.try_create(SINGLE_INSTANCE_MUTEX_NAME)) {
wil::handle_wait(hSingleInstanceMutex.get(), 10000);
}
}

// 即使 mutex 已被释放,Magpie.exe 仍有可能正在后台执行清理工作
Expand All @@ -61,15 +58,14 @@ static bool WaitForMagpieToExit() noexcept {

static void MoveFolder(const std::wstring& src, const std::wstring& dest) noexcept {
WIN32_FIND_DATA findData{};
HANDLE hFind = FindFirstFileEx((std::wstring(src) + L"\\*").c_str(),
FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH);
if (!hFind || hFind == INVALID_HANDLE_VALUE) {
wil::unique_hfind hFind(FindFirstFileEx((std::wstring(src) + L"\\*").c_str(),
FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH));
if (!hFind) {
return;
}

do {
std::wstring_view fileName(findData.cFileName);
if (fileName == L"." || fileName == L"..") {
if (wil::path_is_dot_or_dotdot(findData.cFileName)) {
continue;
}

Expand All @@ -81,9 +77,7 @@ static void MoveFolder(const std::wstring& src, const std::wstring& dest) noexce
CreateDirectory(destPath.c_str(), nullptr);
MoveFolder(curPath, destPath);
}
} while (FindNextFile(hFind, &findData));

FindClose(hFind);
} while (FindNextFile(hFind.get(), &findData));
}

int APIENTRY wWinMain(
Expand All @@ -97,7 +91,7 @@ int APIENTRY wWinMain(
return 0;
}

SetCurDir();
SetWorkingDir();

Version oldVersion;
if (!oldVersion.Parse(Utils::UTF16ToUTF8(lpCmdLine))) {
Expand Down Expand Up @@ -132,7 +126,7 @@ int APIENTRY wWinMain(
MoveFolder(L"update", L".");

// 删除 update 文件夹
std::filesystem::remove_all(L"update");
wil::RemoveDirectoryRecursiveNoThrow(L"update");

// 启动 Magpie
SHELLEXECUTEINFO execInfo{
Expand Down
4 changes: 4 additions & 0 deletions src/Updater/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.240122.1" targetFramework="native" />
</packages>
8 changes: 8 additions & 0 deletions src/Updater/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@
#include <cassert>
#include <span>
#include <optional>

// string_maker<std::wstring> 需要启用异常
#define WIL_ENABLE_EXCEPTIONS
#include <wil/stl.h>
#undef WIL_ENABLE_EXCEPTIONS
#include <wil/resource.h>
#include <wil/win32_helpers.h>
#include <wil/filesystem.h>

0 comments on commit feb70ac

Please sign in to comment.