Skip to content

Commit c9abd91

Browse files
committed
api: Re-enable spawner and get rid of all the asyncs.
1 parent 7e7e28a commit c9abd91

File tree

4 files changed

+56
-44
lines changed

4 files changed

+56
-44
lines changed

VisualPinball.Engine.Mpf.Test/Program.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,31 @@
1717

1818
namespace MpfTest
1919
{
20-
class Program
20+
public static class Program
2121
{
22-
static async Task Main(string[] args)
22+
public static void Main(string[] args)
2323
{
24-
Console.WriteLine("Starting...");
25-
var client = new MpfClient();
26-
client.Connect();
27-
client.Play();
28-
Console.WriteLine("Description = " + client.GetMachineDescription());
29-
Console.WriteLine("Done!");
30-
31-
32-
/*var s = Stopwatch.StartNew();
24+
// Console.WriteLine("Starting...");
25+
// var client = new MpfClient();
26+
// client.Connect();
27+
// client.StartGame(new Dictionary<string, bool> {
28+
// { "sw11", true }
29+
// });
30+
// Console.WriteLine("Description = " + client.GetMachineDescription());
31+
// Console.WriteLine("Done!");
32+
33+
34+
var s = Stopwatch.StartNew();
3335
var mpfApi = new MpfApi(@"../../../VisualPinball.Engine.Mpf/machine");
3436

35-
await mpfApi.Launch();
37+
mpfApi.Launch();
3638

37-
mpfApi.Start(new Dictionary<string, bool> {
39+
mpfApi.StartGame(new Dictionary<string, bool> {
3840
{"sw_11", false},
3941
});
4042

41-
var descr = await mpfApi.GetMachineDescription();
42-
Console.WriteLine($"Description: {descr} in {s.ElapsedMilliseconds}ms");*/
43+
var descr = mpfApi.GetMachineDescription();
44+
Console.WriteLine($"Description: {descr} in {s.ElapsedMilliseconds}ms");
4345
}
4446
}
4547
}

VisualPinball.Engine.Mpf/MpfApi.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,39 @@ public class MpfApi : IDisposable
2525
public MpfApi(string machineFolder)
2626
{
2727
_spawner = new MpfSpawner(Path.GetFullPath(machineFolder));
28-
}
29-
30-
/*/// <summary>
28+
}
29+
30+
/// <summary>
3131
/// Launches MPF in the background and connects to it via gRPC.
3232
/// </summary>
3333
/// <param name="port">gRPC port to use for MPC/VPE communication</param>
3434
/// <returns></returns>
35-
public async Task Launch(int port = 50051)
35+
public void Launch(int port = 50051)
3636
{
37-
await _spawner.Spawn();
38-
await _client.Connect($"localhost:{port}");
37+
_spawner.Spawn();
38+
_client.Connect($"localhost:{port}");
3939
}
4040

4141
/// <summary>
4242
/// Starts MPF, i.e. it will start polling for switches and sending events.
4343
/// </summary>
4444
/// <param name="initialSwitches">Initial switch states of the machine</param>
45-
public void Start(Dictionary<string, bool> initialSwitches = null)
45+
public void StartGame(Dictionary<string, bool> initialSwitches = null)
4646
{
47-
_client.Start(initialSwitches ?? new Dictionary<string, bool>());
47+
_client.StartGame(initialSwitches ?? new Dictionary<string, bool>());
4848
}
4949

5050
/// <summary>
5151
/// Returns the machine description.
5252
/// </summary>
53-
public async Task<MachineDescription> GetMachineDescription()
53+
public MachineDescription GetMachineDescription()
5454
{
55-
return await _client.GetMachineDescription();
55+
return _client.GetMachineDescription();
5656
}
57-
*/
57+
5858
public void Dispose()
5959
{
60-
//_client?.Dispose();
60+
_client?.Shutdown();
6161
}
6262
}
6363
}

VisualPinball.Engine.Mpf/MpfClient.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@
1818

1919
namespace VisualPinball.Engine.Mpf
2020
{
21+
/// <summary>
22+
/// A wrapper with a nicer API than the proto-generated one.
23+
/// </summary>
2124
public class MpfClient
2225
{
2326
private Channel _channel;
2427
private MpfHardwareService.MpfHardwareServiceClient _client;
25-
private readonly string _server = "127.0.0.1:50051";
2628

27-
public void Connect()
29+
public void Connect(string serverIpPort = "127.0.0.1:50051")
2830
{
29-
_channel = new Channel(_server, ChannelCredentials.Insecure);
31+
_channel = new Channel(serverIpPort, ChannelCredentials.Insecure);
3032
_client = new MpfHardwareService.MpfHardwareServiceClient(_channel);
3133
}
3234

33-
public void Play()
35+
public void StartGame(Dictionary<string, bool> initialSwitches)
3436
{
3537
var ms = new MachineState();
36-
ms.InitialSwitchStates.Add("sw11", true);
38+
foreach (var sw in initialSwitches.Keys) {
39+
ms.InitialSwitchStates.Add(sw, initialSwitches[sw]);
40+
}
3741
_client.Start(ms);
3842
}
3943

@@ -42,7 +46,7 @@ public MachineDescription GetMachineDescription()
4246
return _client.GetMachineDescription(new EmptyRequest());
4347
}
4448

45-
private void OnDisable() {
49+
public void Shutdown() {
4650
_channel.ShutdownAsync().Wait();
4751
}
4852
}

VisualPinball.Engine.Mpf/MpfSpawner.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
using System.IO;
1515
using System.Runtime.InteropServices;
1616
using System.Threading;
17-
using System.Threading.Tasks;
1817

1918
namespace VisualPinball.Engine.Mpf
2019
{
21-
public class MpfSpawner
20+
internal class MpfSpawner
2221
{
2322
private Thread _thread;
2423
private readonly string _pwd;
@@ -32,7 +31,7 @@ public MpfSpawner(string machineFolder)
3231
_machineFolder = Path.GetFileName(machineFolder);
3332
}
3433

35-
public async Task Spawn()
34+
public void Spawn()
3635
{
3736
var mpfExe = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "mpf.exe" : "mpf";
3837
var mpfExePath = GetFullPath(mpfExe);
@@ -44,8 +43,9 @@ public async Task Spawn()
4443
Thread.CurrentThread.IsBackground = true;
4544
RunMpf(mpfExePath);
4645
});
46+
4747
_thread.Start();
48-
await _ready.WaitAsync();
48+
_ready.Wait();
4949
}
5050

5151
private void RunMpf(string mpfExePath)
@@ -58,22 +58,28 @@ private void RunMpf(string mpfExePath)
5858
RedirectStandardOutput = true,
5959
};
6060

61-
using (var process = Process.Start(info)) {
62-
using (var reader = process.StandardOutput) {
63-
_ready.Release();
64-
var result = reader.ReadToEnd();
65-
Console.Write(result);
66-
process.WaitForExit();
67-
}
61+
using (var process = Process.Start(info))
62+
using (var reader = process.StandardOutput) {
63+
_ready.Release();
64+
var result = reader.ReadToEnd();
65+
Console.Write(result);
66+
process.WaitForExit();
6867
}
6968
}
7069

70+
/// <summary>
71+
/// Goes through the OS's PATHs to find the provided executable.
72+
/// </summary>
73+
/// <param name="fileName">Executable filename</param>
74+
/// <returns>Full path or null of not found.</returns>
7175
private static string GetFullPath(string fileName)
7276
{
77+
// in current working directory?
7378
if (File.Exists(fileName)) {
7479
return Path.GetFullPath(fileName);
7580
}
7681

82+
// go through all PATHs
7783
var values = Environment.GetEnvironmentVariable("PATH");
7884
foreach (var path in values.Split(Path.PathSeparator)) {
7985
var fullPath = Path.Combine(path, fileName);

0 commit comments

Comments
 (0)