32 lines
923 B
C#
32 lines
923 B
C#
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;
|
|
}
|
|
} |