| 1 | using System.IO.Pipes; |
| 2 | using System.Security.Cryptography; |
| 3 | using System.Security.Principal; |
| 4 | using System.Text; |
| 5 | using System.Text.Json; |
| 6 | using System.Runtime.InteropServices; |
| 7 | |
| 8 | namespace Cage.Windows; |
| 9 | |
| 10 | internal static class Program |
| 11 | { |
| 12 | public static string? SmokeOutput { get; private set; } |
| 13 | [STAThread] |
| 14 | private static int Main(string[] args) |
| 15 | { |
| 16 | if (args is ["--install-update"]) return UpdateInstaller.Run().GetAwaiter().GetResult(); |
| 17 | if (args is ["--smoke-test", var output] && Path.IsPathFullyQualified(output)) |
| 18 | { |
| 19 | SmokeOutput = output; |
| 20 | if (File.Exists(output)) File.Delete(output); |
| 21 | } |
| 22 | else if (args.Length != 0) return 2; |
| 23 | AppDomain.CurrentDomain.UnhandledException += (_, e) => Backend.Report(e.ExceptionObject is Exception error ? error.ToString() : "Unhandled native exception").GetAwaiter().GetResult(); |
| 24 | Application.ThreadException += (_, e) => { |
| 25 | Backend.Report(e.Exception.ToString()).GetAwaiter().GetResult(); |
| 26 | using var dialog = new ThreadExceptionDialog(e.Exception); |
| 27 | if (dialog.ShowDialog() == DialogResult.Abort) Application.Exit(); |
| 28 | }; |
| 29 | TaskScheduler.UnobservedTaskException += (_, e) => _ = Backend.Report(e.Exception.ToString()); |
| 30 | ApplicationConfiguration.Initialize(); |
| 31 | if (RuntimeInformation.OSArchitecture != Architecture.X64 || Environment.OSVersion.Version.Build < 19045) |
| 32 | { |
| 33 | MessageBox.Show("Cage 需要 Windows 10 22H2(19045)或 Windows 11,x64(Intel/AMD)系统。", "Cage"); |
| 34 | return 1; |
| 35 | } |
| 36 | var sid = WindowsIdentity.GetCurrent().User?.Value ?? Environment.UserName; |
| 37 | var key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(sid + "\0" + Path.GetFullPath(Backend.Root).TrimEnd(Path.DirectorySeparatorChar).ToUpperInvariant())))[..24]; |
| 38 | using var mutex = new Mutex(true, @"Local\Cage-" + key, out var owner); |
| 39 | if (!owner) |
| 40 | { |
| 41 | if (SmokeOutput != null) return 3; |
| 42 | try |
| 43 | { |
| 44 | using var client = new NamedPipeClientStream(".", "Cage-" + key, PipeDirection.Out, PipeOptions.CurrentUserOnly); |
| 45 | client.Connect(3000); client.WriteByte(1); |
| 46 | } |
| 47 | catch (IOException) { MessageBox.Show("Cage 已在运行,但窗口暂时无法唤起。请从任务栏打开 Cage。", "Cage"); } |
| 48 | catch (TimeoutException) { MessageBox.Show("Cage 正在启动,请稍候。", "Cage"); } |
| 49 | return 0; |
| 50 | } |
| 51 | using var form = new MainWindow(); |
| 52 | using var cancellation = new CancellationTokenSource(); |
| 53 | _ = Task.Run(async () => |
| 54 | { |
| 55 | while (!cancellation.IsCancellationRequested) |
| 56 | { |
| 57 | try |
| 58 | { |
| 59 | await using var pipe = new NamedPipeServerStream("Cage-" + key, PipeDirection.In, 1, |
| 60 | PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); |
| 61 | await pipe.WaitForConnectionAsync(cancellation.Token); |
| 62 | var data = new byte[1]; |
| 63 | if (await pipe.ReadAsync(data, cancellation.Token) == 1 && data[0] == 1 && form.IsHandleCreated) |
| 64 | form.BeginInvoke(form.RestoreWindow); |
| 65 | } |
| 66 | catch (OperationCanceledException) { break; } |
| 67 | catch (IOException) { } |
| 68 | } |
| 69 | }); |
| 70 | Application.Run(form); cancellation.Cancel(); |
| 71 | if (SmokeOutput == null) return 0; |
| 72 | try |
| 73 | { |
| 74 | using var report = JsonDocument.Parse(File.ReadAllText(SmokeOutput)); |
| 75 | return report.RootElement.TryGetProperty("success", out var success) && success.ValueKind == JsonValueKind.True ? 0 : 1; |
| 76 | } |
| 77 | catch (Exception error) when (error is IOException or UnauthorizedAccessException or JsonException) { return 1; } |
| 78 | } |
| 79 | } |