1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
using System;
using System.Runtime.InteropServices;
namespace Steamworks;
public abstract class CallResult
{
internal abstract Type GetCallbackType();
internal abstract void OnRunCallResult(IntPtr pvParam, bool bFailed, ulong hSteamAPICall);
internal abstract void SetUnregistered();
}
public sealed class CallResult<T> : CallResult, IDisposable
{
public delegate void APIDispatchDelegate(T param, bool bIOFailure);
private SteamAPICall_t m_hAPICall = SteamAPICall_t.Invalid;
private bool m_bDisposed;
public SteamAPICall_t Handle => m_hAPICall;
private event APIDispatchDelegate m_Func;
public static CallResult<T> Create(APIDispatchDelegate func = null)
{
return new CallResult<T>(func);
}
public CallResult(APIDispatchDelegate func = null)
{
this.m_Func = func;
}
~CallResult()
{
Dispose();
}
public void Dispose()
{
if (!m_bDisposed)
{
GC.SuppressFinalize(this);
Cancel();
m_bDisposed = true;
}
}
public void Set(SteamAPICall_t hAPICall, APIDispatchDelegate func = null)
{
if (func != null)
{
this.m_Func = func;
}
if (this.m_Func == null)
{
throw new Exception("CallResult function was null, you must either set it in the CallResult Constructor or via Set()");
}
if (m_hAPICall != SteamAPICall_t.Invalid)
{
CallbackDispatcher.Unregister(m_hAPICall, this);
}
m_hAPICall = hAPICall;
if (hAPICall != SteamAPICall_t.Invalid)
{
CallbackDispatcher.Register(hAPICall, this);
}
}
public bool IsActive()
{
return m_hAPICall != SteamAPICall_t.Invalid;
}
public void Cancel()
{
if (IsActive())
{
CallbackDispatcher.Unregister(m_hAPICall, this);
}
}
internal override Type GetCallbackType()
{
return typeof(T);
}
internal override void OnRunCallResult(IntPtr pvParam, bool bFailed, ulong hSteamAPICall_)
{
if ((SteamAPICall_t)hSteamAPICall_ == m_hAPICall)
{
try
{
this.m_Func((T)Marshal.PtrToStructure(pvParam, typeof(T)), bFailed);
}
catch (Exception e)
{
CallbackDispatcher.ExceptionHandler(e);
}
}
}
internal override void SetUnregistered()
{
m_hAPICall = SteamAPICall_t.Invalid;
}
}
|