forked from kubik-jaroslav/ddda-dinput8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdinput8.cpp
More file actions
143 lines (128 loc) · 4.33 KB
/
Copy pathdinput8.cpp
File metadata and controls
143 lines (128 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "stdafx.h"
#include "MinHook\MinHook.h"
#include "d3d9.h"
#include "SaveBackup.h"
#include "Misc.h"
#include "Cheats.h"
#include "InGameClock.h"
#include "Hotkeys.h"
#include "ItemEditor.h"
#include "PlayerStats.h"
#include "Server.h"
#include "Portcrystals.h"
#include "WeaponSets.h"
#include "DamageLog.h"
typedef HRESULT(WINAPI *tDirectInput8Create)(HINSTANCE inst_handle, DWORD version, const IID& r_iid, LPVOID* out_wrapper, LPUNKNOWN p_unk);
tDirectInput8Create oDirectInput8Create = nullptr;
BYTE *codeBase, *codeEnd, *dataBase, *dataEnd;
std::ofstream logFile("dinput8.log", std::ios_base::out);
iniConfig config(".\\dinput8.ini");
BYTE **pBase = nullptr, **pWorld = nullptr;
void InitHooks()
{
BYTE sigBase[] = { 0x8B, 0x15, 0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0xDB, 0x8B, 0xF8 };
BYTE sigWorld[] = { 0x89, 0x35, 0xCC, 0xCC, 0xCC, 0xCC, 0x89, 0xBE, 0x70, 0x09, 0x00, 0x00 };
BYTE *pOffset;
if (Hooks::FindSignature("BasePointer", sigBase, &pOffset))
pBase = *(BYTE***)(pOffset + 2);
if (Hooks::FindSignature("WorldPointer", sigWorld, &pOffset))
pWorld = *(BYTE***)(pOffset + 2);
Hooks::SaveBackup();
Hooks::Hotkeys();
Hooks::Misc();
Hooks::Cheats();
Hooks::Server();
Hooks::WeaponSets();
if (Hooks::InGameUI())
{
Hooks::Portcrystals();
Hooks::PlayerStats();
Hooks::ItemEditor();
Hooks::InGameClock();
Hooks::DamageLog();
}
}
void Initialize()
{
DWORD base = (DWORD)GetModuleHandle(nullptr);
auto idh = (PIMAGE_DOS_HEADER)base;
auto inh = (PIMAGE_NT_HEADERS)(base + idh->e_lfanew);
auto ioh = &inh->OptionalHeader;
codeBase = (BYTE*)(base + ioh->BaseOfCode);
codeEnd = codeBase + ioh->SizeOfCode;
dataBase = (BYTE*)(base + ioh->BaseOfData);
dataEnd = dataBase + ioh->SizeOfInitializedData;
logFile << "MH_Initialize: " << MH_StatusToString(MH_Initialize()) << std::endl;
InitHooks();
string loadLibrary = config.getStr("main", "loadLibrary");
if (!loadLibrary.empty())
{
HMODULE hMod = LoadLibrary(loadLibrary.c_str());
oDirectInput8Create = (tDirectInput8Create)GetProcAddress(hMod, "DirectInput8Create");
}
if (!oDirectInput8Create)
{
CHAR syspath[MAX_PATH];
GetSystemDirectory(syspath, MAX_PATH);
strcat_s(syspath, "\\dinput8.dll");
HMODULE hMod = LoadLibrary(syspath);
oDirectInput8Create = (tDirectInput8Create)GetProcAddress(hMod, "DirectInput8Create");
}
}
void Unitialize()
{
logFile << "MH_DisableHook: " << MH_StatusToString(MH_DisableHook(MH_ALL_HOOKS)) << std::endl;
logFile << "MH_Uninitialize: " << MH_StatusToString(MH_Uninitialize()) << std::endl;
if (logFile)
logFile.close();
}
void Hooks::CreateHook(LPCSTR msg, LPVOID pTarget, LPVOID pDetour, LPVOID* ppOriginal, bool enable)
{
logFile << msg << " hook: " << MH_StatusToString(MH_CreateHook(pTarget, pDetour, ppOriginal)) << ", ";
if (enable)
logFile << MH_StatusToString(MH_EnableHook(pTarget)) << std::endl;
else
logFile << "disabled" << std::endl;
}
void Hooks::SwitchHook(LPCSTR msg, LPVOID pTarget, bool enable)
{
if (enable)
logFile << msg << " enable: " << MH_StatusToString(MH_EnableHook(pTarget)) << std::endl;
else
logFile << msg << " disable: " << MH_StatusToString(MH_DisableHook(pTarget)) << std::endl;
}
bool Hooks::FindSignature(LPCSTR msg, BYTE* signature, size_t len, BYTE** offset) { return Find(msg, codeBase, codeEnd, signature, len, offset); }
bool Hooks::FindData(LPCSTR msg, BYTE* signature, size_t len, BYTE** offset) { return Find(msg, dataBase, dataEnd, signature, len, offset); }
bool Hooks::Find(LPCSTR msg, BYTE* start, BYTE* end, BYTE *signature, size_t len, BYTE **offset)
{
for (*offset = start; *offset < end; (*offset)++)
{
for (unsigned int i = 0; i < len; i++)
{
if (signature[i] != 0xCC && signature[i] != (*offset)[i])
break;
if (i == len - 1)
{
logFile << msg << " pointer: " << (LPVOID)*offset << std::endl;
return true;
}
}
}
logFile << msg << " pointer: not found" << std::endl;
return false;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hModule);
Initialize();
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
Unitialize();
return TRUE;
}
HRESULT WINAPI DirectInput8Create(HINSTANCE inst_handle, DWORD version, const IID& r_iid, LPVOID* out_wrapper, LPUNKNOWN p_unk)
{
return oDirectInput8Create(inst_handle, version, r_iid, out_wrapper, p_unk);
}