1
This commit is contained in:
32
Plugin.ClientModLoader/Utils/CallResultAsync.cs
Normal file
32
Plugin.ClientModLoader/Utils/CallResultAsync.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Steamworks;
|
||||
|
||||
namespace Plugin.ClientModLoader.Utils;
|
||||
|
||||
public static class CallResultAsync<T> where T : struct
|
||||
{
|
||||
public static async Task<T> Create(SteamAPICall_t call)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<T>();
|
||||
using var callResult = CallResult<T>.Create((args, failed) =>
|
||||
{
|
||||
if (failed)
|
||||
{
|
||||
ref var result = ref Unsafe.As<T, GenericCallResult>(ref args);
|
||||
tcs.TrySetException(new Exception($"CallResult failed: {result.m_eResult}"));
|
||||
}
|
||||
else tcs.TrySetResult(args);
|
||||
});
|
||||
callResult.Set(call);
|
||||
|
||||
return await tcs.Task;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
||||
private struct GenericCallResult
|
||||
{
|
||||
public UGCQueryHandle_t m_handle;
|
||||
public EResult m_eResult;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user