Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
[cheat] Added support for C++ exceptions when using manual mapping.
Browse files Browse the repository at this point in the history
- The cheat should no longer crash when exception is catched.
- Note that the cheat will still crash if try&catch block is triggered before DllMain, e.g. inside a constructor.
  • Loading branch information
oxiKKK committed Apr 23, 2023
1 parent ccbb79f commit c29da51
Show file tree
Hide file tree
Showing 23 changed files with 182 additions and 89 deletions.
16 changes: 8 additions & 8 deletions src/assets/assets.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; M_ASSETS;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> M_ASSETS;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)src/public/;</AdditionalIncludeDirectories>
<TreatWarningAsError>false</TreatWarningAsError>
<ExceptionHandling>Sync</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
Expand All @@ -201,16 +201,16 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; M_ASSETS;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> M_ASSETS;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)src/public/;</AdditionalIncludeDirectories>
<TreatWarningAsError>false</TreatWarningAsError>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand All @@ -231,16 +231,16 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; M_ASSETS;_RETAIL;NDEBUG;WIN32;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> M_ASSETS;_RETAIL;NDEBUG;WIN32;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)src/public/;</AdditionalIncludeDirectories>
<TreatWarningAsError>false</TreatWarningAsError>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand Down
16 changes: 8 additions & 8 deletions src/cheat/cheater.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; M_CHEATER;WIN32;_DEBUG;CHEATER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> M_CHEATER;WIN32;_DEBUG;CHEATER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>precompiled.h</PrecompiledHeaderFile>
Expand All @@ -141,7 +141,7 @@
<SupportJustMyCode>false</SupportJustMyCode>
<ShowIncludes>false</ShowIncludes>
<TreatWarningAsError>false</TreatWarningAsError>
<ExceptionHandling>Sync</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
Expand All @@ -161,17 +161,17 @@
</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; M_CHEATER;WIN32;NDEBUG;CHEATER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> M_CHEATER;WIN32;NDEBUG;CHEATER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>precompiled.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(ProjectDir);$(SolutionDir)src/public/;$(SolutionDir)src/external/imgui/;$(SolutionDir)src/external/phnt/;$(SolutionDir)src/external/;$(SolutionDir)src/external/glfw/include/</AdditionalIncludeDirectories>
<ShowIncludes>false</ShowIncludes>
<TreatWarningAsError>false</TreatWarningAsError>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand All @@ -191,17 +191,17 @@
</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; M_CHEATER;_RETAIL;NDEBUG;WIN32;CHEATER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> M_CHEATER;_RETAIL;NDEBUG;WIN32;CHEATER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>precompiled.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(ProjectDir);$(SolutionDir)src/public/;$(SolutionDir)src/external/imgui/;$(SolutionDir)src/external/phnt/;$(SolutionDir)src/external/;$(SolutionDir)src/external/glfw/include/</AdditionalIncludeDirectories>
<ShowIncludes>false</ShowIncludes>
<TreatWarningAsError>false</TreatWarningAsError>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand Down
56 changes: 56 additions & 0 deletions src/cheat/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,65 @@ extern "C" DLLEXPORT bool CommunicativeDllEntryPoint(injector_information_packag

//-------------------------------------------------------------------------------

#include "tier/MessageBox.h"

bool insert_inverted_function_table(uintptr_t* module_base)
{
// byte pattern for RtlInsertInvertedFunctionTable inside ntdll.
CBytePattern RltIIFT_pattern("\x8B\xFF\x55\x8B\xEC\x83\xEC\x0C\x53\x56\x57\x8D\x45\xF8\x8B\xFA");

DWORD ntdll_base = (DWORD)GetModuleHandleA("ntdll.dll");
DWORD ntdll_base_code = (DWORD)((PIMAGE_NT_HEADERS)((uint8_t*)ntdll_base + ((PIMAGE_DOS_HEADER)ntdll_base)->e_lfanew))->OptionalHeader.BaseOfCode;
DWORD size_of_ntdll_image_code = (DWORD)((PIMAGE_NT_HEADERS)((uint8_t*)ntdll_base + ((PIMAGE_DOS_HEADER)ntdll_base)->e_lfanew))->OptionalHeader.SizeOfCode;
DWORD size_of_ntdll_image = (DWORD)((PIMAGE_NT_HEADERS)((uint8_t*)ntdll_base + ((PIMAGE_DOS_HEADER)ntdll_base)->e_lfanew))->OptionalHeader.SizeOfImage;

// Note that this function's declaration changes rapidly through various windows versions.
// On windows 7, this function has three parameters, but on windows 10 it has only two.
// The byte pattern for this function may change often, too...
//
// This function is normally called by the internal native loader api when loading a dll.
// Without this function call, we aren't able to use C++ exceptions inside of our code.
void(__fastcall * RtlInsertInvertedFunctionTable)(DWORD ImageBase, DWORD SizeOfImage);
RtlInsertInvertedFunctionTable = (decltype(RtlInsertInvertedFunctionTable))RltIIFT_pattern.search_in_loaded_address_space(ntdll_base, ntdll_base + size_of_ntdll_image);

if (RtlInsertInvertedFunctionTable)
{
DWORD size_of_image_current = (DWORD)((PIMAGE_NT_HEADERS)((uint8_t*)module_base + ((PIMAGE_DOS_HEADER)module_base)->e_lfanew))->OptionalHeader.SizeOfImage;
RtlInsertInvertedFunctionTable((DWORD)module_base, size_of_image_current);
}
else
{
CMessageBox::display_error("Couldn't find RtlInsertInvertedFunctionTable function. This function is mandatory. Aborting injection...");
return false;
}

return true;
}

BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
//
// Enable C++ exceptions on x86 module
//
if (!insert_inverted_function_table((uintptr_t*)hModule))
{
return false;
}

#if 0
try
{
throw std::invalid_argument("received negative value");
}
catch (...)
{
CMessageBox::display_error("error");
}
#endif

// TODO: Implement also removing exception function table RtlRemoveInvertedFunctionTable.

return TRUE;
}
22 changes: 14 additions & 8 deletions src/cheat/game/SpriteManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,23 +456,29 @@ void CSpriteMgr::update()

