-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpal-windows.cpp
More file actions
125 lines (114 loc) · 3.54 KB
/
pal-windows.cpp
File metadata and controls
125 lines (114 loc) · 3.54 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
#include <cstdio>
#include <stdlib.h>
#include <signal.h>
#include "commandparsertypes.h"
#include "networkcontroller.h"
#include "prime.h"
#include "pal.h"
// Defines for static GMP lib on Windows
// Solution taken from here: http://stackoverflow.com/questions/30412951/unresolved-external-symbol-imp-fprintf-and-imp-iob-func-sdl2
#pragma comment(lib, "legacy_stdio_definitions.lib")
extern "C"
{
FILE * __iob_func()
{
static FILE* StdFiles[] =
{
// Filler nullptrs discovered via debugger. The addresses of stdin, stdout, and stderr are
// 88 bytes apart. On 64-bit OS, that's 11 pointers.
stdin,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
stdout,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
stderr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
};
return (FILE*) StdFiles;
};
}
extern AllPrimebotSettings ProgramSettings;
extern NetworkController* Controller;
extern Primebot* Bot;
BOOL WINAPI CtrlCHandler(DWORD type)
{
static bool ShutdownInProgress = false;
switch (type)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
if (ProgramSettings.NetworkSettings.Server && Controller != nullptr)
{
if (!ShutdownInProgress)
{
ShutdownInProgress = true;
// Only perform cleanup the first time this is called.
// Subsequent calls just kill the process.
Controller->Shutdown();
}
// Now that cleanup has completed, let the OS kill the program
// by returning FALSE.
return FALSE;
}
else if (Bot != nullptr)
{
Bot->Stop();
// Don't let the OS kill this, because it'll exit on its own.
return TRUE;
}
break;
default:
printf("unrecognized signal: %u", type);
}
// let the OS kill the program by returning FALSE.
return FALSE;
}
bool RegisterSignalHandler()
{
if (!SetConsoleCtrlHandler(CtrlCHandler, TRUE))
{
fprintf(stderr, "failed to set signal handler");
return false;
}
return true;
}
// Borrowed from the solution available here:
// http://stackoverflow.com/questions/1530760/how-do-i-recursively-create-a-folder-in-win32
bool MakeDirectory(const char* Path)
{
char CurrentFolder[MAX_PATH] = { 0 };
const char* End = nullptr;
End = strchr(Path, '/');
while (End != nullptr)
{
strncpy_s(CurrentFolder, Path, End - Path + 1);
if (!CreateDirectory(CurrentFolder, nullptr))
{
DWORD Error = GetLastError();
if (Error != ERROR_ALREADY_EXISTS)
{
fprintf(
stderr,
"Failed to create directory %s with error %u",
CurrentFolder,
Error);
// Don't even try to delete partially-created directories.
return false;
}
}
End = strchr(++End, '/');
}
// Create last folder
if (!CreateDirectory(Path, nullptr))
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
fprintf(
stderr,
"Failed to create directory %s with error %u",
CurrentFolder,
GetLastError());
return false;
}
}
return true;
}