forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD2BS.cpp
179 lines (150 loc) · 5.58 KB
/
D2BS.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Diablo II Botting System Core
#include <shlwapi.h>
#include <io.h>
#include <fcntl.h>
#include "dde.h"
#include "Offset.h"
#include "ScriptEngine.h"
#include "Helpers.h"
#include "D2Handlers.h"
#include "Console.h"
#include "D2BS.h"
#include "D2Ptrs.h"
#include "CommandLine.h"
#ifdef _MSVC_DEBUG
#include "D2Loader.h"
#endif
static HANDLE hD2Thread = INVALID_HANDLE_VALUE;
static HANDLE hEventThread = INVALID_HANDLE_VALUE;
BOOL WINAPI DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason) {
case DLL_PROCESS_ATTACH: {
DisableThreadLibraryCalls(hDll);
if (lpReserved != NULL) {
Vars.pModule = (Module*)lpReserved;
if (!Vars.pModule)
return FALSE;
wcscpy_s(Vars.szPath, MAX_PATH, Vars.pModule->szPath);
Vars.bLoadedWithCGuard = TRUE;
} else {
Vars.hModule = hDll;
GetModuleFileNameW(hDll, Vars.szPath, MAX_PATH);
PathRemoveFileSpecW(Vars.szPath);
wcscat_s(Vars.szPath, MAX_PATH, L"\\");
Vars.bLoadedWithCGuard = FALSE;
}
swprintf_s(Vars.szLogPath, _countof(Vars.szLogPath), L"%slogs\\", Vars.szPath);
CreateDirectoryW(Vars.szLogPath, NULL);
InitCommandLine();
ParseCommandLine(Vars.szCommandLine);
InitSettings();
sLine* command = NULL;
Vars.bUseRawCDKey = FALSE;
if (command = GetCommand(L"-title")) {
int len = wcslen((wchar_t*)command->szText);
wcsncat_s(Vars.szTitle, (wchar_t*)command->szText, len);
}
if (GetCommand(L"-sleepy"))
Vars.bSleepy = TRUE;
if (GetCommand(L"-cachefix"))
Vars.bCacheFix = TRUE;
if (GetCommand(L"-multi"))
Vars.bMulti = TRUE;
if (GetCommand(L"-ftj"))
Vars.bReduceFTJ = TRUE;
if (command = GetCommand(L"-d2c")) {
Vars.bUseRawCDKey = TRUE;
const char* keys = UnicodeToAnsi(command->szText);
strncat_s(Vars.szClassic, keys, strlen(keys));
delete[] keys;
}
if (command = GetCommand(L"-d2x")) {
const char* keys = UnicodeToAnsi(command->szText);
strncat_s(Vars.szLod, keys, strlen(keys));
delete[] keys;
}
#if 0
char errlog[516] = "";
sprintf_s(errlog, 516, "%sd2bs.log", Vars.szPath);
AllocConsole();
int handle = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
FILE* f = _fdopen(handle, "wt");
*stderr = *f;
setvbuf(stderr, NULL, _IONBF, 0);
freopen_s(&f, errlog, "a+t", f);
#endif
Vars.bShutdownFromDllMain = FALSE;
SetUnhandledExceptionFilter(ExceptionHandler);
if (!Startup())
return FALSE;
} break;
case DLL_PROCESS_DETACH:
if (Vars.bNeedShutdown) {
Vars.bShutdownFromDllMain = TRUE;
Shutdown();
}
break;
}
return TRUE;
}
BOOL Startup(void) {
InitializeCriticalSection(&Vars.cEventSection);
InitializeCriticalSection(&Vars.cRoomSection);
InitializeCriticalSection(&Vars.cMiscSection);
InitializeCriticalSection(&Vars.cScreenhookSection);
InitializeCriticalSection(&Vars.cPrintSection);
InitializeCriticalSection(&Vars.cBoxHookSection);
InitializeCriticalSection(&Vars.cFrameHookSection);
InitializeCriticalSection(&Vars.cLineHookSection);
InitializeCriticalSection(&Vars.cImageHookSection);
InitializeCriticalSection(&Vars.cTextHookSection);
InitializeCriticalSection(&Vars.cFlushCacheSection);
InitializeCriticalSection(&Vars.cConsoleSection);
InitializeCriticalSection(&Vars.cGameLoopSection);
InitializeCriticalSection(&Vars.cUnitListSection);
InitializeCriticalSection(&Vars.cFileSection);
Vars.bNeedShutdown = TRUE;
Vars.bChangedAct = FALSE;
Vars.bGameLoopEntered = FALSE;
Vars.SectionCount = 0;
// MessageBox(NULL, "qwe", "qwe", MB_OK);
Genhook::Initialize();
DefineOffsets();
InstallPatches();
InstallConditional();
CreateDdeServer();
if ((hD2Thread = CreateThread(NULL, NULL, D2Thread, NULL, NULL, NULL)) == NULL)
return FALSE;
// hEventThread = CreateThread(0, 0, EventThread, NULL, 0, 0);
return TRUE;
}
void Shutdown(void) {
if (!Vars.bNeedShutdown)
return;
Vars.bActive = FALSE;
if (!Vars.bShutdownFromDllMain)
WaitForSingleObject(hD2Thread, INFINITE);
SetWindowLong(D2GFX_GetHwnd(), GWL_WNDPROC, (LONG)Vars.oldWNDPROC);
RemovePatches();
Genhook::Destroy();
ShutdownDdeServer();
KillTimer(D2GFX_GetHwnd(), Vars.uTimer);
UnhookWindowsHookEx(Vars.hMouseHook);
UnhookWindowsHookEx(Vars.hKeybHook);
DeleteCriticalSection(&Vars.cRoomSection);
DeleteCriticalSection(&Vars.cMiscSection);
DeleteCriticalSection(&Vars.cScreenhookSection);
DeleteCriticalSection(&Vars.cPrintSection);
DeleteCriticalSection(&Vars.cBoxHookSection);
DeleteCriticalSection(&Vars.cFrameHookSection);
DeleteCriticalSection(&Vars.cLineHookSection);
DeleteCriticalSection(&Vars.cImageHookSection);
DeleteCriticalSection(&Vars.cTextHookSection);
DeleteCriticalSection(&Vars.cFlushCacheSection);
DeleteCriticalSection(&Vars.cConsoleSection);
DeleteCriticalSection(&Vars.cGameLoopSection);
DeleteCriticalSection(&Vars.cUnitListSection);
DeleteCriticalSection(&Vars.cFileSection);
Log(L"D2BS Shutdown complete.");
Vars.bNeedShutdown = false;
}