Skip to content

Commit bdaf193

Browse files
jsm174freezy
authored andcommitted
cleanup: add delay to spawn, add extra try/catches, bump deps
1 parent dec5d2b commit bdaf193

File tree

7 files changed

+226
-174
lines changed

7 files changed

+226
-174
lines changed

VisualPinball.Engine.Mpf.Test/Program.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Visual Pinball Engine
1+
// Visual Pinball Engine
22
// Copyright (C) 2021 freezy and VPE Team
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -13,7 +13,6 @@
1313
using System.Collections.Generic;
1414
using System.Diagnostics;
1515
using System.IO;
16-
using System.Linq;
1716
using System.Threading.Tasks;
1817
using VisualPinball.Engine.Mpf;
1918

@@ -51,6 +50,7 @@ public static async Task Main(string[] args)
5150
{"5", true},
5251
{"6", true},
5352
});
53+
5454
mpfApi.Client.OnConfigureHardwareRule += (_, request) => {
5555
Console.WriteLine($"[MPF] configure hw/rule: sw{request.SwitchNumber} -> c{request.CoilNumber} @{request.HoldPower} | pulse: {request.PulseMs}ms @{request.PulsePower}");
5656
};
@@ -73,8 +73,14 @@ public static async Task Main(string[] args)
7373
Console.WriteLine($"[MPF] new DMD frame!");
7474
};
7575

76-
var descr = mpfApi.GetMachineDescription();
77-
Console.WriteLine($"Description: {descr} in {s.ElapsedMilliseconds}ms");
76+
try {
77+
var descr = mpfApi.GetMachineDescription();
78+
Console.WriteLine($"Description: {descr} in {s.ElapsedMilliseconds}ms");
79+
}
80+
81+
catch(Exception e) {
82+
Console.WriteLine($"Unable to get description: {e.Message} in {s.ElapsedMilliseconds}ms");
83+
}
7884

