Skip to content

Commit

Permalink
refactor InputDisplayGenerator and LogEntryGenerator handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Morilli committed Jun 10, 2024
1 parent 42aa9d9 commit bef4dce
Show file tree
Hide file tree
Showing 54 changed files with 189 additions and 245 deletions.
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Classes/JoypadApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public IReadOnlyDictionary<string, object> GetImmediate(int? controller = null)

public void SetFromMnemonicStr(string inputLogEntry)
{
var controller = _movieSession.GenerateMovieController(_inputManager.ActiveController.Definition);
var controller = _movieSession.GenerateMovieController(_inputManager.ActiveController.Definition, null);
try
{
controller.SetFromMnemonic(inputLogEntry);
Expand Down
4 changes: 1 addition & 3 deletions src/BizHawk.Client.Common/Api/Classes/MovieApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public string GetInputAsMnemonic(int frame)
return string.Empty;
}

var lg = _movieSession.Movie.LogGeneratorInstance(
_movieSession.Movie.GetInputState(frame));
return lg.GenerateLogEntry();
return _movieSession.Movie.GetInputState(frame).LogEntryGenerator.GenerateLogEntry();
}

public void Save(string filename = null)
Expand Down
4 changes: 1 addition & 3 deletions src/BizHawk.Client.Common/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace BizHawk.Client.Common
{
public class Controller : IController
{
public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public Controller(ControllerDefinition definition)
{
Definition = definition;
Expand Down Expand Up @@ -180,4 +178,4 @@ public void BindAxis(string button, AnalogBind bind)
.Select(kvp => kvp.Key)
.ToList();
}
}
}
20 changes: 7 additions & 13 deletions src/BizHawk.Client.Common/DisplayManager/OSDManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private string MakeFrameCounter()

return sb.ToString();
}

return _emulator.Frame.ToString();
}

Expand Down Expand Up @@ -173,10 +173,10 @@ public void DrawMessages(IBlitter g)
}

public string InputStrMovie()
=> MakeStringFor(_movieSession.MovieController, cache: true);
=> MakeStringFor(_movieSession.MovieController);

public string InputStrImmediate()
=> MakeStringFor(_inputManager.AutofireStickyXorAdapter, cache: true);
=> MakeStringFor(_inputManager.AutofireStickyXorAdapter);

