Skip to content

Commit 4f6048c

Browse files
committed
[+] MaimollerIO 按键灯光触摸单独设置
1 parent aa329e1 commit 4f6048c

File tree

2 files changed

+163
-62
lines changed

2 files changed

+163
-62
lines changed

AquaMai.Config/Migration/ConfigMigration_V2_3_V2_4.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ public ConfigView Migrate(ConfigView src)
2727
dst.Remove("GameSystem.SkipBoardNoCheck");
2828
}
2929

30+
if (src.TryGetValue<bool>("GameSystem.MaimollerIO.P1", out var mml1p))
31+
{
32+
dst.SetValue("GameSystem.MaimollerIO.Touch1p", mml1p);
33+
dst.SetValue("GameSystem.MaimollerIO.Button1p", mml1p);
34+
dst.SetValue("GameSystem.MaimollerIO.Led1p", mml1p);
35+
dst.Remove("GameSystem.MaimollerIO.P1");
36+
}
37+
38+
if (src.TryGetValue<bool>("GameSystem.MaimollerIO.P2", out var mml2p))
39+
{
40+
dst.SetValue("GameSystem.MaimollerIO.Touch2p", mml2p);
41+
dst.SetValue("GameSystem.MaimollerIO.Button2p", mml2p);
42+
dst.SetValue("GameSystem.MaimollerIO.Led2p", mml2p);
43+
dst.Remove("GameSystem.MaimollerIO.P2");
44+
}
45+
3046
return dst;
3147
}
3248
}

AquaMai.Mods/GameSystem/MaimollerIO/MaimollerIO.cs

Lines changed: 147 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
using System.Reflection;
44
using AquaMai.Config.Attributes;
55
using AquaMai.Config.Types;
6+
using AquaMai.Core.Attributes;
67
using AquaMai.Core.Helpers;
78
using AquaMai.Mods.GameSystem.MaimollerIO.Libs;
89
using HarmonyLib;
9-
using IO;
1010
using Main;
11-
using Manager;
1211
using Mecha;
13-
using MelonLoader;
1412
using UnityEngine;
1513

1614
namespace AquaMai.Mods.GameSystem.MaimollerIO;
@@ -33,16 +31,65 @@ 请删除 ADXHIDIOMod.dll(如果有)并关闭 mai2.ini 中的 DummyTouchpane
3331
public class MaimollerIO
3432
{
3533
[ConfigEntry(
36-
name: "启用 1P",
37-
en: "Enable 1P (If you mix Maimoller with other protocols, please disable for the side that is not Maimoller)",
38-
zh: "启用 1P(如果混用 Maimoller 与其他协议,请对不是 Maimoller 的一侧禁用")]
39-
private static readonly bool p1 = true;
34+
name: "启用 1P 触屏",
35+
en: "Enable 1P TouchScreen (If you mix Maimoller with other protocols, please disable for the side that is not Maimoller)",
36+
zh: "如果混用 Maimoller 与其他协议,请对不是 Maimoller 的一侧禁用")]
37+
private static readonly bool touch1p = true;
4038

4139
[ConfigEntry(
42-
name: "启用 2P",
43-
en: "Enable 2P (If you mix Maimoller with other protocols, please disable for the side that is not Maimoller)",
44-
zh: "启用 2P(如果混用 Maimoller 与其他协议,请对不是 Maimoller 的一侧禁用)")]
45-
private static readonly bool p2 = true;
40+
name: "启用 1P 按键",
41+
en: "Enable 1P Buttons")]
42+
private static readonly bool button1p = true;
43+
44+
[ConfigEntry(
45+
name: "启用 1P 灯光",
46+
en: "Enable 1P LEDs")]
47+
private static readonly bool led1p = true;
48+
49+
[ConfigEntry(
50+
name: "启用 2P 触屏",
51+
en: "Enable 2P")]
52+
private static readonly bool touch2p = true;
53+
54+
[ConfigEntry(
55+
name: "启用 2P 按键",
56+
en: "Enable 2P Buttons")]
57+
private static readonly bool button2p = true;
58+
59+
[ConfigEntry(
60+
name: "启用 2P 灯光",
61+
en: "Enable 2P LEDs")]
62+
private static readonly bool led2p = true;
63+
64+
private static bool ShouldInitForPlayer(int playerNo) => playerNo switch
65+
{
66+
0 => touch1p || button1p || led1p,
67+
1 => touch2p || button2p || led2p,
68+
_ => false,
69+
};
70+
71+
private static bool IsTouchEnabledForPlayer(int playerNo) => playerNo switch
72+
{
73+
0 => touch1p,
74+
1 => touch2p,
75+
_ => false,
76+
};
77+
78+
private static bool IsButtonEnabledForPlayer(int playerNo) => playerNo switch
79+
{
80+
0 => button1p,
81+
1 => button2p,
82+
_ => false,
83+
};
84+
85+
private static bool IsLedEnabledForPlayer(int playerNo) => playerNo switch
86+
{
87+
0 => led1p,
88+
1 => led2p,
89+
_ => false,
90+
};
91+
92+
private static bool IsAnyLedEnabled => led1p || led2p;
4693

4794
[ConfigEntry(name: "按钮 1(三角形)")]
4895
private static readonly IOKeyMap button1 = IOKeyMap.Select1P;
@@ -64,77 +111,83 @@ public class MaimollerIO
64111
/* button4 */ MaimollerInputReport.ButtonMask.COIN,
65112
];
66113

67-
private static bool ShouldEnableForPlayer(int playerNo) => playerNo switch
68-
{
69-
0 => p1,
70-
1 => p2,
71-
_ => false,
72-
};
73-
74114
private static readonly MaimollerDevice[] _devices = [.. Enumerable.Range(0, 2).Select(i => new MaimollerDevice(i))];
75115
private static readonly MaimollerLedManager[] _ledManagers = [.. Enumerable.Range(0, 2).Select(i => new MaimollerLedManager(_devices[i].output))];
76116

77117
public static void OnBeforePatch()
78118
{
79-
if (p1)
119+
for (int i = 0; i < 2; i++)
80120
{
81-
_devices[0].Open();
82-
TouchStatusProvider.RegisterTouchStatusProvider(0, GetTouchState);
121+
if (!ShouldInitForPlayer(i)) continue;
122+
_devices[i].Open();
123+
124+
if (IsTouchEnabledForPlayer(i))
125+
{
126+
TouchStatusProvider.RegisterTouchStatusProvider(i, GetTouchState);
127+
}
83128
}
84-
if (p2)
129+
130+
if (button1p || button2p)
85131
{
86-
_devices[1].Open();
87-
TouchStatusProvider.RegisterTouchStatusProvider(1, GetTouchState);
132+
JvsSwitchHook.RegisterButtonChecker(IsButtonPushed);
133+
JvsSwitchHook.RegisterAuxiliaryStateProvider(GetAuxiliaryState);
88134
}
89-
JvsSwitchHook.RegisterButtonChecker(IsButtonPushed);
90-
JvsSwitchHook.RegisterAuxiliaryStateProvider(GetAuxiliaryState);
91135
}
92136

93-
private static bool IsButtonPushed(int playerNo, int buttonIndex1To8) => buttonIndex1To8 switch
137+
#region Button
138+
139+
private static bool IsButtonPushed(int playerNo, int buttonIndex1To8)
94140
{
95-
1 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_1) != 0,
96-
2 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_2) != 0,
97-
3 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_3) != 0,
98-
4 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_4) != 0,
99-
5 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_5) != 0,
100-
6 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_6) != 0,
101-
7 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_7) != 0,
102-
8 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_8) != 0,
103-
_ => false,
104-
};
141+
if (!IsButtonEnabledForPlayer(playerNo)) return false;
142+
return buttonIndex1To8 switch
143+
{
144+
1 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_1) != 0,
145+
2 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_2) != 0,
146+
3 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_3) != 0,
147+
4 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_4) != 0,
148+
5 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_5) != 0,
149+
6 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_6) != 0,
150+
7 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_7) != 0,
151+
8 => _devices[playerNo].input.GetSwitchState(MaimollerInputReport.SwitchClass.BUTTON, MaimollerInputReport.ButtonMask.BTN_8) != 0,
152+
_ => false,
153+
};
154+
}
105155

