forked from Ares-Developers/Syringe
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMain.cpp
More file actions
165 lines (135 loc) · 4.71 KB
/
Main.cpp
File metadata and controls
165 lines (135 loc) · 4.71 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
#include "Log.h"
#include "SyringeDebugger.h"
#include "Support.h"
#include "resource.h"
#include <string>
#include <commctrl.h>
#include <shellapi.h>
struct Arguments
{
std::vector<std::string> syringe_args;
std::string game_args;
};
size_t FindSeparator(const std::wstring& cmdLine)
{
bool inQuotes = false;
size_t len = cmdLine.length();
for (size_t i = 0; i < len; ++i)
{
if (cmdLine[i] == L'\\' && i + 1 < len && cmdLine[i + 1] == L'"')
{
i += 1;
continue;
}
if (cmdLine[i] == L'"')
{
inQuotes = !inQuotes;
continue;
}
if (!inQuotes && cmdLine[i] == L' ')
{
if (i + 3 < len && cmdLine[i + 1] == L'-' && cmdLine[i + 2] == L'-' && cmdLine[i + 3] == L' ')
{
return i;
}
}
}
return std::wstring::npos;
}
Arguments GetArguments()
{
std::wstring wszSyringeArgs;
std::wstring wszGameArgs;
std::wstring lpCmdLine = GetCommandLineW();
auto separator = FindSeparator(lpCmdLine);
if (separator != std::wstring::npos)
{
wszSyringeArgs = lpCmdLine.substr(0, separator);
wszGameArgs = lpCmdLine.substr(separator + 4);
}
else
{
wszSyringeArgs = lpCmdLine;
wszGameArgs = L"";
}
// Get argc, argv in wide chars
int argc = 0;
LPWSTR* argvW = CommandLineToArgvW(wszSyringeArgs.c_str(), &argc);
// Convert to UTF-8. Skip the first argument as it contains the path to Syringe itself
std::vector<std::string> argv(argc - 1);
for (int i = 1; i < argc; ++i)
{
int len = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, nullptr, 0, nullptr, nullptr);
argv[i - 1].resize(len - 1);
WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, argv[i - 1].data(), len, nullptr, nullptr);
}
LocalFree(argvW);
int len = WideCharToMultiByte(CP_UTF8, 0, wszGameArgs.c_str(), -1, nullptr, 0, nullptr, nullptr);
std::string gameArgs = std::string(len - 1, '\0');
WideCharToMultiByte(CP_UTF8, 0, wszGameArgs.c_str(), -1, gameArgs.data(), len, nullptr, nullptr);
return {
argv,
gameArgs,
};
}
int Run(const Arguments& arguments)
{
constexpr auto const VersionString = "SyringeEx " SYRINGEEX_VER_TEXT ", based on Syringe 0.7.2.0";
InitCommonControls();
Log::Open("syringe.log");
Log::WriteLine(VersionString);
Log::WriteLine("===============");
Log::WriteLine();
Log::WriteLine("WinMain: arguments = \"%.*s\"", printable(arguments.syringe_args));
auto failure = "Could not load executable.";
auto exit_code = ERROR_ERRORS_ENCOUNTERED;
try
{
auto const command = parse_command_line(arguments.syringe_args);
Log::WriteLine(
"WinMain: Trying to load executable file \"%.*s\"...",
printable(command.executable_name));
Log::WriteLine();
SyringeDebugger Debugger{ command.executable_name, command.syringe_arguments };
failure = "Could not run executable.";
Log::WriteLine("WinMain: SyringeDebugger::FindDLLs();");
Log::WriteLine();
Debugger.FindDLLs();
Log::WriteLine(
"WinMain: SyringeDebugger::Run(\"%.*s\");",
printable(arguments.game_args));
Log::WriteLine();
Debugger.Run(arguments.game_args);
Log::WriteLine("WinMain: SyringeDebugger::Run finished.");
Log::WriteLine("WinMain: Exiting on success.");
return ERROR_SUCCESS;
}
catch (lasterror const& e)
{
auto const message = replace(e.message, "%1", e.insert);
Log::WriteLine("WinMain: %s (%d)", message.c_str(), e.error);
auto const msg = std::string(failure) + "\n\n" + message;
MessageBoxA(nullptr, msg.c_str(), VersionString, MB_OK | MB_ICONERROR);
exit_code = static_cast<long>(e.error);
}
catch (invalid_command_arguments const&)
{
MessageBoxA(
nullptr, "Syringe cannot be run like that.\n\n"
"Usage:\nSyringe.exe <exe name> [-i=<injectedfile.dll> ...] [-- <arguments>]",
VersionString, MB_OK | MB_ICONINFORMATION);
Log::WriteLine(
"WinMain: Invalid command line arguments given, exiting...");
exit_code = ERROR_INVALID_PARAMETER;
}
Log::WriteLine("WinMain: Exiting on failure.");
return static_cast<int>(exit_code);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
return Run(GetArguments());
}