diff --git a/OriModding.BF.Speedrun/Turbo.cs b/OriModding.BF.Speedrun/Turbo.cs index c543596..89b6343 100644 --- a/OriModding.BF.Speedrun/Turbo.cs +++ b/OriModding.BF.Speedrun/Turbo.cs @@ -1,4 +1,7 @@ using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; using SmartInput; using UnityEngine; @@ -6,17 +9,20 @@ namespace OriModding.BF.Speedrun; public class TurboController : MonoBehaviour { - class TurboButton + class TurboButton(IButtonInput button, global::Core.Input.InputButtonProcessor target) { - public IButtonInput button; - public global::Core.Input.InputButtonProcessor target; - public int value = 0; + readonly IButtonInput button = button; + readonly global::Core.Input.InputButtonProcessor target = target; + int value = 0; public void Update() { if (button.GetButton()) { - target.Update(value == 0); + target.Update(false); + if (value == 0) + target.Update(true); + value = (value + 1) % 3; } else @@ -30,14 +36,8 @@ public void Update() void Awake() { - turboButtons = new List - { - new TurboButton - { - button = new KeyCodeButtonInput(KeyCode.Y), - target = global::Core.Input.SpiritFlame - } - }; + turboButtons = new List(); + Load(Path.Combine(OutputFolder.PlayerDataFolderPath, "turbo.txt")); On.PlayerInput.FixedUpdate += (orig, self) => { @@ -47,6 +47,56 @@ void Awake() }; } + private void Load(string filepath) + { + // Format per line: + // : + // e.g. SpiritFlame:T,LT+FaceX + // Spirit flame turbo is active if T is held, or if left trigger and X are held on a controller + + if (!File.Exists(filepath)) + return; + + var targets = typeof(global::Core.Input).GetFields(BindingFlags.Public | BindingFlags.Static) + .Where(x => x.FieldType == typeof(global::Core.Input.InputButtonProcessor)) + .ToList(); + + using var reader = new StreamReader(filepath); + while (!reader.EndOfStream) + { + var line = reader.ReadLine().Split(new[] { ':' }, System.StringSplitOptions.RemoveEmptyEntries); + + if (line.Length == 0) // blank line + continue; + + if (line[0].Trim().StartsWith("#")) // comment + continue; + + if (line.Length != 2) // invalid line + { + Plugin.Logger.LogWarning($"Invalid turbo configuration (format): {line}"); + continue; + } + + var targetInput = targets.FirstOrDefault(x => x.Name.ToLower() == line[0].ToLower()); + if (targetInput == null) + { + Plugin.Logger.LogWarning($"Invalid turbo configuration (target): {line}"); + continue; + } + + var input = new CompoundButtonInput(InputLib.CustomInput.ParseButtons(line[1])); + if (input.Buttons.Length != line[1].Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).Length) + { + Plugin.Logger.LogWarning($"Invalid turbo configuration (buttons): {line}"); + continue; + } + + turboButtons.Add(new TurboButton(input, targetInput.GetValue(null) as global::Core.Input.InputButtonProcessor)); + Plugin.Logger.LogInfo($"Added turbo: {line[0]}:{line[1]}"); + } + } + void OnGUI() { if (global::Core.Input.SpiritFlame.IsPressed)