Skip to content

Commit 11cc641

Browse files
committed
Show dialogs if executable is not found or not set
1 parent 61c4c26 commit 11cc641

File tree

4 files changed

+92
-58
lines changed

4 files changed

+92
-58
lines changed

GameAwaiter.csproj

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<PublishAot>true</PublishAot>
9+
<UseSystemResourceKeys>true</UseSystemResourceKeys>
10+
<InvariantGlobalization>true</InvariantGlobalization>
11+
<PublishLzmaCompressed>true</PublishLzmaCompressed>
912
</PropertyGroup>
1013

14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="PublishAotCompressed" Version="1.0.3" />
19+
</ItemGroup>
20+
1121
</Project>

NativeMethods.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MessageBox

Program.cs

+79-56
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,94 @@
11
using System.Diagnostics;
2+
using Windows.Win32;
3+
using Windows.Win32.Foundation;
4+
using Windows.Win32.UI.WindowsAndMessaging;
25

36
namespace GameAwaiter
47
{
5-
public static class Program
6-
{
8+
public static class Program
9+
{
710

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+
}
1525

16-
var app = args[0];
17-
var delay = args.Length >= 2 ? int.Parse(args[1]) : 5000;
26+
var app = args[0];
1827

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+
}
2139

22-
Console.WriteLine($"Starting: {app}");
40+
var delay = args.Length >= 2 ? int.Parse(args[1]) : 5000;
2341

24-
Thread.Sleep(delay);
42+
if (WaitForNewProcess(app))
43+
return 0;
2544

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}");
3646

37-
WaitForNewProcess(app);
47+
Thread.Sleep(delay);
3848

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+
}
4159

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);
5861

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+
}
7194
}

Properties/PublishProfiles/Build.pubxml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
99
<PublishDir>bin\Release\publish\</PublishDir>
1010
<PublishProtocol>FileSystem</PublishProtocol>
1111
<_TargetId>Folder</_TargetId>
12-
<TargetFramework>net7.0</TargetFramework>
12+
<TargetFramework>net8.0</TargetFramework>
1313
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1414
<SelfContained>true</SelfContained>
1515
<PublishSingleFile>false</PublishSingleFile>

0 commit comments

Comments
 (0)