Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support zero-prefixed switch names #29

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ public GamelogicEngineLamp[] RequestedLamps {

[NonSerialized] private Player _player;
[NonSerialized] private PinMame.PinMame _pinMame;
[NonSerialized] private BallManager _ballManager;
[NonSerialized] private PlayfieldComponent _playfieldComponent;

[SerializeReference] private PinMameGame _game;


private Dictionary<string, GamelogicEngineSwitch> _switches = new();
private Dictionary<int, string> _pinMameIdToSwitchIdMappings = new();
private Dictionary<string, int> _switchIdToPinMameIdMappings = new();
Expand Down Expand Up @@ -218,6 +222,8 @@ private void OnDestroy()
public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
{
string vpmPath = null;
_ballManager = ballManager;
_playfieldComponent = GetComponentInChildren<PlayfieldComponent>();

#if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR
vpmPath = Path.Combine(Application.persistentDataPath, "pinmame");
Expand Down Expand Up @@ -327,6 +333,7 @@ private void UpdateCaches()
_pinMameIdToSwitchIdMappings.Clear();
_switchIdToPinMameIdMappings.Clear();

// check aliases first (the switches/coils that aren't an integer)
foreach (var alias in _game.AvailableAliases) {
switch (alias.AliasType) {
case AliasType.Switch:
Expand All @@ -346,16 +353,30 @@ private void UpdateCaches()
}
}


// retrieve the game's switches
foreach (var @switch in _game.AvailableSwitches) {
_switches[@switch.Id] = @switch;

if (int.TryParse(@switch.Id, out int pinMameId)) {
if (int.TryParse(@switch.Id, out var pinMameId)) {
_pinMameIdToSwitchIdMappings[pinMameId] = @switch.Id;
_switchIdToPinMameIdMappings[@switch.Id] = pinMameId;

// add mappings with prefixed 0.
if (pinMameId < 10) {
_switchIdToPinMameIdMappings["0" + @switch.Id] = pinMameId;
_switchIdToPinMameIdMappings["00" + @switch.Id] = pinMameId;

_switches["0" + @switch.Id] = @switch;
_switches["00" + @switch.Id] = @switch;
}
if (pinMameId < 100) {
_switchIdToPinMameIdMappings["0" + @switch.Id] = pinMameId;
_switches["0" + @switch.Id] = @switch;
}
}
}

// retrieve the game's coils
foreach (var coil in _game.AvailableCoils) {
_coils[coil.Id] = coil;

Expand All @@ -365,6 +386,7 @@ private void UpdateCaches()
}
}

// retrieve the game's lamps
foreach (var lamp in _game.AvailableLamps) {
_lamps[lamp.Id] = lamp;

Expand Down Expand Up @@ -712,6 +734,10 @@ public void Switch(string id, bool isClosed)
}
Logger.Info($"[PinMAME] => sw {id}: {isClosed} | {_switches[id].Description}");
_pinMame.SetSwitch(_switchIdToPinMameIdMappings[_switches[id].Id], isClosed);
} else if (id == "s_spawn_ball") {
if (isClosed) {
_ballManager.CreateBall(new DebugBallCreator(630, _playfieldComponent.Height / 2f, _playfieldComponent.TableHeight));
}
} else {
Logger.Error($"[PinMAME] Unknown switch \"{id}\".");
}
Expand Down