From 8e342de77a62fa8c4ee4ae7d159eb0de30fb0d4c Mon Sep 17 00:00:00 2001 From: Harry Potter Date: Sun, 9 Dec 2018 18:54:44 +0100 Subject: [PATCH 1/5] Changed class name --- src/Smooth Interior Camera/Game/Hooks.cpp | 10 +++++----- src/Smooth Interior Camera/Game/prism.hpp | 6 ++++-- src/Smooth Interior Camera/Mod/Camera.hpp | 6 +++--- src/Smooth Interior Camera/Mod/Config.hpp | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Smooth Interior Camera/Game/Hooks.cpp b/src/Smooth Interior Camera/Game/Hooks.cpp index 0b6cffd..681ebd5 100644 --- a/src/Smooth Interior Camera/Game/Hooks.cpp +++ b/src/Smooth Interior Camera/Game/Hooks.cpp @@ -28,8 +28,8 @@ namespace Hooks void __cdecl CameraEvent(uintptr_t gameCamAddr) { - auto pGameCam = reinterpret_cast(gameCamAddr + gameCamOffset); - auto pGameCamPos = reinterpret_cast(gameCamAddr + gameCamPosOffset); + auto pGameCam = reinterpret_cast(gameCamAddr + gameCamOffset); + auto pGameCamPos = reinterpret_cast(gameCamAddr + gameCamPosOffset); auto pCam = g_pMod->GetCamera(); pCam->UpdateGameCamera(pGameCamPos); @@ -46,7 +46,7 @@ namespace Hooks { for (short i = 0; i < 6; ++i) { - if (floatEquals(pGameCam->m_rxEnd, Config::Get()->GetDefaultValue((Config::GameCameraPos)i))) + if (floatEquals(pGameCam->m_rxEnd, Config::Get()->GetDefaultValue((Config::InteriorCameraPos)i))) { g_pMod->Log(SCS_LOG_TYPE_message, "New value for [%d] %f is %f", i, Config::Get()->m_interiorCamPos[i], pGameCamPos->m_rx); Config::Get()->m_interiorCamPos[i] = pGameCamPos->m_rx; @@ -66,9 +66,9 @@ namespace Hooks for (short i = 0; i < 6; ++i) { - if (floatEquals(pGameCam->m_rxEnd, Config::Get()->GetDefaultValue((Config::GameCameraPos)i))) + if (floatEquals(pGameCam->m_rxEnd, Config::Get()->GetDefaultValue((Config::InteriorCameraPos)i))) { - rx = Config::Get()->GetValue((Config::GameCameraPos)i); + rx = Config::Get()->GetValue((Config::InteriorCameraPos)i); #ifdef TESTING std::cout << "New value for '" << pGameCam->m_rxEnd << "' is '" << rx << "'\n"; diff --git a/src/Smooth Interior Camera/Game/prism.hpp b/src/Smooth Interior Camera/Game/prism.hpp index cf0a5f2..cc926f9 100644 --- a/src/Smooth Interior Camera/Game/prism.hpp +++ b/src/Smooth Interior Camera/Game/prism.hpp @@ -9,18 +9,20 @@ namespace prism { - class GameCamera + class InteriorCamera { public: unsigned m_keyboardEv; // +0 float m_rxEnd; // +4 float m_ryEnd; // +8 + // +12 }; - class GameCameraPos + class InteriorCameraPos { public: float m_rx; // +0 float m_ry; // +4 + // +8 }; } diff --git a/src/Smooth Interior Camera/Mod/Camera.hpp b/src/Smooth Interior Camera/Mod/Camera.hpp index 50fb250..5961db8 100644 --- a/src/Smooth Interior Camera/Mod/Camera.hpp +++ b/src/Smooth Interior Camera/Mod/Camera.hpp @@ -10,7 +10,7 @@ class Camera { private: - prism::GameCameraPos *m_pGameCamera = nullptr; + prism::InteriorCameraPos *m_pGameCamera = nullptr; float m_rxCurr = 0.f; float m_rxStart = 0.f; @@ -34,9 +34,9 @@ class Camera void MoveTo(float rx); void UpdateRX(float rx) { m_rxCurr = rx; } - void UpdateGameCamera(prism::GameCameraPos *gameCamera) { m_pGameCamera = gameCamera; } + void UpdateGameCamera(prism::InteriorCameraPos *gameCamera) { m_pGameCamera = gameCamera; } - prism::GameCameraPos *GetGameCamera() { return m_pGameCamera; } + prism::InteriorCameraPos *GetGameCamera() { return m_pGameCamera; } void Pulse(); diff --git a/src/Smooth Interior Camera/Mod/Config.hpp b/src/Smooth Interior Camera/Mod/Config.hpp index 12cc48b..dacef14 100644 --- a/src/Smooth Interior Camera/Mod/Config.hpp +++ b/src/Smooth Interior Camera/Mod/Config.hpp @@ -32,7 +32,7 @@ class Config float m_interiorCamPos[6]; - enum GameCameraPos + enum InteriorCameraPos { INTERIOR_LOOK_FORWARD = 0, INTERIOR_LOOK_UP_RIGHT, From b78d20ac28b57191db0535dee8062b8e443508ed Mon Sep 17 00:00:00 2001 From: Harry Potter Date: Wed, 19 Dec 2018 20:05:41 +0100 Subject: [PATCH 2/5] Micro optimization --- src/Smooth Interior Camera/Game/Hooks.cpp | 2 -- src/Smooth Interior Camera/Game/HooksASM.asm | 3 +-- src/Smooth Interior Camera/Memory/MemMgr.cpp | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Smooth Interior Camera/Game/Hooks.cpp b/src/Smooth Interior Camera/Game/Hooks.cpp index 681ebd5..60d1117 100644 --- a/src/Smooth Interior Camera/Game/Hooks.cpp +++ b/src/Smooth Interior Camera/Game/Hooks.cpp @@ -107,9 +107,7 @@ namespace Hooks __asm { pushad - push ecx call CameraEvent_Address - add esp, 4 popad mov esp, ebp diff --git a/src/Smooth Interior Camera/Game/HooksASM.asm b/src/Smooth Interior Camera/Game/HooksASM.asm index 752c0a7..759e568 100644 --- a/src/Smooth Interior Camera/Game/HooksASM.asm +++ b/src/Smooth Interior Camera/Game/HooksASM.asm @@ -18,8 +18,7 @@ Asm_CameraEvent PROC push r8 ; call library function - mov rax, CameraEvent_Address - call rax + call CameraEvent_Address ; restore registers pop r8 diff --git a/src/Smooth Interior Camera/Memory/MemMgr.cpp b/src/Smooth Interior Camera/Memory/MemMgr.cpp index cad8d2e..159dd3a 100644 --- a/src/Smooth Interior Camera/Memory/MemMgr.cpp +++ b/src/Smooth Interior Camera/Memory/MemMgr.cpp @@ -12,7 +12,7 @@ #ifdef X64 /* push rax - mov rax addr ; @addr - address to original + mov rax. [addr] ; @addr - address to original xchg qword ptr ss:[rsp], rax ret */ From d7f45da9c62cb3945cc83496a44d233fbd2b9c6c Mon Sep 17 00:00:00 2001 From: Harry Potter Date: Wed, 19 Dec 2018 20:07:04 +0100 Subject: [PATCH 3/5] Fixed unhooking --- src/Smooth Interior Camera/Game/Hooks.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Smooth Interior Camera/Game/Hooks.cpp b/src/Smooth Interior Camera/Game/Hooks.cpp index 60d1117..150904f 100644 --- a/src/Smooth Interior Camera/Game/Hooks.cpp +++ b/src/Smooth Interior Camera/Game/Hooks.cpp @@ -90,6 +90,8 @@ namespace Hooks auto CameraEvent_pattern = "8B 81 ?? ?? 00 00 89 81 ?? ?? 00 00 8B 81 ?? ?? 00 00 89 81 ?? ?? 00 00 C7 81 ?? ?? 00 00 00 00 00 00"; + uint8_t baseBytes[34] = { 0 }; + #if defined(X64) extern "C" @@ -133,7 +135,17 @@ namespace Hooks gameCamOffset = *reinterpret_cast(CameraEvent_addr + 2) - 4; gameCamPosOffset = *reinterpret_cast(CameraEvent_addr + 8); + #ifdef TESTING printf("Offsets: %i %i\n", gameCamOffset, gameCamPosOffset); + printf("Number of bytes to backup: %lld\n", sizeof(baseBytes)); + #endif + // backup bytes + for (int i = 0; i < sizeof(baseBytes); ++i) + { + baseBytes[i] = *reinterpret_cast(CameraEvent_addr + i); + } + + CameraEvent_Address = reinterpret_cast(CameraEvent); MemMgr::JmpHook(CameraEvent_addr, (uintptr_t)Asm_CameraEvent); @@ -176,7 +188,11 @@ namespace Hooks std::cout << "Unhooking...\n"; #endif - memcpy((uint8_t*)CameraEvent_addr, CameraEvent_pattern, sizeof(CameraEvent_pattern)); + // restore bytes + for (int i = 0; i < sizeof(baseBytes); ++i) + { + *reinterpret_cast(CameraEvent_addr + i) = baseBytes[i]; + } } } } From b3c231d22db59492f1a1ad4463b4baf30dde7a5b Mon Sep 17 00:00:00 2001 From: Harry Potter Date: Wed, 19 Dec 2018 20:55:31 +0100 Subject: [PATCH 4/5] Improvements for compatibility with future updates for ETS2/ATS --- src/Smooth Interior Camera/Game/Hooks.cpp | 9 +++++---- src/Smooth Interior Camera/Game/HooksASM.asm | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Smooth Interior Camera/Game/Hooks.cpp b/src/Smooth Interior Camera/Game/Hooks.cpp index 150904f..e72d931 100644 --- a/src/Smooth Interior Camera/Game/Hooks.cpp +++ b/src/Smooth Interior Camera/Game/Hooks.cpp @@ -97,12 +97,14 @@ namespace Hooks extern "C" { uintptr_t CameraEvent_Address = 0; + uintptr_t CameraEvent_RetnAddress = 0; void Asm_CameraEvent(); } #elif defined(X86) uintptr_t CameraEvent_Address = 0; + uintptr_t CameraEvent_RetnAddress = 0; void __declspec(naked) Asm_CameraEvent() { @@ -112,9 +114,7 @@ namespace Hooks call CameraEvent_Address popad - mov esp, ebp - pop ebp - ret + jmp CameraEvent_RetnAddress } } @@ -145,9 +145,10 @@ namespace Hooks baseBytes[i] = *reinterpret_cast(CameraEvent_addr + i); } - + MemMgr::UnprotectMemory(CameraEvent_addr, sizeof(baseBytes)); CameraEvent_Address = reinterpret_cast(CameraEvent); + CameraEvent_RetnAddress = CameraEvent_addr + sizeof(baseBytes); MemMgr::JmpHook(CameraEvent_addr, (uintptr_t)Asm_CameraEvent); return true; diff --git a/src/Smooth Interior Camera/Game/HooksASM.asm b/src/Smooth Interior Camera/Game/HooksASM.asm index 759e568..7fd3951 100644 --- a/src/Smooth Interior Camera/Game/HooksASM.asm +++ b/src/Smooth Interior Camera/Game/HooksASM.asm @@ -6,6 +6,7 @@ IFDEF RAX extern CameraEvent_Address: qword +extern CameraEvent_RetnAddress: qword .code @@ -26,9 +27,8 @@ Asm_CameraEvent PROC pop rcx pop rax - add rsp, 38h - ret - + ; jump to End + jmp CameraEvent_RetnAddress Asm_CameraEvent ENDP ENDIF From 66b5fe6f7d29b983c413ddbf815a65f2492e1e81 Mon Sep 17 00:00:00 2001 From: Harry Potter Date: Wed, 19 Dec 2018 21:02:22 +0100 Subject: [PATCH 5/5] 1.3.1 --- src/Smooth Interior Camera/Game/Hooks.cpp | 3 ++- src/Smooth Interior Camera/Version.hpp | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Smooth Interior Camera/Game/Hooks.cpp b/src/Smooth Interior Camera/Game/Hooks.cpp index e72d931..e021a17 100644 --- a/src/Smooth Interior Camera/Game/Hooks.cpp +++ b/src/Smooth Interior Camera/Game/Hooks.cpp @@ -138,7 +138,8 @@ namespace Hooks #ifdef TESTING printf("Offsets: %i %i\n", gameCamOffset, gameCamPosOffset); printf("Number of bytes to backup: %lld\n", sizeof(baseBytes)); - #endif + #endif + // backup bytes for (int i = 0; i < sizeof(baseBytes); ++i) { diff --git a/src/Smooth Interior Camera/Version.hpp b/src/Smooth Interior Camera/Version.hpp index 79af6b1..888fe3e 100644 --- a/src/Smooth Interior Camera/Version.hpp +++ b/src/Smooth Interior Camera/Version.hpp @@ -1,8 +1,8 @@ #pragma once -#define CURRENT_VERSION "1.3.0.0" -#define CURRENT_VERSION_SHORT 1300 -#define CURRENT_VERSION_NUMBER 1,3,0,0 +#define CURRENT_VERSION "1.3.1.0" +#define CURRENT_VERSION_SHORT 1310 +#define CURRENT_VERSION_NUMBER 1,3,1,0 #ifdef _WIN64