quantum-space-buddies/EpicRerouter/ExeSide/EpicPlatformManager.cs

138 lines
3.2 KiB
C#
Raw Normal View History

2022-02-07 06:22:58 +00:00
using Epic.OnlineServices;
using Epic.OnlineServices.Auth;
using Epic.OnlineServices.Platform;
using System;
2022-02-07 07:09:55 +00:00
namespace EpicRerouter.ExeSide
2022-02-07 06:22:58 +00:00
{
public static class EpicPlatformManager
{
2022-02-07 06:37:44 +00:00
private const string _eosProductID = "prod-starfish";
private const string _eosSandboxID = "starfish";
private const string _eosDeploymentID = "e176ecc84fbc4dd8934664684f44dc71";
private const string _eosClientID = "5c553c6accee4111bc8ea3a3ae52229b";
private const string _eosClientSecret = "k87Nfp75BzPref4nJFnnbNjYXQQR";
2022-02-07 06:22:58 +00:00
2022-02-07 06:37:44 +00:00
public static PlatformInterface PlatformInterface;
public static EpicAccountId LocalUserId;
2022-02-07 06:22:58 +00:00
2022-02-07 06:37:44 +00:00
public static OWEvent OnAuthSuccess = new(1);
2022-02-07 06:22:58 +00:00
public static void Init()
{
if (PlatformInterface == null)
{
try
{
InitPlatform();
}
catch (EOSInitializeException ex)
{
if (ex.Result == Result.AlreadyConfigured)
{
2022-02-07 06:40:55 +00:00
Console.Error.WriteLine("[EOS] platform already configured!");
2022-02-07 06:22:58 +00:00
}
}
}
Auth();
}
2022-02-07 06:40:55 +00:00
public static void Tick() =>
2022-02-07 06:37:44 +00:00
PlatformInterface.Tick();
2022-02-07 06:22:58 +00:00
public static void Uninit()
{
PlatformInterface.Release();
PlatformInterface = null;
PlatformInterface.Shutdown();
}
private static void InitPlatform()
{
var result = PlatformInterface.Initialize(new InitializeOptions
{
2022-02-07 06:37:44 +00:00
ProductName = Program.ProductName,
ProductVersion = Program.Version
2022-02-07 06:22:58 +00:00
});
if (result != Result.Success)
{
throw new EOSInitializeException("Failed to initialize Epic Online Services platform: ", result);
}
var options = new Options
{
2022-02-07 06:37:44 +00:00
ProductId = _eosProductID,
SandboxId = _eosSandboxID,
2022-02-07 06:22:58 +00:00
ClientCredentials = new ClientCredentials
{
2022-02-07 06:37:44 +00:00
ClientId = _eosClientID,
ClientSecret = _eosClientSecret
2022-02-07 06:22:58 +00:00
},
2022-02-07 06:37:44 +00:00
DeploymentId = _eosDeploymentID
2022-02-07 06:22:58 +00:00
};
PlatformInterface = PlatformInterface.Create(options);
2022-02-07 06:40:55 +00:00
Console.WriteLine("[EOS] Platform interface has been created");
2022-02-07 06:22:58 +00:00
}
private static void Auth()
{
2022-02-07 06:40:55 +00:00
Console.WriteLine("[EOS] Authenticating...");
2022-02-07 06:22:58 +00:00
var loginOptions = new LoginOptions
{
Credentials = new Credentials
{
Type = LoginCredentialType.ExchangeCode,
Id = null,
Token = GetPasswordFromCommandLine()
},
ScopeFlags = 0
};
if (PlatformInterface == null)
{
2022-02-07 06:40:55 +00:00
Console.Error.WriteLine("[EOS] Platform interface is null!");
2022-02-07 06:37:44 +00:00
return;
2022-02-07 06:22:58 +00:00
}
PlatformInterface.GetAuthInterface().Login(loginOptions, null, OnLogin);
}
private static string GetPasswordFromCommandLine()
{
var commandLineArgs = Environment.GetCommandLineArgs();
foreach (var arg in commandLineArgs)
{
if (arg.Contains("AUTH_PASSWORD"))
{
return arg.Split('=')[1];
}
}
return null;
}
private static void OnLogin(LoginCallbackInfo loginCallbackInfo)
{
if (loginCallbackInfo.ResultCode == Result.Success)
{
LocalUserId = loginCallbackInfo.LocalUserId;
2022-02-07 08:13:33 +00:00
LocalUserId.ToString(out var s);
Console.WriteLine($"[EOS SDK] login success! user ID: {s}");
2022-02-07 06:37:44 +00:00
OnAuthSuccess.Invoke();
2022-02-07 06:22:58 +00:00
return;
}
2022-02-07 06:40:55 +00:00
Console.Error.WriteLine("[EOS SDK] Login failed");
2022-02-07 06:22:58 +00:00
}
2022-02-07 06:37:44 +00:00
private class EOSInitializeException : Exception
2022-02-07 06:22:58 +00:00
{
public readonly Result Result;
2022-02-07 06:40:55 +00:00
public EOSInitializeException(string msg, Result initResult) :
base(msg) =>
Result = initResult;
2022-02-07 06:22:58 +00:00
}
}
}