public string InputPrevious()
{
Expand All @@ -197,15 +197,9 @@ public string InputStrOrAll()
? MakeStringFor(_inputManager.AutofireStickyXorAdapter.Or(_movieSession.Movie.GetInputState(_emulator.Frame - 1)))
: InputStrImmediate();

private string MakeStringFor(IController controller, bool cache = false)
private string MakeStringFor(IController controller)
{
var idg = controller.InputDisplayGenerator;
if (idg is null)
{
idg = new Bk2InputDisplayGenerator(_emulator.SystemId, controller);
if (cache) controller.InputDisplayGenerator = idg;
}
return idg.Generate();
return _inputManager.InputDisplayGenerator.Generate(controller);
}

public string MakeIntersectImmediatePrevious()
Expand Down Expand Up @@ -293,7 +287,7 @@ public void DrawScreenInfo(IBlitter g)
// in order to achieve this we want to avoid drawing anything pink that isn't actually held down right now
// so we make an AND adapter and combine it using immediate & sticky
// (adapter creation moved to InputManager)
var autoString = MakeStringFor(_inputManager.WeirdStickyControllerForInputDisplay, cache: true);
var autoString = MakeStringFor(_inputManager.WeirdStickyControllerForInputDisplay);
g.DrawString(autoString, autoColor, point.X, point.Y);

//recolor everything that's changed from the previous input
Expand Down Expand Up @@ -355,4 +349,4 @@ public void DrawScreenInfo(IBlitter g)
}
}
}
}
}
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/controllers/AutofireController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace BizHawk.Client.Common
{
public class AutofireController : IController
{
public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public AutofireController(IEmulator emulator, int on, int off)
{
On = on < 1 ? 0 : on;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class ClickyVirtualPadController : IController

public ControllerDefinition Definition { get; set; }

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button) => _pressed.Contains(button);

public int AxisValue(string name) => 0;
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/controllers/SimpleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class SimpleController : IController
{
public ControllerDefinition Definition { get; }

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

protected WorkingDictionary<string, bool> Buttons { get; private set; } = new WorkingDictionary<string, bool>();
protected WorkingDictionary<string, int> Axes { get; private set; } = new WorkingDictionary<string, int>();
protected WorkingDictionary<string, int> HapticFeedback { get; private set; } = new WorkingDictionary<string, int>();
Expand Down
35 changes: 19 additions & 16 deletions src/BizHawk.Client.Common/display/IInputDisplayGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,40 @@
namespace BizHawk.Client.Common
{
/// <summary>
/// An implementation of <see cref="IInputDisplayGenerator"/> that
/// uses .bk2 mnemonics as the basis for display
/// Generates a display friendly version of the input log entry
/// using .bk2 mnemonics as the basis for display
/// </summary>
public class Bk2InputDisplayGenerator : IInputDisplayGenerator
public class Bk2InputDisplayGenerator
{
/// <remarks>either <c>Range</c> or <c>Mnemonic</c> is always non-null</remarks>
private readonly IReadOnlyList<(string Name, AxisSpec? Range, char? Mnemonic)> _cachedInputSpecs;
private readonly ControllerDefinition _sourceDefinition;

private readonly IController _source;

public Bk2InputDisplayGenerator(string systemId, IController source)
public Bk2InputDisplayGenerator(string systemId, ControllerDefinition sourceDefinition)
{
const string ERR_MSG = nameof(ControllerDefinition.OrderedControlsFlat) + "/" + nameof(ControllerDefinition.ControlsOrdered) + " contains an input name which is neither a button nor an axis";
_cachedInputSpecs = source.Definition.OrderedControlsFlat.Select(button =>
const string ERR_MSG = $"{nameof(ControllerDefinition.OrderedControlsFlat)}/{nameof(ControllerDefinition.ControlsOrdered)} contains an input name which is neither a button nor an axis: {{0}}";
_cachedInputSpecs = sourceDefinition.OrderedControlsFlat.Select(button =>
{
if (source.Definition.Axes.TryGetValue(button, out var range)) return (button, range, null);
if (source.Definition.BoolButtons.Contains(button)) return (button, (AxisSpec?) null, (char?) Bk2MnemonicLookup.Lookup(button, systemId));
throw new Exception(ERR_MSG);
}).ToList();
_source = source;
if (sourceDefinition.Axes.TryGetValue(button, out var range)) return (button, range, null);
if (sourceDefinition.BoolButtons.Contains(button)) return (button, (AxisSpec?) null, (char?) Bk2MnemonicLookup.Lookup(button, systemId));
throw new InvalidOperationException(string.Format(ERR_MSG, button));
}).ToArray();
_sourceDefinition = sourceDefinition;
}

public string Generate()
public string Generate(IController source)
{
#if DEBUG
if (!_sourceDefinition.OrderedControlsFlat.SequenceEqual(source.Definition.OrderedControlsFlat))
throw new InvalidOperationException("Attempting to generate input display string for mismatched controller definition!");
#endif
var sb = new StringBuilder();

foreach (var (button, range, mnemonicChar) in _cachedInputSpecs)
{
if (range is not null)
{
var val = _source.AxisValue(button);
var val = source.AxisValue(button);

if (val == range.Value.Neutral)
{
Expand All @@ -51,7 +54,7 @@ public string Generate()
}
else
{
sb.Append(_source.IsPressed(button)
sb.Append(source.IsPressed(button)
? mnemonicChar.Value
: ' ');
}
Expand Down
6 changes: 0 additions & 6 deletions src/BizHawk.Client.Common/inputAdapters/BitwiseAdapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class AndAdapter : IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button)
{
if (Source != null && SourceAnd != null)
Expand All @@ -36,8 +34,6 @@ public class XorAdapter : IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button)
{
if (Source != null && SourceXor != null)
Expand All @@ -64,8 +60,6 @@ public class ORAdapter : IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button)
{
return (Source?.IsPressed(button) ?? false)
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/inputAdapters/CopyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class CopyControllerAdapter : IInputAdapter
{
public ControllerDefinition Definition => Curr.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button) => Curr.IsPressed(button);

public int AxisValue(string name) => Curr.AxisValue(name);
Expand Down
14 changes: 11 additions & 3 deletions src/BizHawk.Client.Common/inputAdapters/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace BizHawk.Client.Common
// (1)->Input Display
public class InputManager
{
public Bk2InputDisplayGenerator InputDisplayGenerator { get; private set; }

// the original source controller, bound to the user, sort of the "input" port for the chain, i think
public Controller ActiveController { get; private set; }

Expand Down Expand Up @@ -51,17 +53,23 @@ public class InputManager

public Func<(Point Pos, long Scroll, bool LMB, bool MMB, bool RMB, bool X1MB, bool X2MB)> GetMainFormMouseInfo { get; set; }

private void SetActiveController(Controller controller, string systemId)
{
ActiveController = controller;
InputDisplayGenerator = new Bk2InputDisplayGenerator(systemId, ActiveController.Definition);
}

public void ResetMainControllers(AutofireController nullAutofireController)
{
ActiveController = new(NullController.Instance.Definition);
SetActiveController(new Controller(NullController.Instance.Definition), VSystemID.Raw.NULL);
AutoFireController = nullAutofireController;
}

public void SyncControls(IEmulator emulator, IMovieSession session, Config config)
{
var def = emulator.ControllerDefinition;

ActiveController = BindToDefinition(def, config.AllTrollers, config.AllTrollersAnalog, config.AllTrollersFeedbacks);
SetActiveController(BindToDefinition(def, config.AllTrollers, config.AllTrollersAnalog, config.AllTrollersFeedbacks), emulator.SystemId);
AutoFireController = BindToDefinitionAF(emulator, config.AllTrollersAutoFire, config.AutofireOn, config.AutofireOff);

// allow propagating controls that are in the current controller definition but not in the prebaked one
Expand Down Expand Up @@ -156,4 +164,4 @@ private static AutofireController BindToDefinitionAF(
return ret;
}
}
}
}
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/inputAdapters/OverrideAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class OverrideAdapter : IController
{
public ControllerDefinition Definition { get; private set; }

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

private readonly Dictionary<string, bool> _overrides = new Dictionary<string, bool>();
private readonly Dictionary<string, int> _axisOverrides = new Dictionary<string, int>();
private readonly List<string> _inverses = new List<string>();
Expand Down
6 changes: 1 addition & 5 deletions src/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class StickyXorAdapter : IStickyAdapter
{
public ControllerDefinition Definition => Source.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button)
{
var source = Source.IsPressed(button);
Expand Down Expand Up @@ -117,8 +115,6 @@ public class AutoFireStickyXorAdapter : IStickyAdapter, IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public bool IsPressed(string button)
{
var source = Source.IsPressed(button);
Expand Down Expand Up @@ -229,4 +225,4 @@ public void MassToggleStickyState(List<string> buttons)
_justPressed = buttons;
}
}
}
}
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/inputAdapters/UDLRController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class UdlrControllerAdapter : IInputAdapter

public ControllerDefinition Definition => Source.Definition;

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public OpposingDirPolicy OpposingDirPolicy { get; set; }

public bool IsPressed(string button)
Expand Down
14 changes: 10 additions & 4 deletions src/BizHawk.Client.Common/movie/MovieSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ public MovieSession(
public IInputAdapter MovieOut { get; } = new CopyControllerAdapter();
public IStickyAdapter StickySource { get; set; }

public IMovieController MovieController { get; private set; } = new Bk2Controller("", NullController.Instance.Definition);
public IMovieController MovieController { get; private set; }

public IMovieController GenerateMovieController(ControllerDefinition definition = null)
public IMovieController GenerateMovieController()
{
// TODO: expose Movie.LogKey and pass in here
return new Bk2Controller("", definition ?? MovieController.Definition);
return new Bk2Controller("", MovieController.Definition, Movie.SystemID);
}

public IMovieController GenerateMovieController(ControllerDefinition definition, string systemId)
{
// TODO: expose Movie.LogKey and pass in here
return new Bk2Controller("", definition, systemId);
}

public void HandleFrameBefore()
Expand Down Expand Up @@ -234,7 +240,7 @@ public void QueueNewMovie(

public void RunQueuedMovie(bool recordMode, IEmulator emulator)
{
MovieController = new Bk2Controller(emulator.ControllerDefinition);
MovieController = new Bk2Controller(emulator.ControllerDefinition, emulator.SystemId);

Movie = _queuedMovie;
Movie.Attach(emulator);
Expand Down
17 changes: 10 additions & 7 deletions src/BizHawk.Client.Common/movie/bk2/Bk2Controller.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class Bk2Controller : IMovieController
private readonly WorkingDictionary<string, int> _myAxisControls = new();

private readonly Bk2ControllerDefinition _type;
private readonly string _systemId;

private IList<ControlMap> _controlsOrdered;

Expand All @@ -23,25 +24,24 @@ internal class Bk2Controller : IMovieController
IsBool = _type.BoolButtons.Contains(c),
IsAxis = _type.Axes.ContainsKey(c)
})
.ToList();
.ToArray();

public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;

public Bk2Controller(string key, ControllerDefinition definition) : this(definition)
public Bk2Controller(string key, ControllerDefinition definition, string systemId) : this(definition, systemId)
{
if (!string.IsNullOrEmpty(key))
{
var groups = key.Split(new[] { "#" }, StringSplitOptions.RemoveEmptyEntries);

_type.ControlsFromLog = groups
.Select(group => group.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries).ToList())
.ToList();
.Select(group => group.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
}
}

public Bk2Controller(ControllerDefinition definition)
public Bk2Controller(ControllerDefinition definition, string systemId)
{
_type = new Bk2ControllerDefinition(definition);
_systemId = systemId;
foreach ((string axisName, AxisSpec range) in definition.Axes)
{
_myAxisControls[axisName] = range.Neutral;
Expand All @@ -57,6 +57,9 @@ public Bk2Controller(ControllerDefinition definition)

public void SetHapticChannelStrength(string name, int strength) {}

private Bk2LogEntryGenerator _logEntryGenerator;
public Bk2LogEntryGenerator LogEntryGenerator => _logEntryGenerator ??= new Bk2LogEntryGenerator(_systemId, this);

public void SetFrom(IController source)
{
for (int index = 0; index < Definition.BoolButtons.Count; index++)
Expand Down
Loading

0 comments on commit bef4dce

Please sign in to comment.