<commit_before>// This was largely written by Ryan 'Azuisleet' Kistner for Steam4NET2
// Licensed for use under the Public Domain
// https://github.com/azuisleet
// https://github.com/SteamRE/open-steamworks/tree/master/Steam4NET2/Steam4NET2

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Steamworks {
	public interface ICallback {
		void Run(IntPtr param);
	}

	public interface ICallResult : ICallback {
		int GetExpectedSize();
		int GetExpectedCallback();
		void ClearAPICallHandle();
	}

	public class Callback<CallbackType> : ICallback {
		public delegate void DispatchDelegate(CallbackType param);
		public event DispatchDelegate OnRun;

		public Callback() {
			CallbackDispatcher.RegisterCallback(this, CallbackIdentities.GetCallbackIdentity(typeof(CallbackType)));
		}

		public Callback(DispatchDelegate myFunc)
			: this() {
			this.OnRun += myFunc;
		}

		~Callback() {
			UnRegister();
		}

		public void UnRegister() {
			CallbackDispatcher.UnRegisterCallback(this, CallbackIdentities.GetCallbackIdentity(typeof(CallbackType)));
		}

		public void Run(IntPtr pubParam) {
			if (this.OnRun != null)
				this.OnRun((CallbackType)Marshal.PtrToStructure(pubParam, typeof(CallbackType)));
		}
	}

	public class CallResult<CallbackType> : ICallResult {
		private int callback;
		private int size;
		private SteamAPICall_t callhandle = SteamAPICall_t.Invalid;

		public delegate void APIDispatchDelegate(SteamAPICall_t callHandle, CallbackType param);
		public event APIDispatchDelegate OnRun;

		public CallResult() {
			callback = CallbackIdentities.GetCallbackIdentity(typeof(CallbackType));
			size = Marshal.SizeOf(typeof(CallbackType));
		}

		public CallResult(APIDispatchDelegate myFunc)
			: this() {
			this.OnRun += myFunc;
		}

		public CallResult(APIDispatchDelegate myFunc, SteamAPICall_t apicallhandle)
			: this(myFunc) {
			SetAPICallHandle(apicallhandle);
		}

		~CallResult() {
			ClearAPICallHandle();
		}

		public void SetAPICallHandle(SteamAPICall_t newcallhandle) {
			if (callhandle != SteamAPICall_t.Invalid)
				ClearAPICallHandle();

			callhandle = newcallhandle;
			CallbackDispatcher.RegisterCallResult(this, newcallhandle);
		}

		public void ClearAPICallHandle() {
			CallbackDispatcher.ClearCallResult(this, callhandle);
		}

		public void Run(IntPtr pubParam) {
			if (this.OnRun != null)
				this.OnRun(callhandle, (CallbackType)Marshal.PtrToStructure(pubParam, typeof(CallbackType)));
		}

		public int GetExpectedSize() {
			return size;
		}

		public int GetExpectedCallback() {
			return callback;
		}
	}

	public class CallbackDispatcher {
		private static Dictionary<int, ICallback> registeredCallbacks = new Dictionary<int, ICallback>();
		private static Dictionary<SteamAPICall_t, ICallResult> registeredAPICallbacks = new Dictionary<SteamAPICall_t, ICallResult>();

		public static HSteamPipe LastActivePipe { get; private set; }
		public static Callback<SteamAPICallCompleted_t> APICallbackCompleted = new Callback<SteamAPICallCompleted_t>(RunAPICallback);


		public static void RegisterCallback(ICallback callback, int iCallback) {
			registeredCallbacks.Add(iCallback, callback);
		}

		public static void UnRegisterCallback(ICallback callback, int iCallback) {
			if (registeredCallbacks[iCallback] == callback) {
				registeredCallbacks.Remove(iCallback);
			}
		}

		public static void RegisterCallResult(ICallResult callback, SteamAPICall_t callhandle) {
			registeredAPICallbacks.Add(callhandle, callback);
		}

		public static void ClearCallResult(ICallResult callback, SteamAPICall_t callhandle) {<commit_msg>Wrapped RegisterCallback in a try-catch block and formatting fixes<commit_after>// This was largely written by Ryan 'Azuisleet' Kistner for Steam4NET2
// Licensed for use under the Public Domain
// https://github.com/azuisleet
// https://github.com/SteamRE/open-steamworks/tree/master/Steam4NET2/Steam4NET2

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Steamworks {
	public interface ICallback {
		void Run(IntPtr param);
	}

	public interface ICallResult : ICallback {
		int GetExpectedSize();
		int GetExpectedCallback();
		void ClearAPICallHandle();
	}

	public class Callback<CallbackType> : ICallback {
		public delegate void DispatchDelegate(CallbackType param);
		public event DispatchDelegate OnRun;

		public Callback() {
			CallbackDispatcher.RegisterCallback(this, CallbackIdentities.GetCallbackIdentity(typeof(CallbackType)));
		}

		public Callback(DispatchDelegate myFunc) : this() {
			this.OnRun += myFunc;
		}

		~Callback() {
			UnRegister();
		}

		public void UnRegister() {
			CallbackDispatcher.UnRegisterCallback(this, CallbackIdentities.GetCallbackIdentity(typeof(CallbackType)));
		}

		public void Run(IntPtr pubParam) {
			if (this.OnRun != null)
				this.OnRun((CallbackType)Marshal.PtrToStructure(pubParam, typeof(CallbackType)));
		}
	}

	public class CallResult<CallbackType> : ICallResult {
		private int callback;
		private int size;
		private SteamAPICall_t callhandle = SteamAPICall_t.Invalid;

		public delegate void APIDispatchDelegate(SteamAPICall_t callHandle, CallbackType param);
		public event APIDispatchDelegate OnRun;

		public CallResult() {
			callback = CallbackIdentities.GetCallbackIdentity(typeof(CallbackType));
			size = Marshal.SizeOf(typeof(CallbackType));
		}

		public CallResult(APIDispatchDelegate myFunc) : this() {
			this.OnRun += myFunc;
		}

		public CallResult(APIDispatchDelegate myFunc, SteamAPICall_t apicallhandle) : this(myFunc) {
			SetAPICallHandle(apicallhandle);
		}

		~CallResult() {
			ClearAPICallHandle();
		}

		public void SetAPICallHandle(SteamAPICall_t newcallhandle) {
			if (callhandle != SteamAPICall_t.Invalid)
				ClearAPICallHandle();

			callhandle = newcallhandle;
			CallbackDispatcher.RegisterCallResult(this, newcallhandle);
		}

		public void ClearAPICallHandle() {
			CallbackDispatcher.ClearCallResult(this, callhandle);
		}

		public void Run(IntPtr pubParam) {
			if (this.OnRun != null)
				this.OnRun(callhandle, (CallbackType)Marshal.PtrToStructure(pubParam, typeof(CallbackType)));
		}

		public int GetExpectedSize() {
			return size;
		}

		public int GetExpectedCallback() {
			return callback;
		}
	}

	public class CallbackDispatcher {
		private static Dictionary<int, ICallback> registeredCallbacks = new Dictionary<int, ICallback>();
		private static Dictionary<SteamAPICall_t, ICallResult> registeredAPICallbacks = new Dictionary<SteamAPICall_t, ICallResult>();

		public static HSteamPipe LastActivePipe { get; private set; }
		public static Callback<SteamAPICallCompleted_t> APICallbackCompleted = new Callback<SteamAPICallCompleted_t>(RunAPICallback);


		public static void RegisterCallback(ICallback callback, int iCallback) {
			try {
				registeredCallbacks.Add(iCallback, callback);
			}
			catch (ArgumentException e) {
				UnityEngine.Debug.LogError("You tried to register a specific Callback multiple times.\nIf you need a callback to end up in multiple places then register it once and delegate it elsewhere from there.\n" + e);
			}
		}

		public static void UnRegisterCallback(ICallback callback, int iCallback) {
			if (registeredCallbacks[iCallback] == callback) {
				registeredCallbacks.Remove(iCallback);
			}
		}

		public static void RegisterCallResult(ICallResult callback, SteamAPICall_t callhandle) {
			try {
				registeredAPICallbacks.Add(callhandle, callback);
			}
			catch (ArgumentException e) {
				UnityEngine.Debug.LogError("You tried to register a CallResult multiple times.\nIf you need a callresult to end up in multiple places then register it once and delegate it elsewhere from there.\n" + e);
			}
		}

		public static void ClearCallResult(ICallResult callback, SteamAPICall_t callhandle) {
{'fraction_non_alphanumeric': 0.08865293590891646, 'fraction_numerical': 0.0015351157733145709, 'mean_word_length': 5.1269592476489025, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3215459', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2292, 'n_words': 543}