Browse Source

Clean up log access and add more detailed preloader logging

Bepis 6 years ago
parent
commit
3a658c5fc6

+ 1 - 1
BepInEx.Preloader/Logger/PreloaderLogWriter.cs

@@ -33,7 +33,7 @@ namespace BepInEx.Preloader
 		{
 			LogEvents.Add(eventArgs);
 
-			string log = $"[{eventArgs.Level}:{((ILogSource)sender).SourceName}] {eventArgs.Data}\r\n";
+			string log = $"[{eventArgs.Level,-7}:{((ILogSource)sender).SourceName,10}] {eventArgs.Data}\r\n";
 
 			LogBuilder.Append(log);
 

+ 7 - 9
BepInEx.Preloader/Patching/AssemblyPatcher.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
-using BepInEx.Logging;
 using BepInEx.Preloader.RuntimeFixes;
 using Mono.Cecil;
 
@@ -20,7 +19,7 @@ namespace BepInEx.Preloader.Patching
 	/// </summary>
 	internal static class AssemblyPatcher
 	{
-		private static readonly List<PatcherPlugin> patchers = new List<PatcherPlugin>();
+		public static List<PatcherPlugin> PatcherPlugins { get; } = new List<PatcherPlugin>();
 
 		/// <summary>
 		///     Configuration value of whether assembly dumping is enabled or not.
@@ -34,7 +33,7 @@ namespace BepInEx.Preloader.Patching
 		/// <param name="patcher">Patcher to apply.</param>
 		public static void AddPatcher(PatcherPlugin patcher)
 		{
-			patchers.Add(patcher);
+			PatcherPlugins.Add(patcher);
 		}
 
 		/// <summary>
@@ -67,13 +66,13 @@ namespace BepInEx.Preloader.Patching
 
 		private static void InitializePatchers()
 		{
-			foreach (var assemblyPatcher in patchers)
+			foreach (var assemblyPatcher in PatcherPlugins)
 				assemblyPatcher.Initializer?.Invoke();
 		}
 
 		private static void FinalizePatching()
 		{
-			foreach (var assemblyPatcher in patchers)
+			foreach (var assemblyPatcher in PatcherPlugins)
 				assemblyPatcher.Finalizer?.Invoke();
 		}
 
@@ -82,7 +81,7 @@ namespace BepInEx.Preloader.Patching
 		/// </summary>
 		public static void DisposePatchers()
 		{
-			patchers.Clear();
+			PatcherPlugins.Clear();
 		}
 
 		/// <summary>
@@ -112,8 +111,7 @@ namespace BepInEx.Preloader.Patching
 
 				if (UnityPatches.AssemblyLocations.ContainsKey(assembly.FullName))
 				{
-					Logger.Log(LogLevel.Warning,
-						$"Tried to load duplicate assembly {Path.GetFileName(assemblyPath)} from Managed folder! Skipping...");
+					Logger.LogWarning($"Tried to load duplicate assembly {Path.GetFileName(assemblyPath)} from Managed folder! Skipping...");
 					continue;
 				}
 
@@ -126,7 +124,7 @@ namespace BepInEx.Preloader.Patching
 
 			// Then, perform the actual patching
 			var patchedAssemblies = new HashSet<string>();
-			foreach (var assemblyPatcher in patchers)
+			foreach (var assemblyPatcher in PatcherPlugins)
 				foreach (string targetDll in assemblyPatcher.TargetDLLs)
 					if (assemblies.TryGetValue(targetDll, out var assembly))
 					{

+ 21 - 14
BepInEx.Preloader/Preloader.cs

@@ -35,16 +35,14 @@ namespace BepInEx.Preloader
 				
 				Logger.Sources.Add(TraceLogSource.CreateSource());
 
-				PreloaderLog = new PreloaderConsoleListener(
-						Utility.SafeParseBool(Config.GetEntry("preloader-logconsole", "false", "BepInEx")));
+				PreloaderLog = new PreloaderConsoleListener(Utility.SafeParseBool(Config.GetEntry("preloader-logconsole", "false", "BepInEx")));
 
 				Logger.Listeners.Add(PreloaderLog);
 				
 
-				string consoleTile =
-					$"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Process.GetCurrentProcess().ProcessName}";
-				ConsoleWindow.Title = consoleTile;
+				string consoleTile = $"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Process.GetCurrentProcess().ProcessName}";
 
+				ConsoleWindow.Title = consoleTile;
 				Logger.LogMessage(consoleTile);
 
 				//See BuildInfoAttribute for more information about this section.
@@ -57,6 +55,14 @@ namespace BepInEx.Preloader
 					Logger.LogMessage(attribute.Info);
 				}
 
+#if UNITY_2018
+				Logger.LogMessage("Compiled in Unity v2018 mode");
+#else
+				Logger.LogMessage("Compiled in Legacy Unity mode");
+#endif
+
+				Logger.LogInfo($"Running under Unity v{Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion}");
+
 				Logger.LogMessage("Preloader started");
 
 				string entrypointAssembly = Config.GetEntry("entrypoint-assembly", "UnityEngine.dll", "Preloader");
@@ -65,6 +71,8 @@ namespace BepInEx.Preloader
 					{ TargetDLLs = new[] { entrypointAssembly }, Patcher = PatchEntrypoint });
 				AssemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath, GetPatcherMethods);
 
