Skip to content

Commit 7682132

Browse files
committed
refactor: EffectsService 和 LocalizationService 移到 Magpie.Core
1 parent eadbf0e commit 7682132

25 files changed

Lines changed: 101 additions & 108 deletions
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ winrt::fire_and_forget EffectsService::Initialize() {
5959
CommonSharedConstants::EFFECTS_DIR, L"\\", effectNames[id], L".hlsl");
6060
std::string source;
6161
Win32Helper::ReadTextFile(fileName.c_str(), source);
62-
EffectInfo2 effectInfo;
62+
EffectInfo effectInfo;
6363
std::string errorMsg = ShaderEffectParser::ParseForInfo(
6464
StrHelper::UTF16ToUTF8(effectNames[id]), std::move(source), effectInfo);
6565
if (!errorMsg.empty()) {
@@ -80,12 +80,12 @@ void EffectsService::Uninitialize() {
8080
_WaitForInitialize();
8181
}
8282

83-
const std::vector<EffectInfo2>& EffectsService::GetEffects() noexcept {
83+
const std::vector<EffectInfo>& EffectsService::GetEffects() noexcept {
8484
_WaitForInitialize();
8585
return _effects;
8686
}
8787

88-
const EffectInfo2* EffectsService::GetEffect(std::wstring_view name) noexcept {
88+
const EffectInfo* EffectsService::GetEffect(std::wstring_view name) noexcept {
8989
_WaitForInitialize();
9090

9191
auto it = _effectsMap.find(name);
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#include "pch.h"
2-
#include "AppSettings.h"
32
#include "LocalizationService.h"
3+
#include "CommonSharedConstants.h"
44
#include <bcp47mrm.h>
55
#include <winrt/Windows.System.UserProfile.h>
66

7-
using namespace winrt;
8-
97
namespace Magpie {
108

119
// 标签必须为小写
12-
static std::array SUPPORTED_LANGUAGES{
10+
static std::array SUPPORTED_LANGUAGES = {
1311
L"de",
1412
L"en-us",
1513
L"es",
@@ -32,7 +30,7 @@ static std::array SUPPORTED_LANGUAGES{
3230
void LocalizationService::EarlyInitialize() {
3331
// 非打包应用默认使用“Windows 显示语言”,这里自行切换至“首选语言”
3432
std::wstring userLanguages;
35-
for (const hstring& language : UserProfile::GlobalizationPreferences::Languages()) {
33+
for (const winrt::hstring& language : winrt::UserProfile::GlobalizationPreferences::Languages()) {
3634
userLanguages += language;
3735
userLanguages += L'\0';
3836
}
@@ -62,22 +60,26 @@ void LocalizationService::EarlyInitialize() {
6260
_Language(bestLanguage);
6361
}
6462

65-
void LocalizationService::Initialize() {
66-
AppSettings& settings = AppSettings::Get();
67-
68-
int language = settings.Language();
63+
void LocalizationService::Initialize(int language) {
6964
if (language >= 0) {
7065
_Language(SUPPORTED_LANGUAGES[language]);
7166
}
67+
68+
_resourceLoader = winrt::ResourceLoader::GetForViewIndependentUse(
69+
CommonSharedConstants::APP_RESOURCE_MAP_ID);
7270
}
7371

74-
std::span<const wchar_t*> LocalizationService::SupportedLanguages() noexcept {
72+
std::span<const wchar_t*> LocalizationService::GetSupportedLanguages() noexcept {
7573
return SUPPORTED_LANGUAGES;
7674
}
7775

76+
winrt::hstring LocalizationService::GetLocalizedString(std::wstring_view resName) const noexcept {
77+
return _resourceLoader.GetString(resName);
78+
}
79+
7880
void LocalizationService::_Language(const wchar_t* tag) {
7981
_language = tag;
80-
ResourceContext::SetGlobalQualifierValue(L"Language", tag);
82+
winrt::ResourceContext::SetGlobalQualifierValue(L"Language", tag);
8183
}
8284

8385
}

src/Magpie.Core/Magpie.Core.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
<ClInclude Include="FrameProducer.h" />
6767
<ClInclude Include="CursorHelper.h" />
6868
<ClInclude Include="include\EffectInfo.h" />
69+
<ClInclude Include="include\EffectsService.h" />
70+
<ClInclude Include="include\LocalizationService.h" />
6971
<ClInclude Include="include\ShaderEffectParser.h" />
7072
<ClInclude Include="ShaderEffectCompilerService.h" />
7173
<ClInclude Include="ShaderEffectDesc.h" />
@@ -99,9 +101,11 @@
99101
<ClCompile Include="DuplicateFrameChecker.cpp" />
100102
<ClCompile Include="DescriptorHeap.cpp" />
101103
<ClCompile Include="EffectsDrawer.cpp" />
104+
<ClCompile Include="EffectsService.cpp" />
102105
<ClCompile Include="ExclModeHelper.cpp" />
103106
<ClCompile Include="FrameProducer.cpp" />
104107
<ClCompile Include="CursorHelper.cpp" />
108+
<ClCompile Include="LocalizationService.cpp" />
105109
<ClCompile Include="ShaderEffectCompilerService.cpp" />
106110
<ClCompile Include="ShaderEffectDrawer.cpp" />
107111
<ClCompile Include="GraphicsCaptureFrameSource.cpp" />

src/Magpie.Core/Magpie.Core.vcxproj.filters

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
<ClInclude Include="DuplicateFrameChecker.h">
9595
<Filter>Capture</Filter>
9696
</ClInclude>
97-
<ClInclude Include="RectHelper.h" />
9897
<ClInclude Include="DirtyRectsOptimizer.h">
9998
<Filter>Capture</Filter>
10099
</ClInclude>
@@ -129,6 +128,15 @@
129128
<Filter>Render</Filter>
130129
</ClInclude>
131130
<ClInclude Include="Renderer.h" />
131+
<ClInclude Include="RectHelper.h">
132+
<Filter>Helpers</Filter>
133+
</ClInclude>
134+
<ClInclude Include="include\EffectsService.h">
135+
<Filter>Include</Filter>
136+
</ClInclude>
137+
<ClInclude Include="include\LocalizationService.h">
138+
<Filter>Include</Filter>
139+
</ClInclude>
132140
</ItemGroup>
133141
<ItemGroup>
134142
<ClCompile Include="ScalingRuntime.cpp" />
@@ -199,6 +207,12 @@
199207
<Filter>Render</Filter>
200208
</ClCompile>
201209
<ClCompile Include="Renderer.cpp" />
210+
<ClCompile Include="LocalizationService.cpp">
211+
<Filter>Services</Filter>
212+
</ClCompile>
213+
<ClCompile Include="EffectsService.cpp">
214+
<Filter>Services</Filter>
215+
</ClCompile>
202216
</ItemGroup>
203217
<ItemGroup>
204218
<FxCompile Include="shaders\MaskedCursorPS.hlsl">
@@ -240,10 +254,12 @@
240254
<FxCompile Include="shaders\CursorResizerPS.hlsl">
241255
<Filter>Shaders</Filter>
242256
</FxCompile>
243-
<FxCompile Include="shaders\TextureBlitPS.hlsl" />
244257
<FxCompile Include="shaders\FullscreenVS.hlsl">
245258
<Filter>Shaders</Filter>
246259
</FxCompile>
260+
<FxCompile Include="shaders\TextureBlitPS.hlsl">
261+
<Filter>Shaders</Filter>
262+
</FxCompile>
247263
</ItemGroup>
248264
<ItemGroup>
249265
<None Include="packages.config" />

src/Magpie.Core/ScalingWindow.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,11 @@ static UINT WM_MAGPIE_SCALINGCHANGED;
1919
// 窗口模式缩放时缩放窗口应遮挡源窗口和它的阴影,在四周留出 50 x DPI 缩放的空间
2020
static constexpr int WINDOWED_MODE_MIN_SPACE_AROUND = 2 * 50;
2121

22-
static void InitMessage() noexcept {
23-
[[maybe_unused]] static Ignore _ = []() {
24-
WM_MAGPIE_SCALINGCHANGED =
25-
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_SCALINGCHANGED);
26-
27-
return Ignore();
28-
}();
29-
}
30-
3122
static bool IsTopmostWindow(HWND hWnd) noexcept {
3223
return GetWindowExStyle(hWnd) & WS_EX_TOPMOST;
3324
}
3425

35-
ScalingWindow::ScalingWindow() noexcept :
36-
_resourceLoader(winrt::ResourceLoader::GetForViewIndependentUse(CommonSharedConstants::APP_RESOURCE_MAP_ID)) {}
26+
ScalingWindow::ScalingWindow() noexcept {}
3727

3828
ScalingWindow::~ScalingWindow() noexcept {}
3929

@@ -93,8 +83,6 @@ ScalingError ScalingWindow::_StartImpl(HWND hwndSrc) noexcept {
9383
return ScalingError::ScalingFailedGeneral;
9484
}
9585

96-
InitMessage();
97-
9886
bool isSrcInvisibleOrMinimized = false;
9987
if (ScalingError error = _srcTracker.Set(hwndSrc, _options, isSrcInvisibleOrMinimized);
10088
error != ScalingError::NoError
@@ -120,7 +108,10 @@ ScalingError ScalingWindow::_StartImpl(HWND hwndSrc) noexcept {
120108
}
121109

122110
[[maybe_unused]] static Ignore _ = []() {
123-
WNDCLASSEXW wcex{
111+
WM_MAGPIE_SCALINGCHANGED =
112+
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_SCALINGCHANGED);
113+
114+
WNDCLASSEXW wcex = {
124115
.cbSize = sizeof(wcex),
125116
.lpfnWndProc = _WndProc,
126117
.hInstance = wil::GetModuleInstanceHandle(),
@@ -462,10 +453,6 @@ void ScalingWindow::CleanAfterSrcRepositioned() noexcept {
462453
_isSrcRepositioning = false;
463454
}
464455

465-
winrt::hstring ScalingWindow::GetLocalizedString(std::wstring_view resName) const {
466-
return _resourceLoader.GetString(resName);
467-
}
468-
469456
LRESULT ScalingWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
470457
switch (msg) {
471458
case WM_CREATE:

src/Magpie.Core/ScalingWindow.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class ScalingWindow final : public WindowBaseT<ScalingWindow> {
6666

6767
void CleanAfterSrcRepositioned() noexcept;
6868

69-
winrt::hstring GetLocalizedString(std::wstring_view resName) const;
70-
7169
void ShowToast(std::wstring_view msg) const noexcept {
7270
_options.showToast(Handle(), msg);
7371
}
@@ -162,8 +160,6 @@ class ScalingWindow final : public WindowBaseT<ScalingWindow> {
162160

163161
class SrcTracker _srcTracker;
164162

165-
winrt::ResourceLoader _resourceLoader{ nullptr };
166-
167163
wil::unique_mutex_nothrow _exclModeMutex;
168164

169165
std::array<wil::unique_hwnd, 4> _hwndResizeHelpers{};

src/Magpie.Core/ShaderEffectParser.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static bool ResolveHeaderSortName(
393393
return false;
394394
}
395395

396-
((EffectInfo2*)data)->sortName = sortName;
396+
((EffectInfo*)data)->sortName = sortName;
397397
return true;
398398
}
399399

@@ -418,9 +418,9 @@ static bool ResolveHeaderCapability(
418418

419419
static constexpr std::array FLAG_INFOS = {
420420
// 以下为必需
421-
std::make_pair("FP16", EffectInfoFlags2::SupportFP16),
421+
std::make_pair("FP16", EffectInfoFlags::SupportFP16),
422422
// 以下为可选
423-
std::make_pair("ADVANCEDCOLOR", EffectInfoFlags2::SupportAdvancedColor)
423+
std::make_pair("ADVANCEDCOLOR", EffectInfoFlags::SupportAdvancedColor)
424424
};
425425

426426
std::bitset<FLAG_INFOS.size()> processed;
@@ -440,7 +440,7 @@ static bool ResolveHeaderCapability(
440440
}
441441
processed[idx] = true;
442442

443-
((EffectInfo2*)data)->flags |= it->second;
443+
((EffectInfo*)data)->flags |= it->second;
444444
} else {
445445
Logger::Get().Warn(StrHelper::Concat("使用了未知 CAPABILITY 标志: ", token));
446446
}
@@ -454,7 +454,7 @@ static bool ResolveHeaderScaleFactor(
454454
ParserState& state,
455455
void* data
456456
) noexcept {
457-
if (!GetNextNumber(source, state, ((EffectInfo2*)data)->scaleFactor)) {
457+
if (!GetNextNumber(source, state, ((EffectInfo*)data)->scaleFactor)) {
458458
return false;
459459
}
460460

@@ -468,7 +468,7 @@ static bool ResolveHeaderScaleFactor(
468468
static bool ResolveHeader(
469469
std::string_view source,
470470
uint32_t startLineNumer,
471-
EffectInfo2& effectInfo
471+
EffectInfo& effectInfo
472472
) noexcept {
473473
static constexpr std::array COMMAND_INFOS = {
474474
CommandInfo{ "VERSION", ResolveHeaderVersion, true },
@@ -653,7 +653,7 @@ static bool ResolveParameter(
653653
std::string ShaderEffectParser::ParseForInfo(
654654
std::string&& name,
655655
std::string&& source,
656-
EffectInfo2& effectInfo
656+
EffectInfo& effectInfo
657657
) noexcept {
658658
assert(!name.empty() && !source.empty());
659659

@@ -671,7 +671,7 @@ std::string ShaderEffectParser::ParseForInfo(
671671
std::string_view sourceView(source);
672672

673673
if (!CheckMagic(sourceView, state)) {
674-
Logger::Get().Error("检查 MagpieFX 头失败");
674+
Logger::Get().Error(StrHelper::Concat("CheckMagic 失败\n\t错误消息: ", state.errorMsg));
675675
return std::move(state.errorMsg);
676676
}
677677

@@ -775,14 +775,14 @@ std::string ShaderEffectParser::ParseForInfo(
775775
completeCurrentBlock(BlockType::Header, sourceView.size(), std::numeric_limits<size_t>::max());
776776

777777
if (!ResolveHeader(headerBlock.source, headerBlock.startLineNumer, effectInfo)) {
778-
Logger::Get().Error(StrHelper::Concat("解析 Header 块失败: ", state.errorMsg));
778+
Logger::Get().Error(StrHelper::Concat("ResolveHeader 失败\n\t错误消息: ", state.errorMsg));
779779
return std::move(state.errorMsg);
780780
}
781781

782782
effectInfo.params.resize(paramBlocks.size());
783783
for (size_t i = 0; i < paramBlocks.size(); ++i) {
784784
if (!ResolveParameter(paramBlocks[i].source, paramBlocks[i].startLineNumer, effectInfo.params[i])) {
785-
Logger::Get().Error(fmt::format("解析 Parameter#{} 块失败", i + 1));
785+
Logger::Get().Error(fmt::format("ResolveParameter#{} 失败\n\t错误消息: ", state.errorMsg));
786786
return std::move(state.errorMsg);
787787
}
788788
}

src/Magpie.Core/include/EffectInfo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ struct EffectInfoParameter {
1212
float step;
1313
};
1414

15-
enum class EffectInfoFlags2 {
15+
enum class EffectInfoFlags {
1616
None,
1717
SupportFP16 = 1,
1818
SupportAdvancedColor = 1 << 1,
1919
};
20-
DEFINE_ENUM_FLAG_OPERATORS(EffectInfoFlags2)
20+
DEFINE_ENUM_FLAG_OPERATORS(EffectInfoFlags)
2121

22-
struct EffectInfo2 {
22+
struct EffectInfo {
2323
std::string name;
2424
std::string sortName;
2525
std::vector<EffectInfoParameter> params;
2626
// 0 表示可以自由缩放
2727
uint32_t scaleFactor = 0;
28-
EffectInfoFlags2 flags = EffectInfoFlags2::None;
28+
EffectInfoFlags flags = EffectInfoFlags::None;
2929
};
3030

3131
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ class EffectsService {
1818

1919
void Uninitialize();
2020

21-
const std::vector<EffectInfo2>& GetEffects() noexcept;
21+
const std::vector<EffectInfo>& GetEffects() noexcept;
2222

2323
// 由于 WinUI 使用 UTF-16,这里也以 UTF-16 作为参数以减少编码转换
24-
const EffectInfo2* GetEffect(std::wstring_view name) noexcept;
24+
const EffectInfo* GetEffect(std::wstring_view name) noexcept;
2525

2626
private:
2727
EffectsService() = default;
2828

2929
void _WaitForInitialize() noexcept;
3030

31-
std::vector<EffectInfo2> _effects;
31+
std::vector<EffectInfo> _effects;
3232
phmap::flat_hash_map<std::wstring, uint32_t> _effectsMap;
3333
std::atomic<bool> _initialized = false;
3434
bool _initializedCache = false;

src/Magpie/LocalizationService.h renamed to src/Magpie.Core/include/LocalizationService.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@ class LocalizationService {
1515
// 在初始化 AppSettings 前调用以使用系统默认语言,然后就可以从 AppSettings 里读取语言设置
1616
void EarlyInitialize();
1717

18-
void Initialize();
18+
// -1 表示使用系统设置
19+
void Initialize(int language);
1920

2021
// 支持的所有语言的标签,均为小写
21-
static std::span<const wchar_t* > SupportedLanguages() noexcept;
22+
static std::span<const wchar_t*> GetSupportedLanguages() noexcept;
2223

23-
const wchar_t* Language() const noexcept {
24+
const wchar_t* GetLanguage() const noexcept {
2425
return _language;
2526
}
2627

28+
winrt::hstring GetLocalizedString(std::wstring_view resName) const noexcept;
29+
2730
private:
2831
LocalizationService() = default;
2932

3033
void _Language(const wchar_t* tag);
3134

3235
const wchar_t* _language = nullptr;
36+
winrt::ResourceLoader _resourceLoader{ nullptr };
3337
};
3438

3539
}

0 commit comments

Comments
 (0)