|
| 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 | +} |
0 commit comments