+				Logger.LogInfo($"{AssemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");
+
 				AssemblyPatcher.PatchAndLoad(Paths.ManagedPath);
 
 				AssemblyPatcher.DisposePatchers();
@@ -82,12 +90,12 @@ namespace BepInEx.Preloader
 			{
 				try
 				{
-					Logger.Log(LogLevel.Fatal, "Could not run preloader!");
-					Logger.Log(LogLevel.Fatal, ex);
+					Logger.LogFatal("Could not run preloader!");
+					Logger.LogFatal(ex);
 
 					PreloaderLog?.Dispose();
 
-					if (!ConsoleWindow.IsAttatched)
+					if (!ConsoleWindow.IsAttached)
 					{
 						//if we've already attached the console, then the log will already be written to the console
 						AllocateConsole();
@@ -187,12 +195,11 @@ namespace BepInEx.Preloader
 				}
 				catch (Exception ex)
 				{
-					Logger.Log(LogLevel.Warning, $"Could not load patcher methods from {assembly.GetName().Name}");
-					Logger.Log(LogLevel.Warning, $"{ex}");
+					Logger.LogWarning($"Could not load patcher methods from {assembly.GetName().Name}");
+					Logger.LogWarning(ex);
 				}
 
-			Logger.Log(LogLevel.Info,
-				$"Loaded {patcherMethods.Count} patcher methods from {assembly.GetName().Name}");
+			Logger.LogInfo($"Loaded {patcherMethods.Count} patcher methods from {assembly.GetName().Name}");
 
 			return patcherMethods;
 		}
@@ -299,8 +306,8 @@ namespace BepInEx.Preloader
 			}
 			catch (Exception ex)
 			{
-				Logger.Log(LogLevel.Error, "Failed to allocate console!");
-				Logger.Log(LogLevel.Error, ex);
+				Logger.LogError("Failed to allocate console!");
+				Logger.LogError(ex);
 			}
 		}
 	}

+ 7 - 6
BepInEx/Bootstrap/Chainloader.cs

@@ -67,7 +67,7 @@ namespace BepInEx.Bootstrap
 				Logger.Sources.Add(new UnityLogSource());
 
 
-			Logger.Log(LogLevel.Message, "Chainloader ready");
+			Logger.LogMessage("Chainloader ready");
 
 			_initialized = true;
 		}
