Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an Exception interceptor to catch ReflectionTypeLoadException and…
… properly blame broken DLLs
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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() | ||
{ | ||
baseLogHandler = Debug.unityLogger.logHandler; | ||
Debug.unityLogger.logHandler = this; | ||
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) | ||
{ | ||
ModuleManager.Log("Intercepted a ReflectionTypeLoadException. List of broken DLLs:"); | ||
var assemblies = ex.Types.Where(x => x != null).Select(x => x.Assembly).Distinct(); | ||
foreach (Assembly assembly in assemblies) | ||
{ | ||
if (Warnings == "") | ||
{ | ||
Warnings = "ModuleManager mod(s) DLL that are not compatible with this version of KSP\n"; | ||
} | ||
if (!brokenAssemblies.Contains(assembly)) | ||
{ | ||
brokenAssemblies.Add(assembly); | ||
Warnings += assembly.GetName().Name + " " + assembly.GetName().Version + " " + assembly.Location.Remove(0, gamePathLength) + "\n"; | ||
} | ||
ModuleManager.Log(assembly.GetName().Name + " " + assembly.GetName().Version + " " + assembly.Location.Remove(0, gamePathLength)); | ||
} | ||
} | ||
baseLogHandler.LogException(exception, context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters