|
| 1 | +using System; |
| 2 | +using AquaMai.Config.Attributes; |
| 3 | +using LibUsbDotNet.Main; |
| 4 | +using LibUsbDotNet; |
| 5 | +using MelonLoader; |
| 6 | +using UnityEngine; |
| 7 | +using AquaMai.Core.Helpers; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace AquaMai.Mods.GameSystem.ExclusiveTouch; |
| 11 | + |
| 12 | +[ConfigCollapseNamespace] |
| 13 | +[ConfigSection(exampleHidden: true)] |
| 14 | +public class ExclusiveTouch |
| 15 | +{ |
| 16 | + [ConfigEntry] |
| 17 | + public static readonly bool enable1p; |
| 18 | + |
| 19 | + [ConfigEntry] |
| 20 | + public static readonly int vid1p; |
| 21 | + [ConfigEntry] |
| 22 | + public static readonly int pid1p; |
| 23 | + [ConfigEntry] |
| 24 | + public static readonly string serialNumber1p = ""; |
| 25 | + [ConfigEntry] |
| 26 | + public static readonly byte configuration1p = 1; |
| 27 | + [ConfigEntry] |
| 28 | + public static readonly int interfaceNumber1p = 0; |
| 29 | + [ConfigEntry] |
| 30 | + public static readonly int reportId1p; |
| 31 | + [ConfigEntry] |
| 32 | + public static readonly ReadEndpointID endpoint1p = ReadEndpointID.Ep01; |
| 33 | + [ConfigEntry] |
| 34 | + public static readonly int packetSize1p = 64; |
| 35 | + [ConfigEntry] |
| 36 | + public static readonly int minX1p; |
| 37 | + [ConfigEntry] |
| 38 | + public static readonly int minY1p; |
| 39 | + [ConfigEntry] |
| 40 | + public static readonly int maxX1p; |
| 41 | + [ConfigEntry] |
| 42 | + public static readonly int maxY1p; |
| 43 | + [ConfigEntry] |
| 44 | + public static readonly bool flip1p; |
| 45 | + |
| 46 | + [ConfigEntry("触摸体积半径", zh: "基准是 1440x1440")] |
| 47 | + public static readonly int radius1p; |
| 48 | + |
| 49 | + private static UsbDevice[] devices = new UsbDevice[2]; |
| 50 | + private static TouchSensorMapper[] touchSensorMappers = new TouchSensorMapper[2]; |
| 51 | + |
| 52 | + private class TouchPoint |
| 53 | + { |
| 54 | + public ulong Mask; |
| 55 | + public DateTime LastUpdate; |
| 56 | + public bool IsActive; |
| 57 | + } |
| 58 | + |
| 59 | + // [玩家][手指ID] |
| 60 | + private static readonly TouchPoint[][] allFingerPoints = new TouchPoint[2][]; |
| 61 | + |
| 62 | + // 防吃键 |
| 63 | + private static readonly ulong[] frameAccumulators = new ulong[2]; |
| 64 | + private static readonly object[] touchLocks = [new object(), new object()]; |
| 65 | + |
| 66 | + private const int TouchTimeoutMs = 20; |
| 67 | + |
| 68 | + public static void OnBeforePatch() |
| 69 | + { |
| 70 | + if (enable1p) |
| 71 | + { |
| 72 | + // 方便组 2P |
| 73 | + var serialNumber = string.IsNullOrWhiteSpace(serialNumber1p) ? null : serialNumber1p; |
| 74 | + var finder = new UsbDeviceFinder(vid1p, pid1p, serialNumber); |
| 75 | + var device = UsbDevice.OpenUsbDevice(finder); |
| 76 | + if (device == null) |
| 77 | + { |
| 78 | + MelonLogger.Msg("[ExclusiveTouch] Cannot connect 1P"); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + IUsbDevice wholeDevice = device as IUsbDevice; |
| 83 | + if (wholeDevice != null) |
| 84 | + { |
| 85 | + wholeDevice.SetConfiguration(configuration1p); |
| 86 | + wholeDevice.ClaimInterface(interfaceNumber1p); |
| 87 | + } |
| 88 | + touchSensorMappers[0] = new TouchSensorMapper(minX1p, minY1p, maxX1p, maxY1p, radius1p, flip1p); |
| 89 | + Application.quitting += () => |
| 90 | + { |
| 91 | + devices[0] = null; |
| 92 | + if (wholeDevice != null) |
| 93 | + { |
| 94 | + wholeDevice.ReleaseInterface(interfaceNumber1p); |
| 95 | + } |
| 96 | + device.Close(); |
| 97 | + }; |
| 98 | + |
| 99 | + allFingerPoints[0] = new TouchPoint[256]; |
| 100 | + for (int i = 0; i < 256; i++) |
| 101 | + { |
| 102 | + allFingerPoints[0][i] = new TouchPoint(); |
| 103 | + } |
| 104 | + |
| 105 | + devices[0] = device; |
| 106 | + Thread readThread = new Thread(() => ReadThread(0)); |
| 107 | + readThread.Start(); |
| 108 | + TouchStatusProvider.RegisterTouchStatusProvider(0, GetTouchState); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static void ReadThread(int playerNo) |
| 114 | + { |
| 115 | + byte[] buffer = new byte[packetSize1p]; |
| 116 | + var reader = devices[playerNo].OpenEndpointReader(endpoint1p); |
| 117 | + while (devices[playerNo] != null) |
| 118 | + { |
| 119 | + int bytesRead; |
| 120 | + ErrorCode ec = reader.Read(buffer, 100, out bytesRead); // 100ms 超时 |
| 121 | + |
| 122 | + if (ec != ErrorCode.None) |
| 123 | + { |
| 124 | + if (ec == ErrorCode.IoTimedOut) continue; // 超时就继续等 |
| 125 | + MelonLogger.Msg($"[ExclusiveTouch] {playerNo + 1}P: 读取错误: {ec}"); |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + if (bytesRead > 0) |
| 130 | + { |
| 131 | + OnTouchData(playerNo, buffer); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static void OnTouchData(int playerNo, byte[] data) |
| 137 | + { |
| 138 | + byte reportId = data[0]; |
| 139 | + if (reportId != reportId1p) return; |
| 140 | + |
| 141 | +#if true // PDX |
| 142 | + for (int i = 0; i < 10; i++) |
| 143 | + { |
| 144 | + var index = i * 6 + 1; |
| 145 | + if (data[index] == 0) continue; |
| 146 | + bool isPressed = (data[index] & 0x01) == 1; |
| 147 | + var fingerId = data[index + 1]; |
| 148 | + ushort x = BitConverter.ToUInt16(data, index + 2); |
| 149 | + ushort y = BitConverter.ToUInt16(data, index + 4); |
| 150 | + HandleFinger(x, y, fingerId, isPressed, playerNo); |
| 151 | + } |
| 152 | +#else // 凌莞的便携屏 |
| 153 | + // 解析第一根手指 |
| 154 | + if (data.Length >= 7) |
| 155 | + { |
| 156 | + byte status1 = data[1]; |
| 157 | + int fingerId1 = (status1 >> 4) & 0x0F; // 高4位:手指ID |
| 158 | + bool isPressed1 = (status1 & 0x01) == 1; // 低位:按下状态 |
| 159 | + ushort x1 = BitConverter.ToUInt16(data, 2); |
| 160 | + ushort y1 = BitConverter.ToUInt16(data, 4); |
| 161 | + |
| 162 | + HandleFinger(x1, y1, fingerId1, isPressed1, playerNo); |
| 163 | + } |
| 164 | + |
| 165 | + // 解析第二根手指 |
| 166 | + if (data.Length >= 14) |
| 167 | + { |
| 168 | + byte status2 = data[6]; |
| 169 | + int fingerId2 = (status2 >> 4) & 0x0F; |
| 170 | + bool isPressed2 = (status2 & 0x01) == 1; |
| 171 | + ushort x2 = BitConverter.ToUInt16(data, 7); |
| 172 | + ushort y2 = BitConverter.ToUInt16(data, 9); |
| 173 | + |
| 174 | + // 只有坐标非零才处理第二根手指 |
| 175 | + if (x2 != 0 || y2 != 0) |
| 176 | + { |
| 177 | + HandleFinger(x2, y2, fingerId2, isPressed2, playerNo); |
| 178 | + } |
| 179 | + } |
| 180 | +#endif |
| 181 | + } |
| 182 | + |
| 183 | + private static void HandleFinger(ushort x, ushort y, int fingerId, bool isPressed, int playerNo) |
| 184 | + { |
| 185 | + // 安全检查,防止越界 |
| 186 | + if (fingerId < 0 || fingerId >= 256) return; |
| 187 | + |
| 188 | + lock (touchLocks[playerNo]) |
| 189 | + { |
| 190 | + var point = allFingerPoints[playerNo][fingerId]; |
| 191 | + |
| 192 | + if (isPressed) |
| 193 | + { |
| 194 | + ulong touchMask = touchSensorMappers[playerNo].ParseTouchPoint(x, y); |
| 195 | + |
| 196 | + if (!point.IsActive) |
| 197 | + { |
| 198 | + point.IsActive = true; |
| 199 | + MelonLogger.Msg($"[ExclusiveTouch] {playerNo + 1}P: 手指{fingerId} 按下 at ({x}, {y}) -> 0x{touchMask:X}"); |
| 200 | + } |
| 201 | + |
| 202 | + point.Mask = touchMask; |
| 203 | + point.LastUpdate = DateTime.Now; |
| 204 | + |
| 205 | + frameAccumulators[playerNo] |= touchMask; |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + if (point.IsActive) |
| 210 | + { |
| 211 | + point.IsActive = false; |
| 212 | + MelonLogger.Msg($"[ExclusiveTouch] {playerNo + 1}P: 手指{fingerId} 松开"); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public static ulong GetTouchState(int playerNo) |
| 219 | + { |
| 220 | + lock (touchLocks[playerNo]) |
| 221 | + { |
| 222 | + ulong currentTouchData = 0; |
| 223 | + var now = DateTime.Now; |
| 224 | + var points = allFingerPoints[playerNo]; |
| 225 | + |
| 226 | + for (int i = 0; i < points.Length; i++) |
| 227 | + { |
| 228 | + var point = points[i]; |
| 229 | + if (point.IsActive) |
| 230 | + { |
| 231 | + if ((now - point.LastUpdate).TotalMilliseconds > TouchTimeoutMs) |
| 232 | + { |
| 233 | + point.IsActive = false; |
| 234 | + MelonLogger.Msg($"[ExclusiveTouch] {playerNo + 1}P: 手指{i} 超时自动释放"); |
| 235 | + } |
| 236 | + else |
| 237 | + { |
| 238 | + currentTouchData |= point.Mask; |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + ulong finalResult = currentTouchData | frameAccumulators[playerNo]; |
| 244 | + frameAccumulators[playerNo] = 0; |
| 245 | + |
| 246 | + return finalResult; |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments