VOID_Tools: New GetWindowHandler method to wrap window functions in some exception handling code, to avoid baking it in to every window.
VOID_Tools: New GetWindowHandler method to wrap window functions in some exception handling code, to avoid baking it in to every window.

--- a/VOID_Tools.cs
+++ b/VOID_Tools.cs
@@ -28,6 +28,7 @@
 
 using KSP;
 using System;
+using System.Collections.Generic;
 using UnityEngine;
 
 namespace VOID
@@ -256,6 +257,39 @@
 		}
 		#endregion
 
+		private static Dictionary<int, GUI.WindowFunction> functionCache;
+		public static UnityEngine.GUI.WindowFunction GetWindowHandler(Action<int> func)
+		{
+			if (functionCache == null)
+			{
+				functionCache = new Dictionary<int, GUI.WindowFunction>();
+			}
+
+			int hashCode = func.GetHashCode();
+
+			if (!functionCache.ContainsKey(hashCode))
+			{
+				functionCache[hashCode] = delegate (int id)
+				{
+					try
+					{
+						func(id);
+					}
+					catch (ArgumentException ex)
+					{
+						Debug.LogWarning(
+							string.Format("[{0}]: ArgumentException caught during window call.", func.Target.GetType().Name)
+						);
+						#if DEBUG
+						Debug.LogException(ex);
+						#endif
+					}
+				};
+			}
+
+			return functionCache[hashCode];
+		}
+
 		/// <summary>
 		/// Converts the interval given in seconds to a human-friendly
 		/// time period in [years], [days], hours, minutes, and seconds.