Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 27, 2024
1 parent 0944f00 commit ee54da9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Threading;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Performance;
using Osu.Stubs;

namespace Osu.Patcher.Hook.Patches.LivePerformance;

/// <summary>
/// Hooks <c>Ruleset::OnIncreaseScoreHit(...)</c> to send score updates to our performance calculator
/// so it can recalculate based on new HitObject judgements.
/// </summary>
[HarmonyPatch]
[UsedImplicitly]
public static class PatchUpdatePerformanceCalculator
public static class PatchTrackOnScoreHit
{
private static OsuPerformance? _performance;

internal static void ResetCalculator() => new Thread(() =>
{
Debug.WriteLine("Resetting performance calculator");

_performance?.Dispose();
_performance = null;

var currentScore = Player.CurrentScore.Get();
if (currentScore == null) return;

var modsObfuscated = Score.EnabledMods.Get(currentScore);
var mods = Score.EnabledModsGetValue.Invoke(modsObfuscated);

// Clear relax mod for now (live pp calculations for relax are fucking garbage)
mods &= ~(1 << 7);

var beatmap = Score.Beatmap.Get(currentScore);
if (beatmap == null) return;

var beatmapPath = Beatmap.GetBeatmapPath(beatmap);
if (beatmapPath == null) return;

_performance = new OsuPerformance(beatmapPath, (uint)mods);
_performance.OnNewCalculation += Console.WriteLine;
}).Start();

[UsedImplicitly]
[HarmonyTargetMethod]
private static MethodBase Target() => Ruleset.OnIncreaseScoreHit.Reference;
Expand All @@ -54,7 +28,12 @@ private static void After(
[HarmonyArgument(0)] int increaseScoreType,
[HarmonyArgument(2)] bool increaseCombo)
{
if (_performance == null) return;
Console.WriteLine(increaseScoreType);
if (!PerformanceCalculator.IsInitialized)
{
Console.WriteLine("OnIncreaseScoreHit called before performance calculator initialized!");
return;
}

var judgement = (increaseScoreType & ~IncreaseScoreType.OsuComboModifiers) switch
{
Expand All @@ -74,7 +53,7 @@ private static void After(
var CurrentScore = Ruleset.CurrentScore.Get(__instance);
var MaxCombo = Score.MaxCombo.Get(CurrentScore);

_performance.AddJudgement(judgement, (uint)MaxCombo);
PerformanceCalculator.Calculator?.AddJudgement(judgement, (uint)MaxCombo);
}

[UsedImplicitly]
Expand All @@ -84,7 +63,7 @@ private static void Finalizer(Exception? __exception)
{
if (__exception != null)
{
Console.WriteLine($"Exception due to {nameof(PatchUpdatePerformanceCalculator)}: {__exception}");
Console.WriteLine($"Exception due to {nameof(PatchTrackOnScoreHit)}: {__exception}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@

namespace Osu.Patcher.Hook.Patches.LivePerformance;

/// <summary>
/// Hooks <c>Ruleset::ResetScore()</c> to also reset our performance calculator.
/// </summary>
[HarmonyPatch]
[UsedImplicitly]
internal class PatchClearPerformanceCalculator
internal class PatchTrackResetScore
{
[UsedImplicitly]
[HarmonyTargetMethod]
private static MethodBase Target() => Ruleset.ResetScore.Reference;

[UsedImplicitly]
[HarmonyPostfix]
private static void After() => PatchUpdatePerformanceCalculator.ResetCalculator();
private static void After() => PerformanceCalculator.ResetCalculator();

[UsedImplicitly]
[HarmonyFinalizer]
Expand All @@ -26,7 +29,7 @@ private static void Finalizer(Exception? __exception)
{
if (__exception != null)
{
Console.WriteLine($"Exception due to {nameof(PatchClearPerformanceCalculator)}: {__exception}");
Console.WriteLine($"Exception due to {nameof(PatchTrackResetScore)}: {__exception}");
}
}
}
61 changes: 61 additions & 0 deletions Osu.Patcher.Hook/Patches/LivePerformance/PerformanceCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Diagnostics;
using Osu.Performance;
using Osu.Stubs;

namespace Osu.Patcher.Hook.Patches.LivePerformance;

/// <summary>
/// Handles initializing and disposing the performance calculator based on current score and settings.
/// </summary>
internal static class PerformanceCalculator
{
public static OsuPerformance? Calculator { get; private set; }

public static bool IsInitialized => Calculator != null;

/// <summary>
/// Disposes the existing performance calculator, and initializes a new one according to the current score info.
/// This must be called after <c>Ruleset::ResetScore()</c>
/// </summary>
public static void ResetCalculator()
{
try
{
ResetCalculatorSync();
}
catch (Exception e)
{
Console.WriteLine($"Failed to reset performance calculator: {e}");
}
}

private static void ResetCalculatorSync()
{
Debug.WriteLine("Resetting performance calculator");

// TODO: don't dispose it if the beatmap is the same, reuse parsed beatmap in native side
Calculator?.Dispose();
Calculator = null;

var currentScore = Player.CurrentScore.Get();
if (currentScore == null) return;

var modsObfuscated = Score.EnabledMods.Get(currentScore);
var mods = Score.EnabledModsGetValue.Invoke(modsObfuscated);

// Clear relax mod for now (live pp calculations for relax are fucking garbage)
mods &= ~(1 << 7);

var beatmap = Score.Beatmap.Get(currentScore);
if (beatmap == null) return;

var beatmapPath = Beatmap.GetBeatmapPath(beatmap);
if (beatmapPath == null) return;

Calculator = new OsuPerformance(beatmapPath, (uint)mods);
Calculator.OnNewCalculation += Console.WriteLine;

Debug.WriteLine("Initialized performance calculator!");
}
}

0 comments on commit ee54da9

Please sign in to comment.