Skip to content

Commit

Permalink
select account by username and change default handling
Browse files Browse the repository at this point in the history
  • Loading branch information
InvoxiPlayGames committed Nov 23, 2023
1 parent 04433f7 commit 813afb5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 42 deletions.
106 changes: 72 additions & 34 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -16,12 +17,13 @@ static void PrintUsage()
Console.WriteLine("Usage: EricLauncher.exe [executable path] (options) (game arguments)");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --accountId [id] - use a specific Epic Games account ID 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(" --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();
}

Expand All @@ -37,6 +39,7 @@ static async Task Main(string[] args)

// parse the cli arguments
string? account_id = null;
string? account_name = null;
string? manifest_path = null;
bool set_default = false;
bool no_manifest = false;
Expand All @@ -52,6 +55,8 @@ static async Task Main(string[] args)
{
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")
Expand All @@ -72,6 +77,9 @@ static async Task Main(string[] args)
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];

Expand Down Expand Up @@ -120,23 +128,13 @@ static async Task Main(string[] args)
}

// check if we have an account saved already
StoredAccountInfo? storedInfo;
bool is_default = account_id == null || set_default;
if (account_id != null) {
Console.WriteLine($"Using account {account_id}");
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);
// check if the account id specified is the current default account
if (!set_default)
{
StoredAccountInfo? default_account = GetAccountInfo();
if (default_account?.AccountId == storedInfo?.AccountId)
is_default = true;
}
} else
{
Console.WriteLine("Using default account");
storedInfo = GetAccountInfo();
}
if (storedInfo == null)
{
needs_code_login = true;
Expand Down Expand Up @@ -206,11 +204,18 @@ static async Task Main(string[] args)
}

// if the user provided an account id at the command line but this isn't the same account, quit out
if (account_id != null && account!.AccountId != account_id)
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 ID ({account.AccountId}) isn't the same as the one provided at the command line ({account_id}).");
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(), false);
StoreAccountInfo(account!.MakeStoredAccountInfo());
return;
}

Expand All @@ -223,7 +228,7 @@ static async Task Main(string[] args)
// save our refresh token for later usage
if (!Directory.Exists(BaseAppDataFolder))
Directory.CreateDirectory(BaseAppDataFolder);
StoreAccountInfo(account!.MakeStoredAccountInfo(), is_default);
StoreAccountInfo(account!.MakeStoredAccountInfo(), set_default);

// fetch the game's manifest from the installed epic games launcher
EGLManifest? manifest = null;
Expand Down Expand Up @@ -365,7 +370,7 @@ static async Task<Process> LaunchGame(string filename, string? exchange, EpicAcc
static async Task<string?> GetOwnershipTokenPath(EpicAccount account, EGLManifest manifest)
{
Directory.CreateDirectory(BaseOVTFolder);
string ovt_path = $"{BaseOVTFolder}/{manifest.MainGameAppName!}.ovt";
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)
Expand Down Expand Up @@ -407,19 +412,17 @@ static async Task<Process> LaunchGame(string filename, string? exchange, EpicAcc
return null;
}

static void StoreAccountInfo(StoredAccountInfo info, bool is_default = true)
static void StoreAccountInfo(StoredAccountInfo info, bool set_default = false)
{
string jsonstring = JsonSerializer.Serialize(info);
File.WriteAllText($"{BaseAppDataFolder}/{info.AccountId!}.json", jsonstring);
if (is_default)
File.WriteAllText($"{BaseAppDataFolder}/default.json", jsonstring);
if (set_default)
File.WriteAllText($"{BaseAppDataFolder}/default.json", $"{{\"AccountId\": \"{info.AccountId!}\"}}");
}

static StoredAccountInfo? GetAccountInfo(string? account_id = null)
static StoredAccountInfo? GetAccountInfo(string account_id)
{
string path = $"{BaseAppDataFolder}/default.json";
if (account_id != null && File.Exists($"{BaseAppDataFolder}/{account_id}.json"))
path = $"{BaseAppDataFolder}/{account_id}.json";
string path = $"{BaseAppDataFolder}/{account_id}.json";
if (!File.Exists(path))
return null;
try
Expand All @@ -431,5 +434,40 @@ static void StoreAccountInfo(StoredAccountInfo info, bool is_default = true)
} 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($"{BaseAppDataFolder}/default.json"))
return null;
try
{
string jsonstring = File.ReadAllText(path);
StoredAccountInfo? info = JsonSerializer.Deserialize<StoredAccountInfo>(jsonstring);
if (info != null)
return info.AccountId;
}
catch { }
return null;
}
}
}
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This application was written for personal usage and isn't 100% user oriented. If
## Features

- Logging in to Epic Games accounts.
- Multi-account support. (specify an `--accountId` at the command line.)
- Multi-account support. (specify an `--accountId` or `--account` at the command line.)
- Checking for Fortnite updates. (if an update is available, the game will not launch.)
- Windows and macOS support, as well as providing launch args on Linux.
- Support for at least some games. (tested with Fortnite, Borderlands 3, Death Stranding and FUSER.)
Expand All @@ -25,15 +25,16 @@ This is designed to be run from the command line, but you can drag and drop a ga
Alternatively you could make a batch file or shortcut, or create a shortcut in a launcher such as Steam - pointing to EricLauncher, with the game you want to launch as the launch arguments

```
Usage: EricLauncher.exe [executable path] (options)
Usage: EricLauncher.exe [executable path] (options) (game arguments)
Options:
--accountId [id] - use a specific Epic Games account ID to sign in.
--noManifest - don't check the local Epic Games Launcher install folder for the manifest.
--stayOpen - keeps EricLauncher open in the background until the game is closed.
--dryRun - goes through the Epic Games login flow, but does not launch the game.
--offline - skips the Epic Games login flow, to launch the game in offline mode.
--manifest [file] - specify a specific manifest file to use.
--accountId [id] - use a specific Epic Games account ID to sign in.
--account [username] - use a specific Epic Games account username to sign in.
--noManifest - don't check the local Epic Games Launcher install folder for the manifest.
--stayOpen - keeps EricLauncher open in the background until the game is closed.
--dryRun - goes through the Epic Games login flow, but does not launch the game.
--offline - skips the Epic Games login flow, to launch the game in offline mode.
--manifest [file] - specify a specific manifest file to use.
```

The account ID parameter is only required if you are using multiple accounts. Omitting this value will use (or save) a default account.
Expand Down

0 comments on commit 813afb5

Please sign in to comment.