7985
ConsoleKeyInfo key;
8086
do {

VisualPinball.Engine.Mpf.Unity/Runtime/MpfExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Visual Pinball Engine
1+
// Visual Pinball Engine
22
// Copyright (C) 2021 freezy and VPE Team
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -9,18 +9,23 @@
99
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1010
// SOFTWARE.
1111

12+
using System;
1213
using System.Collections.Generic;
1314
using System.Linq;
1415
using System.Text.RegularExpressions;
1516
using Mpf.Vpe;
1617
using VisualPinball.Engine.Common;
1718
using VisualPinball.Engine.Game.Engines;
1819
using VisualPinball.Unity;
20+
using NLog;
21+
using Logger = NLog.Logger;
1922

2023
namespace VisualPinball.Engine.Mpf.Unity
2124
{
2225
public static class MpfExtensions
2326
{
27+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
28+
2429
public static IEnumerable<GamelogicEngineSwitch> GetSwitches(this MachineDescription md)
2530
{
2631
return md.Switches.Select(sw => {
@@ -103,7 +108,10 @@ public static IEnumerable<GamelogicEngineCoil> GetCoils(this MachineDescription
103108
public static IEnumerable<GamelogicEngineLamp> GetLights(this MachineDescription md)
104109
{
105110
// todo color
106-
return md.Lights.Select(light => new GamelogicEngineLamp(light.Name, int.Parse(light.HardwareChannelColor)));
111+
return md.Lights.Select(light => {
112+
Logger.Info(light);
113+
return new GamelogicEngineLamp(light.Name, int.Parse(light.HardwareChannelNumber));
114+
});
107115
}
108116
}
109117
}

VisualPinball.Engine.Mpf.Unity/Runtime/MpfGamelogicEngine.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Visual Pinball Engine
1+
// Visual Pinball Engine
22
// Copyright (C) 2021 freezy and VPE Team
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -126,10 +126,21 @@ public void Switch(string id, bool isClosed)
126126

127127
public void GetMachineDescription()
128128
{
129-
var md = MpfApi.GetMachineDescription(machineFolder);
130-
availableSwitches = md.GetSwitches().ToArray();
131-
availableCoils = md.GetCoils().ToArray();
132-
availableLamps = md.GetLights().ToArray();
129+
MachineDescription md = null;
130+
131+
try {
132+
md = MpfApi.GetMachineDescription(machineFolder);
133+
}
134+
135+
catch (Exception e) {
136+
Logger.Error($"Unable to get machine description. Check maching config. {e.Message}");
137+
}
138+
139+
if (md != null) {
140+
availableSwitches = md.GetSwitches().ToArray();
141+
availableCoils = md.GetCoils().ToArray();
142+
availableLamps = md.GetLights().ToArray();
143+
}
133144
}
134145

135146
private void OnEnableCoil(object sender, EnableCoilRequest e)

VisualPinball.Engine.Mpf/MpfClient.cs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Visual Pinball Engine
1+
// Visual Pinball Engine
22
// Copyright (C) 2021 freezy and VPE Team
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -74,36 +74,43 @@ await _switchStream.RequestStream.WriteAsync(new SwitchChanges
7474
private async void ReceiveCommands()
7575
{
7676
Logger.Info("Client started, retrieving commands...");
77-
while (await _commandStream.ResponseStream.MoveNext()) {
78-
var commands = _commandStream.ResponseStream.Current;
79-
switch (commands.CommandCase) {
80-
case Commands.CommandOneofCase.None:
81-
break;
82-
case Commands.CommandOneofCase.FadeLight:
83-
OnFadeLight?.Invoke(this, commands.FadeLight);
84-
break;
85-
case Commands.CommandOneofCase.PulseCoil:
86-
OnPulseCoil?.Invoke(this, commands.PulseCoil);
87-
break;
88-
case Commands.CommandOneofCase.EnableCoil:
89-
OnEnableCoil?.Invoke(this, commands.EnableCoil);
90-
break;
91-
case Commands.CommandOneofCase.DisableCoil:
92-
OnDisableCoil?.Invoke(this, commands.DisableCoil);
93-
break;
94-
case Commands.CommandOneofCase.ConfigureHardwareRule:
95-
OnConfigureHardwareRule?.Invoke(this, commands.ConfigureHardwareRule);
96-
break;
97-
case Commands.CommandOneofCase.RemoveHardwareRule:
98-
OnRemoveHardwareRule?.Invoke(this, commands.RemoveHardwareRule);
99-
break;
100-
case Commands.CommandOneofCase.DmdFrameRequest:
101-
OnDmdFrame?.Invoke(this, commands.DmdFrameRequest);
102-
break;
103-
default:
104-
throw new ArgumentOutOfRangeException();
77+
78+
try {
79+
while (await _commandStream.ResponseStream.MoveNext()) {
80+
var commands = _commandStream.ResponseStream.Current;
81+
switch (commands.CommandCase) {
82+
case Commands.CommandOneofCase.None:
83+
break;
84+
case Commands.CommandOneofCase.FadeLight:
85+
OnFadeLight?.Invoke(this, commands.FadeLight);
86+
break;
87+
case Commands.CommandOneofCase.PulseCoil:
88+
OnPulseCoil?.Invoke(this, commands.PulseCoil);
89+
break;
90+
case Commands.CommandOneofCase.EnableCoil:
91+
OnEnableCoil?.Invoke(this, commands.EnableCoil);
92+
break;
93+
case Commands.CommandOneofCase.DisableCoil:
94+
OnDisableCoil?.Invoke(this, commands.DisableCoil);
95+
break;
96+
case Commands.CommandOneofCase.ConfigureHardwareRule:
97+
OnConfigureHardwareRule?.Invoke(this, commands.ConfigureHardwareRule);
98+
break;
99+
case Commands.CommandOneofCase.RemoveHardwareRule:
100+
OnRemoveHardwareRule?.Invoke(this, commands.RemoveHardwareRule);
101+
break;
102+
case Commands.CommandOneofCase.DmdFrameRequest:
103+
OnDmdFrame?.Invoke(this, commands.DmdFrameRequest);
104+
break;
105+
default:
106+
throw new ArgumentOutOfRangeException();
107+
}
105108
}
106109
}
110+
111+
catch(RpcException e) {
112+
Logger.Error($"Unable to retrieve commands: Status={e.Status}");
113+
}
107114
}
108115

109116
public MachineDescription GetMachineDescription()

VisualPinball.Engine.Mpf/MpfSpawner.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Visual Pinball Engine
1+
// Visual Pinball Engine
22
// Copyright (C) 2021 freezy and VPE Team
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -76,6 +76,8 @@ private void RunMpf(string mpfExePath, MpfConsoleOptions options)
7676
Logger.Info($"[MPF] Spawning: > {mpfExePath} {args}");
7777

7878
using (var process = Process.Start(info)) {
79+
Thread.Sleep(1500);
80+
7981
_ready.Release();
8082
if (!options.CatchStdOut) {
8183
process.WaitForExit();

VisualPinball.Engine.Mpf/VisualPinball.Engine.Mpf.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="Grpc.Tools" Version="2.36.1">
19+
<PackageReference Include="Grpc.Tools" Version="2.38.1">
2020
<PrivateAssets>all</PrivateAssets>
2121
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2222
</PackageReference>
23-
<PackageReference Include="Grpc" Version="2.36.1" />
24-
<PackageReference Include="Google.Protobuf" Version="3.15.5" />
25-
<PackageReference Include="NLog" Version="4.7.7" />
26-
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
23+
<PackageReference Include="Grpc" Version="2.38.1" />
24+
<PackageReference Include="Google.Protobuf" Version="3.17.3" />
25+
<PackageReference Include="NLog" Version="4.7.10" />
26+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
2727
<PrivateAssets>all</PrivateAssets>
2828
</PackageReference>
2929
</ItemGroup>

0 commit comments

Comments
 (0)