|
1 | 1 | using System.Diagnostics;
|
| 2 | +using Windows.Win32; |
| 3 | +using Windows.Win32.Foundation; |
| 4 | +using Windows.Win32.UI.WindowsAndMessaging; |
2 | 5 |
|
3 | 6 | namespace GameAwaiter
|
4 | 7 | {
|
5 |
| - public static class Program |
6 |
| - { |
| 8 | + public static class Program |
| 9 | + { |
7 | 10 |
|
8 |
| - public static int Main(string[] args) |
9 |
| - { |
10 |
| - if (args.Length == 0) |
11 |
| - { |
12 |
| - Console.WriteLine("Path not set"); |
13 |
| - return -1; |
14 |
| - } |
| 11 | + public static int Main(string[] args) |
| 12 | + { |
| 13 | + if (args.Length == 0) |
| 14 | + { |
| 15 | + Console.WriteLine("Path not set"); |
| 16 | + if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10240, 0)) |
| 17 | + PInvoke.MessageBox(new HWND(0), "Path not set", "GameAwaiter", MESSAGEBOX_STYLE.MB_OK); |
| 18 | + else |
| 19 | + { |
| 20 | + Console.WriteLine("Path not set"); |
| 21 | + Console.ReadKey(); |
| 22 | + } |
| 23 | + return -1; |
| 24 | + } |
15 | 25 |
|
16 |
| - var app = args[0]; |
17 |
| - var delay = args.Length >= 2 ? int.Parse(args[1]) : 5000; |
| 26 | + var app = args[0]; |
18 | 27 |
|
19 |
| - if (WaitForNewProcess(app)) |
20 |
| - return 0; |
| 28 | + if (!File.Exists(app)) |
| 29 | + { |
| 30 | + if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10240, 0)) |
| 31 | + PInvoke.MessageBox(new HWND(0), $"Executable not found at \"{app}\"", "GameAwaiter", MESSAGEBOX_STYLE.MB_OK); |
| 32 | + else |
| 33 | + { |
| 34 | + Console.WriteLine($"Executable not found at \"{app}\""); |
| 35 | + Console.ReadKey(); |
| 36 | + } |
| 37 | + return -1; |
| 38 | + } |
21 | 39 |
|
22 |
| - Console.WriteLine($"Starting: {app}"); |
| 40 | + var delay = args.Length >= 2 ? int.Parse(args[1]) : 5000; |
23 | 41 |
|
24 |
| - Thread.Sleep(delay); |
| 42 | + if (WaitForNewProcess(app)) |
| 43 | + return 0; |
25 | 44 |
|
26 |
| - using (var p = new Process()) |
27 |
| - { |
28 |
| - p.StartInfo.Verb = "runas"; |
29 |
| - p.StartInfo.FileName = app; |
30 |
| - p.StartInfo.WorkingDirectory = Path.GetDirectoryName(app); |
31 |
| - p.StartInfo.UseShellExecute = true; |
32 |
| - p.Start(); |
33 |
| - Console.WriteLine($"PID: {p.Id}"); |
34 |
| - p.WaitForExit(); |
35 |
| - } |
| 45 | + Console.WriteLine($"Starting: {app}"); |
36 | 46 |
|
37 |
| - WaitForNewProcess(app); |
| 47 | + Thread.Sleep(delay); |
38 | 48 |
|
39 |
| - return 0; |
40 |
| - } |
| 49 | + using (var p = new Process()) |
| 50 | + { |
| 51 | + p.StartInfo.Verb = "runas"; |
| 52 | + p.StartInfo.FileName = app; |
| 53 | + p.StartInfo.WorkingDirectory = Path.GetDirectoryName(app); |
| 54 | + p.StartInfo.UseShellExecute = true; |
| 55 | + p.Start(); |
| 56 | + Console.WriteLine($"PID: {p.Id}"); |
| 57 | + p.WaitForExit(); |
| 58 | + } |
41 | 59 |
|
42 |
| - private static bool WaitForNewProcess(string app) |
43 |
| - { |
44 |
| - using (var process1 = GetNewProcess(app)) |
45 |
| - if (process1 == null) |
46 |
| - return false; |
47 |
| - while (true) |
48 |
| - { |
49 |
| - using (var process2 = GetNewProcess(app)) |
50 |
| - { |
51 |
| - if (process2 == null) |
52 |
| - return true; |
53 |
| - Console.WriteLine($"PID: {process2.Id}"); |
54 |
| - process2.WaitForExit(); |
55 |
| - } |
56 |
| - } |
57 |
| - } |
| 60 | + WaitForNewProcess(app); |
58 | 61 |
|
59 |
| - private static Process? GetNewProcess(string app) |
60 |
| - { |
61 |
| - var processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(app)); |
62 |
| - if (processes.Length != 1) |
63 |
| - { |
64 |
| - foreach (var p in processes) |
65 |
| - p.Dispose(); |
66 |
| - return null; |
67 |
| - } |
68 |
| - return processes[0]; |
69 |
| - } |
70 |
| - } |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + private static bool WaitForNewProcess(string app) |
| 66 | + { |
| 67 | + using (var process1 = GetNewProcess(app)) |
| 68 | + if (process1 == null) |
| 69 | + return false; |
| 70 | + while (true) |
| 71 | + { |
| 72 | + using (var process2 = GetNewProcess(app)) |
| 73 | + { |
| 74 | + if (process2 == null) |
| 75 | + return true; |
| 76 | + Console.WriteLine($"PID: {process2.Id}"); |
| 77 | + process2.WaitForExit(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static Process? GetNewProcess(string app) |
| 83 | + { |
| 84 | + var processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(app)); |
| 85 | + if (processes.Length != 1) |
| 86 | + { |
| 87 | + foreach (var p in processes) |
| 88 | + p.Dispose(); |
| 89 | + return null; |
| 90 | + } |
| 91 | + return processes[0]; |
| 92 | + } |
| 93 | + } |
71 | 94 | }
|
0 commit comments