Skip to content

Commit

Permalink
Merge pull request #341 from BUTR/dev
Browse files Browse the repository at this point in the history
v2.8.4
  • Loading branch information
Aragas authored Jul 5, 2023
2 parents c2d214b + e2890f1 commit d22eefd
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 365 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
shell: pwsh

- name: ReportGenerator
uses: danielpalme/[email protected].22
uses: danielpalme/[email protected].23
with:
reports: '*.xml'
targetdir: 'coveragereport'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
shell: pwsh

- name: ReportGenerator
uses: danielpalme/[email protected].22
uses: danielpalme/[email protected].23
with:
reports: 'coverage_butterlib_stable_debug.xml;coverage_butterlib_stable_release.xml;coverage_butterlib_impl_stable_debug.xml;coverage_butterlib_impl_stable_release.xml;coverage_butterlib_impl_beta_debug.xml;coverage_butterlib_impl_beta_release.xml;'
targetdir: 'coveragereport'
Expand Down
4 changes: 2 additions & 2 deletions build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--Development Variables-->
<PropertyGroup>
<!--Module Version-->
<Version>2.8.3</Version>
<Version>2.8.4</Version>
<!--Harmony Version-->
<HarmonyVersion>2.2.2</HarmonyVersion>
<HarmonyExtensionsVersion>3.2.0.77</HarmonyExtensionsVersion>
Expand All @@ -15,7 +15,7 @@
<ExtensionVersion>2.0.0</ExtensionVersion>
<!--BuildResources Version-->
<BuildResourcesVersion>1.1.0.102</BuildResourcesVersion>
<BUTRSharedVersion>3.0.0.136</BUTRSharedVersion>
<BUTRSharedVersion>3.0.0.137</BUTRSharedVersion>
<BUTRModuleManagerVersion>5.0.209</BUTRModuleManagerVersion>
</PropertyGroup>

Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 2.8.4
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.2.0
* ButterLib is not able to disable v1.2.0 Watchdog, BLSE is required
* Added integration with BLSE's better exception intercepter
---------------------------------------------------------------------------------------------------
Version: 2.8.3
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.2.0
* The UseVanillaCrashHandler setting wasn't set correctly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal class DistanceMatrixSubSystem : ISubSystem
public static DistanceMatrixSubSystem? Instance { get; private set; }

public string Id => "Distance Matrix";
public string Description => "Mod Developer feature! Provides helpers to calculate distance between objects on map.";
public string Name => "{=Ox6uK8fZWs}Distance Matrix";
public string Description => "{=WQ4r2n0mYj}Mod Developer feature! Provides helpers to calculate distance between objects on map.";
public bool IsEnabled { get; private set; } = false;
public bool CanBeDisabled => true;
public bool CanBeSwitchedAtRuntime => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ internal sealed class HotKeySubSystem : ISubSystem
public static HotKeySubSystem? Instance { get; private set; }

public string Id => "Hot Keys";
public string Name => "{=jvV6QDc7AJ}Hot Keys";
public bool IsEnabled { get; private set; }
public string Description => "Mod Developer feature! Provides a better way for mods to create hotkeys";
public string Description => "{=wNNYwgUeus}Mod Developer feature! Provides a better way for mods to create hotkeys";
public bool CanBeDisabled => true;
public bool CanBeSwitchedAtRuntime => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ internal class MBSubModuleBaseExSubSystem : ISubSystem
{
public static MBSubModuleBaseExSubSystem? Instance { get; private set; }
public string Id => "MBSubModuleBase extended";
public string Description => "Mod Developer feature! Introduces a MBSubModuleBase-derived abstract class, that provides new SubModule events.";
public string Name => "{=JGylAT3SrB}MBSubModuleBase Extended";
public string Description => "{=XfveBQYVWH}Mod Developer feature! Introduces a MBSubModuleBase-derived abstract class, that provides new SubModule events.";
public bool IsEnabled { get; private set; }
public bool CanBeDisabled => true;
public bool CanBeSwitchedAtRuntime => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ internal class ObjectSystemSubSystem : ISubSystem
public static ObjectSystemSubSystem? Instance { get; private set; }

public string Id => "Object System";
public string Description => "Mod Developer feature!";
public string Name => "{=IA0mVgHJgo}Object System";
public string Description => "{=mFZTv1nwOx}Mod Developer feature!";
public bool IsEnabled { get; private set; }
public bool CanBeDisabled => true;
public bool CanBeSwitchedAtRuntime => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ internal class SaveSystemSubSystem : ISubSystem
public static SaveSystemSubSystem? Instance { get; private set; }

public string Id => "Save System";
public string Description => @"Extends and fixes the game's save system:
* Fixes possible collision with save names;
* Fixes save corruption & crashes when duplicate types are defined;
* Adds support for saving many more container types;
public string Name => "{=66A5N9278w}Save System";
public string Description => @"{=9ybOxGpWb5}Extends and fixes the game's save system:{NL}
* Fixes possible collision with save names;{NL}
* Fixes save corruption & crashes when duplicate types are defined;{NL}
* Adds support for saving many more container types;{NL}
This might alter the save file, disabling the feature might render the save file unloadable!";
public bool IsEnabled { get; private set; }
public bool CanBeDisabled => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,24 @@

<string id="ZD69h3IpF5" text="Use Vanilla Crash Handler" />
<string id="o0DgSNv5V1" text="Disables ButterLib's and BEW's Crash Handlers with the new Watchdog Crash Handler. Do not enable if not sure." />

<string id="Ox6uK8fZWs" text="Distance Matrix" />
<string id="WQ4r2n0mYj" text="Mod Developer feature! Provides helpers to calculate distance between objects on map." />
<string id="jvV6QDc7AJ" text="Hot Keys" />
<string id="wNNYwgUeus" text="Mod Developer feature! Provides a better way for mods to create hotkeys" />
<string id="JGylAT3SrB" text="MBSubModuleBase Extended" />
<string id="XfveBQYVWH" text="Mod Developer feature! Introduces a MBSubModuleBase-derived abstract class, that provides new SubModule events." />
<string id="IA0mVgHJgo" text="Object System" />
<string id="mFZTv1nwOx" text="Mod Developer feature!" />
<string id="66A5N9278w" text="Save System" />
<string id="9ybOxGpWb5" text="Extends and fixes the game's save system:{NL}* Fixes possible collision with save names;{NL}* Fixes save corruption &amp; crashes when duplicate types are defined;{NL}* Adds support for saving many more container types;{NL}This might alter the save file, disabling the feature might render the save file unloadable!" />
<string id="UsLlrwMTjJ" text="Crash Uploader" />
<string id="hjeoN9NwZm" text="Uploads the crash reports to BUTR for an easy file hosting." />
<string id="joCJ9xpDvM" text="Delayed SubModule" />
<string id="Gznum6kuzv" text="Mod Developer feature! Provides helpers to run methods after SubModule events." />
<string id="ZypQtNNcVN" text="Exception Handler" />
<string id="UreeIeLQYS" text="Captures game crashes and creates reports out of them." />
<string id="NkAAB8EEu2" text="SubModule Wrappers" />
<string id="izmKJPjkjN" text="Mod Developer feature! A wrapper for MBSubModuleBase based on Harmony patches." />
</strings>
</base>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ internal sealed class CrashUploaderSubSystem : ISubSystem
public static CrashUploaderSubSystem? Instance { get; private set; }

public string Id => "CrashUploader";
public string Description => "Uploads the crash reports to BUTR for an easy file hosting.";
public string Name => "{=UsLlrwMTjJ}Crash Uploader";
public string Description => "{=hjeoN9NwZm}Uploads the crash reports to BUTR for an easy file hosting.";
public bool IsEnabled { get; private set; }
public bool CanBeDisabled => true;
public bool CanBeSwitchedAtRuntime => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace Bannerlord.ButterLib.DelayedSubModule
internal class DelayedSubModuleSubSystem : ISubSystem
{
public string Id => "Delayed SubModule";
public string Description => "Mod Developer feature! Provides helpers to run methods after SubModule events.";
public string Name => "{=joCJ9xpDvM}Delayed SubModule";
public string Description => "{=Gznum6kuzv}Mod Developer feature! Provides helpers to run methods after SubModule events.";
public bool IsEnabled => true;
public bool CanBeDisabled => false;
public bool CanBeSwitchedAtRuntime => false;
Expand Down
91 changes: 12 additions & 79 deletions src/Bannerlord.ButterLib/ExceptionHandler/BEWPatch.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using Bannerlord.BLSE;
using Bannerlord.ButterLib.Common.Extensions;
using Bannerlord.ButterLib.ExceptionHandler.DebuggerDetection;
using Bannerlord.ButterLib.ExceptionHandler.DebuggerDetection;

using HarmonyLib;
using HarmonyLib.BUTR.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand All @@ -28,7 +23,6 @@ namespace Bannerlord.ButterLib.ExceptionHandler
// TaleWorlds.MountAndBlade.MissionBehaviour:OnMissionTick -> Called by TaleWorlds.MountAndBlade.Mission:Tick
// TaleWorlds.MountAndBlade.MBSubModuleBase:OnSubModuleLoad -> Replicated

[BLSELoaderInterceptor]
internal sealed class BEWPatch
{
public static bool IsDebuggerAttached()
Expand All @@ -37,9 +31,7 @@ public static bool IsDebuggerAttached()
return true;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return ProcessDebug.CheckProcessDebugObjectHandle();
}

return false;
}
Expand All @@ -54,22 +46,13 @@ internal record ExceptionIdentifier(Type Type, string? StackTrace, string Messag
private static readonly string[] BEW = { "org.calradia.admiralnelson.betterexceptionwindow" };


//private static readonly Assembly[] AutoGeneratedAssemblies = AccessTools2.AllAssemblies().Where(x => x.GetName().Name.EndsWith(".AutoGeneratedAssemblies")).ToArray();

private static readonly MethodInfo? ManagedApplicationTickMethod = AccessTools2.Method("TaleWorlds.DotNet.Managed:ApplicationTick");
private static readonly MethodInfo? ModuleOnApplicationTickMethod = AccessTools2.Method("TaleWorlds.MountAndBlade.Module:OnApplicationTick");
private static readonly MethodInfo? ScreenManagerTickMethod = AccessTools2.Method("TaleWorlds.ScreenSystem.ScreenManager:Tick");
private static readonly MethodInfo? ManagedScriptHolderTickComponentsMethod = AccessTools2.Method("TaleWorlds.Engine.ManagedScriptHolder:TickComponents");
private static readonly MethodInfo? MissionTickMethod = AccessTools2.Method("TaleWorlds.MountAndBlade.Mission:Tick");
public static readonly MethodInfo? FinalizerMethod = AccessTools2.Method(typeof(BEWPatch), nameof(Finalizer));

private static readonly AccessTools.FieldRef<Module, Dictionary<string, Type>>? LoadedSubModuleTypes =
AccessTools2.FieldRefAccess<Module, Dictionary<string, Type>>("_loadedSubmoduleTypes");

private static ILogger _log = default!;

private static bool _wasButrLoaderInterceptorCalled = false;

private static void Finalizer(Exception? __exception)
{
if (ExceptionHandlerSubSystem.Instance?.DisableWhenDebuggerIsAttached == true && IsDebuggerAttached())
Expand All @@ -81,9 +64,6 @@ private static void Finalizer(Exception? __exception)

internal static void Enable(Harmony harmony)
{
_log = ButterLibSubModule.Instance?.GetServiceProvider()?.GetRequiredService<ILogger<BEWPatch>>()
?? NullLogger<BEWPatch>.Instance;

harmony.Patch(ManagedApplicationTickMethod, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
harmony.Patch(ModuleOnApplicationTickMethod, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
harmony.Patch(ScreenManagerTickMethod, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
Expand Down Expand Up @@ -114,79 +94,32 @@ internal static void Enable(Harmony harmony)
transpiler: AccessTools2.Method(typeof(BEWPatch), nameof(BlankTranspiler)));
}

internal static void Disable(Harmony harmony)
{
harmony.Unpatch(ManagedApplicationTickMethod, FinalizerMethod);
harmony.Unpatch(ModuleOnApplicationTickMethod, FinalizerMethod);
harmony.Unpatch(ScreenManagerTickMethod, FinalizerMethod);
harmony.Unpatch(ManagedScriptHolderTickComponentsMethod, FinalizerMethod);
harmony.Unpatch(MissionTickMethod, FinalizerMethod);
}

/*
internal static void EnableWithDebug(Harmony harmony)
internal static void EnableAutoGenCatch(Harmony harmony)
{
_log = ButterLibSubModule.Instance?.GetServiceProvider()?.GetRequiredService<ILogger<BEWPatch>>()
?? NullLogger<BEWPatch>.Instance;
harmony.Patch(ModuleOnApplicationTickMethod, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
harmony.Patch(MissionTickMethod, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
var callbacksGeneratedTypes = AutoGeneratedAssemblies.SelectMany(x => x.GetTypes().Where(y => y.Name.EndsWith("CallbacksGenerated")));
var callbacksGeneratedTypes = AccessTools2.AllAssemblies().SelectMany(x => x.GetTypes().Where(y => y.Name.EndsWith("CallbacksGenerated")));
var callbackGeneratedMethods = callbacksGeneratedTypes.SelectMany(AccessTools.GetDeclaredMethods);
foreach (var method in callbackGeneratedMethods.Where(x => x.GetCustomAttributesData().Any(y => y.AttributeType.Name == "MonoPInvokeCallbackAttribute")))
harmony.Patch(method, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
if (!_wasButrLoaderInterceptorCalled)
PatchSubModules(harmony);
}

internal static void DisableWithDebug(Harmony harmony)
internal static void Disable(Harmony harmony)
{
harmony.Unpatch(ManagedApplicationTickMethod, FinalizerMethod);
harmony.Unpatch(ModuleOnApplicationTickMethod, FinalizerMethod);
harmony.Unpatch(ScreenManagerTickMethod, FinalizerMethod);
harmony.Unpatch(ManagedScriptHolderTickComponentsMethod, FinalizerMethod);
harmony.Unpatch(MissionTickMethod, FinalizerMethod);
}

var callbacksGeneratedTypes = AutoGeneratedAssemblies.SelectMany(x => x.GetTypes().Where(y => y.Name.EndsWith("CallbacksGenerated")));
internal static void DisableAutoGenCatch(Harmony harmony)
{
var callbacksGeneratedTypes = AccessTools2.AllAssemblies().SelectMany(x => x.GetTypes().Where(y => y.Name.EndsWith("CallbacksGenerated")));
var callbackGeneratedMethods = callbacksGeneratedTypes.SelectMany(AccessTools.GetDeclaredMethods);
foreach (var method in callbackGeneratedMethods.Where(x => x.GetCustomAttributesData().Any(y => y.AttributeType.Name == "MonoPInvokeCallbackAttribute")))
harmony.Unpatch(method, FinalizerMethod);
}
*/

[MethodImpl(MethodImplOptions.NoInlining)]
private static IEnumerable<CodeInstruction> BlankTranspiler(IEnumerable<CodeInstruction> instructions) => instructions;

/// <summary>
/// We need to patch MBSubModuleBase.OnSubModuleLoad because they generally can't be catched.
/// The reason is, Harmony can't patch a method it's running in.
/// The exception handling (this one) is started within the
/// Module.Initialize() -> Module.LoadSubModules() -> Module.InitializeSubModules() -> MBSubModuleBase.OnSubModuleLoad()
/// We start the exception interception within this scope, so if anything is throwing while within Module.Initialize(),
/// we will lose that. It's easier to intercept every exception in MBSubModuleBase.OnSubModuleLoad() instead.
/// </summary>
private static bool PatchSubModules(Harmony harmony)
{
/*
if (LoadedSubModuleTypes is null)
return false;
foreach (var (_, type) in LoadedSubModuleTypes(Module.CurrentModule))
{
if (AccessTools2.Method(type, "OnSubModuleLoad") is not { } method || method.DeclaringType == typeof(MBSubModuleBase))
continue;
harmony.Patch(method, finalizer: new HarmonyMethod(FinalizerMethod, before: BEW));
}
*/
return true;
}

// BUTRLoader gives un the ability to intercept every exception call.
// We will use the earlier entrypoint instead
private static void OnInitializeSubModulesPrefix()
{
_wasButrLoaderInterceptorCalled = true;
PatchSubModules(new Harmony("Bannerlord.ButterLib.ExceptionHandler.BUTRLoadingInterceptor"));
}
}
}

This file was deleted.

Loading

0 comments on commit d22eefd

Please sign in to comment.