Skip to content

Commit

Permalink
Add LinuxMessageBox class and cross-platform SimpleMessageBox alias
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Jul 26, 2023
1 parent 70f42c6 commit e185847
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
9 changes: 2 additions & 7 deletions Source/Helpers/PatternNotFoundHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
#include <MemorySearch/BytePattern.h>
#include "PatternNotFoundHandler.h"
#include <Platform/Macros/IsPlatform.h>
#include <Platform/SimpleMessageBox.h>

#include <Utils/StringBuilder.h>

#if IS_WIN32()
#include <Platform/Windows/WindowsMessageBox.h>
#endif

struct PatternToString {
explicit PatternToString(BytePattern pattern)
: pattern{ pattern }
Expand Down Expand Up @@ -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());
}
11 changes: 3 additions & 8 deletions Source/Interfaces/InterfaceFinderWithLog.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#pragma once

#include <Platform/Macros/IsPlatform.h>

#if IS_WIN32()
#include <string>
#include <Platform/Windows/WindowsMessageBox.h>
#endif

#include <Platform/SimpleMessageBox.h>

#include "InterfaceFinder.h"

Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions Source/Osiris.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@
<ClInclude Include="Platform\Macros\CallStack.h" />
<ClInclude Include="Platform\Macros\IsPlatform.h" />
<ClInclude Include="Platform\Macros\PlatformSpecific.h" />
<ClInclude Include="Platform\SimpleMessageBox.h" />
<ClInclude Include="Platform\TypeInfoPrecedingVmt.h" />
<ClInclude Include="Platform\Windows\WindowsDynamicLibrary.h" />
<ClInclude Include="Platform\Windows\PebLdr.h" />
Expand Down
3 changes: 3 additions & 0 deletions Source/Osiris.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,9 @@
<ClInclude Include="Platform\Windows\WindowsMessageBox.h">
<Filter>Platform\Windows</Filter>
</ClInclude>
<ClInclude Include="Platform\SimpleMessageBox.h">
<Filter>Platform</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<FxCompile Include="Resources\Shaders\default_vs.hlsl">
Expand Down
27 changes: 27 additions & 0 deletions Source/Platform/Linux/LinuxMessageBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <SDL2/SDL.h>

#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<decltype(&SDL_ShowSimpleMessageBox)>();
};
17 changes: 17 additions & 0 deletions Source/Platform/SimpleMessageBox.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e185847

Please sign in to comment.