Skip to content

Commit

Permalink
Load from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirefel committed Aug 11, 2024
1 parent ba8c886 commit afd95a3
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions OriModding.BF.Speedrun/Turbo.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using SmartInput;
using UnityEngine;

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
Expand All @@ -30,14 +36,8 @@ public void Update()

void Awake()
{
turboButtons = new List<TurboButton>
{
new TurboButton
{
button = new KeyCodeButtonInput(KeyCode.Y),
target = global::Core.Input.SpiritFlame
}
};
turboButtons = new List<TurboButton>();
Load(Path.Combine(OutputFolder.PlayerDataFolderPath, "turbo.txt"));

On.PlayerInput.FixedUpdate += (orig, self) =>
{
Expand All @@ -47,6 +47,56 @@ void Awake()
};
}

private void Load(string filepath)
{
// Format per line:
// <target>:<control string>
// 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)
Expand Down

0 comments on commit afd95a3

Please sign in to comment.