Skip to content

Commit

Permalink
ButterLib now properly reads settings and will not enable a SubSystem…
Browse files Browse the repository at this point in the history
… if disabled

Otherwise we will always create Harmony patches for method, which is not reversible by disabling them
  • Loading branch information
Aragas committed Sep 25, 2024
1 parent 0679f7e commit ede8ac9
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 87 deletions.
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Version: 2.9.11
Game Versions: v1.0.x,v1.1.x,v1.2.x
* Updated Crash Reporter to v14
* OS Type/Version and loaded Native Modules are now saved in the crash report
* Fixed methods not being debuggable when Debugger is attached by Cephas369
* Fixed methods not being debuggable when Debugger is attached (Thanks Cephas369)
---------------------------------------------------------------------------------------------------
Version: 2.9.10
Game Versions: v1.0.x,v1.1.x,v1.2.x
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bannerlord.ButterLib.SubSystems;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubSystems;
using Bannerlord.ButterLib.SubSystems.Settings;

using System.Collections.Generic;
Expand All @@ -18,13 +19,22 @@ internal class DistanceMatrixSubSystem : ISubSystem, ISubSystemSettings<Distance
internal bool GameInitialized { get; set; } = false;
public bool ConsiderVillages { get; set; } = true;

private bool _wasInitialized;

public DistanceMatrixSubSystem()
{
Instance = this;
}

