-
Notifications
You must be signed in to change notification settings - Fork 22
/
loader.cpp
161 lines (136 loc) · 5.88 KB
/
loader.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
#include "pch/pch.h"
#include "loader/loader.h"
#include "api/ApiInterface.h"
#include "windows/WinApiInterface.h"
#include "curl/CurlInterface.h"
#include "data/PluginDataLocations.h"
#include "update/UpdateBinaries.h"
#include "euroscope/EuroScopePlugIn.h"
// Define the functions we need from the various libraries
typedef bool(CALLBACK* PERFORMUPDATES)();
typedef EuroScopePlugIn::CPlugIn*(CALLBACK* LOADPLUGINLIBRARY)();
typedef void(CALLBACK* UNLOADPLUGINLIBRARY)();
void RunUpdater(UKControllerPlugin::Windows::WinApiInterface& windows)
{
LogInfo("Loading updater library");
HINSTANCE updaterHandle = windows.LoadLibraryRelative(GetUpdaterBinaryRelativePath());
if (!updaterHandle) {
LogInfo("Unable to run the updater, binary does not exist.");
std::wstring message = L"Unable to start updater.\r\n";
message += L"Please contact the VATSIM UK Web Services Department.\r\n";
windows.OpenMessageBox(message.c_str(), L"UKCP Update Failed", MB_OK | MB_ICONSTOP);
throw std::exception("UKCP broke");
}
LogInfo("Performing updates, please refer to the updater log for more information");
PERFORMUPDATES PerformUpdates =
reinterpret_cast<PERFORMUPDATES>(windows.GetFunctionPointerFromLibrary(updaterHandle, "PerformUpdates"));
/*
* If the updater returns false, it means the user wouldn't let it update.
* Stop them here.
*/
if (!PerformUpdates || !PerformUpdates()) {
windows.UnloadLibrary(updaterHandle);
throw std::exception();
}
LogInfo("Updates complete");
windows.UnloadLibrary(updaterHandle);
}
HINSTANCE LoadPluginLibrary(UKControllerPlugin::Windows::WinApiInterface& windows)
{
LogInfo("Loading core plugin library");
return windows.LoadLibraryRelative(GetCoreBinaryRelativePath());
}
EuroScopePlugIn::CPlugIn*
LoadPlugin(HINSTANCE pluginLibraryHandle, UKControllerPlugin::Windows::WinApiInterface& windows)
{
if (pluginLibraryHandle == nullptr) {
LogInfo("Not loading core plugin, library handle is null");
return nullptr;
}
LOADPLUGINLIBRARY LoadPlugin =
reinterpret_cast<LOADPLUGINLIBRARY>(windows.GetFunctionPointerFromLibrary(pluginLibraryHandle, "LoadPlugin"));
if (!LoadPlugin) {
LogError("Cannot find LoadPlugin function in Core binary");
std::wstring message = L"Unable find LoadPlugin function in Core Binary.\r\n\r\n";
message += L"Please contact the Web Services Department.";
DisplayLoaderError(windows, message);
return nullptr;
}
LogInfo("Performing core plugin load");
return LoadPlugin();
}
void UnloadPlugin(HINSTANCE pluginLibraryHandle, UKControllerPlugin::Windows::WinApiInterface& windows)
{
if (pluginLibraryHandle == nullptr) {
LogInfo("Not unloading core plugin, library handle is null");
return;
}
UNLOADPLUGINLIBRARY UnloadPlugin = reinterpret_cast<UNLOADPLUGINLIBRARY>(
windows.GetFunctionPointerFromLibrary(pluginLibraryHandle, "UnloadPlugin"));
if (!UnloadPlugin) {
LogError("Cannot find UnloadPlugin function in Core binary");
std::wstring message = L"Unable find UnloadPlugin function in Core Binary.\r\n\r\n";
message += L"Please contact the Web Services Department.";
DisplayLoaderError(windows, message);
return;
}
LogInfo("Performing core plugin shutdown");
UnloadPlugin();
}
void UnloadPluginLibrary(HINSTANCE instance, UKControllerPlugin::Windows::WinApiInterface& windows)
{
if (instance == nullptr) {
LogInfo("Not unloading core library, was never loaded");
return;
}
LogInfo("Unloading core library");
windows.UnloadLibrary(instance);
}
bool FirstTimeDownload(
const UKControllerPlugin::Api::ApiInterface& api,
UKControllerPlugin::Windows::WinApiInterface& windows,
UKControllerPlugin::Curl::CurlInterface& curl)
{
if (windows.FileExists(GetUpdaterBinaryRelativePath())) {
LogInfo("Updater binary exists in filesystem, skipping first time download");
return true;
}
if (!DisplayFirstTimeDownloadMessage(windows)) {
LogWarning("User did not consent to first-time download.");
return false;
}
LogInfo("Performing first time download of updater");
nlohmann::json updateData;
try {
updateData = UKControllerPlugin::GetUpdateData(api, "stable");
} catch (std::exception) {
LogError("Failed to get first time update data");
DisplayFirstTimeDownloadFailedMessage(windows);
return false;
}
if (!UKControllerPlugin::DownloadUpdater(updateData, windows, curl)) {
LogError("Failed to perform first time updater download");
DisplayFirstTimeDownloadFailedMessage(windows);
return false;
}
LogInfo("First time download of updater successful");
return true;
}
bool DisplayFirstTimeDownloadMessage(UKControllerPlugin::Windows::WinApiInterface& windows)
{
std::wstring message = L"Performing first time download. This will download and run the UKCP Updater ";
message += L"to install the core plugin.\r\n\r\n";
message += L"Select OK to continue.";
return windows.OpenMessageBox(message.c_str(), L"UKCP First Time Download", MB_OKCANCEL | MB_ICONINFORMATION) ==
IDOK;
}
void DisplayFirstTimeDownloadFailedMessage(UKControllerPlugin::Windows::WinApiInterface& windows)
{
std::wstring message = L"Unable to perform first time download of the UKCP updater.\r\n";
message += L"Please contact the VATSIM UK Web Services Department.\r\n";
DisplayLoaderError(windows, message);
}
void DisplayLoaderError(UKControllerPlugin::Windows::WinApiInterface& windows, std::wstring message)
{
windows.OpenMessageBox(message.c_str(), L"UKCP Loader Error", MB_OK | MB_ICONSTOP);
}