Skip to content

Commit c1a9e92

Browse files
committed
spawner: Make command line options configurable.
1 parent e4d78c2 commit c1a9e92

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

VisualPinball.Engine.Mpf.Test/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static async Task Main(string[] args)
3838
var s = Stopwatch.StartNew();
3939
var mpfApi = new MpfApi(machineFolder);
4040

41-
mpfApi.Launch();
41+
mpfApi.Launch(new MpfConsoleOptions { ShowLogInsteadOfConsole = false });
4242

4343
mpfApi.StartGame(new Dictionary<string, bool> {
4444
{"sw_11", false},
@@ -60,9 +60,6 @@ public static async Task Main(string[] args)
6060
};
6161
mpfApi.Client.OnFadeLight += (_, request) => {
6262
Console.WriteLine($"[MPF] light fades ({request.CommonFadeMs}ms):");
63-
foreach (var fade in request.Fades.ToList()) {
64-
Console.WriteLine($" l{fade.LightNumber} @{fade.TargetBrightness}");
65-
}
6663
};
6764

6865
var descr = mpfApi.GetMachineDescription();
@@ -76,7 +73,7 @@ public static async Task Main(string[] args)
7673
await mpfApi.Switch("0", true);
7774
break;
7875
case ConsoleKey.S:
79-
await mpfApi.Switch("1", false);
76+
await mpfApi.Switch("0", false);
8077
break;
8178
}
8279
} while (key.Key != ConsoleKey.Escape);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
6868
_coilNames[coil.InternalId.ToString()] = coil.Id;
6969
}
7070
_api = new MpfApi(machineFolder);
71-
_api.Launch();
71+
_api.Launch(new MpfConsoleOptions {
72+
ShowLogInsteadOfConsole = false,
73+
VerboseLogging = true,
74+
UseMediaController = false,
75+
});
7276

7377
_api.Client.OnEnableCoil += OnEnableCoil;
7478
_api.Client.OnDisableCoil += OnDisableCoil;

VisualPinball.Engine.Mpf/MpfApi.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static MachineDescription GetMachineDescription(string machineFolder)
3131
{
3232
var client = new MpfClient();
3333
var spawner = new MpfSpawner(machineFolder);
34-
spawner.Spawn();
34+
spawner.Spawn(new MpfConsoleOptions());
3535
client.Connect("localhost:50051");
3636
client.StartGame(new Dictionary<string, bool>(), false);
3737
var description = client.GetMachineDescription();
@@ -42,11 +42,12 @@ public static MachineDescription GetMachineDescription(string machineFolder)
4242
/// <summary>
4343
/// Launches MPF in the background and connects to it via gRPC.
4444
/// </summary>
45+
/// <param name="options">MPF options</param>
4546
/// <param name="port">gRPC port to use for MPC/VPE communication</param>
4647
/// <returns></returns>
47-
public void Launch(int port = 50051)
48+
public void Launch(MpfConsoleOptions options, int port = 50051)
4849
{
49-
_spawner.Spawn();
50+
_spawner.Spawn(options);
5051
Client.Connect($"localhost:{port}");
5152
}
5253

VisualPinball.Engine.Mpf/MpfSpawner.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MpfSpawner(string machineFolder)
3131
_machineFolder = Path.GetFileName(machineFolder);
3232
}
3333

34-
public void Spawn()
34+
public void Spawn(MpfConsoleOptions options)
3535
{
3636
var mpfExe = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "mpf.exe" : "mpf";
3737
var mpfExePath = GetFullPath(mpfExe);
@@ -41,28 +41,36 @@ public void Spawn()
4141

4242
_thread = new Thread(() => {
4343
Thread.CurrentThread.IsBackground = true;
44-
RunMpf(mpfExePath);
44+
RunMpf(mpfExePath, options);
4545
});
4646

4747
_thread.Start();
4848
_ready.Wait();
4949
}
5050

51-
private void RunMpf(string mpfExePath)
51+
private void RunMpf(string mpfExePath, MpfConsoleOptions options)
5252
{
53+
var args = $"\"{_machineFolder}\"";
54+
if (!options.UseMediaController) {
55+
args += " -b";
56+
}
57+
if (options.ShowLogInsteadOfConsole) {
58+
args += " -t";
59+
}
60+
if (options.VerboseLogging) {
61+
args += " -v -V";
62+
}
5363
var info = new ProcessStartInfo {
5464
FileName = mpfExePath,
5565
WorkingDirectory = _pwd,
56-
Arguments = $"\"{_machineFolder}\" -t -v -V -b",
57-
UseShellExecute = false,
58-
RedirectStandardOutput = true,
66+
Arguments = args,
67+
UseShellExecute = true,
68+
RedirectStandardOutput = false,
69+
5970
};
6071

61-
using (var process = Process.Start(info))
62-
using (var reader = process.StandardOutput) {
72+
using (var process = Process.Start(info)) {
6373
_ready.Release();
64-
var result = reader.ReadToEnd();
65-
Console.Write(result);
6674
process.WaitForExit();
6775
}
6876
}
@@ -90,4 +98,16 @@ private static string GetFullPath(string fileName)
9098
return null;
9199
}
92100
}
101+
102+
/// <summary>
103+
/// A few things we can configure when launching MPF
104+
/// <seealso cref="https://docs.missionpinball.org/en/latest/running/commands/game.html">Documentation</seealso>
105+
///
106+
/// </summary>
107+
public class MpfConsoleOptions
108+
{
109+
public bool UseMediaController;
110+
public bool ShowLogInsteadOfConsole;
111+
public bool VerboseLogging = true;
112+
}
93113
}

0 commit comments

Comments
 (0)