public void Enable()
{
if (!_wasInitialized)
{
_wasInitialized = true;
var isEnabledViaSettings = SettingsProvider.PopulateSubSystemSettings(this) ?? true;
if (!isEnabledViaSettings) return;
}

if (IsEnabled) return;

if (GameInitialized) return;
Expand Down
10 changes: 10 additions & 0 deletions src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeySubSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

using Bannerlord.ButterLib.Implementation.HotKeys.Patches;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubSystems;

using HarmonyLib;
Expand All @@ -26,13 +27,22 @@ internal sealed class HotKeySubSystem : ISubSystem

private readonly Harmony _harmony = new("Bannerlord.ButterLib.HotKeySystem");

private bool _wasInitialized;

public HotKeySubSystem()
{
Instance = this;
}

public void Enable()
{
if (!_wasInitialized)
{
_wasInitialized = true;
var isEnabledViaSettings = SettingsProvider.PopulateSubSystemSettings(this) ?? true;
if (!isEnabledViaSettings) return;
}

if (IsEnabled) return;
IsEnabled = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bannerlord.ButterLib.Implementation.MBSubModuleBaseExtended.Patches;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubSystems;

using HarmonyLib;
Expand All @@ -23,12 +24,21 @@ internal class MBSubModuleBaseExSubSystem : ISubSystem

private readonly Harmony _harmony = new("Bannerlord.ButterLib.MBSubModuleBaseEx");

private bool _wasInitialized;

public MBSubModuleBaseExSubSystem()
{
Instance = this;
}
public void Enable()
{
if (!_wasInitialized)
{
_wasInitialized = true;
var isEnabledViaSettings = SettingsProvider.PopulateSubSystemSettings(this) ?? true;
if (!isEnabledViaSettings) return;
}

if (IsEnabled) return;
IsEnabled = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bannerlord.ButterLib.Implementation.SaveSystem.Patches;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubSystems;

using HarmonyLib;
Expand All @@ -22,13 +23,22 @@ internal class SaveSystemSubSystem : ISubSystem

private readonly Harmony _harmony = new("Bannerlord.ButterLib.SaveSystem");

private bool _wasInitialized;

public SaveSystemSubSystem()
{
Instance = this;
}

public void Enable()
{
if (!_wasInitialized)
{
_wasInitialized = true;
var isEnabledViaSettings = SettingsProvider.PopulateSubSystemSettings(this) ?? true;
if (!isEnabledViaSettings) return;
}

if (IsEnabled) return;
IsEnabled = true;

Expand Down
1 change: 1 addition & 0 deletions src/Bannerlord.ButterLib.Implementation/SubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected override void OnSubModuleLoad()
Logger.LogInformation("Wrapping DebugManager of type {Type} with DebugManagerWrapper", Debug.DebugManager.GetType());
Debug.DebugManager = new DebugManagerWrapper(Debug.DebugManager, serviceProvider.GetRequiredService<ILoggerFactory>());

DistanceMatrixSubSystem.Instance?.Enable();
HotKeySubSystem.Instance?.Enable();
MBSubModuleBaseExSubSystem.Instance?.Enable();
SaveSystemSubSystem.Instance?.Enable();
Expand Down
12 changes: 5 additions & 7 deletions src/Bannerlord.ButterLib/ButterLibSubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Bannerlord.ButterLib.DelayedSubModule;
using Bannerlord.ButterLib.DynamicAPI;
using Bannerlord.ButterLib.ExceptionHandler;
using Bannerlord.ButterLib.Extensions;
using Bannerlord.ButterLib.ObjectSystem.Extensions;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubModuleWrappers2;
Expand All @@ -21,12 +20,9 @@

using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.Engine;
using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;

using Path = System.IO.Path;

namespace Bannerlord.ButterLib;

/// <summary>
Expand Down Expand Up @@ -66,16 +62,16 @@ public void OnServiceRegistration()
Services.AddOptions();
Services.Configure<ButterLibOptions>(o =>
{
var defaultJsonOptions = new JsonButterLibOptionsModel();
var defaultJsonOptions = SettingsProvider.GetSettings();
o.MinLogLevel = defaultJsonOptions.MinLogLevel;
});

foreach (var action in BeforeInitialization)
action?.Invoke(Services);

this.AddDefaultSerilogLogger();
this.AddSerilogLoggerProvider("butterlib.txt", new[] { "Bannerlord.ButterLib.*" });
this.AddSerilogLoggerProvider("trace.txt", new[] { "System.Diagnostics.Logger.*" });
this.AddSerilogLoggerProvider("butterlib.txt", ["Bannerlord.ButterLib.*"]);
this.AddSerilogLoggerProvider("trace.txt", ["System.Diagnostics.Logger.*"]);
//this.AddSteamWriterLogger("harmony.txt", out var harmonyStreamWriter);
//HarmonyLib.FileLog.LogWriter = harmonyStreamWriter;

Expand Down Expand Up @@ -110,6 +106,8 @@ protected override void OnSubModuleLoad()
if (!DelayedServiceCreation)
InitializeServices();

DelayedSubModuleSubSystem.Instance?.Enable();
SubModuleWrappers2SubSystem.Instance?.Enable();
ExceptionHandlerSubSystem.Instance?.Enable();
CrashUploaderSubSystem.Instance?.Enable();

Expand Down
11 changes: 10 additions & 1 deletion src/Bannerlord.ButterLib/CrashUploader/CrashUploaderSubSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bannerlord.ButterLib.SubSystems;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubSystems;

namespace Bannerlord.ButterLib.CrashUploader;

Expand All @@ -13,6 +14,7 @@ internal sealed class CrashUploaderSubSystem : ISubSystem
public bool CanBeDisabled => true;
public bool CanBeSwitchedAtRuntime => true;

private bool _wasInitialized;

public CrashUploaderSubSystem()
{
Expand All @@ -21,6 +23,13 @@ public CrashUploaderSubSystem()

public void Enable()
{
if (!_wasInitialized)
{
_wasInitialized = true;
var isEnabledViaSettings = SettingsProvider.PopulateSubSystemSettings(this) ?? true;
if (!isEnabledViaSettings) return;
}

if (IsEnabled) return;
IsEnabled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ namespace Bannerlord.ButterLib.DelayedSubModule;

internal class DelayedSubModuleSubSystem : ISubSystem
{
public static DelayedSubModuleSubSystem? Instance { get; private set; }

public string Id => "Delayed SubModule";
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;

public DelayedSubModuleSubSystem()
{
Instance = this;
}

public void Enable() { }
public void Disable() { }
}
3 changes: 3 additions & 0 deletions src/Bannerlord.ButterLib/ExceptionHandler/BEWPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ private static void Finalizer(Exception? __exception)
{
if (__exception is not null)
{
if (ExceptionHandlerSubSystem.Instance?.DisableWhenDebuggerIsAttached == true && IsDebuggerAttached())
return;

ExceptionReporter.Show(__exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bannerlord.BLSE;
using Bannerlord.ButterLib.Options;
using Bannerlord.ButterLib.SubSystems;
using Bannerlord.ButterLib.SubSystems.Settings;

Expand Down Expand Up @@ -37,33 +38,38 @@ public bool DisableWhenDebuggerIsAttached
get => _disableWhenDebuggerIsAttached;
private set
{
if (_disableWhenDebuggerIsAttached != value)
{
_disableWhenDebuggerIsAttached = value;
if (_disableWhenDebuggerIsAttached == value) return;

_disableWhenDebuggerIsAttached = value;

if (BEWPatch.IsDebuggerAttached())
if (BEWPatch.IsDebuggerAttached())
{
if (_disableWhenDebuggerIsAttached)
{
if (_disableWhenDebuggerIsAttached)
UnsubscribeToUnhandledException();
else
SubscribeToUnhandledException();
Disable();
}
else
{
SubscribeToUnhandledException();
}
}
else
{
SubscribeToUnhandledException();
}
}
}

private bool _wasInitialized;

/// <inheritdoc />
public IReadOnlyCollection<SubSystemSettingsDeclaration<ExceptionHandlerSubSystem>> Declarations { get; } = new SubSystemSettingsDeclaration<ExceptionHandlerSubSystem>[]
{
public IReadOnlyCollection<SubSystemSettingsDeclaration<ExceptionHandlerSubSystem>> Declarations { get; } =
[
new SubSystemSettingsPropertyBool<ExceptionHandlerSubSystem>(
"{=B7bfrDNzIk} Disable when Debugger is Attached",
"{=r3ktQzFMRz} Stops the Exception Handler when a debugger is attached.",
x => x.DisableWhenDebuggerIsAttached),
};
];


public ExceptionHandlerSubSystem()
Expand All @@ -73,12 +79,19 @@ public ExceptionHandlerSubSystem()

public void Enable()
{
if (!_wasInitialized)
{
_wasInitialized = true;
var isEnabledViaSettings = SettingsProvider.PopulateSubSystemSettings(this) ?? true;
if (!isEnabledViaSettings) return;
}

if (IsEnabled) return;
IsEnabled = true;

if (!BEWPatch.IsDebuggerAttached())
SubscribeToUnhandledException();
else if (Instance?.DisableWhenDebuggerIsAttached == true)
else if (_disableWhenDebuggerIsAttached)
return;

if (!_wasButrLoaderInterceptorCalled)
Expand Down
6 changes: 4 additions & 2 deletions src/Bannerlord.ButterLib/Options/ButterLibOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Bannerlord.ButterLib.Options;
using Microsoft.Extensions.Logging;

namespace Bannerlord.ButterLib.Options;

public sealed class ButterLibOptions
{
public int MinLogLevel { get; set; }
public int MinLogLevel { get; set; } = (int) LogLevel.Information;
}
62 changes: 0 additions & 62 deletions src/Bannerlord.ButterLib/Options/JsonButterLibOptionsModel.cs

This file was deleted.

Loading

0 comments on commit ede8ac9

Please sign in to comment.