Skip to content

Commit

Permalink
refactor: 优化架构
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Dec 13, 2024
1 parent 14d1f4e commit de9150d
Show file tree
Hide file tree
Showing 87 changed files with 548 additions and 572 deletions.
13 changes: 6 additions & 7 deletions src/Magpie.Core/CursorDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
#include "CursorDrawer.h"
#include "DeviceResources.h"
#include "Logger.h"
#include "Utils.h"
#include "DirectXHelper.h"
#include "ScalingOptions.h"
#include "shaders/SimpleVS.h"
#include "shaders/SimplePS.h"
#include "shaders/MaskedCursorPS.h"
#include "shaders/MonochromeCursorPS.h"
#include <DirectXMath.h>
#include "Win32Utils.h"
#include "Win32Helper.h"
#include "ScalingWindow.h"
#include "Renderer.h"
#include "CursorManager.h"
#include "StrUtils.h"
#include "StrHelper.h"

using namespace DirectX;

Expand Down Expand Up @@ -123,8 +122,8 @@ void CursorDrawer::Draw() noexcept {
if (cursorScaling < 1e-5) {
// 光标缩放和源窗口相同
const Renderer& renderer = ScalingWindow::Get().Renderer();
const SIZE srcSize = Win32Utils::GetSizeOfRect(renderer.SrcRect());
const SIZE destSize = Win32Utils::GetSizeOfRect(renderer.DestRect());
const SIZE srcSize = Win32Helper::GetSizeOfRect(renderer.SrcRect());
const SIZE destSize = Win32Helper::GetSizeOfRect(renderer.DestRect());
cursorScaling = (((float)destSize.cx / srcSize.cx) + ((float)destSize.cy / srcSize.cy)) / 2;
}

Expand All @@ -148,7 +147,7 @@ void CursorDrawer::Draw() noexcept {
return;
}

const SIZE viewportSize = Win32Utils::GetSizeOfRect(_viewportRect);
const SIZE viewportSize = Win32Helper::GetSizeOfRect(_viewportRect);
float left = (cursorRect.left - _viewportRect.left) / (float)viewportSize.cx * 2 - 1.0f;
float top = 1.0f - (cursorRect.top - _viewportRect.top) / (float)viewportSize.cy * 2;
float right = left + cursorSize.cx / (float)viewportSize.cx * 2;
Expand Down Expand Up @@ -522,7 +521,7 @@ const CursorDrawer::_CursorInfo* CursorDrawer::_ResolveCursor(HCURSOR hCursor) n
}

const char* CURSOR_TYPES[] = { "彩色","彩色掩码","单色" };
Logger::Get().Info(StrUtils::Concat("已解析", CURSOR_TYPES[(int)cursorInfo.type], "光标"));
Logger::Get().Info(StrHelper::Concat("已解析", CURSOR_TYPES[(int)cursorInfo.type], "光标"));

return &_cursorInfos.emplace(hCursor, std::move(cursorInfo)).first->second;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Magpie.Core/CursorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "CursorManager.h"
#include "Logger.h"
#include <magnification.h>
#include "Win32Utils.h"
#include "Win32Helper.h"
#include "ScalingOptions.h"
#include "ScalingWindow.h"
#include "Renderer.h"
Expand Down Expand Up @@ -47,8 +47,8 @@ static POINT ScalingToSrc(POINT pt) noexcept {
const RECT& srcRect = renderer.SrcRect();
const RECT& destRect = renderer.DestRect();

const SIZE srcSize = Win32Utils::GetSizeOfRect(srcRect);
const SIZE destSize = Win32Utils::GetSizeOfRect(destRect);
const SIZE srcSize = Win32Helper::GetSizeOfRect(srcRect);
const SIZE destSize = Win32Helper::GetSizeOfRect(destRect);

POINT result = { srcRect.left, srcRect.top };

Expand Down Expand Up @@ -267,8 +267,8 @@ void CursorManager::_AdjustCursorSpeed() noexcept {
}

const Renderer& renderer = ScalingWindow::Get().Renderer();
const SIZE srcSize = Win32Utils::GetSizeOfRect(renderer.SrcRect());
const SIZE destSize = Win32Utils::GetSizeOfRect(renderer.DestRect());
const SIZE srcSize = Win32Helper::GetSizeOfRect(renderer.SrcRect());
const SIZE destSize = Win32Helper::GetSizeOfRect(renderer.DestRect());
const double scale = ((double)destSize.cx / srcSize.cx + (double)destSize.cy / srcSize.cy) / 2;

INT newSpeed = 0;
Expand Down Expand Up @@ -343,7 +343,7 @@ static bool PtInWindow(HWND hWnd, POINT pt) noexcept {
// https://github.com/tongzx/nt5src/blob/daad8a087a4e75422ec96b7911f1df4669989611/Source/XPSP1/NT/windows/core/ntuser/kernel/winwhere.c#L47

RECT clientRect;
if (!Win32Utils::GetClientScreenRect(hWnd, clientRect)) {
if (!Win32Helper::GetClientScreenRect(hWnd, clientRect)) {
// 出错返回 true,因为已经确定光标在窗口内
return true;
}
Expand Down Expand Up @@ -684,8 +684,8 @@ void CursorManager::_StartCapture(POINT& cursorPos) noexcept {
//
// 在有黑边的情况下自动将光标调整到画面内

SIZE srcFrameSize = Win32Utils::GetSizeOfRect(srcRect);
SIZE outputSize = Win32Utils::GetSizeOfRect(destRect);
SIZE srcFrameSize = Win32Helper::GetSizeOfRect(srcRect);
SIZE outputSize = Win32Helper::GetSizeOfRect(destRect);

if (ScalingWindow::Get().Options().IsAdjustCursorSpeed()) {
_AdjustCursorSpeed();
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie.Core/DDSLoderHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "DDS.h"
#include "pch.h"
#include "Win32Utils.h"
#include "Win32Helper.h"


//--------------------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/Magpie.Core/DesktopDuplicationFrameSource.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "pch.h"
#include "DesktopDuplicationFrameSource.h"
#include "Logger.h"
#include "Win32Utils.h"
#include "Win32Helper.h"
#include "ScalingWindow.h"
#include "DeviceResources.h"
#include "DirectXHelper.h"
Expand Down Expand Up @@ -39,7 +39,7 @@ static winrt::com_ptr<IDXGIOutput1> FindMonitor(IDXGIAdapter1* adapter, HMONITOR

bool DesktopDuplicationFrameSource::_Initialize() noexcept {
// WDA_EXCLUDEFROMCAPTURE 只在 Win10 20H1 及更新版本中可用
if (!Win32Utils::GetOSVersion().Is20H1OrNewer()) {
if (!Win32Helper::GetOSVersion().Is20H1OrNewer()) {
Logger::Get().Error("当前操作系统无法使用 Desktop Duplication");
return false;
}
Expand All @@ -60,7 +60,7 @@ bool DesktopDuplicationFrameSource::_Initialize() noexcept {
}

// 最大化的窗口无需调整位置
if (Win32Utils::GetWindowShowCmd(hwndSrc) != SW_SHOWMAXIMIZED) {
if (Win32Helper::GetWindowShowCmd(hwndSrc) != SW_SHOWMAXIMIZED) {
if (!_CenterWindowIfNecessary(hwndSrc, mi.rcWork)) {
Logger::Get().Error("居中源窗口失败");
return false;
Expand Down Expand Up @@ -172,7 +172,7 @@ FrameSourceBase::UpdateState DesktopDuplicationFrameSource::_Update() noexcept {
for (uint32_t i = 0; i < nRect; ++i) {
const DXGI_OUTDUPL_MOVE_RECT& rect =
((DXGI_OUTDUPL_MOVE_RECT*)_dupMetaData.data())[i];
if (Win32Utils::CheckOverlap(_srcClientInMonitor, rect.DestinationRect)) {
if (Win32Helper::CheckOverlap(_srcClientInMonitor, rect.DestinationRect)) {
noUpdate = false;
break;
}
Expand All @@ -192,7 +192,7 @@ FrameSourceBase::UpdateState DesktopDuplicationFrameSource::_Update() noexcept {
nRect = bufSize / sizeof(RECT);
for (uint32_t i = 0; i < nRect; ++i) {
const RECT& rect = ((RECT*)_dupMetaData.data())[i];
if (Win32Utils::CheckOverlap(_srcClientInMonitor, rect)) {
if (Win32Helper::CheckOverlap(_srcClientInMonitor, rect)) {
noUpdate = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie.Core/DesktopDuplicationFrameSource.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "FrameSourceBase.h"
#include "Win32Utils.h"
#include "Win32Helper.h"
#include "SmallVector.h"

namespace Magpie::Core {
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie.Core/DeviceResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "DeviceResources.h"
#include "ScalingOptions.h"
#include "Logger.h"
#include "StrUtils.h"
#include "StrHelper.h"
#include "DirectXHelper.h"
#include "ScalingWindow.h"

Expand Down
6 changes: 3 additions & 3 deletions src/Magpie.Core/DirectXHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "DirectXHelper.h"
#include <d3dcompiler.h>
#include "Logger.h"
#include "StrUtils.h"
#include "StrHelper.h"

namespace Magpie::Core {

Expand Down Expand Up @@ -38,14 +38,14 @@ bool DirectXHelper::CompileComputeShader(
entryPoint, "cs_5_0", flags, 0, blob, errorMsgs.put());
if (FAILED(hr)) {
if (errorMsgs) {
Logger::Get().ComError(StrUtils::Concat("编译计算着色器失败: ", (const char*)errorMsgs->GetBufferPointer()), hr);
Logger::Get().ComError(StrHelper::Concat("编译计算着色器失败: ", (const char*)errorMsgs->GetBufferPointer()), hr);
}
return false;
}

// 警告消息
if (errorMsgs) {
Logger::Get().Warn(StrUtils::Concat("编译计算着色器时产生警告: ", (const char*)errorMsgs->GetBufferPointer()));
Logger::Get().Warn(StrHelper::Concat("编译计算着色器时产生警告: ", (const char*)errorMsgs->GetBufferPointer()));
}

return true;
Expand Down
32 changes: 16 additions & 16 deletions src/Magpie.Core/EffectCacheManager.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "pch.h"
#include "EffectCacheManager.h"
#include <regex>
#include "StrUtils.h"
#include "StrHelper.h"
#include "Logger.h"
#include "CommonSharedConstants.h"
#include <d3dcompiler.h>
#include "Utils.h"
#include "FastHasher.h"
#include "YasHelper.h"

namespace yas::detail {
Expand Down Expand Up @@ -131,7 +131,7 @@ bool EffectCacheManager::_LoadFromMemCache(const std::wstring& cacheFileName, Ef
if (it != _memCache.end()) {
desc = it->second.first;
it->second.second = ++_lastAccess;
Logger::Get().Info(StrUtils::Concat("已读取缓存 ", StrUtils::UTF16ToUTF8(cacheFileName)));
Logger::Get().Info(StrHelper::Concat("已读取缓存 ", StrHelper::UTF16ToUTF8(cacheFileName)));
return true;
}
return false;
Expand All @@ -146,12 +146,12 @@ bool EffectCacheManager::Load(std::wstring_view effectName, std::wstring_view ha
return true;
}

if (!Win32Utils::FileExists(cacheFileName.c_str())) {
if (!Win32Helper::FileExists(cacheFileName.c_str())) {
return false;
}

std::vector<BYTE> buf;
if (!Win32Utils::ReadFile(cacheFileName.c_str(), buf) || buf.empty()) {
if (!Win32Helper::ReadFile(cacheFileName.c_str(), buf) || buf.empty()) {
return false;
}

Expand All @@ -168,7 +168,7 @@ bool EffectCacheManager::Load(std::wstring_view effectName, std::wstring_view ha

_AddToMemCache(cacheFileName, desc);

Logger::Get().Info(StrUtils::Concat("已读取缓存 ", StrUtils::UTF16ToUTF8(cacheFileName)));
Logger::Get().Info(StrHelper::Concat("已读取缓存 ", StrHelper::UTF16ToUTF8(cacheFileName)));
return true;
}

Expand Down Expand Up @@ -200,13 +200,13 @@ void EffectCacheManager::Save(std::wstring_view effectName, std::wstring_view ha

WIN32_FIND_DATA findData{};
wil::unique_hfind hFind(FindFirstFileEx(
StrUtils::Concat(CommonSharedConstants::CACHE_DIR, L"*").c_str(),
StrHelper::Concat(CommonSharedConstants::CACHE_DIR, L"*").c_str(),
FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH));
if (hFind) {
do {
// 缓存文件名至少有 19 个字符
// {Name}_{1}{16}
if (StrUtils::StrLen(findData.cFileName) < 19) {
if (StrHelper::StrLen(findData.cFileName) < 19) {
continue;
}

Expand All @@ -215,9 +215,9 @@ void EffectCacheManager::Save(std::wstring_view effectName, std::wstring_view ha
continue;
}

if (!DeleteFile(StrUtils::Concat(CommonSharedConstants::CACHE_DIR, findData.cFileName).c_str())) {
Logger::Get().Win32Error(StrUtils::Concat("删除缓存文件 ",
StrUtils::UTF16ToUTF8(findData.cFileName), " 失败"));
if (!DeleteFile(StrHelper::Concat(CommonSharedConstants::CACHE_DIR, findData.cFileName).c_str())) {
Logger::Get().Win32Error(StrHelper::Concat("删除缓存文件 ",
StrHelper::UTF16ToUTF8(findData.cFileName), " 失败"));
}
} while (FindNextFile(hFind.get(), &findData));
} else {
Expand All @@ -226,17 +226,17 @@ void EffectCacheManager::Save(std::wstring_view effectName, std::wstring_view ha
}

std::wstring cacheFileName = GetCacheFileName(linearEffectName, hash, desc.flags);
if (!Win32Utils::WriteFile(cacheFileName.c_str(), buf.data(), buf.size())) {
if (!Win32Helper::WriteFile(cacheFileName.c_str(), buf.data(), buf.size())) {
Logger::Get().Error("保存缓存失败");
}

_AddToMemCache(cacheFileName, desc);

Logger::Get().Info(StrUtils::Concat("已保存缓存 ", StrUtils::UTF16ToUTF8(cacheFileName)));
Logger::Get().Info(StrHelper::Concat("已保存缓存 ", StrHelper::UTF16ToUTF8(cacheFileName)));
}

static std::wstring HexHash(std::span<const BYTE> data) {
uint64_t hashBytes = Utils::HashData(data);
uint64_t hashBytes = FastHasher::HashData(data);

static wchar_t oct2Hex[16] = {
L'0',L'1',L'2',L'3',L'4',L'5',L'6',L'7',
Expand Down Expand Up @@ -267,7 +267,7 @@ std::wstring EffectCacheManager::GetHash(
str.append(fmt::format("VERSION:{}\n", EFFECT_CACHE_VERSION));
if (inlineParams) {
for (const auto& pair : *inlineParams) {
str.append(fmt::format("{}:{}\n", StrUtils::UTF16ToUTF8(pair.first), std::lroundf(pair.second * 10000)));
str.append(fmt::format("{}:{}\n", StrHelper::UTF16ToUTF8(pair.first), std::lroundf(pair.second * 10000)));
}
}

Expand All @@ -282,7 +282,7 @@ std::wstring EffectCacheManager::GetHash(std::string& source, const phmap::flat_
source.append(fmt::format("VERSION:{}\n", EFFECT_CACHE_VERSION));
if (inlineParams) {
for (const auto& pair : *inlineParams) {
source.append(fmt::format("{}:{}\n", StrUtils::UTF16ToUTF8(pair.first), std::lroundf(pair.second * 10000)));
source.append(fmt::format("{}:{}\n", StrHelper::UTF16ToUTF8(pair.first), std::lroundf(pair.second * 10000)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Magpie.Core/EffectCacheManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "Win32Utils.h"
#include "Win32Helper.h"
#include "EffectDesc.h"
#include <parallel_hashmap/phmap.h>

Expand Down
Loading

0 comments on commit de9150d

Please sign in to comment.