Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[+] GameSystem.KeyMap.{DisableIO4, DisableDebugInput} #19

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions AquaMai.Mods/Fix/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using AquaMai.Config.Attributes;
using AquaMai.Core.Attributes;
using Process;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection;
using AquaMai.Mods.GameSystem;

namespace AquaMai.Mods.Fix;

Expand All @@ -29,7 +29,9 @@ private static bool PreIniFileClear()

[ConfigEntry] private readonly static bool fixDebugInput = true;

[EnableIf(nameof(fixDebugInput))]
private static bool FixDebugKeyboardInput => fixDebugInput && !KeyMap.disableDebugInput;

[EnableIf(nameof(FixDebugKeyboardInput))]
[HarmonyPrefix]
[HarmonyPatch(typeof(DebugInput), "GetKey")]
private static bool GetKey(ref bool __result, KeyCode name)
Expand All @@ -38,7 +40,7 @@ private static bool GetKey(ref bool __result, KeyCode name)
return false;
}

[EnableIf(nameof(fixDebugInput))]
[EnableIf(nameof(FixDebugKeyboardInput))]
[HarmonyPrefix]
[HarmonyPatch(typeof(DebugInput), "GetKeyDown")]
private static bool GetKeyDown(ref bool __result, KeyCode name)
Expand Down Expand Up @@ -168,4 +170,4 @@ inst.operand is FieldInfo field &&
// Remove all instructions before the target instruction.
return instList.Skip(onceDispIndex);
}
}
}
66 changes: 64 additions & 2 deletions AquaMai.Mods/GameSystem/KeyMap.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,77 @@
using System.Reflection;
using AMDaemon;
using AquaMai.Config.Attributes;
using AquaMai.Config.Types;
using AquaMai.Core.Attributes;
using HarmonyLib;
using UnityEngine;

namespace AquaMai.Mods.GameSystem;

[ConfigSection(
en: "These settings will work regardless of whether you have enabled segatools' io4 emulation.",
zh: "这里的设置无论你是否启用了 segatools 的 io4 模拟都会工作")]
en: """
Enable or disable IO4 and DebugInput. Configure the key mapping for DebugInput.
DebugInput works independently of IO4 (IO4-compatible board / segatools IO4 emulation).
(You should enable at least one input source, unless you use other input solutions like AdxHidInput.)
""",
zh: """
启用或禁用 IO4 和 DebugInput。配置 DebugInput 的按键映射。
DebugInput 与 IO4(兼容 IO4 板 / segatools IO4 模拟)独立工作。
(你应该至少启用一个输入源,除非你使用其他输入方案如 AdxHidInput。)
""",
defaultOn: true)]
public class KeyMap
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题(复杂性): 考虑使用枚举而不是两个布尔标志来表示输入模式,这可以简化逻辑并防止无效状态。

考虑将两个布尔标志替换为枚举,以明确有效的输入配置并降低复杂性:

public enum InputMode 
{
    IO4Only,        // 传统 IO4 板/segatools
    DebugInputOnly, // 键盘/调试输入
    Both,           // 两者都启用(不建议用于生产)
    None            // 无输入(用于特殊情况)
}

[ConfigEntry(
    en: """
        选择输入模式。IO4Only 使用 IO4 兼容板/segatools。
        DebugInputOnly 使用下面定义的键盘映射。
        Both 不建议用于生产使用。
        """)]
public static InputMode inputMode = InputMode.IO4Only;

这允许合并 IO4 禁用补丁:

[EnableIf(nameof(inputMode), InputMode.DebugInputOnly, InputMode.None)]
[HarmonyPatch(typeof(SwitchInput))]
public class SwitchInputPatch 
{
    [HarmonyPatch("get_IsOn")]
    [HarmonyPrefix]
    public static bool PreGetIsOn(ref bool __result)
    {
        __result = false;
        return false;
    }
    // 其他方法类似合并
}

这种方法:

  1. 明确有效配置
  2. 防止模糊状态
  3. 降低补丁复杂性
  4. 保持所有当前功能
Original comment in English

issue (complexity): Consider using an enum instead of two boolean flags to represent input modes, which simplifies the logic and prevents invalid states.

Consider replacing the two boolean flags with an enum to make the valid input configurations explicit and reduce complexity:

public enum InputMode 
{
    IO4Only,        // Traditional IO4 board/segatools
    DebugInputOnly, // Keyboard/debug input only
    Both,           // Both enabled (not recommended for production)
    None            // No input (for special cases)
}

[ConfigEntry(
    en: """
        Select input mode. IO4Only uses IO4-compatible board/segatools.
        DebugInputOnly uses keyboard mapping defined below.
        Both is not recommended for production use.
        """)]
public static InputMode inputMode = InputMode.IO4Only;

This allows consolidating the IO4 disable patches:

[EnableIf(nameof(inputMode), InputMode.DebugInputOnly, InputMode.None)]
[HarmonyPatch(typeof(SwitchInput))]
public class SwitchInputPatch 
{
    [HarmonyPatch("get_IsOn")]
    [HarmonyPrefix]
    public static bool PreGetIsOn(ref bool __result)
    {
        __result = false;
        return false;
    }
    // Other methods consolidated similarly
}

This approach:

  1. Makes valid configurations explicit
  2. Prevents ambiguous states
  3. Reduces patch complexity
  4. Maintains all current functionality

{
[ConfigEntry(
en: """
Disable IO4 (IO4-compatible board / segatools IO4 emulation) input.
With IO4 input disabled, your IO4-compatible board or segatools IO4 emulation is ignored.
""",
zh: """
禁用 IO4(兼容 IO4 板 / segatools IO4 模拟)输入。
在禁用 IO4 输入后,你的兼容 IO4 板或 segatools IO4 模拟将被忽略。
""")]
private static readonly bool disableIO4 = false;

[ConfigEntry(
en: """
Disable DebugInput. The key mapping below will not work.
With DebugInput disabled, you'll need a IO4-compatible board, segatools IO4 emulation or other custom input solutions to play.
You may want to configure IO4 emulation key mapping in segatools.ini's [io4] and [button] section.
""",
zh: """
禁用 DebugInput,下列按键映射将不起作用。
在禁用 DebugInput 后,你需要兼容 IO4 板、segatools IO4 模拟或其他自定义输入方案才能游玩。
如果使用 IO4 模拟,你可以在 segatools.ini 的 [io4] 和 [button] 部分配置按键映射。
""")]
public static readonly bool disableDebugInput = false; // Implemented in AquaMai.Mods/Fix/Common.cs

[EnableIf(nameof(disableIO4))]
[HarmonyPatch("IO.Jvs+JvsSwitch", ".ctor", MethodType.Constructor, [typeof(int), typeof(string), typeof(KeyCode), typeof(bool), typeof(bool)])]
[HarmonyPrefix]
public static void PreJvsSwitchConstructor(ref bool invert)
{
invert = false;
}

[EnableIf(nameof(disableIO4))]
[HarmonyPatch(typeof(SwitchInput), "get_IsOn")]
[HarmonyPrefix]
public static bool PreGetIsOn(ref bool __result)
{
__result = false;
return false;
}

[EnableIf(nameof(disableIO4))]
[HarmonyPatch(typeof(SwitchInput), "get_HasOnNow")]
[HarmonyPrefix]
public static bool PreGetHasOnNow(ref bool __result)
{
__result = false;
return false;
}

[ConfigEntry]
public static readonly KeyCodeID Test = (KeyCodeID)115;

Expand Down