[O] better polyfill load

pull/55/head
Clansty 2024-09-28 15:31:58 +08:00
parent 74e39c437d
commit 24ecaab570
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
2 changed files with 108 additions and 58 deletions

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using HarmonyLib; using HarmonyLib;
using MAI2.Util; using MAI2.Util;
@ -11,27 +10,112 @@ using UnityEngine;
namespace AquaMai.Fix; namespace AquaMai.Fix;
[HarmonyPatch]
public class DebugFeature public class DebugFeature
{ {
public static bool IsPatchingSkipped { get; private set; } public static bool IsPolyfill { get; private set; }
private static bool isPause; private static GameProcess _gameProcess;
private static double timer; 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)
{
_gameProcess = __instance;
_gameMovie = ____gameMovie;
_monitors = ____monitors;
}
public static void DoCustomPatch(HarmonyLib.Harmony h)
{ {
var original = typeof(GameProcess).GetField("debugFeature", BindingFlags.NonPublic | BindingFlags.Instance); var original = typeof(GameProcess).GetField("debugFeature", BindingFlags.NonPublic | BindingFlags.Instance);
if (original is not null) if (original is null)
{ {
MelonLogger.Msg("[DebugFeature] Skipped because already included"); MelonLogger.Msg(" > [DebugFeature] Running Polyfill");
IsPatchingSkipped = true; IsPolyfill = true;
return []; h.PatchAll(typeof(PolyFill));
}
else
{
MelonLogger.Msg(" > [DebugFeature] Already included");
h.PatchAll(typeof(GetOriginal));
}
} }
return [AccessTools.Method(typeof(GameProcess), "OnUpdate")]; public static void SetPause(bool pause)
{
if (IsPolyfill)
{
PolyFill.isPause = pause;
}
else
{
var debugFeatureClass = typeof(GameProcess).GetNestedType("DebugFeature", BindingFlags.Instance | BindingFlags.NonPublic);
debugFeatureClass?.GetField("_debugPause", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(_debugFeatureOriginal, pause);
} }
public static void Postfix(GameProcess __instance, byte ____sequence, MovieController ____gameMovie, GameMonitor[] ____monitors) SoundManager.PauseMusic(pause);
_gameMovie.Pause(pause);
NotesManager.Pause(pause);
}
[HarmonyPatch]
private static class GetOriginal
{
[HarmonyPatch(typeof(GameProcess), "OnStart")]
[HarmonyPostfix]
public static void Postfix(object ___debugFeature)
{
_debugFeatureOriginal = ___debugFeature;
}
}
[HarmonyPatch]
private static class PolyFill
{
public static bool isPause;
public static double timer;
public static void DebugTimeSkip(int addMsec)
{
_gameMovie.Pause(pauseFlag: true);
NotesManager.Pause(true);
if (addMsec >= 0)
{
timer += addMsec;
}
else
{
timer = timer + addMsec >= 0.0 ? timer + addMsec : 0.0;
}
_gameMovie.SetSeekFrame(timer);
SoundManager.SeekMusic((int)timer);
for (int i = 0; i < _monitors.Length; i++)
{
_monitors[i].Seek((int)timer);
}
// magic number, dont know why
NotesManager.StartPlay((int)timer + 91);
NotesManager.Pause(isPause);
if (!isPause)
{
SoundManager.PauseMusic(pause: false);
_gameMovie.Pause(pauseFlag: false);
}
else
{
_gameMovie.Pause(pauseFlag: true);
}
_gameProcess.UpdateNotes();
}
[HarmonyPatch(typeof(GameProcess), "OnUpdate")]
[HarmonyPostfix]
public static void Postfix(byte ____sequence)
{ {
if (____sequence != 4) return; if (____sequence != 4) return;
// GameSequence.Play // GameSequence.Play
@ -48,7 +132,7 @@ public class DebugFeature
{ {
isPause = !isPause; isPause = !isPause;
SoundManager.PauseMusic(isPause); SoundManager.PauseMusic(isPause);
____gameMovie.Pause(isPause); _gameMovie.Pause(isPause);
NotesManager.Pause(isPause); NotesManager.Pause(isPause);
} }
else if (DebugInput.GetKeyDown(KeyCode.LeftArrow) || DebugInput.GetKeyDown(KeyCode.RightArrow)) else if (DebugInput.GetKeyDown(KeyCode.LeftArrow) || DebugInput.GetKeyDown(KeyCode.RightArrow))
@ -68,44 +152,6 @@ public class DebugFeature
Singleton<GamePlayManager>.Instance.Initialize(); Singleton<GamePlayManager>.Instance.Initialize();
DebugTimeSkip(addMsec); DebugTimeSkip(addMsec);
} }
return;
void DebugTimeSkip(int addMsec)
{
____gameMovie.Pause(pauseFlag: true);
NotesManager.Pause(true);
if (addMsec >= 0)
{
timer += addMsec;
}
else
{
timer = timer + addMsec >= 0.0 ? timer + addMsec : 0.0;
}
____gameMovie.SetSeekFrame(timer);
SoundManager.SeekMusic((int)timer);
for (int i = 0; i < ____monitors.Length; i++)
{
____monitors[i].Seek((int)timer);
}
// magic number, dont know why
NotesManager.StartPlay((int)timer + 91);
NotesManager.Pause(isPause);
if (!isPause)
{
SoundManager.PauseMusic(pause: false);
____gameMovie.Pause(pauseFlag: false);
}
else
{
____gameMovie.Pause(pauseFlag: true);
}
__instance.UpdateNotes();
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using AquaMai.Fix; using AquaMai.Fix;
using AquaMai.Helpers; using AquaMai.Helpers;
@ -35,6 +36,9 @@ namespace AquaMai
{ {
Patch(nested); Patch(nested);
} }
var customMethod = type.GetMethod("DoCustomPatch", BindingFlags.Public | BindingFlags.Static);
customMethod?.Invoke(null, [HarmonyInstance]);
} }
catch (Exception e) catch (Exception e)
{ {