Skip to content

Commit

Permalink
feat(PatcherOptions): reload options when finished initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Apr 17, 2024
1 parent a36411f commit 52454d8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Osu.Patcher.Hook/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static int Initialize(string _)
_harmony = new Harmony("osu!patcher");
InitializePatches(_harmony);

// Forcefully reload options to allow patches to inject new options
OptionsUtils.ReloadOptions();

Notifications.ShowMessage(
"osu!patcher initialized!",
NotificationColor.Neutral,
Expand Down
43 changes: 43 additions & 0 deletions Osu.Stubs/Helpers/Scheduler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using JetBrains.Annotations;
using Osu.Utils.Lazy;
using static System.Reflection.Emit.OpCodes;

namespace Osu.Stubs.Helpers;

[PublicAPI]
public class Scheduler
{
/// <summary>
/// Original: <c>osu_common.Helpers.Scheduler</c>
/// </summary>
[Stub]
public static readonly LazyType Class = new(
"osu_common.Helpers.Scheduler",
() => Add!.Reference.DeclaringType
);

/// <summary>
/// Original: <c>Add(VoidDelegate task, bool forceScheduled)</c>
/// Returns whether the supplied task was immediately run without scheduling.
/// </summary>
[Stub]
public static readonly LazyMethod<bool> Add = LazyMethod<bool>.ByPartialSignature(
"osu_common.Helpers.Scheduler::Add(VoidDelegate, bool)",
[
Ldloca_S,
Call,
Ldarg_0,
Ldfld,
Ldarg_1,
Callvirt,
Leave_S,
Ldloc_1,
Brfalse_S,
Ldloc_0,
Call,
Endfinally,
Ldc_I4_0,
Ret,
]
);
}
15 changes: 15 additions & 0 deletions Osu.Stubs/Root/GameBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Utils.Extensions;
using Osu.Utils.Lazy;
using static System.Reflection.Emit.OpCodes;

Expand Down Expand Up @@ -71,4 +72,18 @@ public static class GameBase
.GetDeclaredFields()
.First(field => field.FieldType == GameModes.Options.Options.Class.Reference)
);

/// <summary>
/// Original: <c>Scheduler</c>
/// This is the main thread scheduler.
/// </summary>
[Stub]
public static readonly LazyField<object> Scheduler = new(
"osu.GameBase::Scheduler",
// There's two fields of type Scheduler, get the first one
() => Class.Reference
.GetDeclaredFields()
.Where(field => field.FieldType == Helpers.Scheduler.Class.Reference)
.FirstOrNull()!
);
}
28 changes: 28 additions & 0 deletions Osu.Stubs/Wrappers/OptionsUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using JetBrains.Annotations;
using Osu.Stubs.GameModes.Options;
using Osu.Stubs.Helpers;
using Osu.Stubs.Root;

namespace Osu.Stubs.Wrappers;

[PublicAPI]
public static class OptionsUtils
{
/// <summary>
/// Reloads the options panel to reset the available options.
/// </summary>
/// <param name="scrollToTop">Forcefully scroll to the top after reloading.</param>
public static void ReloadOptions(bool scrollToTop = true)
{
var mainScheduler = GameBase.Scheduler.Get();
var options = GameBase.Options.Get();

var task = VoidDelegate.MakeInstance(() =>
Options.ReloadElements.Invoke(options, [scrollToTop]));

Scheduler.Add.Invoke(mainScheduler, [
/* task: */ task,
/* forceScheduled: */ false,
]);
}
}

0 comments on commit 52454d8

Please sign in to comment.