From ff2ed50dea4ac6950814fdccde2c2ef8e109c660 Mon Sep 17 00:00:00 2001 From: Clansty Date: Sun, 27 Oct 2024 23:34:41 +0800 Subject: [PATCH] [+] Mark supported game versions with attributes --- .github/workflows/aquamai.yaml | 2 +- AquaMai/Attributes/GameVersionAttribute.cs | 9 ++++++ AquaMai/Cheat/UnlockUtage.cs | 6 ++-- AquaMai/Fix/ExtendNotesPool.cs | 2 ++ AquaMai/Fix/FixConnSlide.cs | 10 ++++--- AquaMai/Fix/FixLevelDisplay.cs | 4 ++- AquaMai/Main.cs | 32 ++++++++++++++++------ AquaMai/UX/QuickSkip.cs | 10 ++----- AquaMai/Utils/SelectionDetail.cs | 2 ++ 9 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 AquaMai/Attributes/GameVersionAttribute.cs diff --git a/.github/workflows/aquamai.yaml b/.github/workflows/aquamai.yaml index 43752300..6bafbc81 100644 --- a/.github/workflows/aquamai.yaml +++ b/.github/workflows/aquamai.yaml @@ -49,7 +49,7 @@ jobs: - uses: actions/upload-artifact@v4 with: name: AquaMai - path: AquaMai\Output\AquaMai.dll + path: AquaMai\Output\Upload - name: Send to Telegram if: github.event_name != 'pull_request' diff --git a/AquaMai/Attributes/GameVersionAttribute.cs b/AquaMai/Attributes/GameVersionAttribute.cs new file mode 100644 index 00000000..5c61b7f7 --- /dev/null +++ b/AquaMai/Attributes/GameVersionAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace AquaMai.Attributes; + +public class GameVersionAttribute(uint minVersion = 0, uint maxVersion = 0) : Attribute +{ + public uint MinVersion { get; } = minVersion; + public uint MaxVersion { get; } = maxVersion; +} diff --git a/AquaMai/Cheat/UnlockUtage.cs b/AquaMai/Cheat/UnlockUtage.cs index 7bf3ced7..7aa8ace4 100644 --- a/AquaMai/Cheat/UnlockUtage.cs +++ b/AquaMai/Cheat/UnlockUtage.cs @@ -1,9 +1,11 @@ -using HarmonyLib; +using AquaMai.Attributes; +using HarmonyLib; using MAI2System; using Manager; namespace AquaMai.Cheat { + [GameVersion(24000)] public class UnlockUtage { [HarmonyPrefix] @@ -15,4 +17,4 @@ public static bool CanUnlockUtageTotalJudgement(out ConstParameter.ResultOfUnloc return false; } } -} \ No newline at end of file +} diff --git a/AquaMai/Fix/ExtendNotesPool.cs b/AquaMai/Fix/ExtendNotesPool.cs index bd32cdb5..5a4edd07 100644 --- a/AquaMai/Fix/ExtendNotesPool.cs +++ b/AquaMai/Fix/ExtendNotesPool.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using AquaMai.Attributes; using HarmonyLib; using MAI2.Util; using Manager; @@ -9,6 +10,7 @@ namespace AquaMai.Fix; +[GameVersion(23000)] public class ExtendNotesPool { [HarmonyPostfix] diff --git a/AquaMai/Fix/FixConnSlide.cs b/AquaMai/Fix/FixConnSlide.cs index 138716ad..99a251f3 100644 --- a/AquaMai/Fix/FixConnSlide.cs +++ b/AquaMai/Fix/FixConnSlide.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Reflection; +using AquaMai.Attributes; using HarmonyLib; using Manager; using MelonLoader; @@ -7,12 +8,13 @@ namespace AquaMai.Fix; +[GameVersion(23000)] public class FixConnSlide { /* 这个 Patch 用于修复以下 bug: * 非 ConnSlide 被错误解析为 ConnSlide (Fes 首日刹那旅程爆机 bug) * 原 method 逻辑如下: - * + * * if (this.IsSlideAll(noteData1.type) && (index1 + 1 < this._note._noteData.Count ? 1 : 0) != 0) * { * int targetNote = noteData1.slideData.targetNote; @@ -31,7 +33,7 @@ public class FixConnSlide * } * } * } - * + * * 修复 bug 需要把第二次调用 this.IsSlideAll() 更改为 this.IsConnectNote(), 这里使用 Transpiler 解决 */ [HarmonyTranspiler] @@ -42,7 +44,7 @@ private static IEnumerable Fix(IEnumerable ins bool found = false; MethodInfo methodIsSlideAll = AccessTools.Method(typeof(NotesReader), "IsSlideAll"); MethodInfo methodIsConnectNote = AccessTools.Method(typeof(NotesReader), "IsConnectNote"); - + for (int i = 0; i < instList.Count; i++) { CodeInstruction inst = instList[i]; @@ -61,4 +63,4 @@ private static IEnumerable Fix(IEnumerable ins } return instList; } -} \ No newline at end of file +} diff --git a/AquaMai/Fix/FixLevelDisplay.cs b/AquaMai/Fix/FixLevelDisplay.cs index e04ac69f..95eeacb2 100644 --- a/AquaMai/Fix/FixLevelDisplay.cs +++ b/AquaMai/Fix/FixLevelDisplay.cs @@ -1,4 +1,5 @@ -using HarmonyLib; +using AquaMai.Attributes; +using HarmonyLib; using MAI2.Util; using Manager; using Monitor; @@ -7,6 +8,7 @@ namespace AquaMai.Fix; +[GameVersion(24000)] public class FixLevelDisplay { [HarmonyPostfix] diff --git a/AquaMai/Main.cs b/AquaMai/Main.cs index 39fc69ab..a64573b5 100644 --- a/AquaMai/Main.cs +++ b/AquaMai/Main.cs @@ -3,6 +3,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; +using AquaMai.Attributes; using AquaMai.Fix; using AquaMai.Helpers; using AquaMai.Resources; @@ -29,15 +30,33 @@ public class AquaMai : MelonMod public static Config AppConfig { get; private set; } private static bool _hasErrors; - private void Patch(Type type) + private void Patch(Type type, bool isNested = false) { + var versionAttr = type.GetCustomAttribute(); + var compatible = true; + if (versionAttr != null) + { + if (versionAttr.MinVersion > 0 && versionAttr.MinVersion > GameInfo.GameVersion) compatible = false; + if (versionAttr.MaxVersion > 0 && versionAttr.MaxVersion < GameInfo.GameVersion) compatible = false; + } + + if (!compatible) + { + if (!isNested) + { + MelonLogger.Warning($"> Skipping incompatible patch: {type}"); + } + + return; + } + MelonLogger.Msg($"> Patching {type}"); try { HarmonyInstance.PatchAll(type); foreach (var nested in type.GetNestedTypes()) { - Patch(nested); + Patch(nested, true); } var customMethod = type.GetMethod("DoCustomPatch", BindingFlags.Public | BindingFlags.Static); @@ -143,15 +162,12 @@ public override void OnInitializeMelon() Patch(typeof(FixCharaCrash)); Patch(typeof(BasicFix)); Patch(typeof(DisableReboot)); - if (GameInfo.GameVersion >= 23000) - Patch(typeof(ExtendNotesPool)); + Patch(typeof(ExtendNotesPool)); Patch(typeof(FixCheckAuth)); Patch(typeof(DebugFeature)); - if (GameInfo.GameVersion >= 23000) - Patch(typeof(FixConnSlide)); + Patch(typeof(FixConnSlide)); Patch(typeof(FixSlideAutoPlay)); // Rename: SlideAutoPlayTweak -> FixSlideAutoPlay, 不过这个应该无副作用所以不需要改配置文件 - if (GameInfo.GameVersion >= 24000) - Patch(typeof(FixLevelDisplay)); + Patch(typeof(FixLevelDisplay)); // UX Patch(typeof(CustomVersionString)); Patch(typeof(CustomPlaceName)); diff --git a/AquaMai/UX/QuickSkip.cs b/AquaMai/UX/QuickSkip.cs index 3bc1a8ea..5b5b985d 100644 --- a/AquaMai/UX/QuickSkip.cs +++ b/AquaMai/UX/QuickSkip.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using AquaMai.Attributes; using AquaMai.Helpers; using HarmonyLib; using Mai2.Mai2Cue; @@ -86,13 +87,8 @@ public static void PostGameProcessUpdate(GameProcess __instance, Message[] ____m } } - public static void DoCustomPatch(HarmonyLib.Harmony h) - { - if (GameInfo.GameVersion < 23000) return; - h.PatchAll(typeof(FestivalAndLaterQuickRetryPatch)); - } - - private class FestivalAndLaterQuickRetryPatch + [GameVersion(23000)] + public class FestivalAndLaterQuickRetryPatch { [HarmonyPrefix] [HarmonyPatch(typeof(QuickRetry), "IsQuickRetryEnable")] diff --git a/AquaMai/Utils/SelectionDetail.cs b/AquaMai/Utils/SelectionDetail.cs index e3ce2a64..7789cfa8 100644 --- a/AquaMai/Utils/SelectionDetail.cs +++ b/AquaMai/Utils/SelectionDetail.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using AquaMai.Attributes; using AquaMai.Helpers; using AquaMai.Resources; using HarmonyLib; @@ -13,6 +14,7 @@ namespace AquaMai.Utils; +[GameVersion(23500)] public class SelectionDetail { private static readonly Window[] window = new Window[2];