feat(classifier): check to make sure game is alive
This commit is contained in:
parent
8eb0507041
commit
72494ebcee
|
@ -5,6 +5,7 @@
|
|||
<TargetFramework>net48</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>1.1.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -22,7 +23,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.ML" Version="1.5.4" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="costura64\*.dll" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using JKang.IpcServiceFramework.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
@ -9,8 +11,15 @@ using NoSoliciting.Internal.Interface;
|
|||
|
||||
namespace NoSoliciting.MessageClassifier {
|
||||
internal static class Program {
|
||||
private static void Main() {
|
||||
Host.CreateDefaultBuilder()
|
||||
private static void Main(string[] args) {
|
||||
if (!int.TryParse(args[0], out var gamePid)) {
|
||||
Console.WriteLine("No game PID provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
var gameName = args[1];
|
||||
|
||||
var host = Host.CreateDefaultBuilder()
|
||||
.ConfigureServices(services => {
|
||||
services.AddSingleton<IClassifier, ClassifierService>();
|
||||
})
|
||||
|
@ -20,8 +29,28 @@ namespace NoSoliciting.MessageClassifier {
|
|||
options.Serializer = new BetterIpcSerialiser();
|
||||
});
|
||||
})
|
||||
.Build()
|
||||
.Run();
|
||||
.Build();
|
||||
|
||||
Task.Run(async () => {
|
||||
while (true) {
|
||||
Process process;
|
||||
try {
|
||||
process = Process.GetProcessById(gamePid);
|
||||
} catch (Exception) {
|
||||
await host.StopAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
if (process.ProcessName != gameName) {
|
||||
await host.StopAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(5_000);
|
||||
}
|
||||
});
|
||||
|
||||
host.Run();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -105,9 +105,12 @@ namespace NoSoliciting.Ml {
|
|||
}
|
||||
|
||||
private static Process StartClassifier(string exePath, string pidPath) {
|
||||
var game = Process.GetCurrentProcess();
|
||||
|
||||
var startInfo = new ProcessStartInfo(exePath) {
|
||||
CreateNoWindow = true,
|
||||
UseShellExecute = false,
|
||||
Arguments = $"{game.Id} \"{game.ProcessName}\"",
|
||||
};
|
||||
var process = Process.Start(startInfo);
|
||||
File.WriteAllText(pidPath, process!.Id.ToString());
|
||||
|
@ -135,6 +138,10 @@ namespace NoSoliciting.Ml {
|
|||
|
||||
try {
|
||||
var old = Process.GetProcessById(pid);
|
||||
if (old.ProcessName != "NoSoliciting.MessageClassifier.exe") {
|
||||
return;
|
||||
}
|
||||
|
||||
old.Kill();
|
||||
old.WaitForExit();
|
||||
} catch (ArgumentException) {
|
||||
|
@ -213,8 +220,12 @@ namespace NoSoliciting.Ml {
|
|||
}
|
||||
|
||||
public void Dispose() {
|
||||
this.Process.Kill();
|
||||
this.Process.Dispose();
|
||||
try {
|
||||
this.Process.Kill();
|
||||
this.Process.Dispose();
|
||||
} catch (Exception) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue