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