AquaDX/AquaMai/Fix/DebugFeature.cs

158 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Reflection;
using HarmonyLib;
2024-09-27 18:59:27 +08:00
using MAI2.Util;
using Manager;
using MelonLoader;
2024-09-27 18:59:27 +08:00
using Monitor;
using Process;
using UnityEngine;
namespace AquaMai.Fix;
public class DebugFeature
{
2024-09-28 15:31:58 +08:00
public static bool IsPolyfill { get; private set; }
private static GameProcess _gameProcess;
private static MovieController _gameMovie;
private static GameMonitor[] _monitors;
private static object _debugFeatureOriginal;
2024-09-28 15:31:58 +08:00
[HarmonyPatch(typeof(GameProcess), "OnStart")]
[HarmonyPostfix]
public static void Init(GameProcess __instance, MovieController ____gameMovie, GameMonitor[] ____monitors)
{
2024-09-28 15:31:58 +08:00
_gameProcess = __instance;
_gameMovie = ____gameMovie;
_monitors = ____monitors;
}
2024-09-28 15:31:58 +08:00
public static void DoCustomPatch(HarmonyLib.Harmony h)
{
2024-09-28 15:31:58 +08:00
var original = typeof(GameProcess).GetField("debugFeature", BindingFlags.NonPublic | BindingFlags.Instance);
if (original is null)
2024-09-27 18:59:27 +08:00
{
2024-09-28 15:31:58 +08:00
MelonLogger.Msg(" > [DebugFeature] Running Polyfill");
IsPolyfill = true;
h.PatchAll(typeof(PolyFill));
2024-09-27 18:59:27 +08:00
}
2024-09-28 15:31:58 +08:00
else
{
2024-09-28 15:31:58 +08:00
MelonLogger.Msg(" > [DebugFeature] Already included");
h.PatchAll(typeof(GetOriginal));
}
2024-09-28 15:31:58 +08:00
}
public static void SetPause(bool pause)
{
if (IsPolyfill)
{
2024-09-28 15:31:58 +08:00
PolyFill.isPause = pause;
}
2024-09-28 15:31:58 +08:00
else
2024-09-27 18:59:27 +08:00
{
2024-09-28 15:31:58 +08:00
var debugFeatureClass = typeof(GameProcess).GetNestedType("DebugFeature", BindingFlags.Instance | BindingFlags.NonPublic);
debugFeatureClass?.GetField("_debugPause", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(_debugFeatureOriginal, pause);
}
2024-09-28 15:31:58 +08:00
SoundManager.PauseMusic(pause);
_gameMovie.Pause(pause);
NotesManager.Pause(pause);
}
2024-09-28 15:31:58 +08:00
[HarmonyPatch]
private static class GetOriginal
{
[HarmonyPatch(typeof(GameProcess), "OnStart")]
[HarmonyPostfix]
public static void Postfix(object ___debugFeature)
{
_debugFeatureOriginal = ___debugFeature;
2024-09-27 18:59:27 +08:00
}
2024-09-28 15:31:58 +08:00
}
2024-09-27 18:59:27 +08:00
2024-09-28 15:31:58 +08:00
[HarmonyPatch]
private static class PolyFill
{
public static bool isPause;
public static double timer;
2024-09-27 18:59:27 +08:00
2024-09-28 15:31:58 +08:00
public static void DebugTimeSkip(int addMsec)
2024-09-27 18:59:27 +08:00
{
2024-09-28 15:31:58 +08:00
_gameMovie.Pause(pauseFlag: true);
2024-09-27 18:59:27 +08:00
NotesManager.Pause(true);
if (addMsec >= 0)
{
timer += addMsec;
}
else
{
timer = timer + addMsec >= 0.0 ? timer + addMsec : 0.0;
}
2024-09-28 15:31:58 +08:00
_gameMovie.SetSeekFrame(timer);
2024-09-27 18:59:27 +08:00
SoundManager.SeekMusic((int)timer);
2024-09-28 15:31:58 +08:00
for (int i = 0; i < _monitors.Length; i++)
2024-09-27 18:59:27 +08:00
{
2024-09-28 15:31:58 +08:00
_monitors[i].Seek((int)timer);
2024-09-27 18:59:27 +08:00
}
2024-09-27 18:59:27 +08:00
// magic number, dont know why
NotesManager.StartPlay((int)timer + 91);
NotesManager.Pause(isPause);
if (!isPause)
{
SoundManager.PauseMusic(pause: false);
2024-09-28 15:31:58 +08:00
_gameMovie.Pause(pauseFlag: false);
2024-09-27 18:59:27 +08:00
}
else
{
2024-09-28 15:31:58 +08:00
_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();
2024-09-27 18:59:27 +08:00
}
2024-09-28 15:31:58 +08:00
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);
}
2024-09-27 18:59:27 +08:00
}
}
}