Skip to content

Commit

Permalink
[+] Mark supported game versions with attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Oct 27, 2024
1 parent 6bb2685 commit ff2ed50
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/aquamai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions AquaMai/Attributes/GameVersionAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 4 additions & 2 deletions AquaMai/Cheat/UnlockUtage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using HarmonyLib;
using AquaMai.Attributes;
using HarmonyLib;
using MAI2System;
using Manager;

namespace AquaMai.Cheat
{
[GameVersion(24000)]
public class UnlockUtage
{
[HarmonyPrefix]
Expand All @@ -15,4 +17,4 @@ public static bool CanUnlockUtageTotalJudgement(out ConstParameter.ResultOfUnloc
return false;
}
}
}
}
2 changes: 2 additions & 0 deletions AquaMai/Fix/ExtendNotesPool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using AquaMai.Attributes;
using HarmonyLib;
using MAI2.Util;
using Manager;
Expand All @@ -9,6 +10,7 @@

namespace AquaMai.Fix;

[GameVersion(23000)]
public class ExtendNotesPool
{
[HarmonyPostfix]
Expand Down
10 changes: 6 additions & 4 deletions AquaMai/Fix/FixConnSlide.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System.Collections.Generic;
using System.Reflection;
using AquaMai.Attributes;
using HarmonyLib;
using Manager;
using MelonLoader;
using Monitor;

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;
Expand All @@ -31,7 +33,7 @@ public class FixConnSlide
* }
* }
* }
*
*
* 修复 bug 需要把第二次调用 this.IsSlideAll() 更改为 this.IsConnectNote(), 这里使用 Transpiler 解决
*/
[HarmonyTranspiler]
Expand All @@ -42,7 +44,7 @@ private static IEnumerable<CodeInstruction> Fix(IEnumerable<CodeInstruction> 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];
Expand All @@ -61,4 +63,4 @@ private static IEnumerable<CodeInstruction> Fix(IEnumerable<CodeInstruction> ins
}
return instList;
}
}
}
4 changes: 3 additions & 1 deletion AquaMai/Fix/FixLevelDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using AquaMai.Attributes;
using HarmonyLib;
using MAI2.Util;
using Manager;
using Monitor;
Expand All @@ -7,6 +8,7 @@

namespace AquaMai.Fix;

[GameVersion(24000)]
public class FixLevelDisplay
{
[HarmonyPostfix]
Expand Down
32 changes: 24 additions & 8 deletions AquaMai/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<GameVersionAttribute>();
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);
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 3 additions & 7 deletions AquaMai/UX/QuickSkip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using AquaMai.Attributes;
using AquaMai.Helpers;
using HarmonyLib;
using Mai2.Mai2Cue;
Expand Down Expand Up @@ -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")]
Expand Down
2 changes: 2 additions & 0 deletions AquaMai/Utils/SelectionDetail.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AquaMai.Attributes;
using AquaMai.Helpers;
using AquaMai.Resources;
using HarmonyLib;
Expand All @@ -13,6 +14,7 @@

namespace AquaMai.Utils;

[GameVersion(23500)]
public class SelectionDetail
{
private static readonly Window[] window = new Window[2];
Expand Down

0 comments on commit ff2ed50

Please sign in to comment.