|
1 | 1 | using Nefarius.ViGEm.Client.Targets.Xbox360; |
| 2 | +using Nefarius.ViGEm.Client; // IXbox360Controller の定義元 |
2 | 3 | using System; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Globalization; |
@@ -168,6 +169,215 @@ await Task.Run(() => |
168 | 169 | }, token); |
169 | 170 | } |
170 | 171 |
|
| 172 | + // PlayAsyncの引数にジョイスティックと設定を追加 |
| 173 | + public async Task PlayAsync( |
| 174 | + List<string> macroNames, |
| 175 | + double frameMs, |
| 176 | + int playWaitFrames, |
| 177 | + bool isRepeat, |
| 178 | + bool isRandom, |
| 179 | + bool xAxisReverse, |
| 180 | + int frame, |
| 181 | + CancellationToken token, |
| 182 | + SharpDX.DirectInput.Joystick joystick = null, |
| 183 | + RecordSettingsForm.RecordSettingsConfig recordConfig = null) |
| 184 | + { |
| 185 | + var macroTriggers = new List<(string macroName, List<string> triggers, List<string> waitActions)>(); |
| 186 | + foreach (var macroName in macroNames) |
| 187 | + { |
| 188 | + var macroText = File.ReadAllText(Path.Combine(macroFolder, macroName + ".csv")); |
| 189 | + var lines = macroText.Split('\n'); |
| 190 | + var triggers = new List<string>(); |
| 191 | + var waitActions = new List<string>(); |
| 192 | + foreach (var line in lines) |
| 193 | + { |
| 194 | + var trimmed = line.Trim(); |
| 195 | + if (trimmed.StartsWith("#TRIGGER:")) |
| 196 | + { |
| 197 | + triggers = trimmed.Substring("#TRIGGER:".Length) |
| 198 | + .Split(',') |
| 199 | + .Select(x => |
| 200 | + { |
| 201 | + var key = x.Trim().ToUpper(); |
| 202 | + if (xAxisReverse) |
| 203 | + { |
| 204 | + if (key == "LEFT") key = "RIGHT"; |
| 205 | + else if (key == "RIGHT") key = "LEFT"; |
| 206 | + } |
| 207 | + return key; |
| 208 | + }) |
| 209 | + .Where(x => !string.IsNullOrEmpty(x)) |
| 210 | + .ToList(); |
| 211 | + } |
| 212 | + if (trimmed.StartsWith("#TRIGGERWAIT:")) |
| 213 | + { |
| 214 | + waitActions = trimmed.Substring("#TRIGGERWAIT:".Length) |
| 215 | + .Split(',') |
| 216 | + .Select(x => |
| 217 | + { |
| 218 | + var key = x.Trim().ToUpper(); |
| 219 | + if (xAxisReverse) |
| 220 | + { |
| 221 | + if (key == "LEFT") key = "RIGHT"; |
| 222 | + else if (key == "RIGHT") key = "LEFT"; |
| 223 | + } |
| 224 | + return key; |
| 225 | + }) |
| 226 | + .Where(x => !string.IsNullOrEmpty(x)) |
| 227 | + .ToList(); |
| 228 | + } |
| 229 | + if (!trimmed.StartsWith("#") && (trimmed.Contains(":") || (trimmed.Length > 0 && !trimmed.StartsWith("#")))) |
| 230 | + break; |
| 231 | + } |
| 232 | + macroTriggers.Add((macroName, triggers, waitActions)); |
| 233 | + } |
| 234 | + |
| 235 | + var triggerMacros = macroTriggers.Where(mt => mt.triggers.Count > 0).ToList(); |
| 236 | + // デバッグ出力追加 |
| 237 | + System.Diagnostics.Debug.WriteLine("[MacroPlayer] triggerMacros:"); |
| 238 | + foreach (var mt in triggerMacros) |
| 239 | + { |
| 240 | + System.Diagnostics.Debug.WriteLine( |
| 241 | + $" macroName={mt.macroName}, triggers=[{string.Join(",", mt.triggers)}], waitActions=[{string.Join(",", mt.waitActions)}]" |
| 242 | + ); |
| 243 | + } |
| 244 | + System.Diagnostics.Debug.WriteLine($"[MacroPlayer] joystick: {(joystick == null ? "null" : joystick.ToString())}"); |
| 245 | + System.Diagnostics.Debug.WriteLine($"[MacroPlayer] recordConfig: {(recordConfig == null ? "null" : $"ControllerGuid={recordConfig.ControllerGuid}, ButtonIndices={string.Join(",", recordConfig.ButtonIndices.Select(kv => kv.Key + "=" + (kv.Value?.ToString() ?? "null")))} ZValues={string.Join(",", recordConfig.ZValues.Select(kv => kv.Key + "=" + (kv.Value?.ToString() ?? "null")))}")}"); |
| 246 | + |
| 247 | + // 判定部分 |
| 248 | + if (triggerMacros.Count == 0 || joystick == null || recordConfig == null) |
| 249 | + { |
| 250 | + System.Diagnostics.Debug.WriteLine("[MacroPlayer] PlayAsync(基本) 実行"); |
| 251 | + await PlayAsync( |
| 252 | + macroNames, |
| 253 | + frameMs, |
| 254 | + playWaitFrames, |
| 255 | + isRepeat, |
| 256 | + isRandom, |
| 257 | + xAxisReverse, |
| 258 | + frame, |
| 259 | + token |
| 260 | + ); |
| 261 | + return; |
| 262 | + } |
| 263 | + |
| 264 | + // すべてのTRIGGERWAIT定義から最初のボタン群を抽出 |
| 265 | + List<string> prioritizedWaitActions = null; |
| 266 | + foreach (var mt in triggerMacros) |
| 267 | + { |
| 268 | + if (mt.waitActions.Count > 0) |
| 269 | + { |
| 270 | + prioritizedWaitActions = mt.waitActions; |
| 271 | + break; |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + |
| 276 | + // ジョイスティック判定 |
| 277 | + Random rand = new Random(); |
| 278 | + while (!token.IsCancellationRequested) |
| 279 | + { |
| 280 | + // トリガー待ちの間は prioritizedWaitActions のボタンを押す |
| 281 | + if (prioritizedWaitActions != null && prioritizedWaitActions.Count > 0) |
| 282 | + { |
| 283 | + var keyState = new MacroKeyState(); |
| 284 | + foreach (var kv in prioritizedWaitActions) |
| 285 | + { |
| 286 | + string key = kv.ToUpper(); |
| 287 | + // --- エイリアス対応 --- |
| 288 | + if (key == "LP") key = "X"; |
| 289 | + else if (key == "LK") key = "A"; |
| 290 | + else if (key == "MP") key = "Y"; |
| 291 | + else if (key == "MK") key = "B"; |
| 292 | + else if (key == "HP") key = "RB"; |
| 293 | + else if (key == "HK") key = "LB"; |
| 294 | + keyState.KeyStates[key] = true; |
| 295 | + } |
| 296 | + |
| 297 | + short y = 0, x = 0; |
| 298 | + if (keyState.KeyStates["UP"] && !keyState.KeyStates["DOWN"]) y = short.MaxValue; |
| 299 | + else if (keyState.KeyStates["DOWN"] && !keyState.KeyStates["UP"]) y = short.MinValue; |
| 300 | + |
| 301 | + bool reverse = xAxisReverse; |
| 302 | + if (!reverse) |
| 303 | + { |
| 304 | + if (keyState.KeyStates["LEFT"] && !keyState.KeyStates["RIGHT"]) x = short.MinValue; |
| 305 | + else if (keyState.KeyStates["RIGHT"] && !keyState.KeyStates["LEFT"]) x = short.MaxValue; |
| 306 | + } |
| 307 | + else |
| 308 | + { |
| 309 | + if (keyState.KeyStates["LEFT"] && !keyState.KeyStates["RIGHT"]) x = short.MaxValue; |
| 310 | + else if (keyState.KeyStates["RIGHT"] && !keyState.KeyStates["LEFT"]) x = short.MinValue; |
| 311 | + } |
| 312 | + |
| 313 | + // 軸・ボタンを直接コントローラーに反映 |
| 314 | + controllerService.Controller.SetAxisValue(Xbox360Axis.LeftThumbY, y); |
| 315 | + controllerService.Controller.SetAxisValue(Xbox360Axis.LeftThumbX, x); |
| 316 | + |
| 317 | + controllerService.Controller.SetButtonState(Xbox360Button.A, keyState.KeyStates["A"]); |
| 318 | + controllerService.Controller.SetButtonState(Xbox360Button.B, keyState.KeyStates["B"]); |
| 319 | + controllerService.Controller.SetButtonState(Xbox360Button.X, keyState.KeyStates["X"]); |
| 320 | + controllerService.Controller.SetButtonState(Xbox360Button.Y, keyState.KeyStates["Y"]); |
| 321 | + controllerService.Controller.SetButtonState(Xbox360Button.LeftShoulder, keyState.KeyStates["LB"]); |
| 322 | + controllerService.Controller.SetButtonState(Xbox360Button.RightShoulder, keyState.KeyStates["RB"]); |
| 323 | + } |
| 324 | + |
| 325 | + bool triggered = false; |
| 326 | + while (!triggered && !token.IsCancellationRequested) |
| 327 | + { |
| 328 | + var state = joystick.GetCurrentState(); |
| 329 | + var pressedKeys = new List<string>(); |
| 330 | + |
| 331 | + foreach (var kv in recordConfig.ButtonIndices) |
| 332 | + { |
| 333 | + if (kv.Value != null && state.Buttons.Length > kv.Value.Value && state.Buttons[kv.Value.Value]) |
| 334 | + pressedKeys.Add(kv.Key.Replace("Button", "")); |
| 335 | + } |
| 336 | + foreach (var kv in recordConfig.ZValues) |
| 337 | + { |
| 338 | + if (kv.Value != null && state.Z == kv.Value.Value) |
| 339 | + pressedKeys.Add(kv.Key.Replace("Button", "")); |
| 340 | + } |
| 341 | + if (state.PointOfViewControllers != null && state.PointOfViewControllers.Length > 0) |
| 342 | + { |
| 343 | + int pov = state.PointOfViewControllers[0]; |
| 344 | + if (pov == 0) pressedKeys.Add("UP"); |
| 345 | + else if (pov == 9000) pressedKeys.Add("RIGHT"); |
| 346 | + else if (pov == 18000) pressedKeys.Add("DOWN"); |
| 347 | + else if (pov == 27000) pressedKeys.Add("LEFT"); |
| 348 | + else if (pov == 4500) { pressedKeys.Add("UP"); pressedKeys.Add("RIGHT"); } |
| 349 | + else if (pov == 13500) { pressedKeys.Add("DOWN"); pressedKeys.Add("RIGHT"); } |
| 350 | + else if (pov == 22500) { pressedKeys.Add("DOWN"); pressedKeys.Add("LEFT"); } |
| 351 | + else if (pov == 31500) { pressedKeys.Add("UP"); pressedKeys.Add("LEFT"); } |
| 352 | + } |
| 353 | + |
| 354 | + |
| 355 | + // TRIGGER判定 |
| 356 | + var triggerdMacros = new List<string>(); |
| 357 | + foreach (var mt in triggerMacros) |
| 358 | + { |
| 359 | + if (mt.triggers.All(t => pressedKeys.Contains(t))) |
| 360 | + { |
| 361 | + triggerdMacros.Add(mt.macroName); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + if (triggerdMacros.Count > 0) |
| 366 | + { |
| 367 | + controllerService.AllOff(); |
| 368 | + System.Diagnostics.Debug.WriteLine("[MacroPlayer] PlayAsync(ジョイスティック/設定付き) 実行"); |
| 369 | + await PlayAsync( |
| 370 | + triggerdMacros, |
| 371 | + frameMs, 0, false, isRandom, xAxisReverse, frame, token |
| 372 | + ); |
| 373 | + triggered = true; |
| 374 | + } |
| 375 | + await Task.Delay(10, token); |
| 376 | + } |
| 377 | + } |
| 378 | + return; |
| 379 | + } |
| 380 | + |
171 | 381 | // MacroKeyStateを内包 |
172 | 382 | private class MacroKeyState |
173 | 383 | { |
|
0 commit comments