-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
278 lines (237 loc) · 6.37 KB
/
main.cpp
File metadata and controls
278 lines (237 loc) · 6.37 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <shellapi.h>
#include <filesystem>
#include <string>
#include <sstream>
#include "AutoUpdater.h"
std::string fdmExe = "4DM.exe";
inline static std::string modloaderCore = ".\\4DModLoader-Core.dll";
bool attachedToConsole = false;
PROCESS_INFORMATION startup(LPCSTR lpApplicationName, const std::vector<std::string>& args, bool suspended = true)
{
// additional information
STARTUPINFOA si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// create the shit for arguments
std::ostringstream argsStream;
argsStream << "\"" << lpApplicationName << "\"";
for (auto& arg : args)
argsStream << " \"" << arg << "\"";
// start the process
CreateProcessA
(
lpApplicationName,
const_cast<char*>(argsStream.str().c_str()),
NULL,
NULL,
TRUE,
(!attachedToConsole ? CREATE_NEW_CONSOLE : 0) | (suspended ? CREATE_SUSPENDED : 0),
NULL,
NULL,
&si,
&pi
);
return pi;
}
bool Inject(HANDLE process, HANDLE thread)
{
if (!process) return false;
if (!std::filesystem::exists(modloaderCore)) return false;
LPVOID LoadLibAddr = (LPVOID)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
LPVOID LoadLocation = VirtualAllocEx(process, 0, modloaderCore.size(), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
HANDLE RemoteThread = CreateRemoteThread(process, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibAddr, LoadLocation, 0, 0);
WriteProcessMemory(process, LoadLocation, modloaderCore.c_str(), modloaderCore.size(), NULL);
WaitForSingleObject(RemoteThread, INFINITE);
VirtualFreeEx(process, LoadLocation, modloaderCore.size(), MEM_RELEASE);
CloseHandle(RemoteThread);
CloseHandle(process);
return true;
}
#define DEFAULT_JIT_DEBUGGER_CMD "C:\\Windows\\System32\\vsjitdebugger.exe -p"
int main_(const std::vector<std::string>& args)
{
if (std::filesystem::exists("4D Server.exe"))
{
fdmExe = "4D Server.exe";
}
bool fdmOverride = false;
std::string fdmOverrideStr = "";
bool jitDebug = false;
std::string debugger = DEFAULT_JIT_DEBUGGER_CMD;
bool debuggerOverride = false;
bool offline = false;
for (auto& arg : args)
{
// -help
{
if (arg == "-help" || arg == "--help" || arg == "help")
{
print(std::format(
"4DModLoader.exe flags/launch arguments:\n"
" -help or --help or help - Outputs this and does not launch anything.\n"
" -4dm <path to an .exe> - Overrides the \"4D Miner executable\".\n\tDefault: {}\n"
" -debug - Attaches a debugger to the just started 4D Miner process.\n"
" -debugger <cmd for a debugger with a PID> - Overrides the debugger cmd to be used with -debug.\n\tDefault: \"" DEFAULT_JIT_DEBUGGER_CMD "\"\n"
" -offline - Disable Auto-Updater Online Checks.\n"
" 4DModLoader-Core specific launch arguments:\n"
" -console - Starts the debug console. (since v2.2)\n there might be more if you are on an outdated 4DModLoader.exe that doesn't know about them.\n"
" + whatever mod launch arguments there might be\n"
, fdmExe)
);
std::cin.get();
return 0;
}
}
// -4dm
{
if (!fdmOverride && arg == "-4dm")
{
fdmOverride = true;
continue;
}
if (fdmOverride &&
std::filesystem::exists(arg) && arg.ends_with(".exe"))
{
fdmOverrideStr = arg;
fdmOverride = false;
continue;
}
}
// -debug
{
if (!jitDebug && arg == "-debug")
{
jitDebug = true;
continue;
}
}
// -debugger
{
if (!debuggerOverride && arg == "-debugger")
{
debuggerOverride = true;
continue;
}
if (debuggerOverride)
{
debugger = arg;
debuggerOverride = false;
continue;
}
}
// -offline
{
if (!offline && arg == "-offline")
{
offline = true;
continue;
}
}
}
if (!fdmOverrideStr.empty())
fdmExe = fdmOverrideStr.c_str();
if (!offline)
{
AutoUpdate();
CheckForLibs();
}
auto gameProcessInfo = startup(fdmExe.c_str(), args);
HANDLE& gameProcess = gameProcessInfo.hProcess;
HANDLE& gameThread = gameProcessInfo.hThread;
if (!gameProcess)
{
MessageBoxA(0, std::format("Couldn't open the 4D Miner executable (expected to be at \"{}\")", fdmExe).c_str(), "4DModLoader", MB_OK | MB_ICONERROR);
return -2;
}
if (jitDebug)
{
debugger = std::format("{} {}", debugger, gameProcessInfo.dwProcessId);
int r = system(debugger.c_str());
if (r)
{
MessageBoxA(0, std::format("Couldn't launch the debugger with cmd \"{}\"", debugger).c_str(), "4DModLoader", MB_OK | MB_ICONWARNING);
}
}
if (!Inject(gameProcess, gameThread))
{
MessageBoxA(0, "Couldn't load 4DModLoader-Core.dll", "4DModLoader", MB_OK | MB_ICONERROR);
ResumeThread(gameThread);
return -1;
}
ResumeThread(gameThread);
if (attachedToConsole)
{
WaitForSingleObject(gameProcess, INFINITE);
WaitForSingleObject(gameThread, INFINITE);
}
CloseHandle(gameProcess);
CloseHandle(gameThread);
if (consoleOpen && !attachedToConsole)
{
printf("You can close this window if it is still open for some reason :)\n");
FreeConsole();
}
return 0;
}
/*
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
if (attachedToConsole = AttachConsole(ATTACH_PARENT_PROCESS))
{
AllocConsole();
FILE* fpout;
FILE* fpin;
freopen_s(&fpout, "CONOUT$", "wt", stdout);
freopen_s(&fpin, "CONIN$", "rt", stdin);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT);
}
std::vector<std::string> args;
int argc = 0;
LPWSTR* w_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (w_argv)
{
args.reserve(argc);
for (int i = 0; i < argc; i++)
{
std::wstring wideStr(w_argv[i]);
args.emplace_back(wideStr.begin(), wideStr.end());
}
}
LocalFree(w_argv);
args.erase(args.begin());
int returnV = main_(args);
exit(returnV);
return returnV;
}
*/
int main(int argc, char* argv[])
{
DWORD processList[2];
DWORD processCount = GetConsoleProcessList(processList, 2);
if (processCount <= 1)
{
HWND hwnd = GetConsoleWindow();
if (hwnd)
{
FreeConsole();
}
}
else
{
attachedToConsole = true;
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT);
}
std::vector<std::string> args;
args.reserve(argc - 1);
for (int i = 1; i < argc; ++i)
{
args.emplace_back(argv[i]);
}
return main_(args);
}