Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Aug 8, 2022
1 parent 8df0b2e commit ddc5354
Show file tree
Hide file tree
Showing 38 changed files with 1,551 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/EpicSocket.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added Assets/EpicSocket/CHANGELOG.md
Empty file.
7 changes: 7 additions & 0 deletions Assets/EpicSocket/CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Assets/EpicSocket/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022, James Frowen, amd MirageNet Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions Assets/EpicSocket/LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/EpicSocket/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See full readme here https://github.com/MirageNet/EpicSocket
7 changes: 7 additions & 0 deletions Assets/EpicSocket/Readme.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/EpicSocket/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/EpicSocket/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Reflection;

[assembly: AssemblyVersion("0.1.0")]
11 changes: 11 additions & 0 deletions Assets/EpicSocket/Runtime/AssemblyInfo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Assets/EpicSocket/Runtime/AsyncWaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Cysharp.Threading.Tasks;

namespace Mirage.Sockets.EpicSocket
{
/// <summary>
/// Call that can wait for Callbacks async
/// </summary>
/// <remarks>
/// This must be a class not a struct, other wise copies will be made and callback wont set the _result field correctly for Wait
/// </remarks>
/// <typeparam name="T"></typeparam>
public class AsyncWaiter<T> where T : class
{
private T _result;

public void Callback(T result)
{
_result = result;
}
public async UniTask<T> Wait()
{
while (_result == null)
{
await UniTask.Yield();
}

return _result;
}
}
}

11 changes: 11 additions & 0 deletions Assets/EpicSocket/Runtime/AsyncWaiter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions Assets/EpicSocket/Runtime/DevAuthLogin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Cysharp.Threading.Tasks;
using Epic.OnlineServices;
using Epic.OnlineServices.Connect;
using PlayEveryWare.EpicOnlineServices;
using UnityEngine;

namespace Mirage.Sockets.EpicSocket
{
[System.Serializable]
public struct DevAuthSettings
{
public string CredentialName;
public int Port;
}

internal static class DevAuthLogin
{
public static async UniTask LoginAndConnect(DevAuthSettings settings)
{
// we must authenticate first,
// and then connect to relay
var user = await LogInWithDevAuth(settings);

await Connect(user);
}

private static async UniTask<EpicAccountId> LogInWithDevAuth(DevAuthSettings settings)
{
var type = Epic.OnlineServices.Auth.LoginCredentialType.Developer;
var id = $"localhost:{settings.Port}";
var token = settings.CredentialName;

var waiter = new AsyncWaiter<Epic.OnlineServices.Auth.LoginCallbackInfo>();
EOSManager.Instance.StartLoginWithLoginTypeAndToken(type, id, token, waiter.Callback);
var result = await waiter.Wait();

var epicAccountId = result.LocalUserId;
if (result.ResultCode == Result.InvalidUser)
epicAccountId = await CreateNewAccount(result.ContinuanceToken);
else if (result.ResultCode != Result.Success)
throw new EpicSocketException($"Failed to login with Dev auth, result code={result.ResultCode}");

return epicAccountId;
}

private static async UniTask<EpicAccountId> CreateNewAccount(ContinuanceToken continuanceToken)
{
Debug.Log($"Trying Auth link with external account: {continuanceToken}");

var waiter = new AsyncWaiter<Epic.OnlineServices.Auth.LinkAccountCallbackInfo>();
EOSManager.Instance.AuthLinkExternalAccountWithContinuanceToken(continuanceToken, Epic.OnlineServices.Auth.LinkAccountFlags.NoFlags, waiter.Callback);
var result = await waiter.Wait();
if (result.ResultCode != Result.Success)
throw new EpicSocketException($"Failed to login with Dev auth, result code={result.ResultCode}");

EpicLogger.Verbose($"Create New Account: [User:{result.ResultCode} Selected:{result.SelectedAccountId}]");
return result.LocalUserId;
}

private static async UniTask Connect(EpicAccountId user)
{
var firstTry = await _connect(user);

var result = firstTry.ResultCode;
if (result == Result.InvalidUser)
{
// ask user if they want to connect; sample assumes they do
var createWaiter = new AsyncWaiter<CreateUserCallbackInfo>();
EOSManager.Instance.CreateConnectUserWithContinuanceToken(firstTry.ContinuanceToken, createWaiter.Callback);
var createResult = await createWaiter.Wait();

Debug.Log("Created new account");

var secondTry = await _connect(user);
result = secondTry.ResultCode;
}

if (result != Result.Success)
throw new EpicSocketException($"Failed to login with Dev auth, result code={result}");
}

private static async UniTask<LoginCallbackInfo> _connect(EpicAccountId user)
{
var waiter = new AsyncWaiter<LoginCallbackInfo>();
EOSManager.Instance.StartConnectLoginWithEpicAccount(user, waiter.Callback);
var result = await waiter.Wait();
return result;
}
}
}

