-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bfcdd4
commit afdaf79
Showing
9 changed files
with
201 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Osu.Patcher.Hook/Patches/Mods/AudioPreview/ModAudioEffects.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using Osu.Stubs.Audio; | ||
using Osu.Stubs.Scoring; | ||
using static Osu.Stubs.Other.Mods; | ||
|
||
namespace Osu.Patcher.Hook.Patches.Mods.AudioPreview; | ||
|
||
/// <summary> | ||
/// Handles applying and resetting the audio effects on the AudioEngine. | ||
/// </summary> | ||
internal static class ModAudioEffects | ||
{ | ||
/// <summary> | ||
/// Applies the audio changes to the AudioEngine based on the current global mods. | ||
/// </summary> | ||
internal static void ApplyModEffects() | ||
{ | ||
ResetChanges(); | ||
|
||
var mods = ModManager.ModStatus.Get(); | ||
|
||
// NC always comes with DT | ||
if ((mods & Nightcore) > None) | ||
mods &= ~DoubleTime; | ||
|
||
switch (mods & (DoubleTime | Nightcore | HalfTime)) | ||
{ | ||
case DoubleTime: | ||
UpdateAudioRate(rate => rate * 1.5); | ||
break; | ||
case Nightcore: | ||
AudioEngine.Nightcore.Set(true); | ||
UpdateAudioRate(rate => rate * 1.5); | ||
break; | ||
case HalfTime: | ||
UpdateAudioRate(rate => rate * 0.75); | ||
break; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Resets the audio stream effects back to default. | ||
/// </summary> | ||
private static void ResetChanges() | ||
{ | ||
AudioEngine.Nightcore.Set(false); | ||
UpdateAudioRate(_ => 100); | ||
} | ||
|
||
/// <summary> | ||
/// Gets, modifies, and writes back the CurrentPlaybackRate on the AudioEngine. | ||
/// </summary> | ||
/// <param name="onModify">Rate transformer.</param> | ||
private static void UpdateAudioRate(Func<double, double> onModify) | ||
{ | ||
var currentRate = AudioEngine.GetCurrentPlaybackRate.Invoke(); | ||
var newRate = onModify.Invoke(currentRate); | ||
|
||
AudioEngine.SetCurrentPlaybackRate.Invoke(parameters: [newRate]); | ||
} | ||
} |
65 changes: 6 additions & 59 deletions
65
Osu.Patcher.Hook/Patches/Mods/AudioPreview/ModSelectAudioPreview.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,24 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using HarmonyLib; | ||
using JetBrains.Annotations; | ||
using Osu.Stubs.Audio; | ||
using Osu.Stubs.SongSelect; | ||
using static Osu.Stubs.Other.Mods; | ||
|
||
namespace Osu.Patcher.Hook.Patches.Mods.AudioPreview; | ||
|
||
/// <summary> | ||
/// Hooks the place where ModButtons get updates in the mod selection menu to apply audio effects. | ||
/// </summary> | ||
[OsuPatch] | ||
[HarmonyPatch] | ||
[UsedImplicitly] | ||
internal class ModSelectAudioPreview | ||
{ | ||
[UsedImplicitly] | ||
[HarmonyTargetMethod] | ||
private static MethodBase Target() => ModButton.SetStatus.Reference; | ||
private static MethodBase Target() => ModSelection.UpdateMods.Reference; | ||
|
||
[UsedImplicitly] | ||
[HarmonyPostfix] | ||
[SuppressMessage("ReSharper", "InconsistentNaming")] | ||
private static void After( | ||
object __instance, // typeof(ModButton) | ||
[HarmonyArgument(1)] int mod, | ||
[HarmonyArgument(2)] bool playSound) | ||
{ | ||
// These calls happen for all mods on any mod update | ||
// and don't actually indicate a ModButton being pressed | ||
if (!playSound) return; | ||
|
||
// var availableModStates = ModButton.AvailableStates.Get(__instance); | ||
// | ||
// // Check that this is the DT+NC or HF button | ||
// if (availableModStates[0] is not (DoubleTime or HalfTime)) | ||
// return; | ||
|
||
ApplyChanges(mod); | ||
} | ||
|
||
internal static void ApplyChanges(int mods) | ||
{ | ||
ResetChanges(); | ||
|
||
switch (mods & (DoubleTime | Nightcore | HalfTime)) | ||
{ | ||
case DoubleTime: | ||
UpdateAudioRate(rate => rate * 1.5); | ||
break; | ||
case Nightcore: | ||
AudioEngine.Nightcore.Set(true); | ||
UpdateAudioRate(rate => rate * 1.5); | ||
break; | ||
case HalfTime: | ||
UpdateAudioRate(rate => rate * 0.75); | ||
break; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Resets the audio stream effects back to default. | ||
/// </summary> | ||
private static void ResetChanges() | ||
{ | ||
AudioEngine.Nightcore.Set(false); | ||
UpdateAudioRate(_ => 100); | ||
} | ||
|
||
private static void UpdateAudioRate(Func<double, double> onModify) | ||
{ | ||
var currentRate = AudioEngine.GetCurrentPlaybackRate.Invoke(); | ||
var newRate = onModify.Invoke(currentRate); | ||
|
||
AudioEngine.SetCurrentPlaybackRate.Invoke(parameters: [newRate]); | ||
} | ||
private static void After() => Task.Run(ModAudioEffects.ApplyModEffects); | ||
} |
24 changes: 24 additions & 0 deletions
24
Osu.Patcher.Hook/Patches/Mods/AudioPreview/TrackUpdatePreviewMusic.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using HarmonyLib; | ||
using JetBrains.Annotations; | ||
using Osu.Stubs.Audio; | ||
|
||
namespace Osu.Patcher.Hook.Patches.Mods.AudioPreview; | ||
|
||
/// <summary> | ||
/// Hooks the place where preview audio gets loaded to apply our mod audio effects. | ||
/// </summary> | ||
[OsuPatch] | ||
[HarmonyPatch] | ||
[UsedImplicitly] | ||
public class TrackUpdatePreviewMusic | ||
{ | ||
[UsedImplicitly] | ||
[HarmonyTargetMethod] | ||
private static MethodBase Target() => AudioEngine.LoadAudioForPreview.Reference; | ||
|
||
[HarmonyPostfix] | ||
[UsedImplicitly] | ||
private static void After() => Task.Run(ModAudioEffects.ApplyModEffects); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Linq; | ||
using HarmonyLib; | ||
using JetBrains.Annotations; | ||
using Osu.Stubs.Other; | ||
using Osu.Utils.Lazy; | ||
using static System.Reflection.Emit.OpCodes; | ||
|
||
namespace Osu.Stubs.Scoring; | ||
|
||
[PublicAPI] | ||
public class ModManager | ||
{ | ||
/// <summary> | ||
/// Original: <c>osu.GameplayElements.Scoring.ModManager</c> | ||
/// b20240123: <c></c> | ||
/// </summary> | ||
[Stub] | ||
public static readonly LazyType Class = new( | ||
"osu.GameplayElements.Scoring.ModManager", | ||
() => AllowRanking!.Reference.DeclaringType! | ||
); | ||
|
||
/// <summary> | ||
/// Original: <c>AllowRanking(Mods enabledMods)</c> | ||
/// b20240123: <c></c> | ||
/// </summary> | ||
[Stub] | ||
public static readonly LazyMethod AllowRanking = LazyMethod.ByPartialSignature( | ||
"osu.GameplayElements.Scoring.ModManager::AllowRanking(Mods)", | ||
[ | ||
Ldloc_2, | ||
And, | ||
Ldc_I4_0, | ||
Cgt, | ||
Brfalse_S, | ||
Ldc_I4_0, | ||
Ret, | ||
Ldc_I4, | ||
Ldarg_0, | ||
Stloc_2, | ||
Stloc_1, | ||
Ldloc_1, | ||
Ldloc_2, | ||
] | ||
); | ||
|
||
/// <summary> | ||
/// Original: <c>ActiveMods</c> | ||
/// b20240123: <c></c> | ||
/// </summary> | ||
[Stub] | ||
public static readonly LazyField<int> ModStatus = new( | ||
"osu.GameplayElements.Scoring.ModManager::ModStatus", | ||
() => Class.Reference | ||
.GetDeclaredFields() | ||
.Single(field => field.FieldType == Mods.Type.Reference) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using JetBrains.Annotations; | ||
using Osu.Utils.Lazy; | ||
using static System.Reflection.Emit.OpCodes; | ||
|
||
namespace Osu.Stubs.SongSelect; | ||
|
||
/// <summary> | ||
/// Original: <c>osu.GameModes.Select.ModSelection</c> | ||
/// b20240123: <c></c> | ||
/// </summary> | ||
[PublicAPI] | ||
public class ModSelection | ||
{ | ||
/// <summary> | ||
/// Original: <c>updateMods()</c> | ||
/// b20240123: <c></c> | ||
/// </summary> | ||
[Stub] | ||
public static readonly LazyMethod UpdateMods = LazyMethod.ByPartialSignature( | ||
"osu.GameModes.Select.ModSelection::updateMods()", | ||
[ | ||
Ldc_I4_1, | ||
Conv_I8, | ||
Stloc_0, | ||
Br_S, | ||
Ldloc_0, | ||
Conv_I4, | ||
] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters