-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.cpp
281 lines (248 loc) · 7.42 KB
/
dllmain.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
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
279
280
281
// dllmain.cpp : DLL entry point
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define ATS_EXPORTS
#include <array>
#include <cstddef>
#include <filesystem>
#include <sstream>
#include <string>
#include <string_view>
#include "../encoder.h"
namespace {
HMODULE dll_module_handle;
HANDLE pipe_to_backend, pipe_from_backend;
std::filesystem::path get_module_file_name()
{
std::wstring name;
name.resize(1u << 8);
while (name.size() < 1u << 16) {
DWORD s = GetModuleFileNameW(
dll_module_handle,
name.data(),
static_cast<DWORD>(name.size()));
if (0 < s && s < name.size()) {
name.resize(s);
name.shrink_to_fit();
return std::move(name);
}
name.resize(name.size() * 2);
}
name.clear();
name.shrink_to_fit();
return std::move(name);
}
std::filesystem::path backend_path()
{
std::filesystem::path path = get_module_file_name();
path.replace_extension();
std::wstring name = path.filename().native();
if (!name.empty() && name.back() == L'_') {
name.pop_back();
name += L".exe";
path.replace_filename(name);
}
else {
// Unexpected name; abort
path.clear();
}
return path;
}
bool send_to_backend(const std::string_view &s) {
DWORD written_count;
return WriteFile(
pipe_to_backend,
s.data(),
static_cast<DWORD>(s.size()),
&written_count, NULL);
}
std::string receive_from_backend() {
std::string line;
std::array<char, 8192> buffer{};
for (;;) {
DWORD read_count = 0;
if (!ReadFile(
pipe_from_backend,
buffer.data(),
static_cast<DWORD>(buffer.size()),
&read_count,
NULL))
{
line.clear();
break;
}
std::string_view fragment(
buffer.data(), static_cast<std::size_t>(read_count));
line.append(fragment);
if (fragment.find('\n') != std::string_view::npos) {
break;
}
}
return line;
}
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
dll_module_handle = hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
ATS_API int WINAPI GetPluginVersion() {
return ATS_VERSION;
}
ATS_API void WINAPI Load() {
// Open pipes to communicate with the backend
SECURITY_ATTRIBUTES attrs{};
attrs.nLength = sizeof(attrs);
attrs.lpSecurityDescriptor = NULL;
attrs.bInheritHandle = true;
HANDLE reader_to_backend, writer_to_backend;
if (!CreatePipe(&reader_to_backend, &writer_to_backend, &attrs, 0)) {
return;
}
if (!SetHandleInformation(writer_to_backend, HANDLE_FLAG_INHERIT, 0)) {
CloseHandle(reader_to_backend);
CloseHandle(writer_to_backend);
return;
}
HANDLE reader_from_backend, writer_from_backend;
if (!CreatePipe(&reader_from_backend, &writer_from_backend, &attrs, 0)) {
CloseHandle(reader_to_backend);
CloseHandle(writer_to_backend);
return;
}
if (!SetHandleInformation(reader_from_backend, HANDLE_FLAG_INHERIT, 0)) {
CloseHandle(reader_to_backend);
CloseHandle(writer_to_backend);
CloseHandle(reader_from_backend);
CloseHandle(writer_from_backend);
return;
}
// Start the backend
auto path = backend_path();
STARTUPINFOW startup_info{};
startup_info.cb = sizeof(startup_info);
startup_info.dwFlags = STARTF_USESTDHANDLES;
startup_info.hStdInput = reader_to_backend;
startup_info.hStdOutput = writer_from_backend;
startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
PROCESS_INFORMATION process_info{};
if (!CreateProcessW(
path.c_str(),
NULL,
NULL,
NULL,
TRUE,
CREATE_NO_WINDOW,
NULL,
NULL,
&startup_info,
&process_info)) {
CloseHandle(reader_to_backend);
CloseHandle(writer_to_backend);
CloseHandle(reader_from_backend);
CloseHandle(writer_from_backend);
return;
}
CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread);
// Close pipes that are unnecessary on the frontend
CloseHandle(reader_to_backend);
CloseHandle(writer_from_backend);
pipe_to_backend = writer_to_backend;
pipe_from_backend = reader_from_backend;
// Call the Load function on the backend
send_to_backend("Load\n");
}
ATS_API void WINAPI Dispose() {
// Call the Dispose function on the backend
send_to_backend("Dispose\n");
// Close pipes
CloseHandle(pipe_to_backend);
pipe_to_backend = NULL;
CloseHandle(pipe_from_backend);
pipe_from_backend = NULL;
}
ATS_API void WINAPI SetVehicleSpec(ATS_VEHICLESPEC spec) {
std::ostringstream s;
s << "SetVehicleSpec " << spec << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI Initialize(int brake) {
std::ostringstream s;
s << "Initialize " << brake << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API ATS_HANDLES WINAPI Elapse(
ATS_VEHICLESTATE state, int *panelValues, int *soundStates)
{
ATS_HANDLES handles{};
std::ostringstream s;
s << "Elapse "
<< state
<< arrayref<int, 256>(panelValues)
<< arrayref<int, 256>(soundStates)
<< std::endl;
if (s) {
send_to_backend(std::move(s).str());
std::string buffer = receive_from_backend();
std::istringstream line(std::move(buffer));
line >> handles
>> arrayref<int, 256>(panelValues)
>> arrayref<int, 256>(soundStates);
}
return handles;
}
ATS_API void WINAPI SetPower(int notch) {
std::ostringstream s;
s << "SetPower " << notch << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI SetBrake(int notch) {
std::ostringstream s;
s << "SetBrake " << notch << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI SetReverser(int notch) {
std::ostringstream s;
s << "SetReverser " << notch << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI KeyDown(int key) {
std::ostringstream s;
s << "KeyDown " << key << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI KeyUp(int key) {
std::ostringstream s;
s << "KeyUp " << key << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI HornBlow(int type) {
std::ostringstream s;
s << "HornBlow " << type << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI DoorOpen() {
send_to_backend("DoorOpen\n");
}
ATS_API void WINAPI DoorClose() {
send_to_backend("DoorClose\n");
}
ATS_API void WINAPI SetSignal(int aspect) {
std::ostringstream s;
s << "SetSignal " << aspect << std::endl;
send_to_backend(std::move(s).str());
}
ATS_API void WINAPI SetBeaconData(ATS_BEACONDATA data) {
std::ostringstream s;
s << "SetBeaconData " << data << std::endl;
send_to_backend(std::move(s).str());
}