From 90339aa980cf70ed3d74aa844f1e45586bb59993 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 3 Feb 2019 00:33:17 +0100 Subject: [PATCH] Revisited and cleaned up lightbeam fix --- SilentPatchSA/SilentPatchSA.cpp | 122 +++++++++++++++++++------------- 1 file changed, 72 insertions(+), 50 deletions(-) diff --git a/SilentPatchSA/SilentPatchSA.cpp b/SilentPatchSA/SilentPatchSA.cpp index d550b89..2d02755 100644 --- a/SilentPatchSA/SilentPatchSA.cpp +++ b/SilentPatchSA/SilentPatchSA.cpp @@ -1462,6 +1462,52 @@ namespace VariableResets } +namespace LightbeamFix +{ + template + class RenderStateWrapper + { + private: + static inline void* SavedState; + + static void PushState( RwRenderState state, void* value ) + { + assert( State == state ); + RwRenderStateGet( state, &SavedState ); + RwRenderStateSet( state, value ); + } + static inline const auto pPushState = &PushState; + + static void PopState( RwRenderState state, void* ) + { + assert( State == state ); + RwRenderStateSet( state, SavedState ); + + // Restore states R* did not restore after changing them + if constexpr ( State == rwRENDERSTATEDESTBLEND ) + { + RenderStateWrapper::PopAnotherState(); + } + if constexpr ( State == rwRENDERSTATECULLMODE ) + { + RenderStateWrapper::PopAnotherState(); + RenderStateWrapper::PopAnotherState(); + } + } + static inline const auto pPopState = &PopState; + + public: + static void PopAnotherState() + { + PopState( State, nullptr ); + } + + static inline const uintptr_t PushStatePPtr = reinterpret_cast(&pPushState) - 0x20; + static inline const uintptr_t PopStatePPtr = reinterpret_cast(&pPopState) - 0x20; + }; + +} + #ifndef NDEBUG // ============= QPC spoof for verifying high timer issues ============= @@ -1636,42 +1682,6 @@ void __declspec(naked) UserTracksFix_Steam() } } -void __declspec(naked) ResetAlphaFuncRefAfterRender() -{ - _asm - { - mov edx, [rwengine] - mov edx, [edx] - mov ecx, [esp+7Ch-74h] - push ecx - push rwRENDERSTATEALPHATESTFUNCTIONREF - call dword ptr [edx+20h] - add esp, 8 - pop edi - pop esi - add esp, 74h - retn - } -} - -void __declspec(naked) ResetAlphaFuncRefAfterRender_Steam() -{ - _asm - { - mov edx, [rwengine] - mov edx, [edx] - mov ecx, [esp+80h-74h] - push ecx - push rwRENDERSTATEALPHATESTFUNCTIONREF - call dword ptr [edx+20h] - add esp, 8 - pop edi - pop esi - add esp, 78h - retn - } -} - static void* PlaneAtomicRendererSetup_JumpBack = AddressByVersion(0x4C7986, 0x4C7A06, 0x4D2275); static void* RenderVehicleHiDetailAlphaCB_BigVehicle = AddressByVersion(0x734370, 0x734BA0, 0x76E400); static void* RenderVehicleHiDetailCB_BigVehicle = AddressByVersion(0x733420, 0x733C50, 0x76D6C0); @@ -3176,22 +3186,28 @@ void Patch_SA_10() //Patch(0x5B3ADD, 4); // Lightbeam fix - Nop(0x6A2E95, 3); - Patch(0x6E0F63, 0x0AEB); - Patch(0x6E0F7C, 0x0BEB); - Patch(0x6E0F95, 0x0BEB); - Patch(0x6E0FAF, 0x1AEB); + { + using namespace LightbeamFix; - Patch(0x6E13D5, 0x09EB); - Patch(0x6E13ED, 0x17EB); - Patch(0x6E141F, 0x0AEB); + Patch( 0x6E0F37 + 2, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0F63 + 1, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0F6F + 2, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0F7C + 2, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0F89 + 1, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0F95 + 2, &RenderStateWrapper::PushStatePPtr ); + // rwRENDERSTATETEXTURERASTER not saved + Patch( 0x6E0FAF + 1, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0FBB + 2, &RenderStateWrapper::PushStatePPtr ); + Patch( 0x6E0FCB + 2, &RenderStateWrapper::PushStatePPtr ); - Patch(0x6E0FE0, 0x28); - Patch(0x6E142D, 0x18); - Patch(0x6E0FDB, 0xC8-0x7C); - //InjectHook(0x6A2EDA, CullTest); - - InjectHook(0x6A2EF7, ResetAlphaFuncRefAfterRender, PATCH_JUMP); + // rwRENDERSTATETEXTURERASTER not saved + Patch( 0x6E13E0 + 2, &RenderStateWrapper::PopStatePPtr ); + Patch( 0x6E13ED + 2, &RenderStateWrapper::PopStatePPtr ); + Patch( 0x6E13FA + 1, &RenderStateWrapper::PopStatePPtr ); + Patch( 0x6E1406 + 2, &RenderStateWrapper::PopStatePPtr ); + Patch( 0x6E1413 + 2, &RenderStateWrapper::PopStatePPtr ); + Patch( 0x6E141F + 1, &RenderStateWrapper::PopStatePPtr ); + } // PS2 SUN!!!!!!!!!!!!!!!!! Nop(0x6FB17C, 3); @@ -3820,6 +3836,8 @@ void Patch_SA_11() InjectHook(0x7142FB, HandleMoonStuffStub, PATCH_JUMP); // Lightbeam fix + // Removed in Build 30 because the fix has been revisited + /* Nop(0x6A36B5, 3); Patch(0x6E1793, 0x0AEB); Patch(0x6E17AC, 0x0BEB); @@ -3835,6 +3853,7 @@ void Patch_SA_11() Patch(0x6E180B, 0xC8-0x7C); InjectHook(0x6A3717, ResetAlphaFuncRefAfterRender, PATCH_JUMP); + */ // PS2 SUN!!!!!!!!!!!!!!!!! Nop(0x6FB9AC, 3); @@ -4162,6 +4181,8 @@ void Patch_SA_Steam() InjectHook(0x72F058, HandleMoonStuffStub_Steam, PATCH_JUMP); // Lightbeam fix + // Removed in Build 30 because the fix has been revisited + /* Patch(0x6CFEF9, 0x10EB); Nop(0x6CFF0F, 3); Patch(0x71D1F5, 0x0DEB); @@ -4179,6 +4200,7 @@ void Patch_SA_Steam() //InjectHook(0x6A2EDA, CullTest); InjectHook(0x6CFF69, ResetAlphaFuncRefAfterRender_Steam, PATCH_JUMP); + */ // PS2 SUN!!!!!!!!!!!!!!!!! Nop(0x73362F, 2);