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

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Hary309 committed Nov 26, 2018
2 parents 38bb575 + b367747 commit c273337
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 242 deletions.
5 changes: 1 addition & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
url = git://github.com/nlhans/ets2-sdk-plugin.git
[submodule "src/vendor/json"]
path = src/vendor/json
url = git://github.com/nlohmann/json.git
[submodule "src/vendor/jitasm"]
path = src/vendor/jitasm
url = git://github.com/hikaru-i/jitasm.git
url = git://github.com/nlohmann/json.git
12 changes: 4 additions & 8 deletions src/Smooth Interior Camera/Common.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : Common.cpp
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file Common.cpp
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#include "Common.h"

Expand Down
12 changes: 4 additions & 8 deletions src/Smooth Interior Camera/Common.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : Common.h
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file Common.h
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#pragma once

Expand Down
98 changes: 34 additions & 64 deletions src/Smooth Interior Camera/Game/Hooks.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : Hooks.cpp
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file Hooks.cpp
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#include "Hooks.h"

Expand All @@ -14,15 +10,14 @@
#include <sstream>
#include <iomanip>

#include <jitasm.h>

#include "Memory/Hooking.Patterns.h"
#include "Memory/MemMgr.h"
#include "Mod/Mod.h"
#include "Mod/Camera.h"
#include "Mod/Config.h"
#include "Game/prism.h"
#include "Common.h"
#include "Version.h"

namespace Hooks
{
Expand All @@ -42,7 +37,7 @@ namespace Hooks
pGameCam->m_ry = pGameCam->m_ry_predef;
pGameCam->m_keyboardEv = false;
}
else if (g_pMod->IsConfigurating())
else if (g_pMod->IsConfiguring())
{
if (pGameCam->m_keyboardEv)
{
Expand Down Expand Up @@ -72,9 +67,9 @@ namespace Hooks
{
rx = Config::Get()->GetValue((Config::CameraPos)i);

#ifdef TESTING
#ifdef TESTING
std::cout << "New value for '" << pGameCam->m_rx_predef << "' is '" << rx << "'\n";
#endif
#endif

break;
}
Expand All @@ -88,88 +83,63 @@ namespace Hooks
}
}

struct : jitasm::Frontend
{
uintptr_t callAddr;

void InternalMain() override
{
push(rax);
push(rcx);
push(rdx);
push(r8);

{
mov(rax, callAddr);
call(rax);
}

pop(r8);
pop(rdx);
pop(rcx);
pop(rax);
uintptr_t CameraEvent_addr;

add(rsp, 0x38);
ret();
}
} Asm_CameraEvent;
#if defined(HOOK_V1)
auto CameraEvent_pattern = "8B 81 B0 02 00 00 89 81 48 03 00 00 8B 81 B4 02 00 00 89 81 4C 03 00 00 C7 81 AC 02 00 00 00 00 00 00";
#elif defined(HOOK_V2)
auto CameraEvent_pattern = "8B 81 B8 02 00 00 89 81 50 03 00 00 8B 81 BC 02 00 00 89 81 54 03 00 00 C7 81 B4 02 00 00 00 00 00 00";
#endif

