From 140c8217f67169688e3e5778e9a5cec5ba469dad Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sun, 12 Dec 2021 12:04:13 -0800 Subject: [PATCH] Common: Create HRWrap HRWrap now allows HRESULT to be formatted, giving useful information beyond "it failed" or a hex code that isn't obvious to most users. This commit does not add any uses of it, though. --- Source/Core/Common/CMakeLists.txt | 6 +++++- Source/Core/Common/HRWrap.cpp | 17 ++++++++++++++++ Source/Core/Common/HRWrap.h | 33 +++++++++++++++++++++++++++++++ Source/Core/DolphinLib.props | 2 ++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Source/Core/Common/HRWrap.cpp create mode 100644 Source/Core/Common/HRWrap.h diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 5314f119f0..3ede46e870 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -164,7 +164,7 @@ elseif(WIN32) winmm.lib ) if (_M_X86_64) - target_link_libraries(common PRIVATE opengl32.lib) + target_link_libraries(common PRIVATE opengl32.lib) endif() elseif (ANDROID) target_link_libraries(common @@ -286,6 +286,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux") target_link_libraries(common PUBLIC dl rt) endif() +if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + target_sources(common PUBLIC HRWrap.h HRWrap.cpp) +endif() + if(USE_UPNP) target_link_libraries(common PRIVATE Miniupnpc::miniupnpc) endif() diff --git a/Source/Core/Common/HRWrap.cpp b/Source/Core/Common/HRWrap.cpp new file mode 100644 index 0000000000..cfbd0e4a87 --- /dev/null +++ b/Source/Core/Common/HRWrap.cpp @@ -0,0 +1,17 @@ +// Copyright 2021 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "HRWrap.h" + +#include +#include "Common/StringUtil.h" + +namespace Common +{ +std::string GetHResultMessage(HRESULT hr) +{ + // See https://stackoverflow.com/a/7008111 + _com_error err(hr); + return TStrToUTF8(err.ErrorMessage()); +} +} // namespace Common diff --git a/Source/Core/Common/HRWrap.h b/Source/Core/Common/HRWrap.h new file mode 100644 index 0000000000..71c3bfed87 --- /dev/null +++ b/Source/Core/Common/HRWrap.h @@ -0,0 +1,33 @@ +// Copyright 2021 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include + +namespace Common +{ +std::string GetHResultMessage(HRESULT hr); + +// Wrapper for HRESULT to be used with fmt. Note that we can't create a fmt::formatter directly +// for HRESULT as HRESULT is simply a typedef on long and not a distinct type. +struct HRWrap +{ + constexpr explicit HRWrap(HRESULT hr) : m_hr(hr) {} + const HRESULT m_hr; +}; +} // namespace Common + +template <> +struct fmt::formatter +{ + constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); } + template + auto format(const Common::HRWrap& hr, FormatContext& ctx) + { + return fmt::format_to(ctx.out(), "{} ({:#010x})", Common::GetHResultMessage(hr.m_hr), + static_cast(hr.m_hr)); + } +}; diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index a6271664b4..c0d465aafe 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -107,6 +107,7 @@ + @@ -707,6 +708,7 @@ +