Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Z-Dante committed Jan 22, 2022
2 parents 1f1b0a9 + 29497c5 commit 9e8abd1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
18 changes: 17 additions & 1 deletion Magpie/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
using Magpie.Properties;
// Copyright (c) 2021 - present, Liu Xu
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.


using Magpie.Properties;
using NLog;
using NLog.Config;
using System;
Expand Down
9 changes: 9 additions & 0 deletions Magpie/Properties/Resources.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Msg_Error_Caption" xml:space="preserve">
<value>Ошибка</value>
</data>
<data name="Msg_Error_Failed_To_Capture" xml:space="preserve">
<value>Не удалось захватить, попробуйте изменить способ захвата.</value>
</data>
<data name="Msg_Error_Failed_To_Crop" xml:space="preserve">
<value>Это окно слишком маленькое для обрезки.</value>
</data>
<data name="Msg_Error_Generic" xml:space="preserve">
<value>Произошла ошибка, проверьте журналы для дополнительной информации.</value>
</data>
Expand Down
6 changes: 5 additions & 1 deletion Runtime/EffectDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ bool EffectDrawer::Build(ComPtr<ID3D11Texture2D> input, ComPtr<ID3D11Texture2D>

SIZE outputSize;
if (!CalcOutputSize(inputSize, outputSize)) {
SPDLOG_LOGGER_ERROR(logger, "CalcOutputSize 失败");
return false;
}

Expand All @@ -330,6 +331,7 @@ bool EffectDrawer::Build(ComPtr<ID3D11Texture2D> input, ComPtr<ID3D11Texture2D>
// 从文件加载纹理
_textures[i] = TextureLoader::Load((L"effects\\" + StrUtils::UTF8ToUTF16(_effectDesc.textures[i].source)).c_str());
if (!_textures[i]) {
SPDLOG_LOGGER_ERROR(logger, fmt::format("加载纹理 {} 失败", _effectDesc.textures[i].source));
return false;
}
} else {
Expand All @@ -339,11 +341,13 @@ bool EffectDrawer::Build(ComPtr<ID3D11Texture2D> input, ComPtr<ID3D11Texture2D>
texSize.cx = std::lround(exprParser.Eval());
exprParser.SetExpr(_effectDesc.textures[i].sizeExpr.second);
texSize.cy = std::lround(exprParser.Eval());
} catch (...) {
} catch (const mu::ParserError& e) {
SPDLOG_LOGGER_ERROR(logger, fmt::format("计算中间纹理尺寸失败:{}", e.GetMsg()));
return false;
}

if (texSize.cx <= 0 || texSize.cy <= 0) {
SPDLOG_LOGGER_ERROR(logger, "非法的中间纹理尺寸");
return false;
}

Expand Down
7 changes: 6 additions & 1 deletion Runtime/FrameSourceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ bool FrameSourceBase::_GetMapToOriginDPI(HWND hWnd, double& a, double& bx, doubl
ReleaseDC(hWnd, hdcClient);
});

HBITMAP hBmpWindow = (HBITMAP)GetCurrentObject(hdcWindow, OBJ_BITMAP);
HGDIOBJ hBmpWindow = GetCurrentObject(hdcWindow, OBJ_BITMAP);
if (!hBmpWindow) {
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("GetCurrentObject 失败"));
return false;
}

if (GetObjectType(hBmpWindow) != OBJ_BITMAP) {
SPDLOG_LOGGER_ERROR(logger, "无法获取窗口的重定向表面");
return false;
}

BITMAP bmp{};
if (!GetObject(hBmpWindow, sizeof(bmp), &bmp)) {
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("GetObject 失败"));
Expand Down
23 changes: 22 additions & 1 deletion Runtime/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,10 +741,12 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
rapidjson::Document doc;
if (doc.Parse(effectsJson.c_str(), effectsJson.size()).HasParseError()) {
// 解析 json 失败
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败\n\t错误码:{}", doc.GetParseError()));
return false;
}

if (!doc.IsArray()) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:根元素不为数组");
return false;
}

Expand All @@ -757,23 +759,26 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec

// 不得为空
if (effectsArr.Empty()) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:根元素为空");
return false;
}

for (const auto& effectJson : effectsArr) {
if (!effectJson.IsObject()) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:根数组中存在非法成员");
return false;
}

EffectDrawer& effect = _effects.emplace_back();

auto effectName = effectJson.FindMember("effect");
if (effectName == effectJson.MemberEnd() || !effectName->value.IsString()) {
// 未找到 effect 属性或该属性不合法
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:未找到 effect 属性或该属性的值不合法");
return false;
}

if (!effect.Initialize((L"effects\\" + StrUtils::UTF8ToUTF16(effectName->value.GetString()) + L".hlsl").c_str())) {
SPDLOG_LOGGER_ERROR(logger, fmt::format("初始化效果 {} 失败", effectName->value.GetString()));
return false;
}

Expand All @@ -782,6 +787,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
auto scaleProp = effectJson.FindMember("scale");
if (scaleProp != effectJson.MemberEnd()) {
if (!scaleProp->value.IsArray()) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
return false;
}

Expand All @@ -792,6 +798,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec

const auto& scale = scaleProp->value.GetArray();
if (scale.Size() != 2 || !scale[0].IsNumber() || !scale[1].IsNumber()) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
return false;
}

Expand All @@ -804,18 +811,21 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec

if (scaleX >= DELTA) {
if (scaleY < DELTA) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
return false;
}

outputSize = { std::lroundf(outputSize.cx * scaleX), std::lroundf(outputSize.cy * scaleY) };
} else if (std::abs(scaleX) < DELTA) {
if (std::abs(scaleY) >= DELTA) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
return false;
}

outputSize = hostSize;
} else {
if (scaleY > -DELTA) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
return false;
}

Expand All @@ -832,6 +842,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec

for (const auto& prop : effectJson.GetObject()) {
if (!prop.name.IsString()) {
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的效果名");
return false;
}

Expand All @@ -843,10 +854,12 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
auto type = effect.GetConstantType(name);
if (type == EffectDrawer::ConstantType::Float) {
if (!prop.value.IsNumber()) {
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的类型非法", name));
return false;
}

if (!effect.SetConstant(name, prop.value.GetFloat())) {
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的值非法", name));
return false;
}
} else if (type == EffectDrawer::ConstantType::Int) {
Expand All @@ -857,26 +870,31 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
// bool 值视为 int
value = (int)prop.value.GetBool();
} else {
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的类型非法", name));
return false;
}

if (!effect.SetConstant(name, value)) {
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的值非法", name));
return false;
}
} else {
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:非法成员 {}", name));
return false;
}
}
}

SIZE& outputSize = texSizes.emplace_back();
if (!effect.CalcOutputSize(texSizes[texSizes.size() - 2], outputSize)) {
SPDLOG_LOGGER_ERROR(logger, "CalcOutputSize 失败");
return false;
}
}

if (_effects.size() == 1) {
if (!_effects.back().Build(_effectInput, _backBuffer)) {
SPDLOG_LOGGER_ERROR(logger, "构建效果失败");
return false;
}
} else {
Expand All @@ -901,10 +919,12 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
ComPtr<ID3D11Texture2D> outputTex;
HRESULT hr = _d3dDevice->CreateTexture2D(&desc, nullptr, &outputTex);
if (FAILED(hr)) {
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("CreateTexture2D 失败", hr));
return false;
}

if (!_effects[i].Build(curTex, outputTex)) {
SPDLOG_LOGGER_ERROR(logger, "构建效果失败");
return false;
}

Expand All @@ -913,6 +933,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec

// 最后一个效果输出到后缓冲纹理
if (!_effects.back().Build(curTex, _backBuffer)) {
SPDLOG_LOGGER_ERROR(logger, "构建效果失败");
return false;
}
}
Expand Down

0 comments on commit 9e8abd1

Please sign in to comment.