11 changes: 11 additions & 0 deletions Assets/EpicSocket/Runtime/DevAuthLogin.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions Assets/EpicSocket/Runtime/DeviceIdConnect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Cysharp.Threading.Tasks;
using Epic.OnlineServices;
using Epic.OnlineServices.Connect;
using PlayEveryWare.EpicOnlineServices;

namespace Mirage.Sockets.EpicSocket
{
internal static class DeviceIdConnect
{
public static async UniTask Connect(EOSManager.EOSSingleton manager, ConnectInterface connectInterface, string displayName)
{
var createInfo = await CreateDeviceIdAsync(connectInterface);

ThrowIfResultInvalid(createInfo);

var loginInfo = await LoginAsync(manager, displayName);

EpicLogger.logger.WarnResult("Login Callback", loginInfo.ResultCode);
}

private static void ThrowIfResultInvalid(CreateDeviceIdCallbackInfo createInfo)
{
if (createInfo.ResultCode == Result.Success)
return;

// already exists, this is ok
if (createInfo.ResultCode == Result.DuplicateNotAllowed)
{
EpicLogger.logger.Log($"Device Id already exists");
return;
}

if (createInfo.ResultCode != Result.Success && createInfo.ResultCode != Result.DuplicateNotAllowed)
throw new EpicSocketException($"Failed to Create DeviceId, Result code: {createInfo.ResultCode}");
}

private static UniTask<CreateDeviceIdCallbackInfo> CreateDeviceIdAsync(ConnectInterface connect)
{
var createOptions = new CreateDeviceIdOptions()
{
// todo get device model
#if UNITY_EDITOR
DeviceModel = "DemoModel_Editor"
#else
DeviceModel = "DemoModel"
#endif
};
var waiter = new AsyncWaiter<CreateDeviceIdCallbackInfo>();
connect.CreateDeviceId(createOptions, null, waiter.Callback);
return waiter.Wait();
}

private static UniTask<LoginCallbackInfo> LoginAsync(EOSManager.EOSSingleton manager, string displayName)
{
var waiter = new AsyncWaiter<LoginCallbackInfo>();
manager.StartConnectLoginWithDeviceToken(displayName, waiter.Callback);
return waiter.Wait();
}
}
}

11 changes: 11 additions & 0 deletions Assets/EpicSocket/Runtime/DeviceIdConnect.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Assets/EpicSocket/Runtime/EpicEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Epic.OnlineServices;
using Mirage.SocketLayer;

namespace Mirage.Sockets.EpicSocket
{
internal sealed class EpicEndPoint : IEndPoint
{
public ProductUserId UserId;

public EpicEndPoint() { }
private EpicEndPoint(ProductUserId userId)
{
UserId = userId ?? throw new ArgumentNullException(nameof(userId));
}

IEndPoint IEndPoint.CreateCopy()
{
return new EpicEndPoint(UserId);
}

public override bool Equals(object obj)
{
if (!(obj is EpicEndPoint other))
return false;

// both null
if (UserId == null && UserId == other.UserId)
return true;

return UserId.Equals(other.UserId);
}

public override int GetHashCode()
{
if (UserId == null)
return 0;

return UserId.GetHashCode();
}
}
}

11 changes: 11 additions & 0 deletions Assets/EpicSocket/Runtime/EpicEndPoint.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ddc5354

Please sign in to comment.