Skip to content

Commit

Permalink
Add an Exception interceptor to catch ReflectionTypeLoadException and…
Browse files Browse the repository at this point in the history
… properly blame broken DLLs
  • Loading branch information
sarbian committed Nov 15, 2019
1 parent ff63723 commit d172fc6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ModuleManager/ExceptionIntercept/InterceptLogHandler.cs
@@ -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);
}
}
}
5 changes: 5 additions & 0 deletions ModuleManager/ModuleManager.cs
Expand Up @@ -11,6 +11,7 @@
using ModuleManager.Cats;
using ModuleManager.Extensions;
using ModuleManager.Logging;
using ModuleManager.UnityLogHandle;

namespace ModuleManager
{
Expand Down Expand Up @@ -40,6 +41,8 @@ public class ModuleManager : MonoBehaviour

private MMPatchRunner patchRunner;

private InterceptLogHandler interceptLogHandler;

#endregion state

private static bool loadedInScene;
Expand Down Expand Up @@ -196,6 +199,7 @@ private void Start()
}
}
}
interceptLogHandler = new InterceptLogHandler();
}

private TextMeshProUGUI CreateTextObject(Canvas canvas, string name)
Expand Down Expand Up @@ -289,6 +293,7 @@ internal void Update()
{
if (warning)
{
warning.text = InterceptLogHandler.Warnings;
h = warning.text.Length > 0 ? warning.textBounds.size.y : 0;
offsetY = offsetY + h;
warning.rectTransform.localPosition = new Vector3(0, offsetY);
Expand Down
2 changes: 2 additions & 0 deletions ModuleManager/ModuleManager.csproj
Expand Up @@ -108,6 +108,7 @@
<Compile Include="Threading\ITaskStatus.cs" />
<Compile Include="Threading\TaskStatus.cs" />
<Compile Include="Threading\TaskStatusWrapper.cs" />
<Compile Include="ExceptionIntercept\InterceptLogHandler.cs" />
<Compile Include="Utils\Counter.cs" />
<Compile Include="Utils\FileUtils.cs" />
<Compile Include="CustomConfigsManager.cs" />
Expand Down Expand Up @@ -194,6 +195,7 @@
<ItemGroup>
<None Include="Properties\rainbow2.png" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>sh -c "TARGET_PATH='$(TargetPath)' TARGET_DIR='$(TargetDir)' TARGET_NAME='$(TargetName)' sh '$(ProjectDir)/copy_build.sh'"</PostBuildEvent>
Expand Down

0 comments on commit d172fc6

Please sign in to comment.