uintptr_t CameraEvent_addr;
uint8_t CameraEvent_pattern[] = { 0x8B, 0x81, 0xB0, 0x02, 0x00, 0x00, 0x89, 0x81, 0x48, 0x03, 0x00, 0x00, 0x8B, 0x81, 0xB4, 0x02, 0x00, 0x00, 0x89, 0x81, 0x4C, 0x03, 0x00, 0x00, 0xC7, 0x81, 0xAC, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
extern "C"
{
ptrdiff_t CameraEvent_Address = 0;
void Asm_CameraEvent();
}

bool Hook_CameraEvent()
{
std::stringstream patternStr;

for (int i = 0; i < sizeof(CameraEvent_pattern); ++i)
{
patternStr << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(CameraEvent_pattern[i]) << " ";
}

auto pattern = hook::pattern(static_cast<const char*>(patternStr.str().c_str()));
auto pattern = hook::pattern(CameraEvent_pattern);

if (pattern.size() > 0)
{
CameraEvent_addr = reinterpret_cast<uintptr_t>(pattern.count(1).get(0).get<void>(0));

#ifdef TESTING
#ifdef TESTING
std::cout << "CameraEvent addr: " << std::hex << CameraEvent_addr << "\n";
#endif
#endif

Asm_CameraEvent.callAddr = (uintptr_t)CameraEvent;
MemMgr::LongJmpHook(CameraEvent_addr, (uintptr_t)Asm_CameraEvent.GetCode());
CameraEvent_Address = (uintptr_t)CameraEvent;
MemMgr::LongJmpHook(CameraEvent_addr, (uintptr_t)Asm_CameraEvent);

return true;
}
else
{
Mod::Get()->Log(SCS_LOG_TYPE_error, "Data structure is incorrect!");
#ifdef TESTING
#ifdef TESTING
std::cout << "Hook for CameraEvent not found!\n";
#endif
#endif
return false;
}
}

bool Init()
{
#ifdef TESTING
#ifdef TESTING
std::cout << "Initializing hooks...\n";
#endif

bool inited = true;
#endif

if (!Hook_CameraEvent())
inited = false;
return false;

#ifdef TESTING
if (inited)
std::cout << "Hooks initialized!\n";
#endif
#ifdef TESTING
std::cout << "Hooks initialized!\n";
#endif

g_pMod = Mod::Get();

return inited;
return true;
}

void Unhook()
Expand All @@ -183,4 +153,4 @@ namespace Hooks
memcpy((uint8_t*)CameraEvent_addr, CameraEvent_pattern, sizeof(CameraEvent_pattern));
}
}
}
}
12 changes: 4 additions & 8 deletions src/Smooth Interior Camera/Game/Hooks.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : Hooks.h
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file Hooks.h
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#pragma once

Expand Down
33 changes: 33 additions & 0 deletions src/Smooth Interior Camera/Game/HooksASM.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; ** @file HooksASM.asm
; ** @author Piotr Krupa ([email protected])
; ** @license MIT License
; **

extern CameraEvent_Address: qword

.code

Asm_CameraEvent PROC

; backup registers
push rax
push rcx
push rdx
push r8

; call library function
mov rax, CameraEvent_Address
call rax

; restore registers
pop r8
pop rdx
pop rcx
pop rax

add rsp, 38h
ret

Asm_CameraEvent ENDP

End
40 changes: 23 additions & 17 deletions src/Smooth Interior Camera/Game/prism.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
/*
*********************************************************************
* File : prism.h
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file prism.h
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#pragma once

#include "Version.h"

namespace prism
{
class Camera
template <size_t Offset1, size_t Offset2>
class CameraBase
{
public:
char pad1[684]; // +0
unsigned m_keyboardEv; // +684
float m_rx_predef; // +688
float m_ry_predef; // +692
char pad3[144]; // +696
float m_rx; // +840 main camera
float m_ry; // +844
char pad1[Offset1]; // +0
unsigned m_keyboardEv; // +Offset1
float m_rx_predef; // +Offset1 + 4
float m_ry_predef; // +Offset1 + 8
char pad2[Offset2]; // +Offset1 + 12
float m_rx; // +Offset2
float m_ry; // +Offset2 + 4
};
}


#if defined(HOOK_V1)
using Camera = CameraBase<684, 144>;
#elif defined(HOOK_V2)
using Camera = CameraBase<692, 144>;
#endif
}
14 changes: 5 additions & 9 deletions src/Smooth Interior Camera/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : Main.cpp
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file Main.cpp
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#include "Mod\Mod.h"

Expand Down Expand Up @@ -41,4 +37,4 @@ int WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
}

return TRUE;
}
}
14 changes: 5 additions & 9 deletions src/Smooth Interior Camera/Memory/MemMgr.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : MemMgr.cpp
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file MemMgr.cpp
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#include "MemMgr.h"

Expand All @@ -17,7 +13,7 @@
xchg qword ptr ss:[rsp], rax
ret
*/
BYTE g_jmpHook[] = { 0x50, 0x48, 0xB8, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x48, 0x87, 0x04, 0x24, 0xC3 };
unsigned char g_jmpHook[] = { 0x50, 0x48, 0xB8, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x48, 0x87, 0x04, 0x24, 0xC3 };

void MemMgr::UnprotectMemory(uintptr_t addr, size_t size)
{
Expand Down
12 changes: 4 additions & 8 deletions src/Smooth Interior Camera/Memory/MemMgr.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*
*********************************************************************
* File : MemMgr.h
* Project : Smooth Interior Camera
* Developers : Piotr Krupa ([email protected])
* License : MIT License
*********************************************************************
*/
/** @file MemMgr.h
** @author Piotr Krupa ([email protected])
** @license MIT License
**/

#pragma once

Expand Down
Loading

0 comments on commit c273337

Please sign in to comment.