-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbacks.cs
179 lines (151 loc) · 5.95 KB
/
Callbacks.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Steamworksnt.SteamworksApi;
namespace Steamworksnt
{
public static class Callbacks
{
private static readonly HashSet<Int32> validCallbackIds = new HashSet<Int32>(
(Int32[])Enum.GetValues(typeof(Callback))
);
private static readonly Dictionary<ulong, (Action<object>, Type)> pendingApiCalls = new();
/// <summary>
/// Must be called once before anything else on this class.
/// </summary>
public static void Init()
{
// We use manual callbacks and do not call SteamAPI_RunCallbacks().
Api.SteamAPI_ManualDispatch_Init();
}
/// <summary>
/// This should be called once per frame (that is, in Update()), as it
/// is similar to "SteamAPI_RunCallbacks()" about which the Steamworks
/// SDK docs say "Most games call this once per render-frame".
/// </summary>
public static void RunFrame()
{
Int32 hSteamPipe = Api.SteamAPI_GetHSteamPipe();
if (hSteamPipe == 0)
{
return;
}
Api.SteamAPI_ManualDispatch_RunFrame(hSteamPipe);
// Declared before while loop for efficiency, could be declared as
// an "out" parameter below instead. This would make it easier to
// catch potential SDK issues by noticing an uninitialized callback
// struct.
CallbackMsg_t callback = default;
while (Api.SteamAPI_ManualDispatch_GetNextCallback(hSteamPipe, ref callback))
{
try
{
OnCallback(callback);
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
finally
{
Api.SteamAPI_ManualDispatch_FreeLastCallback(hSteamPipe);
}
}
}
/// <summary>
/// Should only be used by the "SteamAPICall_t" struct.
/// </summary>
public static Task<T> _GetApiCallResponse<T>(ulong apiCallId)
{
var tcs = new TaskCompletionSource<T>();
pendingApiCalls.Add(
apiCallId,
((object result) => tcs.SetResult((T)result), typeof(T))
);
return tcs.Task;
}
private static void OnCallback(CallbackMsg_t msg)
{
// Some callbacks are received with an unrecognized ID, which we ignore.
// See more: https://github.com/Facepunch/Facepunch.Steamworks/issues/507#issuecomment-1771804971
if (!validCallbackIds.Contains((Int32)msg.m_iCallback))
{
return;
}
// UnityEngine.Debug.Log(
// "(Steamworks SDK callback)\n\n"
// + $"{msg.m_iCallback}\n\n"
// + $"m_hSteamUser: {msg.m_hSteamUser}\n"
// + $"m_pubParam: {msg.m_pubParam}\n"
// + $"m_cubParam: {msg.m_cubParam}\n"
// );
// Special case - asynchronous Steamworks SDK API calls.
if (msg.m_iCallback == Callback.SteamAPICallCompleted_t)
{
HandleApiCallCompleted(msg);
}
}
private static void HandleApiCallCompleted(CallbackMsg_t msg)
{
Int32 hSteamPipe = Api.SteamAPI_GetHSteamPipe();
SteamAPICallCompleted_t apiCall = Marshal.PtrToStructure<SteamAPICallCompleted_t>(
msg.m_pubParam
);
// /!\ Important: not ignoring API call results with invalid IDs
// was causing crashes with seemingly random stack traces on the
// Unity editor log.
// Note we also receive callbacks with invalid IDs, not just API
// call results (and even when no SDK method that should trigger one
// had been called).
if (!validCallbackIds.Contains((Int32)apiCall.m_iCallback))
{
return;
}
if (!pendingApiCalls.ContainsKey(apiCall.m_hAsyncCall))
{
return;
}
var (callback, type) = pendingApiCalls[apiCall.m_hAsyncCall];
_ = pendingApiCalls.Remove(apiCall.m_hAsyncCall);
IntPtr resultPtr = Marshal.AllocHGlobal(msg.m_cubParam);
try
{
bool failed = false;
bool success = Api.SteamAPI_ManualDispatch_GetAPICallResult(
hSteamPipe,
apiCall.m_hAsyncCall,
resultPtr,
(int)apiCall.m_cubParam,
(Callback)apiCall.m_iCallback,
ref failed
);
if (!success)
{
// Steamworks SDK code example in "steam_api.h" ignores a
// return value of "false".
throw new Exception("GetAPICallResult() returned false.");
}
if (failed)
{
throw new Exception(
"Failure signaled by Steamworks SDK when attempting to "
+ "fetch asynchronous API call result."
);
}
UnityEngine.Debug.Log(
$"Got Steamworks SDK API call completed callback:"
+ $"m_hAsyncCall: {apiCall.m_hAsyncCall}"
+ $"m_iCallback: {apiCall.m_iCallback}"
+ $"m_cubParam: {apiCall.m_cubParam}"
);
object result = Marshal.PtrToStructure(resultPtr, type);
callback(result);
}
finally
{
Marshal.FreeHGlobal(resultPtr);
}
}
}
}