Skip to content

Commit

Permalink
[O] better polyfill load
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Sep 28, 2024
1 parent 74e39c4 commit 24ecaab
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 51 deletions.
148 changes: 97 additions & 51 deletions AquaMai/Fix/DebugFeature.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using MAI2.Util;
Expand All @@ -11,70 +10,76 @@

namespace AquaMai.Fix;

[HarmonyPatch]
public class DebugFeature
{
public static bool IsPatchingSkipped { get; private set; }
private static bool isPause;
private static double timer;
public static bool IsPolyfill { get; private set; }
private static GameProcess _gameProcess;
private static MovieController _gameMovie;
private static GameMonitor[] _monitors;
private static object _debugFeatureOriginal;

public static IEnumerable<MethodBase> TargetMethods()
[HarmonyPatch(typeof(GameProcess), "OnStart")]
[HarmonyPostfix]
public static void Init(GameProcess __instance, MovieController ____gameMovie, GameMonitor[] ____monitors)
{
var original = typeof(GameProcess).GetField("debugFeature", BindingFlags.NonPublic | BindingFlags.Instance);
if (original is not null)
{
MelonLogger.Msg("[DebugFeature] Skipped because already included");
IsPatchingSkipped = true;
return [];
}

return [AccessTools.Method(typeof(GameProcess), "OnUpdate")];
_gameProcess = __instance;
_gameMovie = ____gameMovie;
_monitors = ____monitors;
}

public static void Postfix(GameProcess __instance, byte ____sequence, MovieController ____gameMovie, GameMonitor[] ____monitors)
public static void DoCustomPatch(HarmonyLib.Harmony h)
{
if (____sequence != 4) return;
// GameSequence.Play
if (!isPause)
var original = typeof(GameProcess).GetField("debugFeature", BindingFlags.NonPublic | BindingFlags.Instance);
if (original is null)
{
timer += GameManager.GetGameMSecAddD();
MelonLogger.Msg(" > [DebugFeature] Running Polyfill");
IsPolyfill = true;
h.PatchAll(typeof(PolyFill));
}

if (Input.GetKeyDown(KeyCode.Home))
else
{
GameManager.AutoPlay = (GameManager.AutoPlayMode)((int)(GameManager.AutoPlay + 1) % Enum.GetNames(typeof(GameManager.AutoPlayMode)).Length);
MelonLogger.Msg(" > [DebugFeature] Already included");
h.PatchAll(typeof(GetOriginal));
}
else if (Input.GetKeyDown(KeyCode.Return))
}

public static void SetPause(bool pause)
{
if (IsPolyfill)
{
isPause = !isPause;
SoundManager.PauseMusic(isPause);
____gameMovie.Pause(isPause);
NotesManager.Pause(isPause);
PolyFill.isPause = pause;
}
else if (DebugInput.GetKeyDown(KeyCode.LeftArrow) || DebugInput.GetKeyDown(KeyCode.RightArrow))
else
{
var num23 = 0;
if (DebugInput.GetKeyDown(KeyCode.LeftArrow))
{
num23 = -1000;
}
var debugFeatureClass = typeof(GameProcess).GetNestedType("DebugFeature", BindingFlags.Instance | BindingFlags.NonPublic);
debugFeatureClass?.GetField("_debugPause", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(_debugFeatureOriginal, pause);
}

if (DebugInput.GetKeyDown(KeyCode.RightArrow))
{
num23 = 1000;
}
SoundManager.PauseMusic(pause);
_gameMovie.Pause(pause);
NotesManager.Pause(pause);
}

int addMsec = ((!DebugInput.GetKey(KeyCode.LeftShift) && !DebugInput.GetKey(KeyCode.RightShift)) ? ((!DebugInput.GetKey(KeyCode.LeftControl) && !DebugInput.GetKey(KeyCode.RightControl)) ? num23 : (num23 * 10)) : (num23 * 5));
Singleton<GamePlayManager>.Instance.Initialize();
DebugTimeSkip(addMsec);
[HarmonyPatch]
private static class GetOriginal
{
[HarmonyPatch(typeof(GameProcess), "OnStart")]
[HarmonyPostfix]
public static void Postfix(object ___debugFeature)
{
_debugFeatureOriginal = ___debugFeature;
}
}

return;

[HarmonyPatch]
private static class PolyFill
{
public static bool isPause;
public static double timer;

void DebugTimeSkip(int addMsec)
public static void DebugTimeSkip(int addMsec)
{
____gameMovie.Pause(pauseFlag: true);
_gameMovie.Pause(pauseFlag: true);
NotesManager.Pause(true);
if (addMsec >= 0)
{
Expand All @@ -85,11 +90,11 @@ void DebugTimeSkip(int addMsec)
timer = timer + addMsec >= 0.0 ? timer + addMsec : 0.0;
}

____gameMovie.SetSeekFrame(timer);
_gameMovie.SetSeekFrame(timer);
SoundManager.SeekMusic((int)timer);
for (int i = 0; i < ____monitors.Length; i++)
for (int i = 0; i < _monitors.Length; i++)
{
____monitors[i].Seek((int)timer);
_monitors[i].Seek((int)timer);
}

// magic number, dont know why
Expand All @@ -98,14 +103,55 @@ void DebugTimeSkip(int addMsec)
if (!isPause)
{
SoundManager.PauseMusic(pause: false);
____gameMovie.Pause(pauseFlag: false);
_gameMovie.Pause(pauseFlag: false);
}
else
{
____gameMovie.Pause(pauseFlag: true);
_gameMovie.Pause(pauseFlag: true);
}

_gameProcess.UpdateNotes();
}

[HarmonyPatch(typeof(GameProcess), "OnUpdate")]
[HarmonyPostfix]
public static void Postfix(byte ____sequence)
{
if (____sequence != 4) return;
// GameSequence.Play
if (!isPause)
{
timer += GameManager.GetGameMSecAddD();
}

__instance.UpdateNotes();
if (Input.GetKeyDown(KeyCode.Home))
{
GameManager.AutoPlay = (GameManager.AutoPlayMode)((int)(GameManager.AutoPlay + 1) % Enum.GetNames(typeof(GameManager.AutoPlayMode)).Length);
}
else if (Input.GetKeyDown(KeyCode.Return))
{
isPause = !isPause;
SoundManager.PauseMusic(isPause);
_gameMovie.Pause(isPause);
NotesManager.Pause(isPause);
}
else if (DebugInput.GetKeyDown(KeyCode.LeftArrow) || DebugInput.GetKeyDown(KeyCode.RightArrow))
{
var num23 = 0;
if (DebugInput.GetKeyDown(KeyCode.LeftArrow))
{
num23 = -1000;
}

if (DebugInput.GetKeyDown(KeyCode.RightArrow))
{
num23 = 1000;
}

int addMsec = ((!DebugInput.GetKey(KeyCode.LeftShift) && !DebugInput.GetKey(KeyCode.RightShift)) ? ((!DebugInput.GetKey(KeyCode.LeftControl) && !DebugInput.GetKey(KeyCode.RightControl)) ? num23 : (num23 * 10)) : (num23 * 5));
Singleton<GamePlayManager>.Instance.Initialize();
DebugTimeSkip(addMsec);
}
}
}
}
4 changes: 4 additions & 0 deletions AquaMai/Main.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using AquaMai.Fix;
using AquaMai.Helpers;
Expand Down Expand Up @@ -35,6 +36,9 @@ private void Patch(Type type)
{
Patch(nested);
}

var customMethod = type.GetMethod("DoCustomPatch", BindingFlags.Public | BindingFlags.Static);
customMethod?.Invoke(null, [HarmonyInstance]);
}
catch (Exception e)
{
Expand Down

0 comments on commit 24ecaab

Please sign in to comment.