106156
// NOTE: Coin button is not supported yet. AquaMai recommands setting fixed number of credits directly in the configuration.
157+
107158
private static AuxiliaryState GetAuxiliaryState()
108159
{
109160
var auxiliaryState = new AuxiliaryState();
110161
IOKeyMap[] keyMaps = [button1, button2, button3, button4];
111162
for (int i = 0; i < 4; i++)
112163
{
113-
var is1PPushed = p1 && _devices[0].input.GetSwitchState(MaimollerInputReport.SwitchClass.SYSTEM, auxiliaryMaskMap[i]) != 0;
114-
var is2PPushed = p2 && _devices[1].input.GetSwitchState(MaimollerInputReport.SwitchClass.SYSTEM, auxiliaryMaskMap[i]) != 0;
164+
var is1PPushed = button1p && _devices[0].input.GetSwitchState(MaimollerInputReport.SwitchClass.SYSTEM, auxiliaryMaskMap[i]) != 0;
165+
var is2PPushed = button2p && _devices[1].input.GetSwitchState(MaimollerInputReport.SwitchClass.SYSTEM, auxiliaryMaskMap[i]) != 0;
115166
switch (keyMaps[i])
116167
{
117-
case IOKeyMap.Select1P:
118-
auxiliaryState.select1P |= is1PPushed || is2PPushed;
119-
break;
120-
case IOKeyMap.Select2P:
121-
auxiliaryState.select2P |= is1PPushed || is2PPushed;
122-
break;
123-
case IOKeyMap.Select:
124-
auxiliaryState.select1P |= is1PPushed;
125-
auxiliaryState.select2P |= is2PPushed;
126-
break;
127-
case IOKeyMap.Service:
128-
auxiliaryState.service = is1PPushed || is2PPushed;
129-
break;
130-
case IOKeyMap.Test:
131-
auxiliaryState.test = is1PPushed || is2PPushed;
132-
break;
168+
case IOKeyMap.Select1P:
169+
auxiliaryState.select1P |= is1PPushed || is2PPushed;
170+
break;
171+
case IOKeyMap.Select2P:
172+
auxiliaryState.select2P |= is1PPushed || is2PPushed;
173+
break;
174+
case IOKeyMap.Select:
175+
auxiliaryState.select1P |= is1PPushed;
176+
auxiliaryState.select2P |= is2PPushed;
177+
break;
178+
case IOKeyMap.Service:
179+
auxiliaryState.service = is1PPushed || is2PPushed;
180+
break;
181+
case IOKeyMap.Test:
182+
auxiliaryState.test = is1PPushed || is2PPushed;
183+
break;
133184
}
134185
}
135186
return auxiliaryState;
136187
}
137188

