Created
September 2, 2023 00:56
-
-
Save RyanGarber/6d4608d7e21b51b6e64d30e12ca87406 to your computer and use it in GitHub Desktop.
C# Async/Await for Epic Online Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EOSExtensions | |
{ | |
public static readonly uint ChunkSize = 4096; | |
#region Auth | |
public static async Task<Auth.LoginCallbackInfo> StartPersistentLogin(this EOSManager.EOSSingleton @this) | |
{ | |
Auth.LoginCallbackInfo? result = null; | |
@this.StartPersistentLogin(r => result = r); | |
while (!result.HasValue) await Task.Yield(); | |
return result.Value; | |
} | |
public static async Task<Auth.LoginCallbackInfo> StartLoginWithLoginOptions(this EOSManager.EOSSingleton @this, Auth.LoginOptions options) | |
{ | |
Auth.LoginCallbackInfo? result = null; | |
@this.StartLoginWithLoginOptions(options, r => result = r); | |
while (!result.HasValue) await Task.Yield(); | |
return result.Value; | |
} | |
#endregion | |
#region Connect | |
public static async Task<Connect.LoginCallbackInfo> StartConnectLoginWithEpicAccount(this EOSManager.EOSSingleton @this, EpicAccountId account) | |
{ | |
Connect.LoginCallbackInfo? result = null; | |
@this.StartConnectLoginWithEpicAccount(account, r => result = r); | |
while (!result.HasValue) await Task.Yield(); | |
return result.Value; | |
} | |
public static async Task<Connect.CreateUserCallbackInfo> CreateConnectUserWithContinuanceToken(this EOSManager.EOSSingleton @this, ContinuanceToken token) | |
{ | |
Connect.CreateUserCallbackInfo? result = null; | |
@this.CreateConnectUserWithContinuanceToken(token, r => result = r); | |
while (!result.HasValue) await Task.Yield(); | |
return result.Value; | |
} | |
public static async Task<Connect.QueryProductUserIdMappingsCallbackInfo> QueryProductUserIdMappings(this Connect.ConnectInterface @this, Connect.QueryProductUserIdMappingsOptions options) | |
{ | |
Connect.QueryProductUserIdMappingsCallbackInfo? result = null; | |
@this.QueryProductUserIdMappings(ref options, null, (ref Connect.QueryProductUserIdMappingsCallbackInfo r) => result = r); | |
while (!result.HasValue) await Task.Yield(); | |
return result.Value; | |
} | |
#endregion | |
#region PlayerDataStorage | |
public static async Task<string> ReadFile(this PlayerDataStorage.PlayerDataStorageInterface @this, string filename) | |
{ | |
byte[] bytes = null; | |
int position = 0; | |
PlayerDataStorage.ReadFileOptions options = new() | |
{ | |
Filename = filename, | |
LocalUserId = EOSManager.Instance.GetProductUserId(), | |
ReadChunkLengthBytes = ChunkSize, | |
ReadFileDataCallback = (ref PlayerDataStorage.ReadFileDataCallbackInfo result) => | |
{ | |
bytes ??= new byte[result.TotalFileSizeBytes]; | |
result.DataChunk.CopyTo(bytes, position); | |
position += result.DataChunk.Count; | |
return PlayerDataStorage.ReadResult.ContinueReading; | |
} | |
}; | |
@this.ReadFile(ref options, null, (ref PlayerDataStorage.ReadFileCallbackInfo result) => Core.Console.LogInfo($"ReadFile result: {result.ResultCode}")); | |
while (bytes == null || position < bytes.Length) | |
await Task.Yield(); | |
return System.Text.Encoding.UTF8.GetString(bytes); | |
} | |
public static async void WriteFile(this PlayerDataStorage.PlayerDataStorageInterface @this, string filename, string contents) | |
{ | |
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(contents); | |
int position = 0; | |
PlayerDataStorage.WriteFileOptions options = new() | |
{ | |
Filename = filename, | |
LocalUserId = EOSManager.Instance.GetProductUserId(), | |
ChunkLengthBytes = ChunkSize, | |
WriteFileDataCallback = (ref PlayerDataStorage.WriteFileDataCallbackInfo result, out ArraySegment<byte> chunk) => | |
{ | |
chunk = new(bytes, position, Mathf.Min(bytes.Length - position, (int)ChunkSize)); | |
position += chunk.Count; | |
return position == bytes.Length | |
? PlayerDataStorage.WriteResult.CompleteRequest | |
: PlayerDataStorage.WriteResult.ContinueWriting; | |
} | |
}; | |
@this.WriteFile(ref options, null, (ref PlayerDataStorage.WriteFileCallbackInfo result) => Core.Console.LogInfo($"WriteFile result: {result.ResultCode}")); | |
while (position < bytes.Length) | |
await Task.Yield(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment