From f510c7f42a0ccc1a908f87c8a6f3f1f520a7eb3a Mon Sep 17 00:00:00 2001
From: Riley Labrecque <rileylabrecque@gmail.com>
Date: Wed, 5 Apr 2017 15:17:04 -0600
Subject: [PATCH] Fixed a crash when repeatedly setting the same CallResult
 within it's CallResult handler

---
 Plugins/Steamworks.NET/CallbackDispatcher.cs | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/Plugins/Steamworks.NET/CallbackDispatcher.cs b/Plugins/Steamworks.NET/CallbackDispatcher.cs
index 815d1800..4d9959a8 100644
--- a/Plugins/Steamworks.NET/CallbackDispatcher.cs
+++ b/Plugins/Steamworks.NET/CallbackDispatcher.cs
@@ -295,6 +295,7 @@ private void OnRunCallback(
 #endif
 			IntPtr pvParam) {
 			m_hAPICall = SteamAPICall_t.Invalid; // Caller unregisters for us
+
 			try {
 				m_Func((T)Marshal.PtrToStructure(pvParam, typeof(T)), false);
 			}
@@ -308,21 +309,17 @@ private void OnRunCallResult(
 #if !STDCALL
 			IntPtr thisptr,
 #endif
-			IntPtr pvParam, bool bFailed, ulong hSteamAPICall) {
-			SteamAPICall_t hAPICall = (SteamAPICall_t)hSteamAPICall;
-			if (hAPICall == m_hAPICall) {
+			IntPtr pvParam, bool bFailed, ulong hSteamAPICall_) {
+			SteamAPICall_t hSteamAPICall = (SteamAPICall_t)hSteamAPICall_;
+			if (hSteamAPICall == m_hAPICall) {
+				m_hAPICall = SteamAPICall_t.Invalid; // Caller unregisters for us
+
 				try {
 					m_Func((T)Marshal.PtrToStructure(pvParam, typeof(T)), bFailed);
 				}
 				catch (Exception e) {
 					CallbackDispatcher.ExceptionHandler(e);
 				}
-
-				// The official SDK sets m_hAPICall to invalid before calling the callresult function,
-				// this doesn't let us access .Handle from within the function though.
-				if (hAPICall == m_hAPICall) { // Ensure that m_hAPICall has not been changed in m_Func
-					m_hAPICall = SteamAPICall_t.Invalid; // Caller unregisters for us
-				}
 			}
 		}