Skip to content

Commit

Permalink
feat: PatchBeatmapThumbnailAlpha
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 24, 2024
1 parent 1464569 commit 822a62e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
72 changes: 72 additions & 0 deletions Osu.Patcher.Hook/Patches/PatchBeatmapThumbnailAlpha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using static System.Reflection.Emit.OpCodes;

namespace Osu.Patcher.Hook.Patches;

/// <summary>
/// Changes the default alpha on beatmap thumbnails in song select.
/// </summary>
[HarmonyPatch]
[UsedImplicitly]
public class PatchBeatmapThumbnailAlpha : BasePatch
{
// TODO: make this user configurable through settings
private const int NewDeselectedAlpha = 185; // 0-255

[UsedImplicitly]
[HarmonyTargetMethods]
private static IEnumerable<MethodBase> Target() =>
[
BeatmapTreeItem.PopulateSprites.Reference,
BeatmapTreeItem.UpdateSprites.Reference,
];

/// <summary>
/// Changes the target color used as an alpha overlay over the thumbnail.
/// Replaces all <c>50</c>s as in <c>new Color(50, 50, 50, ...)</c> with a higher value.
/// Applied to both <c>UpdateSprites</c> and <c>PopulateSprites</c>.
/// </summary>
[UsedImplicitly]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> ChangeDeselectFade(IEnumerable<CodeInstruction> instructions) =>
instructions.Manipulator(
inst => inst.Is(Ldc_I4_S, 50),
inst => inst.operand = NewDeselectedAlpha
);


/// <summary>
/// Avoid re-fading in from zero when alpha already high from previous patches.
/// Replaces the following code with Nop: <code>thumbnail.FadeInFromZero(instant ? 0 : 1000);</code>
/// This is only applicable to <c>UpdateSprites()</c>.
/// </summary>
[UsedImplicitly]
[HarmonyTranspiler]
[SuppressMessage("ReSharper", "InconsistentNaming")]
private static IEnumerable<CodeInstruction> DisableFadeInFromZero(
IEnumerable<CodeInstruction> instructions,
MethodBase __originalMethod)
{
if (__originalMethod != BeatmapTreeItem.UpdateSprites.Reference)
return instructions;

return NoopSignature(instructions, new[]
{
Ldarg_0,
Ldfld,
Ldloc_0,
Ldfld,
Brtrue_S,
Ldc_I4,
Br_S,
Ldc_I4_0,
Callvirt,
Pop,
});
}
}
62 changes: 62 additions & 0 deletions Osu.Stubs/BeatmapTreeItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using JetBrains.Annotations;
using Osu.Stubs.Opcode;
using static System.Reflection.Emit.OpCodes;

namespace Osu.Stubs;

/// <summary>
/// Original: <c>osu.GameplayElements.Beatmaps.BeatmapTreeItem</c>
/// b20240102.2: <c>#=z5dQ2d9vSoRzE9rWp7h5_dJ$Z1Sw13QcKf_J$7ZjIN21zOue2gQ==</c>
/// </summary>
[UsedImplicitly]
public static class BeatmapTreeItem
{
/// <summary>
/// Original: <c>PopulateSprites(TreeItemState lastState, bool instant)</c>
/// b20240102.2: <c>#=ztf5JjnV1ubq2KLwXBg==</c>
/// </summary>
[UsedImplicitly]
public static readonly LazyMethod UpdateSprites = new(
"BeatmapTreeItem#UpdateSprites(...)",
new[]
{
Pop,
Ldsfld,
Ldftn,
Newobj,
Dup,
Stsfld,
Callvirt,
Ret,
Ldarg_0,
Ldfld,
Ldloc_0,
Ldftn,
Newobj,
}
);

/// <summary>
/// Original: <c>PopulateSprites()</c>
/// b20240102.2: <c>#=zGdedQLY8W$wSqdNzBA==</c>
/// </summary>
[UsedImplicitly]
public static readonly LazyMethod PopulateSprites = new(
"BeatmapTreeItem#PopulateSprites()",
new[]
{
Brfalse,
Ldsfld,
Ldfld,
Ldc_I4,
Clt,
Ldc_I4_0,
Ceq,
Stloc_S,
Ldarg_0,
Ldc_I4_5,
Newarr,
Dup,
}
);
}
2 changes: 1 addition & 1 deletion Osu.Stubs/BeatmapTreeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Osu.Stubs;

/// <summary>
/// Original: <c>osu.GameplayElements.Beatmaps:BeatmapTreeManager</c>
/// Original: <c>osu.GameplayElements.Beatmaps.BeatmapTreeManager</c>
/// b20240102.2: <c>#=zV51QPv13Z_vZJRxEY28cvGye77gU6YZQsv_F5muuWuN62V5sIQ==</c>
/// </summary>
[UsedImplicitly]
Expand Down

0 comments on commit 822a62e

Please sign in to comment.