Skip to content

Commit 52cdce4

Browse files
committed
api: Add static method to retrieve machine description only.
1 parent e6c6f54 commit 52cdce4

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

VisualPinball.Engine.Mpf.Test/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.Diagnostics;
15+
using System.IO;
1516
using System.Linq;
1617
using System.Threading.Tasks;
1718
using VisualPinball.Engine.Mpf;
@@ -22,6 +23,8 @@ public static class Program
2223
{
2324
public static async Task Main(string[] args)
2425
{
26+
var machineFolder = Path.GetFullPath(@"../../../../VisualPinball.Engine.Mpf/machine");
27+
2528
// Console.WriteLine("Starting...");
2629
// var client = new MpfClient();
2730
// client.Connect();
@@ -33,7 +36,7 @@ public static async Task Main(string[] args)
3336

3437

3538
var s = Stopwatch.StartNew();
36-
var mpfApi = new MpfApi(@"../../../../VisualPinball.Engine.Mpf/machine");
39+
var mpfApi = new MpfApi(machineFolder);
3740

3841
mpfApi.Launch();
3942

VisualPinball.Engine.Mpf.Unity/Editor/MpfGamelogicEngineInspector.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1010
// SOFTWARE.
1111

12+
using System.IO;
1213
using UnityEditor;
1314
using UnityEngine;
1415

@@ -37,7 +38,18 @@ public override void OnInspectorGUI()
3738
}
3839

3940
if (GUILayout.Button("Synchronize")) {
40-
_mpfEngine.GetMachineDescription();
41+
if (!Directory.Exists(_mpfEngine.MachineFolder)) {
42+
EditorUtility.DisplayDialog("Mission Pinball Framework", "Gotta choose a valid machine folder first!", "Okay");
43+
} else if (!Directory.Exists(Path.Combine(_mpfEngine.MachineFolder, "config"))) {
44+
EditorUtility.DisplayDialog("Mission Pinball Framework", $"{_mpfEngine.MachineFolder} doesn't seem a valid machine folder. We expect a \"config\" subfolder in there!", "Okay");
45+
} else {
46+
_mpfEngine.GetMachineDescription();
47+
}
48+
}
49+
50+
EditorGUILayout.LabelField("Switches", new GUIStyle(GUI.skin.label) { fontStyle = FontStyle.Bold });
51+
foreach (var switchDescription in _mpfEngine.MachineDescription.Switches) {
52+
EditorGUILayout.LabelField(switchDescription.HardwareNumber, switchDescription.Name);
4153
}
4254

4355

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using System;
1313
using System.Collections.Generic;
14+
using Mpf.Vpe;
1415
using UnityEngine;
1516
using VisualPinball.Engine.Game.Engines;
1617
using VisualPinball.Unity;
@@ -28,12 +29,11 @@ public class MpfGamelogicEngine : MonoBehaviour
2829

2930
public string MachineFolder;
3031

32+
public MachineDescription MachineDescription;
33+
3134
public void GetMachineDescription()
3235
{
33-
var client = new MpfClient();
34-
client.Connect();
35-
client.StartGame(new Dictionary<string, bool>());
36-
Debug.Log("Description = " + client.GetMachineDescription());
36+
MachineDescription = MpfApi.GetMachineDescription(MachineFolder);
3737
}
3838

3939
}

VisualPinball.Engine.Mpf/MpfApi.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ public MpfApi(string machineFolder)
2727
_spawner = new MpfSpawner(Path.GetFullPath(machineFolder));
2828
}
2929

30+
public static MachineDescription GetMachineDescription(string machineFolder)
31+
{
32+
var client = new MpfClient();
33+
var spawner = new MpfSpawner(machineFolder);
34+
spawner.Spawn();
35+
client.Connect("localhost:50051");
36+
client.StartGame(new Dictionary<string, bool>(), false);
37+
var description = client.GetMachineDescription();
38+
client.Shutdown();
39+
return description;
40+
}
41+
3042
/// <summary>
3143
/// Launches MPF in the background and connects to it via gRPC.
3244
/// </summary>

VisualPinball.Engine.Mpf/MpfClient.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,20 @@ public void Connect(string serverIpPort = "127.0.0.1:50051")
4646
_client = new MpfHardwareService.MpfHardwareServiceClient(_channel);
4747
}
4848

49-
public void StartGame(Dictionary<string, bool> initialSwitches)
49+
public void StartGame(Dictionary<string, bool> initialSwitches, bool handleStream = true)
5050
{
5151
var ms = new MachineState();
5252
foreach (var sw in initialSwitches.Keys) {
5353
ms.InitialSwitchStates.Add(sw, initialSwitches[sw]);
5454
}
5555

56-
_commandsThread = new Thread(() => ReceiveCommands(ms)) { IsBackground = true };
57-
_commandsThread.Start();
56+
Logger.Info("Starting client...");
57+
_commandStream = _client.Start(ms);
58+
59+
if (handleStream) {
60+
_commandsThread = new Thread(ReceiveCommands) { IsBackground = true };
61+
_commandsThread.Start();
62+
}
5863

5964
_switchStream = _client.SendSwitchChanges();
6065
}
@@ -65,18 +70,12 @@ await _switchStream.RequestStream.WriteAsync(new SwitchChanges
6570
{SwitchNumber = swName, SwitchState = swValue});
6671
}
6772

68-
private async void ReceiveCommands(MachineState ms)
73+
private async void ReceiveCommands()
6974
{
70-
Logger.Info("Starting client...");
71-
_commandStream = _client.Start(ms);
72-
7375
Logger.Info("Client started, retrieving commands...");
74-
var count = 0;
7576
while (await _commandStream.ResponseStream.MoveNext()) {
76-
7777
var commands = _commandStream.ResponseStream.Current;
7878
Logger.Info($"New command: {commands.CommandCase}");
79-
count++;
8079
switch (commands.CommandCase) {
8180
case Commands.CommandOneofCase.None:
8281
break;
@@ -110,9 +109,13 @@ public MachineDescription GetMachineDescription()
110109
return _client.GetMachineDescription(new EmptyRequest());
111110
}
112111

113-
public void Shutdown() {
114-
_commandStream.Dispose();
112+
public void Shutdown()
113+
{
114+
Logger.Info("Shutting down...");
115+
_client.Quit(new QuitRequest());
116+
_commandStream?.Dispose();
115117
_channel.ShutdownAsync().Wait();
118+
Logger.Info("All down.");
116119
}
117120
}
118121
}

0 commit comments

Comments
 (0)