Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
* Added 'BUTRLoader_lasterror.log' file that will contain the error w…
Browse files Browse the repository at this point in the history
…hen the launcher is crashing
  • Loading branch information
Aragas committed Dec 24, 2022
1 parent 2b07027 commit 2c9ad5d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<!--Development Variables-->
<PropertyGroup>
<Version>1.14.4</Version>
<Version>1.14.5</Version>
<HarmonyVersion>2.2.2</HarmonyVersion>
<BUTRSharedVersion>3.0.0.126</BUTRSharedVersion>
<BUTRModuleManagerVersion>5.0.163</BUTRModuleManagerVersion>
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---------------------------------------------------------------------------------------------------
Version: 1.14.5
Game Versions: v1.0.0,v1.0.1,v1.0.2
* Added 'BUTRLoader_lasterror.log' file that will contain the error when the launcher is crashing
---------------------------------------------------------------------------------------------------
Version: 1.14.4
Game Versions: v1.0.0,v1.0.1,v1.0.2
* Added support for loading Novus Presets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

[assembly: InternalsVisibleTo("Bannerlord.BUTRLoader.Tests")]

Expand All @@ -23,6 +24,8 @@ public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
{
base.InitializeNewDomain(appDomainInfo);

AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;

// delete old files
var files = new[]
{
Expand Down Expand Up @@ -62,9 +65,29 @@ public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
};
}

private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
static string GetRecursiveException(Exception ex) => new StringBuilder()
.AppendLine()
.AppendLine($"Type: {ex.GetType().FullName}")
.AppendLine(!string.IsNullOrWhiteSpace(ex.Message) ? $"Message: {ex.Message}" : string.Empty)
.AppendLine(!string.IsNullOrWhiteSpace(ex.Source) ? $"Source: {ex.Source}" : string.Empty)
.AppendLine(!string.IsNullOrWhiteSpace(ex.StackTrace) ? $@"CallStack:{Environment.NewLine}{string.Join(Environment.NewLine, ex.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))}" : string.Empty)
.AppendLine(ex.InnerException != null ? $@"{Environment.NewLine}{Environment.NewLine}Inner {GetRecursiveException(ex.InnerException)}" : string.Empty)
.ToString();

using var fs = File.Open("BUTRLoader_lasterror.log", FileMode.OpenOrCreate, FileAccess.Write);
fs.SetLength(0);
using var writer = new StreamWriter(fs);
writer.Write($@"BUTRLoader Exception:
Version: {typeof(BUTRLoaderAppDomainManager).Assembly.GetName().Version}
{(e.ExceptionObject is Exception ex ? GetRecursiveException(ex) : e.ToString())}");
}


private static void Initialize()
{
Manager.OnDisable += () => AppDomain.CurrentDomain.UnhandledException -= CurrentDomainOnUnhandledException;
Manager.Initialize();

InterceptorFeature.Enable(_featureHarmony);
Expand Down
3 changes: 3 additions & 0 deletions src/Bannerlord.BUTRLoader.LauncherEx/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static class Manager
internal static readonly Harmony _launcherHarmony = new("bannerlord.butrloader.launcherex");
internal static readonly AssemblyCompatibilityChecker _compatibilityChecker = new();

public static event Action OnDisable;

public static void Initialize()
{
AssemblyLoaderPatch.Enable(_launcherHarmony);
Expand Down Expand Up @@ -85,6 +87,7 @@ static byte[] ReadFully(Stream input)

public static void Disable()
{
OnDisable?.Invoke();
_compatibilityChecker.Dispose();
_launcherHarmony.UnpatchAll(_launcherHarmony.Id);
}
Expand Down

0 comments on commit 2c9ad5d

Please sign in to comment.