forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrossBuildSwitch.cpp
More file actions
228 lines (179 loc) · 5.13 KB
/
CrossBuildSwitch.cpp
File metadata and controls
228 lines (179 loc) · 5.13 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
#include <StdInc.h>
#include <CefOverlay.h>
#include <NetLibrary.h>
#include <Utils.h>
#include <json.hpp>
#include <CrossBuildRuntime.h>
#include <PureModeState.h>
#include <PoolSizesState.h>
#include <CommCtrl.h>
#include "../../client/launcher/InstallerExtraction.h"
int gameCacheTargetBuild;
bool gameCacheReplaceExecutable;
extern NetLibrary* netLibrary;
extern std::map<std::string, std::string> UpdateGameCache();
extern void RestartGameToOtherBuild(int build, int pureLevel, std::wstring poolSizesIncreaseSetting, bool replaceExecutable);
static std::function<void(const std::string&)> g_submitFn;
static bool g_cancelable;
static bool g_canceled;
static bool g_hadError;
void PerformStateSwitch(int build, int pureLevel, std::wstring poolSizesIncreaseSetting, bool replaceExecutable);
void InitializeBuildSwitch(int build, int pureLevel, std::wstring poolSizesIncreaseSetting, bool replaceExecutable)
{
if (nui::HasMainUI())
{
g_canceled = false;
g_cancelable = true;
g_hadError = false;
std::string currentPoolSizesIncreaseSetting = "";
if (!fx::PoolSizeManager::GetIncreaseRequest().empty())
{
currentPoolSizesIncreaseSetting = nlohmann::json(fx::PoolSizeManager::GetIncreaseRequest()).dump();
}
auto j = nlohmann::json::object({
{ "build", build },
{ "pureLevel", pureLevel },
{ "poolSizesIncrease", ToNarrow(poolSizesIncreaseSetting) },
{ "replaceExecutable", replaceExecutable },
{ "currentBuild", xbr::GetRequestedGameBuild() },
{ "currentPureLevel", fx::client::GetPureLevel() },
{ "currentPoolSizesIncrease", std::move(currentPoolSizesIncreaseSetting) },
{ "currentReplaceExecutable", xbr::GetReplaceExecutable() }
});
nui::PostFrameMessage("mpMenu", fmt::sprintf(R"({ "type": "connectBuildSwitchRequest", "data": %s })", j.dump()));
g_submitFn = [build, pureLevel, poolSizesIncreaseSetting = std::move(poolSizesIncreaseSetting), replaceExecutable](const std::string& action)
{
if (action == "ok")
{
PerformStateSwitch(build, pureLevel, std::move(poolSizesIncreaseSetting), replaceExecutable);
}
};
}
}
void PerformStateSwitch(int build, int pureLevel, std::wstring poolSizesIncreaseSetting, bool replaceExecutable)
{
if (gameCacheTargetBuild != 0)
{
return;
}
gameCacheTargetBuild = build;
gameCacheReplaceExecutable = replaceExecutable;
std::thread([pureLevel, poolSizesIncreaseSetting = std::move(poolSizesIncreaseSetting), replaceExecutable]()
{
// let's try to update the game cache
if (!UpdateGameCache().empty())
{
RestartGameToOtherBuild(gameCacheTargetBuild, pureLevel, std::move(poolSizesIncreaseSetting), replaceExecutable);
}
// display a generic error if we failed
else if (!g_hadError && !g_canceled)
{
netLibrary->OnConnectionError("Changing game build failed: An unknown error occurred");
}
gameCacheTargetBuild = 0;
}).detach();
}
static HANDLE g_buttonEvent;
static bool g_buttonResponse;
void TaskDialogEmulated(TASKDIALOGCONFIG* config, int* button, void*, void*)
{
if (config->dwCommonButtons == 0)
{
*button = 42;
netLibrary->OnConnectionError(ToNarrow(config->pszContent).c_str());
g_hadError = true;
}
else
{
if (nui::HasMainUI())
{
auto j = nlohmann::json::object({
{ "title", ToNarrow(config->pszMainInstruction) },
{ "content", ToNarrow(config->pszContent) },
});
nui::PostFrameMessage("mpMenu", fmt::sprintf(R"({ "type": "connectBuildSwitch", "data": %s })", j.dump()));
}
g_submitFn = [](const std::string& action)
{
g_buttonResponse = (action == "ok");
SetEvent(g_buttonEvent);
};
g_buttonEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
WaitForSingleObject(g_buttonEvent, INFINITE);
CloseHandle(g_buttonEvent);
g_buttonEvent = nullptr;
*button = g_buttonResponse ? IDYES : IDNO;
}
}
bool XBR_InterceptCardResponse(const nlohmann::json& j)
{
if (g_submitFn)
{
auto jcp = j;
g_submitFn(jcp["data"]["action"]);
g_submitFn = {};
return true;
}
return false;
}
bool XBR_InterceptCancelDefer()
{
if (g_cancelable)
{
if (g_submitFn)
{
g_submitFn("cancel");
g_submitFn = {};
}
g_canceled = true;
g_cancelable = false;
return true;
}
return false;
}
HWND UI_GetWindowHandle()
{
return CoreGetGameWindow();
}
bool UI_IsCanceled()
{
return g_canceled;
}
void UI_DoCreation(bool)
{
}
void UI_DoDestruction()
{
}
static std::string g_topText;
static std::string g_bottomText;
static double g_percentage;
static void UpdateProgressUX()
{
// don't submit more progress when we're canceled
if (g_canceled)
{
return;
}
auto text = fmt::sprintf("%s (%.0f%s)\n%s", g_topText, round(g_percentage), "%", g_bottomText);
netLibrary->OnConnectionProgress(text, 0, 100, true);
}
void UI_UpdateProgress(double percentage)
{
g_percentage = percentage;
UpdateProgressUX();
}
void UI_UpdateText(int textControl, const wchar_t* text)
{
(textControl == 0 ? g_topText : g_bottomText) = ToNarrow(text);
UpdateProgressUX();
}
void UI_DisplayError(const wchar_t* error)
{
trace("Game cache error: %s\n", ToNarrow(error));
g_hadError = true;
netLibrary->OnConnectionError(fmt::sprintf("Changing game build failed: %s", ToNarrow(error)).c_str());
}
void UI_SetSnailState(bool)
{
}