-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
536 lines (495 loc) · 22.8 KB
/
Program.cs
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace EricLauncher
{
internal class Program
{
static string BaseAppDataFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) + "/EricLauncher";
static string BaseOVTFolder = BaseAppDataFolder + "/OVT";
static string RedirectURL = "https://www.epicgames.com/id/api/redirect?clientId=" + EpicLogin.LAUNCHER_CLIENT + "&responseType=code";
static void PrintUsage()
{
Console.WriteLine("Usage: EricLauncher.exe [game executable path or verb] (options) (game arguments)");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --accountId [id] - use a specific Epic Games account ID to sign in.");
Console.WriteLine(" --account [username] - use a specific Epic Games account username to sign in.");
Console.WriteLine(" --noManifest - don't check the local Epic Games Launcher install folder for the manifest.");
Console.WriteLine(" --stayOpen - keeps EricLauncher open in the background until the game is closed.");
Console.WriteLine(" --dryRun - goes through the Epic Games login flow, but does not launch the game.");
Console.WriteLine(" --offline - skips the Epic Games login flow, to launch the game in offline mode.");
Console.WriteLine(" --manifest [file] - specify a specific manifest file to use.");
Console.WriteLine();
Console.WriteLine("Verbs:");
Console.WriteLine(" logout - Logs out of Epic Games.");
Console.WriteLine();
}
static async Task Main(string[] args)
{
if (args.Length == 0) {
PrintUsage();
return;
}
bool needs_code_login = false;
EpicLogin login = new EpicLogin();
EpicAccount? account = null;
// parse the cli arguments
string? account_id = null;
string? account_name = null;
string? manifest_path = null;
bool set_default = false;
bool no_manifest = false;
bool stay_open = false;
bool dry_run = false;
bool offline = false;
bool skip_fortnite_update = false;
bool caldera = false;
string extra_args = "";
if (args.Length > 1)
{
for (int i = 1; i < args.Length; i++)
{
if (args[i] == "--accountId")
account_id = args[++i];
if (args[i] == "--account")
account_name = args[++i];
else if (args[i] == "--manifest")
manifest_path = args[++i];
else if (args[i] == "--setDefault")
set_default = true;
else if (args[i] == "--noManifest")
no_manifest = true;
else if (args[i] == "--stayOpen")
stay_open = true;
else if (args[i] == "--dryRun")
dry_run = true;
else if (args[i] == "--offline")
offline = true;
else if (args[i] == "--caldera")
caldera = true;
else if (args[i] == "--noCheckFn")
skip_fortnite_update = true;
else
extra_args += args[i] + " ";
}
}
// both of these being null implies setting a default account
if (account_id == null && account_name == null)
set_default = true;
string exe_name = args[0];
// handle special exe names
bool exchange_code_only = false;
bool caldera_only = false;
bool access_token_only = false;
bool logout = false;
if (exe_name.StartsWith("exchange")) exchange_code_only = true;
if (exe_name.EndsWith("caldera")) caldera_only = true;
if (exe_name == "access") access_token_only = true;
if (exe_name == "logout") logout = true;
// all these options imply an online dry run with no manifest
if (exchange_code_only || caldera_only || access_token_only || logout)
{
no_manifest = true;
dry_run = true;
offline = false;
}
// always run as a dry run if we're on Linux or FreeBSD
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
dry_run = true;
// if we're launching fortnite, do an update check
if (!skip_fortnite_update && !offline &&
Path.GetFileName(exe_name).ToLower() == "fortnitelauncher.exe")
{
Console.WriteLine("Checking for Fortnite updates...");
// traverse back to the cloud content json
try
{
string cloudcontent_path = Path.GetDirectoryName(exe_name) + @"\..\..\..\Cloud\cloudcontent.json";
string jsonstring = File.ReadAllText(cloudcontent_path);
FortniteCloudContent? cloudcontent = JsonSerializer.Deserialize<FortniteCloudContent>(jsonstring);
Console.WriteLine($"Current version: {cloudcontent!.BuildVersion!} ({cloudcontent!.Platform!})");
bool is_up_to_date = await FortniteUpdateCheck.IsUpToDate(cloudcontent!.BuildVersion!, cloudcontent!.Platform!);
if (!is_up_to_date)
{
Console.WriteLine("Fortnite is not the latest version!");
Console.WriteLine("Please open the Epic Games Launcher to start updating the game.");
Thread.Sleep(2500);
return;
}
} catch
{
Console.WriteLine("There was an error checking for Fortnite updates.");
Console.WriteLine("The game might not let you online. Continuing anyway...");
}
}
// check if we have an account saved already
StoredAccountInfo? storedInfo = null;
if (account_name == null && account_id == null)
account_id = GetDefaultAccount();
if (account_name != null)
storedInfo = GetAccountInfoByName(account_name);
if (account_id != null)
storedInfo = GetAccountInfo(account_id);
if (storedInfo == null)
{
needs_code_login = true;
} else
{
if (storedInfo.DisplayName != null)
Console.Write($"Logging in as {storedInfo.DisplayName} ({storedInfo.AccountId})...");
else
Console.Write($"Logging in as {storedInfo.AccountId}...");
account = new(storedInfo);
if (!offline)
{
// check the expiry date, if the access token has expired then just refresh straight away, otherwise verify our access token
bool verified = account.AccessExpiry >= DateTime.UtcNow ? await account.VerifyToken() : false;
if (!verified)
{
Console.Write("refreshing...");
account = null;
try
{
account = await login.LoginWithRefreshToken(storedInfo.RefreshToken!);
Console.WriteLine("success!");
}
catch { }
if (account == null)
{
Console.WriteLine("failed.");
Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Console.WriteLine("@ WARNING: EPIC GAMES REFRESH TOKEN HAS CHANGED! @");
Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Console.WriteLine("IT IS POSSIBLE THAT SWEENEY IS DOING SOMETHING EPIC!");
needs_code_login = true;
}
} else
{
Console.WriteLine("success!");
}
} else
{
Console.WriteLine("offline.");
}
}
// we don't have an account so refresh credentials with an authorization code
if (needs_code_login)
{
Console.WriteLine($"Open this URL in a web browser (signed into your Epic Games account): {RedirectURL}");
Console.Write("Paste the 'authorizationCode' value: ");
string? auth_code = Console.ReadLine();
if (auth_code == null || auth_code == "")
{
Console.WriteLine("Invalid code!");
return;
}
try
{
account = await login.LoginWithAuthorizationCode(auth_code);
}
catch { }
if (account == null)
{
Console.WriteLine("Failed to log in!");
return;
}
}
// if the user provided an account id at the command line but this isn't the same account, quit out
if (!set_default && account_id != null && account!.AccountId != account_id)
{
Console.WriteLine($"Logged in, but the account ID ({account.AccountId}) isn't the same as the one selected ({account_id}).");
// save the account info later just to save time
StoreAccountInfo(account!.MakeStoredAccountInfo());
return;
}
if (account_name != null && account!.DisplayName != account_name)
{
Console.WriteLine($"Logged in, but the account name ({account.DisplayName}) isn't the same as the one selected ({account_name}).");
// save the account info later just to save time
StoreAccountInfo(account!.MakeStoredAccountInfo());
return;
}
// we've logged in successfully!
if (account!.DisplayName != null)
Console.WriteLine($"Logged in as {account.DisplayName} ({account.AccountId})!");
else
Console.WriteLine($"Logged in as {account.AccountId}!");
if (logout)
{
DeleteAccountInfo(account.AccountId!);
if (GetDefaultAccount() == account.AccountId!)
DeleteDefaultAccount();
bool success = await account.Logout();
if (success)
Console.WriteLine("Successfully logged out!");
else // hdd still doesn't have session but it exists in backend...
Console.WriteLine("Logged out!");
return;
}
// save our refresh token for later usage
if (!Directory.Exists(BaseAppDataFolder))
Directory.CreateDirectory(BaseAppDataFolder);
StoreAccountInfo(account!.MakeStoredAccountInfo(), set_default);
// fetch the game's manifest from the installed epic games launcher
EGLManifest? manifest = null;
if (!no_manifest && manifest_path == null)
{
// always use FortniteLauncher.exe manifest for FortniteGame
// so many edge cases im boutta bust
if (Path.GetFileName(exe_name).StartsWith("FortniteGame"))
manifest = GetEGLManifest("FortniteLauncher.exe");
else
manifest = GetEGLManifest(Path.GetFileName(exe_name));
} else if (!no_manifest && manifest_path != null)
{
string jsonstring = File.ReadAllText(manifest_path);
manifest = JsonSerializer.Deserialize<EGLManifest>(jsonstring);
}
if (manifest == null && !no_manifest)
{
Console.WriteLine("Manifest wasn't loaded! The game might not work properly.");
Console.WriteLine("(Try launching the game via the Epic Games Launcher at least once.)");
}
// launch the game
string exchange = "";
if (!offline)
exchange = await account.GetExchangeCode();
if (exchange_code_only)
{
Console.WriteLine($"Exchange Code: {exchange!}");
if (!caldera_only) return;
}
if (access_token_only)
{
EpicLogin fnLogin = new(EpicLogin.FORTNITE_PC_CLIENT, EpicLogin.FORTNITE_PC_SECRET);
EpicAccount? fnAccount = await fnLogin.LoginWithExchangeCode(exchange);
if (fnAccount != null)
{
Console.WriteLine($"Access Token: {fnAccount.AccessToken}");
} else
{
Console.WriteLine("Failed to get access token.");
}
return;
}
// caldera simulation
if ((caldera && Path.GetFileName(exe_name).StartsWith("Fortnite")) || caldera_only)
{
string? gamedir = Path.GetDirectoryName(exe_name);
CalderaResponse? cal_resp = await EpicCaldera.GetCalderaResponse(account_id!, exchange, "fortnite");
string acargs = $" -caldera={cal_resp!.jwt}";
string acexe = "FortniteClient-Win64-Shipping";
switch (cal_resp.provider)
{
case "EasyAntiCheatEOS":
acargs += " -fromfl=eaceos -noeac -nobe ";
acexe += "_EAC_EOS.exe";
break;
case "EasyAntiCheat":
acargs += " -fromfl=eac -noeaceos -nobe ";
acexe += "_EAC.exe";
break;
case "BattlEye":
acargs += " -fromfl=be -noeaceos -noeac ";
acexe += "_BE.exe";
break;
default:
Console.WriteLine($"Unknown Caldera provider '{cal_resp.provider}'.");
return;
}
extra_args += acargs;
exe_name = Path.Combine(gamedir!, acexe);
if (caldera_only)
{
Console.WriteLine($"AC Provider: {cal_resp.provider!}");
Console.WriteLine($"AC JWT: {cal_resp.jwt!}");
return;
}
}
Console.WriteLine("Launching game...");
Process game = await LaunchGame(exe_name, exchange, account, manifest, dry_run, offline, extra_args);
if (stay_open && !dry_run)
{
game.WaitForExit();
Console.WriteLine($"Game exited with code {game.ExitCode}");
}
}
static async Task<Process> LaunchGame(string filename, string? exchange, EpicAccount? account, EGLManifest? manifest, bool dry_run, bool skip_ovt, string launch_args)
{
Process p = new Process();
p.StartInfo.FileName = filename;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(filename);
p.StartInfo.ArgumentList.Add($"-epicenv=Prod");
p.StartInfo.ArgumentList.Add($"-epiclocale=en-US");
p.StartInfo.ArgumentList.Add($"-EpicPortal");
p.StartInfo.ArgumentList.Add($"-AUTH_LOGIN=unused");
if (exchange != null)
{
p.StartInfo.ArgumentList.Add($"-AUTH_TYPE=exchangecode");
p.StartInfo.ArgumentList.Add($"-AUTH_PASSWORD={exchange}");
}
if (account != null)
{
p.StartInfo.ArgumentList.Add($"-epicuserid={account.AccountId}");
if (account.DisplayName != null)
p.StartInfo.ArgumentList.Add($"-epicusername=\"{account.DisplayName}\"");
}
if (manifest != null)
{
p.StartInfo.ArgumentList.Add($"-epicsandboxid={manifest.MainGameCatalogNamespace}");
p.StartInfo.ArgumentList.Add($"-epicapp={manifest.MainGameAppName}");
if (manifest.LaunchCommand != null && manifest.LaunchCommand.Length > 0)
{
string[] split_args = manifest.LaunchCommand.Split(' ');
foreach (string arg in split_args)
p.StartInfo.ArgumentList.Add(arg);
}
}
if (launch_args != "")
{
string[] split_args = launch_args.Split(' ');
foreach (string arg in split_args)
p.StartInfo.ArgumentList.Add(arg);
}
if (account != null && manifest != null && !skip_ovt &&
manifest.OwnershipToken == "true")
{
string? epicovt_path = await GetOwnershipTokenPath(account, manifest);
if (epicovt_path != null)
p.StartInfo.ArgumentList.Add($"-epicovt=\"{epicovt_path}\"");
}
if (!dry_run)
p.Start();
else
{
string full_command = filename + " ";
foreach (string arg in p.StartInfo.ArgumentList)
{
full_command += arg + " ";
}
Console.WriteLine("Launch: " + full_command);
}
return p;
}
static async Task<string?> GetOwnershipTokenPath(EpicAccount account, EGLManifest manifest)
{
Directory.CreateDirectory(BaseOVTFolder);
string ovt_path = $"{BaseOVTFolder}/{account!.AccountId!}-{manifest.MainGameAppName!}.ovt";
EpicEcom ecom = new(account);
string? epicovt = await ecom.GetOwnershipToken(manifest.CatalogNamespace!, manifest.CatalogItemId!);
if (epicovt != null)
{
File.WriteAllText(ovt_path, epicovt);
return ovt_path;
} else return null;
}
static EGLManifest? GetEGLManifest(string executable_name)
{
IEnumerable<string> files;
string manifestfolder = "/Epic/EpicGamesLauncher/Data/Manifests";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) // .NET 7 doesn't make SpecialFolder.LocalAppliactionData go to the correct folder :)
manifestfolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Application Support") + manifestfolder;
else
manifestfolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData) + manifestfolder;
try
{
files = Directory.EnumerateFiles(manifestfolder);
} catch {
return null;
}
foreach (string file in files)
{
try
{
string jsonstring = File.ReadAllText(file);
EGLManifest? manifest = JsonSerializer.Deserialize<EGLManifest>(jsonstring);
if (manifest != null && manifest.LaunchExecutable != null &&
Path.GetFileName(manifest.LaunchExecutable).ToLower() == executable_name.ToLower())
{
return manifest;
}
}
catch { }
}
return null;
}
static void StoreAccountInfo(StoredAccountInfo info, bool set_default = false)
{
string jsonstring = JsonSerializer.Serialize(info);
File.WriteAllText($"{BaseAppDataFolder}/{info.AccountId!}.json", jsonstring);
if (set_default)
File.WriteAllText($"{BaseAppDataFolder}/default.json", $"{{\"AccountId\": \"{info.AccountId!}\"}}");
}
static void DeleteAccountInfo(string account_id)
{
string path = $"{BaseAppDataFolder}/{account_id}.json";
try
{
File.Delete(path);
}
catch { }
return;
}
static StoredAccountInfo? GetAccountInfo(string account_id)
{
string path = $"{BaseAppDataFolder}/{account_id}.json";
if (!File.Exists(path))
return null;
try
{
string jsonstring = File.ReadAllText(path);
StoredAccountInfo? info = JsonSerializer.Deserialize<StoredAccountInfo>(jsonstring);
if (account_id == null || (info != null && account_id == info.AccountId))
return info;
} catch { }
return null;
}
static StoredAccountInfo? GetAccountInfoByName(string display_name)
{
IEnumerable<string> files = Directory.EnumerateFiles(BaseAppDataFolder);
foreach (string filename in files)
{
if (Path.GetFileNameWithoutExtension(filename).Length != 32) // account id length + .json
continue;
try
{
string jsonstring = File.ReadAllText(filename);
StoredAccountInfo? info = JsonSerializer.Deserialize<StoredAccountInfo>(jsonstring);
if (info != null && info.DisplayName != null && display_name == info.DisplayName)
return info;
}
catch { }
}
return null;
}
static string? GetDefaultAccount()
{
string path = $"{BaseAppDataFolder}/default.json";
if (!File.Exists(path))
return null;
try
{
string jsonstring = File.ReadAllText(path);
StoredAccountInfo? info = JsonSerializer.Deserialize<StoredAccountInfo>(jsonstring);
if (info != null)
return info.AccountId;
}
catch { }
return null;
}
static void DeleteDefaultAccount()
{
string path = $"{BaseAppDataFolder}/default.json";
try
{
File.Delete(path);
}
catch { }
return;
}
}
}