Skip to content

Commit

Permalink
Refactor InputDisplayGenerator + LogEntryGenerator + ControllerDefini…
Browse files Browse the repository at this point in the history
…tion (#3782)

* refactor InputDisplayGenerator and LogEntryGenerator handling
* Fix NRE in Tastudio. I can already see this avalanching into 100 different bugs
* cba, revert 48f4e13 and bring back bullshit dummy default MovieController
* Refactor MnemonicCache + make Bk2LogEntryGenerator and Bk2InputDisplayGenerator static. This should simplify stuff and make the logic clearer
  • Loading branch information
Morilli authored Sep 10, 2024
1 parent 08bd14e commit a080086
Show file tree
Hide file tree
Showing 50 changed files with 239 additions and 401 deletions.
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 @@ -50,9 +50,7 @@ public string GetInputAsMnemonic(int frame)
return string.Empty;
}

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

public void Save(string filename)
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 @@ -186,4 +184,4 @@ public void BindAxis(string button, AnalogBind bind)
.Select(kvp => kvp.Key)
.ToList();
}
}
}
18 changes: 6 additions & 12 deletions src/BizHawk.Client.Common/DisplayManager/OSDManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private string MakeFrameCounter()

return sb.ToString();
}

return _emulator.Frame.ToString();
}

Expand Down Expand Up @@ -172,10 +172,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 @@ -196,15 +196,9 @@ public string InputStrOrAll()
? MakeStringFor(_inputManager.AutofireStickyXorAdapter.Or(_movieSession.Movie.GetInputState(_emulator.Frame - 1)))
: InputStrImmediate();

private string MakeStringFor(IController controller, bool cache = false)
private static 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 Bk2InputDisplayGenerator.Generate(controller);
}

public string MakeIntersectImmediatePrevious()
Expand Down Expand Up @@ -292,7 +286,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
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 @@ -14,8 +14,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 Dictionary<string, int> Axes { get; private set; } = new();

protected Dictionary<string, bool> Buttons { get; private set; } = new();
Expand Down
62 changes: 0 additions & 62 deletions src/BizHawk.Client.Common/display/IInputDisplayGenerator.cs

This file was deleted.

46 changes: 46 additions & 0 deletions src/BizHawk.Client.Common/display/InputDisplayGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using System.Text;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
{
/// <summary>
/// Generates a display friendly version of the input log entry
/// using .bk2 mnemonics as the basis for display
/// </summary>
public static class Bk2InputDisplayGenerator
{
public static string Generate(IController source)
{
if (source.Definition.MnemonicsCache is null)
throw new InvalidOperationException("Can't generate input display string with empty mnemonics cache");

var sb = new StringBuilder();

foreach ((string buttonName, AxisSpec? axisSpec) in source.Definition.ControlsOrdered.SelectMany(x => x))
{
if (axisSpec.HasValue)
{
int val = source.AxisValue(buttonName);

if (val == axisSpec.Value.Neutral)
{
sb.Append(" ");
}
else
{
sb.Append(val.ToString().PadLeft(5, ' ')).Append(',');
}
}
else
{
sb.Append(source.IsPressed(buttonName)
? source.Definition.MnemonicsCache[buttonName]
: ' ');
}
}

return sb.ToString();
}
}
}
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
5 changes: 3 additions & 2 deletions src/BizHawk.Client.Common/inputAdapters/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ public class InputManager

public void ResetMainControllers(AutofireController nullAutofireController)
{
ActiveController = new(NullController.Instance.Definition);
ActiveController = new Controller(NullController.Instance.Definition);
AutoFireController = nullAutofireController;
}

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

ActiveController = BindToDefinition(def, config.AllTrollers, config.AllTrollersAnalog, config.AllTrollersFeedbacks);
AutoFireController = BindToDefinitionAF(emulator, config.AllTrollersAutoFire, config.AutofireOn, config.AutofireOff);
Expand Down Expand Up @@ -157,4 +158,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 @@ -12,8 +12,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 @@ -14,8 +14,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 @@ -110,8 +108,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 @@ -216,4 +212,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
9 changes: 5 additions & 4 deletions src/BizHawk.Client.Common/movie/MovieSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ 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; } = new Bk2Controller(NullController.Instance.Definition);

public IMovieController GenerateMovieController(ControllerDefinition definition = null)
public IMovieController GenerateMovieController(ControllerDefinition definition = null, string logKey = null)
{
// TODO: expose Movie.LogKey and pass in here
return new Bk2Controller("", definition ?? MovieController.Definition);
// TODO: should this fallback to Movie.LogKey?
// this function is kinda weird
return new Bk2Controller(definition ?? MovieController.Definition, logKey);
}

public void HandleFrameBefore()
Expand Down
Loading

0 comments on commit a080086

Please sign in to comment.