-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EngineServer.cs
63 lines (53 loc) · 2.02 KB
/
EngineServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Esperecyan.NCVVoicevox.Properties;
namespace Esperecyan.NCVVoicevox;
internal class EngineServer : IDisposable
{
/// <summary>
/// VOICEVOXエンジンの起動失敗時に標準エラーに含まれる文字列。
/// </summary>
private static readonly string EngineStartupErrorOutput = "Application startup failed.";
/// <summary>
/// <see cref="Dispose"/> が呼ばれる以外でVOICEVOXエンジンが終了したときに呼ばれるイベントハンドラ。
/// </summary>
internal event EventHandler<EventArgs> ExitedUnexpectedly = (_, _) => { };
private readonly Process process;
internal EngineServer(int port)
{
var info = new ProcessStartInfo(
Path.Join(Path.GetDirectoryName(Settings.Default.VoicevoxPath), Settings.Default.EngineFileRelativePath)
)
{
RedirectStandardError = true,
CreateNoWindow = true,
};
foreach (var argument in new[] { "--port", port.ToString() })
{
info.ArgumentList.Add(argument);
}
this.process = Process.Start(info) ?? throw new Exception("Process.Start() が null を返しました。");
this.process.EnableRaisingEvents = true;
this.process.Exited += this.EngineProcess_Exited;
AppDomain.CurrentDomain.UnhandledException += (_, _) => this.Dispose();
}
public void Dispose()
{
this.process.Exited -= this.EngineProcess_Exited;
this.process.Kill();
}
private void EngineProcess_Exited(object? sender, EventArgs e)
{
var error = this.process.StandardError.ReadToEnd();
if (error.Contains(EngineServer.EngineStartupErrorOutput))
{
MessageBox.Show(
$"「{Settings.Default.EngineFileRelativePath}」の起動に失敗しました。\n\n" + error,
App.Title
);
}
this.ExitedUnexpectedly(this, EventArgs.Empty);
}
}