Skip to content

Commit f670577

Browse files
committed
feat: EarlyContinue
1 parent 3cdc4b7 commit f670577

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Collections.Generic;
2+
using System.Reflection;
3+
using System.Reflection.Emit;
4+
using AquaMai.Config.Attributes;
5+
using HarmonyLib;
6+
using Manager;
7+
using GameProcess = global::Process;
8+
9+
namespace AquaMai.Mods.Utils.EarlyContinue;
10+
11+
[ConfigCollapseNamespace]
12+
[ConfigSection(
13+
name: "更早的续关",
14+
zh: "在最后一首歌结束的时候显示续关界面,确认续关后立即增加指定 Track 数")]
15+
public class EarlyContinue
16+
{
17+
[ConfigEntry(name: "增加的曲目数", zh: "设为 0 则为 1P 3首,2P 4首", en: "Number of tracks to add. Set to 0 to add 3 tracks for 1P and 3 tracks for 2P.")]
18+
public static readonly uint addTrackCount = 0;
19+
20+
// ResultProcess.ToNextProcess() 里把 new MapResultProcess(container) 换成自己的 Process,
21+
// 并强制走 FadeProcess 分支
22+
[HarmonyTranspiler]
23+
[HarmonyPatch(typeof(GameProcess.ResultProcess), "ToNextProcess")]
24+
public static IEnumerable<CodeInstruction> ToNextProcessTranspiler(IEnumerable<CodeInstruction> instructions)
25+
{
26+
var original = AccessTools.Constructor(typeof(GameProcess.MapResultProcess), new[] { typeof(GameProcess.ProcessDataContainer) });
27+
var replacement = AccessTools.Constructor(typeof(Process), new[] { typeof(GameProcess.ProcessDataContainer) });
28+
29+
var codes = new List<CodeInstruction>(instructions);
30+
31+
// 替换所有 new MapResultProcess(...) 为自己的 Process,并记录第一个出现位置
32+
var firstNewObj = -1;
33+
for (var i = 0; i < codes.Count; i++)
34+
{
35+
if (codes[i].opcode == OpCodes.Newobj && codes[i].operand as ConstructorInfo == original)
36+
{
37+
codes[i].operand = replacement;
38+
if (firstNewObj < 0) firstNewObj = i;
39+
}
40+
}
41+
42+
// flag8==true 的分支体里有第一个 newobj,往前找守卫它的条件跳转,
43+
// 把跳转前加载 flag8 的指令改成 ldc.i4.0:无论 brtrue/brfalse 都会落到 else(FadeProcess)
44+
for (var i = firstNewObj - 1; i >= 1; i--)
45+
{
46+
if (codes[i].opcode == OpCodes.Brfalse || codes[i].opcode == OpCodes.Brfalse_S ||
47+
codes[i].opcode == OpCodes.Brtrue || codes[i].opcode == OpCodes.Brtrue_S)
48+
{
49+
// 原地改 opcode/operand,保留指令上的 labels 和 blocks
50+
codes[i - 1].opcode = OpCodes.Ldc_I4_0;
51+
codes[i - 1].operand = null;
52+
break;
53+
}
54+
}
55+
56+
return codes;
57+
}
58+
59+
public static uint currentAddTrackCount = 0;
60+
61+
[HarmonyPostfix]
62+
[HarmonyPatch(typeof(GameManager), nameof(GameManager.SetMaxTrack))]
63+
public static void SetMaxTrack()
64+
{
65+
GameManager.TempMaxTrackCount += currentAddTrackCount;
66+
}
67+
68+
[HarmonyPostfix]
69+
[HarmonyPatch(typeof(GameManager), nameof(GameManager.Clear))]
70+
public static void GameManagerClear()
71+
{
72+
currentAddTrackCount = 0;
73+
}
74+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using HarmonyLib;
2+
using MAI2.Util;
3+
using Manager;
4+
using Process;
5+
using UnityEngine;
6+
7+
namespace AquaMai.Mods.Utils.EarlyContinue;
8+
9+
public class Process : ContinueProcess
10+
{
11+
private readonly Traverse<ContinueSequence> state;
12+
public Process(ProcessDataContainer dataContainer) : base(dataContainer)
13+
{
14+
state = Traverse.Create(this).Field<ContinueSequence>("_state");
15+
}
16+
17+
private float _timeCounter;
18+
public override void OnUpdate()
19+
{
20+
if (state.Value != ContinueSequence.DispEnd)
21+
{
22+
base.OnUpdate();
23+
return;
24+
}
25+
26+
_timeCounter += Time.deltaTime;
27+
if (_timeCounter < 3f)
28+
{
29+
return;
30+
}
31+
_timeCounter = 0f;
32+
if (GameManager.IsSelectContinue[0] || GameManager.IsSelectContinue[1])
33+
{
34+
var addCount = EarlyContinue.addTrackCount;
35+
if (addCount == 0)
36+
{
37+
addCount = (uint)(Singleton<UserDataManager>.Instance.IsSingleUser() ? 3 : 4);
38+
}
39+
EarlyContinue.currentAddTrackCount += addCount;
40+
GameManager.SetMaxTrack();
41+
container.processManager.AddProcess(new NextTrackProcess(container, this), 50);
42+
}
43+
else
44+
{
45+
bool notShowFade = true;
46+
for (int j = 0; j < 2; j++)
47+
{
48+
UserData userData = Singleton<UserDataManager>.Instance.GetUserData(j);
49+
if (userData.MapList.Count != 0 && userData.IsEntry && !userData.IsGuest())
50+
{
51+
notShowFade = false;
52+
break;
53+
}
54+
}
55+
56+
if (notShowFade)
57+
{
58+
container.processManager.AddProcess(new MapResultProcess(container), 50);
59+
container.processManager.ReleaseProcess(this);
60+
}
61+
else
62+
{
63+
container.processManager.AddProcess(new FadeProcess(container, this, new MapResultProcess(container)), 50);
64+
}
65+
}
66+
container.processManager.SetVisibleTimers(isVisible: false);
67+
}
68+
}

0 commit comments

Comments
 (0)