Sprite_t* CSpriteMgr::get_sprite(const std::string& name) const
{
auto sprite = &s_sprite_map.at(name);
if (!sprite)
Sprite_t* sprite = nullptr;
try
{
CConsole::the().error("Couldn't find sprite '{}'!", name);
return nullptr;
sprite = &s_sprite_map.at(name);
}
catch (...)
{
CConsole::the().error("Tried to find unknown sprite: '{}'", name);
}

return sprite;
}

SpriteAtlas_t* CSpriteMgr::get_sprite_atlas(const std::string& name) const
{
auto sprite_atlas = &s_sprite_atlas_map.at(name);
if (!sprite_atlas)
SpriteAtlas_t* sprite_atlas = nullptr;
try
{
sprite_atlas = &s_sprite_atlas_map.at(name);
}
catch (...)
{
CConsole::the().error("Couldn't find sprite atlas '{}'!", name);
return nullptr;
CConsole::the().error("Tried to find unknown sprite atlas: '{}'", name);
}

return sprite_atlas;
Expand Down
16 changes: 15 additions & 1 deletion src/cheat/ui/OxWareUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,21 @@ void COxWareUI::handle_ingame_mouseevents()
// SDL_SetRelativeMouseMode with bad state. Therefore, the function wasn't executed, because the relative mode
// wasn't "on", having the mouse cursor stuck, until the function (BaseUISurface::SetCursor) weren't ran again
// (by calling CBaseUI::ActivateGameUI)..
SDL_SetRelativeMouseMode(!m_is_any_interactible_rendering_context_active);
if (gameuifuncs)
{
if (gameuifuncs->IsGameUIActive())
{
// without this the cursor stays hidden if we're displaying GameUI and we close the UI.
if (m_is_any_interactible_rendering_context_active)
{
SDL_SetRelativeMouseMode(FALSE);
}
}
else
{
SDL_SetRelativeMouseMode(!m_is_any_interactible_rendering_context_active);
}
}

last = m_is_any_interactible_rendering_context_active;
}
Expand Down
16 changes: 8 additions & 8 deletions src/external/detours/detours.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<SupportJustMyCode>false</SupportJustMyCode>
<ExceptionHandling>Sync</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
Expand All @@ -167,13 +167,13 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand All @@ -194,13 +194,13 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; _RETAIL;WIN32;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> _RETAIL;WIN32;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand Down
16 changes: 8 additions & 8 deletions src/external/imgui/imgui.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)src/public/;$(SolutionDir)src/external/imgui/;$(SolutionDir)src/external/glfw/include/</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<SupportJustMyCode>false</SupportJustMyCode>
<ExceptionHandling>Sync</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
Expand All @@ -178,15 +178,15 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)src/public/;$(SolutionDir)src/external/imgui/;$(SolutionDir)src/external/glfw/include/</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand All @@ -207,15 +207,15 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0; _RETAIL;WIN32;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions> _RETAIL;WIN32;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)src/public/;$(SolutionDir)src/external/imgui/;$(SolutionDir)src/external/glfw/include/</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<ExceptionHandling>false</ExceptionHandling>
<ExceptionHandling>Async</ExceptionHandling>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FloatingPointExceptions>false</FloatingPointExceptions>
<FloatingPointExceptions>true</FloatingPointExceptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
Expand Down
Loading

0 comments on commit c29da51

Please sign in to comment.