Bläddra i källkod

Move TryDo to Utility class

ghorsington 4 år sedan
förälder
incheckning
27cd071828
2 ändrade filer med 27 tillägg och 19 borttagningar
  1. 2 15
      BepInEx.Preloader/Preloader.cs
  2. 25 4
      BepInEx/Utility.cs

+ 2 - 15
BepInEx.Preloader/Preloader.cs

@@ -44,19 +44,6 @@ namespace BepInEx.Preloader
 				"ShimHarmonySupport",
 				"If enabled, basic Harmony functionality is patched to use MonoMod's RuntimeDetour instead.\nTry using this if Harmony does not work in a game.",
 				!Utility.CLRSupportsDynamicAssemblies);
-        }
-
-		private static void TryDo(Action action, out Exception exception)
-		{
-			exception = null;
-			try
-			{
-				action();
-			}
-			catch (Exception e)
-			{
-				exception = e;
-			}
 		}
 
 		public static void Run()
@@ -65,13 +52,13 @@ namespace BepInEx.Preloader
 			{
 				AllocateConsole();
 
-				TryDo(() =>
+				Utility.TryDo(() =>
 				{
 					if (ConfigShimHarmony.Value)
 						HarmonyDetourBridge.Init();
 				}, out var harmonyBridgeException);
 
-				TryDo(() =>
+				Utility.TryDo(() =>
 				{
 					if (ConfigApplyRuntimePatches.Value)
 						UnityPatches.Apply();

+ 25 - 4
BepInEx/Utility.cs

@@ -32,11 +32,32 @@ namespace BepInEx
 		}
 
 		/// <summary>
-		/// Combines multiple paths together, as the specific method is not available in .NET 3.5.
+		/// Try to perform an action.
 		/// </summary>
-		/// <param name="parts">The multiple paths to combine together.</param>
-		/// <returns>A combined path.</returns>
-		public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
+		/// <param name="action">Action to perform.</param>
+		/// <param name="exception">Possible exception that gets returned.</param>
+		/// <returns>True, if action succeeded, false if an exception occured.</returns>
+		public static bool TryDo(Action action, out Exception exception)
+		{
+			exception = null;
+			try
+			{
+				action();
+				return true;
+			}
+			catch (Exception e)
+			{
+				exception = e;
+				return false;
+			}
+		}
+
+        /// <summary>
+        /// Combines multiple paths together, as the specific method is not available in .NET 3.5.
+        /// </summary>
+        /// <param name="parts">The multiple paths to combine together.</param>
+        /// <returns>A combined path.</returns>
+        public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
 
 		/// <summary>
 		/// Tries to parse a bool, with a default value if unable to parse.