Skip to content

Commit

Permalink
refactor(OsuPatch): use attributes instead
Browse files Browse the repository at this point in the history
Since patches are static classes, there isn't a way to override stuff from a base class; Attributes are the only other way.
  • Loading branch information
rushiiMachine committed Mar 30, 2024
1 parent bdcf92f commit f51439d
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 67 deletions.
3 changes: 1 addition & 2 deletions Osu.Patcher.Hook/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ private static void InitializePatches(Harmony harmony)
{
foreach (var type in AccessTools.GetTypesFromAssembly(typeof(Hook).Assembly))
{
// Check if the type extends OsuPatch
if (!type.IsSubclassOf(typeof(OsuPatch)))
if (!OsuPatchProcessor.IsOsuPatch(type))
continue;

Debug.WriteLine($"Processing Patch {type.Name}", "Hook");
Expand Down
29 changes: 13 additions & 16 deletions Osu.Patcher.Hook/Patches/BeatmapMirror/EnableOsuDirect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
Expand All @@ -25,23 +24,11 @@ namespace Osu.Patcher.Hook.Patches.BeatmapMirror;
/// if (8 > Permissions.None || ...)
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class EnableOsuDirect : OsuPatch
internal static class EnableOsuDirect
{
private static readonly OpCode[] Signature =
{
// Call,
// Ldc_I4_4,
// And,
// -- Inject right here to replace the result of And --
Ldc_I4_0,
Bgt_S,
Ldsfld,
Call,
Ldc_I4_S,
};

[UsedImplicitly]
[HarmonyTargetMethod]
private static MethodBase Target() => OsuDirect.HandlePickup.Reference;
Expand All @@ -51,7 +38,17 @@ internal class EnableOsuDirect : OsuPatch
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
instructions = instructions.InsertBeforeSignature(
Signature,
[
// Call,
// Ldc_I4_4,
// And,
// -- Inject right here to replace the result of And --
Ldc_I4_0,
Bgt_S,
Ldsfld,
Call,
Ldc_I4_S,
],
new CodeInstruction[]
{
// Replace the result of Add with a higher value than compared against
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ namespace Osu.Patcher.Hook.Patches.LivePerformance;
/// the performance counter to the ScoreDisplay's sprite manager.
/// To display "pp" this needs <c>[email protected]</c>/<c>score-p.png</c> in your skin's defined score font.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AddPerformanceToUi : OsuPatch
internal static class AddPerformanceToUi
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
3 changes: 2 additions & 1 deletion Osu.Patcher.Hook/Patches/LivePerformance/TrackOnScoreHit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ namespace Osu.Patcher.Hook.Patches.LivePerformance;
/// Hooks <c>Ruleset::OnIncreaseScoreHit(...)</c> to send score updates to our performance calculator
/// so it can recalculate based on new HitObject judgements.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class TrackOnScoreHit : OsuPatch
internal static class TrackOnScoreHit
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
5 changes: 3 additions & 2 deletions Osu.Patcher.Hook/Patches/LivePerformance/TrackResetScore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
namespace Osu.Patcher.Hook.Patches.LivePerformance;

/// <summary>
/// Hooks <c>Ruleset::ResetScore()</c> to also reset our performance calculator and caches.
/// Hooks <c>Ruleset::ResetScore()</c> to also reset our performance calculator.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class TrackResetScore : OsuPatch
internal static class TrackResetScore
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
8 changes: 4 additions & 4 deletions Osu.Patcher.Hook/Patches/Misc/AllowPlayModeReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ namespace Osu.Patcher.Hook.Patches.Misc;
/// return true;
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AllowPlayModeReload : OsuPatch
internal static class AllowPlayModeReload
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand All @@ -40,15 +41,14 @@ internal class AllowPlayModeReload : OsuPatch
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
instructions = instructions.InsertBeforeSignature(
new[]
{
[
// Ldsfld, // Loads the ReplayScore to check if it's null
// -- Inject right here to override the value --
Brfalse_S,
Ldsfld,
Ldfld,
Call,
},
],
new CodeInstruction[]
{
new(Pop),
Expand Down
3 changes: 2 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/DisableErrorReporting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace Osu.Patcher.Hook.Patches.Misc;
/// <summary>
/// Disable the error reporter to prevent sentry from being spammed with errors possibly caused by this patcher.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class DisableErrorReporting : OsuPatch
internal static class DisableErrorReporting
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
4 changes: 2 additions & 2 deletions Osu.Patcher.Hook/Patches/Misc/FixDoubleSkipping.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
Expand All @@ -21,9 +20,10 @@ namespace Osu.Patcher.Hook.Patches.Misc;
/// int leadIn = leadInTime < 0 ? -leadInTime : 0;
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class FixDoubleSkipping : OsuPatch
internal static class FixDoubleSkipping
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
3 changes: 2 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/LogOsuLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace Osu.Patcher.Hook.Patches.Misc;
/// <summary>
/// Hook <c>osu_common.Helpers.Logger:Log</c> to print the live log to our console.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class LogOsuLogger : OsuPatch
internal static class LogOsuLogger
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
3 changes: 2 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/LogSoftErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace Osu.Patcher.Hook.Patches.Misc;
/// <summary>
/// Hooks <c>osu.GameBase:softHandle(Exception)</c> to log all soft exceptions thrown inside osu! to our console.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class LogSoftErrors : OsuPatch
internal static class LogSoftErrors
{
[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
11 changes: 8 additions & 3 deletions Osu.Patcher.Hook/Patches/Mods/PatchSuddenDeathAutoRetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ namespace Osu.Patcher.Hook.Patches.Mods;
/// if ((this.CurrentScore.EnabledMods & mask) > Mods.None)
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class PatchSuddenDeathAutoRetry : OsuPatch
internal static class PatchSuddenDeathAutoRetry
{
private const int ModPerfect = 1 << 14;
private const int ModSuddenDeath = 1 << 5;
Expand All @@ -38,9 +39,13 @@ internal class PatchSuddenDeathAutoRetry : OsuPatch
/// </summary>
[UsedImplicitly]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) =>
instructions.Manipulator(
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
instructions = instructions.Manipulator(
inst => inst.opcode == Ldc_I4 && inst.OperandIs(ModPerfect),
inst => inst.operand = ModPerfect | ModSuddenDeath
);

return instructions;
}
}
7 changes: 5 additions & 2 deletions Osu.Patcher.Hook/Patches/OsuPatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;

namespace Osu.Patcher.Hook.Patches;

/// <summary>
/// A base patch that provides utility methods for signature-based patching.
/// A marker for osu! patches for <see cref="OsuPatchProcessor" /> to handle.
/// </summary>
public abstract class OsuPatch;
[AttributeUsage(AttributeTargets.Class)]
public class OsuPatch : Attribute;
8 changes: 7 additions & 1 deletion Osu.Patcher.Hook/Patches/OsuPatchProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public OsuPatchProcessor(Harmony instance, Type type) : base(instance, type)
AddFinalizerPatch(type);
}

/// <summary>
/// Checks if the specified type is marked as an <see cref="OsuPatch" />.
/// </summary>
public static bool IsOsuPatch(Type type) =>
type.GetCustomAttribute(typeof(OsuPatch)) != null;

/// <summary>
/// Adds a new <see cref="HarmonyFinalizer" /> attribute patch to the
/// current patch processor targeting a specific <paramref name="type" />.
Expand All @@ -54,7 +60,7 @@ private void AddFinalizerPatch(Type type)
[UsedImplicitly]
[HarmonyFinalizer]
[SuppressMessage("ReSharper", "InconsistentNaming")]
private static void BaseFinalizer<P>(Exception? __exception) where P : OsuPatch
private static void BaseFinalizer<P>(Exception? __exception)
{
if (__exception == null)
return;
Expand Down
7 changes: 4 additions & 3 deletions Osu.Patcher.Hook/Patches/Relax/AllowRelaxComboBreakSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ namespace Osu.Patcher.Hook.Patches.Relax;
/// if (this.ComboCounter.HitCombo > 20)
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AllowRelaxComboBreakSound : OsuPatch
internal static class AllowRelaxComboBreakSound
{
// #=z04fOmc1I_BS0TV6TAo2QOUQvjceryuOcqoleWPg=:#=zSio4IZHzUUrC
private static readonly OpCode[] Signature =
{
[
OpCodes.Ldarg_0,
OpCodes.Ldfld,
OpCodes.Callvirt,
Expand All @@ -39,7 +40,7 @@ internal class AllowRelaxComboBreakSound : OsuPatch
OpCodes.Brtrue_S, // All no-oped (4 inst)
OpCodes.Ldsfld,
OpCodes.Brtrue_S, // --------
};
];

[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
7 changes: 4 additions & 3 deletions Osu.Patcher.Hook/Patches/Relax/AllowRelaxDrawMisses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ namespace Osu.Patcher.Hook.Patches.Relax;
/// if (increaseScoreType == (IncreaseScoreType)(-131072))
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AllowRelaxDrawMisses : OsuPatch
internal static class AllowRelaxDrawMisses
{
// #=zTBjFb7Vm$jY$rY4MsKxmcIvGHnQN:\u0005\u200A\u2002\u2002\u2001\u2004\u2003\u2007\u2001\u2002\u2002\u2000
private static readonly OpCode[] Signature =
{
[
OpCodes.Ldarg_1,
OpCodes.Ldc_I4_8,
OpCodes.Callvirt,
Expand All @@ -40,7 +41,7 @@ internal class AllowRelaxDrawMisses : OsuPatch
OpCodes.Brtrue, // No-oped (4 inst)
OpCodes.Ldsfld,
OpCodes.Brtrue, // ----------
};
];

[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
7 changes: 4 additions & 3 deletions Osu.Patcher.Hook/Patches/Relax/AllowRelaxFailing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ namespace Osu.Patcher.Hook.Patches.Relax;
/// if ((mods & Mods.NoFail) <= Mods.None && ...)
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AllowRelaxFailing : OsuPatch
internal static class AllowRelaxFailing
{
// #=zeXZ7VnmadWamDozl0oXkDPqWT5QR:#=zwMd5KYaUmGit
private static readonly OpCode[] Signature =
{
[
OpCodes.And,
OpCodes.Ldc_I4_0,
OpCodes.Cgt,
Expand All @@ -37,7 +38,7 @@ internal class AllowRelaxFailing : OsuPatch
OpCodes.Brtrue_S, // No-oped (4 inst)
OpCodes.Ldsfld,
OpCodes.Brtrue_S, // ---------
};
];

[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
7 changes: 4 additions & 3 deletions Osu.Patcher.Hook/Patches/Relax/AllowRelaxLowHpGlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ namespace Osu.Patcher.Hook.Patches.Relax;
/// GameBase.FadeState == (FadeStates)2)
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AllowRelaxLowHpGlow : OsuPatch
internal static class AllowRelaxLowHpGlow
{
private static readonly OpCode[] Signature =
{
[
OpCodes.Ldarg_0,
OpCodes.Ldfld,
OpCodes.Ldfld,
Expand All @@ -51,7 +52,7 @@ internal class AllowRelaxLowHpGlow : OsuPatch
OpCodes.Brtrue, // No-oped (4 inst)
OpCodes.Ldsfld,
OpCodes.Brtrue, // ---------
};
];

[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
7 changes: 4 additions & 3 deletions Osu.Patcher.Hook/Patches/Relax/AutoSaveRelaxScores.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ namespace Osu.Patcher.Hook.Patches.Relax;
/// {
/// ]]></code>
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal class AutoSaveRelaxScores : OsuPatch
internal static class AutoSaveRelaxScores
{
// #=zG9n2xn5fBJ3KmhYrFhPv_ouHnledvs2AJ1Dwx_c=:#=zPWtjIx_tsaf1
private static readonly OpCode[] Signature =
{
[
OpCodes.Ldarg_0,
OpCodes.Ldfld,
OpCodes.Ldfld,
Expand Down Expand Up @@ -60,7 +61,7 @@ internal class AutoSaveRelaxScores : OsuPatch
OpCodes.Ldc_I4_0,
OpCodes.Cgt,
OpCodes.Brtrue, // <-------------
};
];

[UsedImplicitly]
[HarmonyTargetMethod]
Expand Down
Loading

0 comments on commit f51439d

Please sign in to comment.