From e185847c4e0fbd447777b3fe11641585df1c3021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Krupi=C5=84ski?= Date: Wed, 26 Jul 2023 22:07:52 +0200 Subject: [PATCH] Add LinuxMessageBox class and cross-platform SimpleMessageBox alias --- Source/Helpers/PatternNotFoundHandler.cpp | 9 ++------ Source/Interfaces/InterfaceFinderWithLog.h | 11 +++------ Source/Osiris.vcxproj | 1 + Source/Osiris.vcxproj.filters | 3 +++ Source/Platform/Linux/LinuxMessageBox.h | 27 ++++++++++++++++++++++ Source/Platform/SimpleMessageBox.h | 17 ++++++++++++++ 6 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 Source/Platform/Linux/LinuxMessageBox.h create mode 100644 Source/Platform/SimpleMessageBox.h diff --git a/Source/Helpers/PatternNotFoundHandler.cpp b/Source/Helpers/PatternNotFoundHandler.cpp index b428da6bda3..15ab65d0906 100644 --- a/Source/Helpers/PatternNotFoundHandler.cpp +++ b/Source/Helpers/PatternNotFoundHandler.cpp @@ -5,13 +5,10 @@ #include #include "PatternNotFoundHandler.h" #include +#include #include -#if IS_WIN32() -#include -#endif - struct PatternToString { explicit PatternToString(BytePattern pattern) : pattern{ pattern } @@ -41,7 +38,5 @@ struct PatternToString { void PatternNotFoundHandler::operator()(BytePattern pattern) const { assert(false && "Pattern needs to be updated!"); -#if IS_WIN32() - WindowsMessageBox{}.showWarning("Osiris", StringBuilderStorage<300>{}.builder().put("Failed to find pattern:\n", PatternToString{ pattern }).cstring()); -#endif + SimpleMessageBox{}.showWarning("Osiris", StringBuilderStorage<300>{}.builder().put("Failed to find pattern:\n", PatternToString{ pattern }).cstring()); } diff --git a/Source/Interfaces/InterfaceFinderWithLog.h b/Source/Interfaces/InterfaceFinderWithLog.h index 70f0c3a6b0c..20ab0c8e10a 100644 --- a/Source/Interfaces/InterfaceFinderWithLog.h +++ b/Source/Interfaces/InterfaceFinderWithLog.h @@ -1,11 +1,8 @@ #pragma once -#include - -#if IS_WIN32() #include -#include -#endif + +#include #include "InterfaceFinder.h" @@ -20,9 +17,7 @@ struct InterfaceFinderWithLog { if (const auto foundInterface = finder(name)) return foundInterface; -#if IS_WIN32() - WindowsMessageBox{}.showError("Osiris", ("Failed to find " + std::string{ name } + " interface!").c_str()); -#endif + SimpleMessageBox{}.showError("Osiris", ("Failed to find " + std::string{ name } + " interface!").c_str()); std::exit(EXIT_FAILURE); } diff --git a/Source/Osiris.vcxproj b/Source/Osiris.vcxproj index f8fefdd42fc..5cb852e2098 100644 --- a/Source/Osiris.vcxproj +++ b/Source/Osiris.vcxproj @@ -421,6 +421,7 @@ + diff --git a/Source/Osiris.vcxproj.filters b/Source/Osiris.vcxproj.filters index 4be965a3849..b6e45c70877 100644 --- a/Source/Osiris.vcxproj.filters +++ b/Source/Osiris.vcxproj.filters @@ -1225,6 +1225,9 @@ Platform\Windows + + Platform + diff --git a/Source/Platform/Linux/LinuxMessageBox.h b/Source/Platform/Linux/LinuxMessageBox.h new file mode 100644 index 00000000000..abf07bf7af8 --- /dev/null +++ b/Source/Platform/Linux/LinuxMessageBox.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "LinuxDynamicLibrary.h" + +class LinuxMessageBox { +public: + void showWarning(const char* title, const char* message) const noexcept + { + showMessageBox(title, message, SDL_MESSAGEBOX_WARNING); + } + + void showError(const char* title, const char* message) const noexcept + { + showMessageBox(title, message, SDL_MESSAGEBOX_ERROR); + } + +private: + void showMessageBox(const char* title, const char* message, unsigned int flags) const noexcept + { + if (showSimpleMessageBox) + showSimpleMessageBox(flags, title, message, nullptr); + } + + decltype(&SDL_ShowSimpleMessageBox) showSimpleMessageBox = LinuxDynamicLibrary{ "libSDL2-2.0.so.0" }.getFunctionAddress("SDL_ShowSimpleMessageBox").as(); +}; diff --git a/Source/Platform/SimpleMessageBox.h b/Source/Platform/SimpleMessageBox.h new file mode 100644 index 00000000000..03ccb5d975d --- /dev/null +++ b/Source/Platform/SimpleMessageBox.h @@ -0,0 +1,17 @@ +#pragma once + +#include "Macros/IsPlatform.h" + +#if IS_WIN32() || IS_WIN64() + +#include "Windows/WindowsMessageBox.h" + +using SimpleMessageBox = WindowsMessageBox; + +#elif IS_LINUX() + +#include "Linux/LinuxMessageBox.h" + +using SimpleMessageBox = LinuxMessageBox; + +#endif