|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using UnityEngine; |
| 7 | +using Object = UnityEngine.Object; |
| 8 | + |
| 9 | +namespace ModuleManager.UnityLogHandle |
| 10 | +{ |
| 11 | + class InterceptLogHandler : ILogHandler |
| 12 | + { |
| 13 | + private readonly ILogHandler baseLogHandler; |
| 14 | + private readonly List<Assembly> brokenAssemblies = new List<Assembly>(); |
| 15 | + private readonly int gamePathLength; |
| 16 | + |
| 17 | + public static string Warnings { get; private set; } = ""; |
| 18 | + |
| 19 | + public InterceptLogHandler() |
| 20 | + { |
| 21 | + baseLogHandler = Debug.unityLogger.logHandler; |
| 22 | + Debug.unityLogger.logHandler = this; |
| 23 | + gamePathLength = Path.GetFullPath(KSPUtil.ApplicationRootPath).Length; |
| 24 | + } |
| 25 | + |
| 26 | + public void LogFormat(LogType logType, Object context, string format, params object[] args) |
| 27 | + { |
| 28 | + baseLogHandler.LogFormat(logType, context, format, args); |
| 29 | + } |
| 30 | + |
| 31 | + public void LogException(Exception exception, Object context) |
| 32 | + { |
| 33 | + baseLogHandler.LogException(exception, context); |
| 34 | + |
| 35 | + if (exception is ReflectionTypeLoadException ex) |
| 36 | + { |
| 37 | + ModuleManager.Log("Intercepted a ReflectionTypeLoadException. List of broken DLLs:"); |
| 38 | + var assemblies = ex.Types.Where(x => x != null).Select(x => x.Assembly).Distinct(); |
| 39 | + foreach (Assembly assembly in assemblies) |
| 40 | + { |
| 41 | + if (Warnings == "") |
| 42 | + { |
| 43 | + Warnings = "ModuleManager mod(s) DLL that are not compatible with this version of KSP\n"; |
| 44 | + } |
| 45 | + if (!brokenAssemblies.Contains(assembly)) |
| 46 | + { |
| 47 | + brokenAssemblies.Add(assembly); |
| 48 | + Warnings += assembly.GetName().Name + " " + assembly.GetName().Version + " " + assembly.Location.Remove(0, gamePathLength) + "\n"; |
| 49 | + } |
| 50 | + ModuleManager.Log(assembly.GetName().Name + " " + assembly.GetName().Version + " " + assembly.Location.Remove(0, gamePathLength)); |
| 51 | + } |
| 52 | + } |
| 53 | + baseLogHandler.LogException(exception, context); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments