forked from prototype99/dsfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
152 lines (152 loc) · 5.14 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#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"
#include "SaveManager.h"
#include "FPS.h"
//globals
tDirect3DCreate9 oDirect3DCreate9 = Direct3DCreate9;
tDirectInput8Create oDirectInput8Create;
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("DSfix.log"), std::ios::out);
sdlogtime();
SDLOG(0, "===== start DSfix %s = fn: %s\n", VERSION, fileName);
//load settings
Settings::get().load();
Settings::get().report();
KeyActions::get().load();
KeyActions::get().report();
//load original dinput8.dll
HMODULE hMod;
if(Settings::get().getDinput8dllWrapper().empty() || (Settings::get().getDinput8dllWrapper().find("none") == 0)) {
char syspath[320];
GetSystemDirectory(syspath, 320);
strcat_s(syspath, "\\dinput8.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");
}
oDirectInput8Create = (tDirectInput8Create)GetProcAddress(hMod, "DirectInput8Create");
SaveManager::get().init();
earlyDetour();
initFPSTimer();
if(Settings::get().getUnlockFPS()) applyFPSPatch();
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(const char *filename) {
__declspec(thread) 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.good()) {
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) {
return std::ifstream(filename).good();
}
void createDirectory(const char *fileName) {
CreateDirectory(GetDirectoryFile(fileName), nullptr);
DWORD error = GetLastError();
if (error && error != ERROR_ALREADY_EXISTS) {
SDLOG(0, "Failed to create %s: %s\n", fileName, formatMessage(error));
}
}
bool writeFile(const char *filename, const char *data, size_t length) {
std::ofstream file(filename, std::ios::out | std::ios::binary);
if (!file) {
SDLOG(0, "Failed to open %s: %s\n", filename, strError(errno));
return false;
}
file.write(data, length);
if (!file) {
SDLOG(0, "Failed to write to %s: %s\n", filename, strError(errno));
return false;
}
file.close();
if (!file) {
SDLOG(0, "Failed to close %s: %s\n", filename, strError(errno));
return false;
}
return true;
}
std::string formatMessage(DWORD messageId) {
char *buffer = nullptr;
size_t length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, messageId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buffer, 0, nullptr);
std::string result(buffer, length);
LocalFree(buffer);
return result;
}
std::string strError(int err) {
std::string result(4096, '\0');
strerror_s(&result[0], result.length(), err);
return result;
}