[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.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using MAI2.Util;
@ -11,70 +10,76 @@ using UnityEngine;
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));
}
else
{
MelonLogger.Msg(" > [DebugFeature] Already included");
h.PatchAll(typeof(GetOriginal));
}
}
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);
}
if (Input.GetKeyDown(KeyCode.Home))
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)
{
GameManager.AutoPlay = (GameManager.AutoPlayMode)((int)(GameManager.AutoPlay + 1) % Enum.GetNames(typeof(GameManager.AutoPlayMode)).Length);
_debugFeatureOriginal = ___debugFeature;
}
else if (Input.GetKeyDown(KeyCode.Return))
}
[HarmonyPatch]
private static class PolyFill
{
public static bool isPause;
public static double timer;
public static void DebugTimeSkip(int addMsec)
{
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);
}
return;
void DebugTimeSkip(int addMsec)
{
____gameMovie.Pause(pauseFlag: true);
_gameMovie.Pause(pauseFlag: true);
NotesManager.Pause(true);
if (addMsec >= 0)
{
@ -85,11 +90,11 @@ public class DebugFeature
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
@ -98,14 +103,55 @@ public class DebugFeature
if (!isPause)
{
SoundManager.PauseMusic(pause: false);
____gameMovie.Pause(pauseFlag: false);
_gameMovie.Pause(pauseFlag: false);
}
else
{
____gameMovie.Pause(pauseFlag: true);
_gameMovie.Pause(pauseFlag: true);
}
__instance.UpdateNotes();
_gameProcess.UpdateNotes();
}
[HarmonyPatch(typeof(GameProcess), "OnUpdate")]
[HarmonyPostfix]
public static void Postfix(byte ____sequence)
{
if (____sequence != 4) return;
// GameSequence.Play
if (!isPause)
{
timer += GameManager.GetGameMSecAddD();
}
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);
}
}
}
}

View File

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