@@ -96,7 +96,7 @@ namespace BepInEx.Bootstrap
 					ConsoleWindow.Title =
 						$"BepInEx {Assembly.GetExecutingAssembly().GetName().Version} - {productNameProp.GetValue(null, null)}";
 
-				Logger.Log(LogLevel.Message, "Chainloader started");
+				Logger.LogMessage("Chainloader started");
 
 				ManagerObject = new GameObject("BepInEx_Manager");
 
@@ -134,7 +134,7 @@ namespace BepInEx.Bootstrap
 										  })
 										  .ToList();
 
-				Logger.Log(LogLevel.Info, $"{selectedPluginTypes.Count} / {globalPluginTypes.Count} plugins to load");
+				Logger.LogInfo($"{selectedPluginTypes.Count} / {globalPluginTypes.Count} plugins to load");
 
 				Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
 
@@ -151,7 +151,7 @@ namespace BepInEx.Bootstrap
 					{
 						var metadata = MetadataHelper.GetMetadata(t);
 
-						Logger.Log(LogLevel.Info, $"Cannot load [{metadata.Name}] due to missing dependencies.");
+						Logger.LogWarning($"Cannot load [{metadata.Name}] due to missing dependencies.");
 					}
 				}
 
@@ -162,7 +162,7 @@ namespace BepInEx.Bootstrap
 					try
 					{
 						var metadata = MetadataHelper.GetMetadata(t);
-						Logger.Log(LogLevel.Info, $"Loading [{metadata.Name} {metadata.Version}]");
+						Logger.LogInfo($"Loading [{metadata.Name} {metadata.Version}]");
 
 						var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);
 
@@ -170,7 +170,8 @@ namespace BepInEx.Bootstrap
 					}
 					catch (Exception ex)
 					{
-						Logger.Log(LogLevel.Info, $"Error loading [{t.Name}] : {ex.Message}");
+						Logger.LogError($"Error loading [{t.Name}] : {ex.Message}");
+						Logger.LogDebug(ex);
 					}
 				}
 			}

+ 2 - 3
BepInEx/Bootstrap/TypeLoader.cs

@@ -3,7 +3,6 @@ using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
 using System.Text;
-using BepInEx.Logging;
 
 namespace BepInEx.Bootstrap
 {
@@ -39,8 +38,8 @@ namespace BepInEx.Bootstrap
 				catch (BadImageFormatException) { } //unmanaged DLL
 				catch (ReflectionTypeLoadException ex)
 				{
-					Logger.Log(LogLevel.Error, $"Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
-					Logger.Log(LogLevel.Debug, TypeLoadExceptionToString(ex));
+					Logger.LogError($"Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
+					Logger.LogDebug(TypeLoadExceptionToString(ex));
 				}
 			}
 

+ 2 - 2
BepInEx/Config.cs

@@ -73,7 +73,7 @@ namespace BepInEx
 			catch (Exception ex)
 			{
 				Logger.Log(LogLevel.Error | LogLevel.Message, "Unable to read config entry!");
-				Logger.Log(LogLevel.Error, ex);
+				Logger.LogError(ex);
 				return defaultValue;
 			}
 		}
@@ -158,7 +158,7 @@ namespace BepInEx
 			catch (Exception ex)
 			{
 				Logger.Log(LogLevel.Error | LogLevel.Message, "Unable to save config entry!");
-				Logger.Log(LogLevel.Error, ex);
+				Logger.LogError(ex);
 			}
 		}
 

+ 2 - 2
BepInEx/ConfigWrapper.cs

@@ -122,7 +122,7 @@ namespace BepInEx
 			}
 			catch (Exception ex)
 			{
-				Logger.Log(LogLevel.Error, "ConfigWrapper Get Converter Exception: " + ex.Message);
+				Logger.LogError("ConfigWrapper Get Converter Exception: " + ex.Message);
 				return _default;
 			}
 		}
@@ -145,7 +145,7 @@ namespace BepInEx
 			}
 			catch (Exception ex)
 			{
-				Logger.Log(LogLevel.Error, "ConfigWrapper Set Converter Exception: " + ex.Message);
+				Logger.LogError("ConfigWrapper Set Converter Exception: " + ex.Message);
 			}
 		}
 

+ 6 - 6
BepInEx/ConsoleUtil/ConsoleWindow.cs

@@ -12,7 +12,7 @@ namespace UnityInjector.ConsoleUtil
 {
 	internal class ConsoleWindow
 	{
-		public static bool IsAttatched { get; private set; }
+		public static bool IsAttached { get; private set; }
 		private static IntPtr _cOut;
 		private static IntPtr _oOut;
 
@@ -20,7 +20,7 @@ namespace UnityInjector.ConsoleUtil
 
 		public static void Attach()
 		{
-			if (IsAttatched)
+			if (IsAttached)
 				return;
 
 			if (_oOut == IntPtr.Zero)
@@ -44,14 +44,14 @@ namespace UnityInjector.ConsoleUtil
 				throw new Exception("SetStdHandle() failed");
 			Init();
 
-			IsAttatched = true;
+			IsAttached = true;
 		}
 
 		public static string Title
 		{
 			set
 			{
-				if (!IsAttatched)
+				if (!IsAttached)
 					return;
 
 				if (value == null)
@@ -73,7 +73,7 @@ namespace UnityInjector.ConsoleUtil
 
 		public static void Detach()
 		{
-			if (!IsAttatched)
+			if (!IsAttached)
 				return;
 
 			if (!CloseHandle(_cOut))
@@ -85,7 +85,7 @@ namespace UnityInjector.ConsoleUtil
 				throw new Exception("SetStdHandle() failed");
 			Init();
 
-			IsAttatched = false;
+			IsAttached = false;
 		}
 
 		[DllImport("user32.dll")]

+ 1 - 1
BepInEx/Logging/ConsoleLogListener.cs

@@ -10,7 +10,7 @@ namespace BepInEx.Logging
 	{
 		public void LogEvent(object sender, LogEventArgs eventArgs)
 		{
-			string log = $"[{eventArgs.Level}:{((ILogSource)sender).SourceName}] {eventArgs.Data}\r\n";
+			string log = $"[{eventArgs.Level, -7}:{((ILogSource)sender).SourceName, 10}] {eventArgs.Data}\r\n";
 
 			Kon.ForegroundColor = eventArgs.Level.GetConsoleColor();
 			Console.Write(log);

+ 7 - 7
BepInEx/Logging/Logger.cs

@@ -28,17 +28,17 @@ namespace BepInEx
 		/// </summary>
 		/// <param name="level">The level of the entry.</param>
 		/// <param name="entry">The textual value of the entry.</param>
-		public static void Log(LogLevel level, object data)
+		internal static void Log(LogLevel level, object data)
 		{
 			InternalLogSource.Log(level, data);
 		}
 
-		public static void LogFatal(object data) => Log(LogLevel.Fatal, data);
-		public static void LogError(object data) => Log(LogLevel.Error, data);
-		public static void LogWarning(object data) => Log(LogLevel.Warning, data);
-		public static void LogMessage(object data) => Log(LogLevel.Message, data);
-		public static void LogInfo(object data) => Log(LogLevel.Info, data);
-		public static void LogDebug(object data) => Log(LogLevel.Debug, data);
+		internal static void LogFatal(object data) => Log(LogLevel.Fatal, data);
+		internal static void LogError(object data) => Log(LogLevel.Error, data);
+		internal static void LogWarning(object data) => Log(LogLevel.Warning, data);
+		internal static void LogMessage(object data) => Log(LogLevel.Message, data);
+		internal static void LogInfo(object data) => Log(LogLevel.Info, data);
+		internal static void LogDebug(object data) => Log(LogLevel.Debug, data);
 
 		public static ManualLogSource CreateLogSource(string sourceName)
 		{