189+
#endregion
190+
138191
private static ulong GetTouchState(int i)
139192
{
140193
ulong s = 0;
@@ -148,62 +201,94 @@ private static ulong GetTouchState(int i)
148201

149202
[HarmonyPrefix]
150203
[HarmonyPatch(typeof(GameMain), "Update")]
204+
[EnableIf(nameof(IsAnyLedEnabled))]
151205
public static void PreGameMainUpdate(bool ____isInitialize)
152206
{
153207
if (!____isInitialize) return;
154208
for (int i = 0; i < 2; i++)
155209
{
156-
if (!ShouldEnableForPlayer(i)) continue;
210+
if (!ShouldInitForPlayer(i)) continue;
157211
_devices[i].Update();
158212
}
159213
}
160214

161215
[HarmonyPatch]
216+
[EnableIf(typeof(MaimollerIO), nameof(IsAnyLedEnabled))]
162217
public static class JvsOutputPwmPatch
163218
{
164219
public static MethodInfo TargetMethod() => typeof(IO.Jvs).GetNestedType("JvsOutputPwm", BindingFlags.NonPublic | BindingFlags.Public).GetMethod("Set");
165220

166-
public static void Prefix(byte index, Color32 color) => _ledManagers[index].SetBillboardColor(color);
221+
public static void Prefix(byte index, Color32 color)
222+
{
223+
if (!IsLedEnabledForPlayer(index)) return;
224+
_ledManagers[index].SetBillboardColor(color);
225+
}
167226
}
168227

169228
[HarmonyPostfix]
170229
[HarmonyPatch(typeof(Bd15070_4IF), "PreExecute")]
171-
public static void PostPreExecute(Bd15070_4IF.InitParam ____initParam) =>
230+
[EnableIf(nameof(IsAnyLedEnabled))]
231+
public static void PostPreExecute(Bd15070_4IF.InitParam ____initParam)
232+
{
233+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
172234
_ledManagers[____initParam.index].PreExecute();
235+
}
236+
173237

174238
[HarmonyPrefix]
175239
[HarmonyPatch(typeof(Bd15070_4IF), "_setColor")]
176-
public static void Pre_setColor(byte ledPos, Color32 color, Bd15070_4IF.InitParam ____initParam) =>
240+
[EnableIf(nameof(IsAnyLedEnabled))]
241+
public static void Pre_setColor(byte ledPos, Color32 color, Bd15070_4IF.InitParam ____initParam)
242+
{
243+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
177244
_ledManagers[____initParam.index].SetButtonColor(ledPos, color);
245+
}
178246

179247
[HarmonyPrefix]
180248
[HarmonyPatch(typeof(Bd15070_4IF), "_setColorMulti")]
181-
public static void Pre_setColorMulti(Color32 color, byte speed, Bd15070_4IF.InitParam ____initParam) =>
249+
[EnableIf(nameof(IsAnyLedEnabled))]
250+
public static void Pre_setColorMulti(Color32 color, byte speed, Bd15070_4IF.InitParam ____initParam)
251+
{
252+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
182253
_ledManagers[____initParam.index].SetButtonColor(-1, color);
254+
}
183255

184256
[HarmonyPrefix]
185257
[HarmonyPatch(typeof(Bd15070_4IF), "_setColorMultiFade")]
186-
public static void Pre_setColorMultiFade(Color32 color, byte speed, Bd15070_4IF.InitParam ____initParam) =>
258+
[EnableIf(nameof(IsAnyLedEnabled))]
259+
public static void Pre_setColorMultiFade(Color32 color, byte speed, Bd15070_4IF.InitParam ____initParam)
260+
{
261+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
187262
_ledManagers[____initParam.index].SetButtonColorFade(-1, color, GetByte2Msec(speed));
263+
}
188264

189265
[HarmonyPrefix]
190266
[HarmonyPatch(typeof(Bd15070_4IF), "_setColorMultiFet")]
267+
[EnableIf(nameof(IsAnyLedEnabled))]
191268
public static void Pre_setColorMultiFet(Color32 color, Bd15070_4IF.InitParam ____initParam)
192269
{
270+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
193271
_ledManagers[____initParam.index].SetBodyIntensity(8, color.r);
194272
_ledManagers[____initParam.index].SetBodyIntensity(9, color.g);
195273
_ledManagers[____initParam.index].SetBodyIntensity(10, color.b);
196274
}
197275

198276
[HarmonyPrefix]
199277
[HarmonyPatch(typeof(Bd15070_4IF), "_setColorFet")]
200-
public static void Pre_setColorFet(byte ledPos, byte color, Bd15070_4IF.InitParam ____initParam) =>
278+
[EnableIf(nameof(IsAnyLedEnabled))]
279+
public static void Pre_setColorFet(byte ledPos, byte color, Bd15070_4IF.InitParam ____initParam)
280+
{
281+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
201282
_ledManagers[____initParam.index].SetBodyIntensity(ledPos, color);
283+
}
284+
202285

203286
[HarmonyPrefix]
204287
[HarmonyPatch(typeof(Bd15070_4IF), "_setLedAllOff")]
288+
[EnableIf(nameof(IsAnyLedEnabled))]
205289
public static void Pre_setLedAllOff(Bd15070_4IF.InitParam ____initParam)
206290
{
291+
if (!IsLedEnabledForPlayer(____initParam.index)) return;
207292
_ledManagers[____initParam.index].SetBodyIntensity(-1, 0);
208293
_ledManagers[____initParam.index].SetButtonColor(-1, new Color32(0, 0, 0, byte.MaxValue));
209294
}

0 commit comments

Comments
 (0)