forked from PeterTh/dpfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
131 lines (109 loc) · 3.63 KB
/
main.cpp
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
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include <windows.h>
#include <fstream>
#include <ostream>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <strsafe.h>
#include "main.h"
#include "d3d9.h"
#include "d3dutil.h"
#include "Settings.h"
#include "KeyActions.h"
#include "Detouring.h"
// globals
tDirect3DCreate9 oDirect3DCreate9 = Direct3DCreate9;
std::ofstream ofile;
char dlldir[320];
bool WINAPI DllMain(HMODULE hDll, DWORD dwReason, PVOID pvReserved) {
TCHAR fileName[512];
GetModuleFileName(NULL, fileName, 512);
if(dwReason == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(hDll);
GetModuleFileName(hDll, dlldir, 512);
for(int i = strlen(dlldir); i > 0; i--) { if(dlldir[i] == '\\') { dlldir[i+1] = 0; break; } }
ofile.open(GetDirectoryFile(LOG_FILE_NAME), std::ios::out);
sdlogtime();
SDLOG(0, "===== start "INTERCEPTOR_NAME" %s = fn: %s\n", VERSION, fileName);
// load settings
Settings::get().load();
Settings::get().report();
KeyActions::get().load();
KeyActions::get().report();
// load original d3d9.dll
HMODULE hMod;
if(Settings::get().getDinput8dllWrapper().empty() || (Settings::get().getDinput8dllWrapper().find("none") == 0)) {
char syspath[320];
GetSystemDirectory(syspath, 320);
strcat_s(syspath, "\\d3d9.dll");
hMod = LoadLibrary(syspath);
} else {
sdlog(0, "Loading dinput wrapper %s\n", Settings::get().getDinput8dllWrapper().c_str());
hMod = LoadLibrary(Settings::get().getDinput8dllWrapper().c_str());
}
if(!hMod) {
sdlog("Could not load original dinput8.dll\nABORTING.\n");
errorExit("Loading of specified dinput wrapper");
}
oDirect3DCreate9 = (tDirect3DCreate9)GetProcAddress(hMod, "Direct3DCreate9");
earlyDetour();
return true;
} else if(dwReason == DLL_PROCESS_DETACH) {
Settings::get().shutdown();
endDetour();
SDLOG(0, "===== end = fn: %s\n", fileName);
if(ofile) { ofile.close(); }
}
return false;
}
char *GetDirectoryFile(char *filename) {
static char path[320];
strcpy_s(path, dlldir);
strcat_s(path, filename);
return path;
}
void __cdecl sdlogtime() {
char timebuf[26];
time_t ltime;
struct tm gmt;
time(<ime);
_gmtime64_s(&gmt, <ime);
asctime_s(timebuf, 26, &gmt);
timebuf[24] = '\0'; // remove newline
SDLOG(0, "===== %s =====\n", timebuf);
}
void __cdecl sdlog(const char *fmt, ...) {
if(ofile != NULL) {
if(!fmt) { return; }
va_list va_alist;
char logbuf[9999] = {0};
va_start (va_alist, fmt);
_vsnprintf_s(logbuf+strlen(logbuf), sizeof(logbuf) - strlen(logbuf), _TRUNCATE, fmt, va_alist);
va_end (va_alist);
ofile << logbuf;
ofile.flush();
}
}
void errorExit(LPTSTR lpszFunction) {
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
bool fileExists(const char *filename) {
std::ifstream ifile(filename);
return NULL != ifile;
}