Skip to content

Commit

Permalink
III/VC: Improve the way RwD3D8Get/SetRenderState is located
Browse files Browse the repository at this point in the history
Improves compatibility with other mods and provides a safe fallback.

Fixes #65
  • Loading branch information
CookiePLMonster committed Nov 1, 2024
1 parent 2f79eb1 commit 75a3f16
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
5 changes: 4 additions & 1 deletion SilentPatch/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "SVF.h"
#include "ParseUtils.hpp"
#include "Random.h"
#include "RWGTA.h"

#include "Utils/DelimStringReader.h"

Expand Down Expand Up @@ -195,6 +196,8 @@ namespace Common {
using namespace Memory;
using namespace hook::txn;

const bool HasRwD3D8 = RWGTA::Patches::TryLocateRwD3D8();

// Delayed patching
try
{
Expand Down Expand Up @@ -249,7 +252,7 @@ namespace Common {


// Fixed static shadows not rendering under fire and pickups
try
if (HasRwD3D8) try
{
using namespace StaticShadowAlphaFix;

Expand Down
46 changes: 41 additions & 5 deletions SilentPatch/RWGTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define RwEngineInstance (*rwengine)

#include <rwcore.h>
#include "RWGTA.h"

// GTA versions of RenderWare functions/macros for GTA III/Vice City
// since we cannot use RwEngineInstance directly
Expand Down Expand Up @@ -39,14 +40,16 @@ void** rwengine = []() -> void** {
return nullptr;
}();

static void* varRwD3D8SetRenderState = Memory::ReadCallFrom( hook::get_pattern( "0F 8C ? ? ? ? 6A 05 6A 19", 10 ) );
WRAPPER RwBool RwD3D8SetRenderState(RwUInt32 state, RwUInt32 value) { VARJMP(varRwD3D8SetRenderState); }
static decltype(::RwD3D8SetRenderState)* fnRwD3D8SetRenderState;
RwBool RwD3D8SetRenderState(RwUInt32 state, RwUInt32 value)
{
return fnRwD3D8SetRenderState(state, value);
}

static RwUInt32* _rwD3D8RenderStates = *static_cast<RwUInt32**>(Memory::ReadCallFrom( hook::get_pattern( "0F 8C ? ? ? ? 6A 05 6A 19", 10 ), 8 + 3 ));
static decltype(::RwD3D8GetRenderState)* fnRwD3D8GetRenderState;
void RwD3D8GetRenderState(RwUInt32 state, void* value)
{
RwUInt32* valuePtr = static_cast<RwUInt32*>(value);
*valuePtr = _rwD3D8RenderStates[ 2 * state ];
fnRwD3D8GetRenderState(state, value);
}

RwReal RwIm2DGetNearScreenZ()
Expand All @@ -66,3 +69,36 @@ RwBool RwRenderStateSet(RwRenderState state, void *value)

// Unreachable stub
RwBool RwMatrixDestroy(RwMatrix* mpMat) { assert(!"Unreachable!"); return TRUE; }

bool RWGTA::Patches::TryLocateRwD3D8() try
{
using namespace Memory;
using namespace hook::txn;

auto fnRwD3D8SetRenderState = [] {
try {
// Everything except for III Steam
return static_cast<decltype(RwD3D8SetRenderState)*>(get_pattern("39 0C C5 ? ? ? ? 74 31", -8));
} catch (const hook::txn_exception&) {
// III Steam
return static_cast<decltype(RwD3D8SetRenderState)*>(get_pattern("8B 0C C5 ? ? ? ? 3B CA", -8));
}
}();
auto fnRwD3D8GetRenderState = [] {
try {
// Everything except for III Steam
return static_cast<decltype(RwD3D8GetRenderState)*>(get_pattern("8B 0C C5 ? ? ? ? 89 0A C3", -8));
} catch (const hook::txn_exception&) {
// III Steam
return static_cast<decltype(RwD3D8GetRenderState)*>(get_pattern("8B 04 C5 ? ? ? ? 89 02 C3", -8));
}
}();

::fnRwD3D8SetRenderState = fnRwD3D8SetRenderState;
::fnRwD3D8GetRenderState = fnRwD3D8GetRenderState;
return true;
}
catch (const hook::txn_exception&)
{
return false;
}
6 changes: 6 additions & 0 deletions SilentPatch/RWGTA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

namespace RWGTA::Patches
{
bool TryLocateRwD3D8();
}
1 change: 1 addition & 0 deletions SilentPatchIII/SilentPatchIII.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<ClInclude Include="..\SilentPatch\Maths.h" />
<ClInclude Include="..\SilentPatch\ParseUtils.hpp" />
<ClInclude Include="..\SilentPatch\Random.h" />
<ClInclude Include="..\SilentPatch\RWGTA.h" />
<ClInclude Include="..\SilentPatch\StdAfx.h" />
<ClInclude Include="..\SilentPatch\StoredCar.h" />
<ClInclude Include="..\SilentPatch\SVF.h" />
Expand Down
3 changes: 3 additions & 0 deletions SilentPatchIII/SilentPatchIII.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<ClInclude Include="..\SilentPatch\Random.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\SilentPatch\RWGTA.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\SilentPatch\SilentPatch.rc">
Expand Down
1 change: 1 addition & 0 deletions SilentPatchVC/SilentPatchVC.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<ClInclude Include="..\SilentPatch\Maths.h" />
<ClInclude Include="..\SilentPatch\ParseUtils.hpp" />
<ClInclude Include="..\SilentPatch\Random.h" />
<ClInclude Include="..\SilentPatch\RWGTA.h" />
<ClInclude Include="..\SilentPatch\RWUtils.hpp" />
<ClInclude Include="..\SilentPatch\StdAfx.h" />
<ClInclude Include="..\SilentPatch\StoredCar.h" />
Expand Down
3 changes: 3 additions & 0 deletions SilentPatchVC/SilentPatchVC.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<ClInclude Include="EntityVC.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\SilentPatch\RWGTA.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\SilentPatch\Timer.cpp">
Expand Down

0 comments on commit 75a3f16

Please sign in to comment.