Skip to content

Commit 4dd770a

Browse files
committed
release: Check if window is visible.
1 parent 6c1f8ef commit 4dd770a

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

src/Flarial.Launcher.Runtime/Flarial.Launcher.Runtime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<PlatformTarget>x64</PlatformTarget>
5-
<TargetFramework>net481</TargetFramework>
5+
<TargetFramework>net48</TargetFramework>
66

77
<Nullable>enable</Nullable>
88
<LangVersion>latest</LangVersion>

src/Flarial.Launcher.Runtime/Game/MinecraftGDK.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Management.Automation;
55
using System.Management.Automation.Runspaces;
6+
using System.Threading;
67
using Flarial.Launcher.Runtime.Services;
78
using Windows.ApplicationModel;
89
using Windows.Win32.Foundation;
@@ -36,14 +37,14 @@ static MinecraftGDK()
3637

3738
protected override uint? Activate()
3839
{
39-
if (GetProcessId() is { } processId)
40-
return processId;
41-
4240
/*
4341
- We use PowerShell to directly start the game.
4442
- This simplifies the activation contract.
4543
*/
4644

45+
if (GetProcessId() is { } processId)
46+
return processId;
47+
4748
using var powershell = PowerShell.Create(s_state);
4849
powershell.AddCommand("Invoke-CommandInDesktopPackage");
4950

@@ -68,14 +69,17 @@ static MinecraftGDK()
6869
if (!IsGamingServicesInstalled)
6970
throw new Win32Exception((int)ERROR_INSTALL_PREREQUISITE_FAILED);
7071

71-
if (GetWindow() is { } target)
72+
if (GetWindow() is { } @_ && _.IsVisible)
7273
{
73-
target.Switch();
74-
return target.ProcessId;
74+
_.Switch();
75+
return _.ProcessId;
7576
}
7677

77-
if (Activate() is not { } processId) return null;
78-
if (Open(PROCESS_SYNCHRONIZE, processId) is not { } process) return null;
78+
if (Activate() is not { } processId)
79+
return null;
80+
81+
if (Open(PROCESS_SYNCHRONIZE, processId) is not { } process)
82+
return null;
7983

8084
using (process)
8185
{
@@ -89,9 +93,14 @@ static MinecraftGDK()
8993
{
9094
while (process.Wait(1))
9195
{
92-
if (GetWindow() is not { } instance) continue;
93-
if (instance.ProcessId != processId) continue;
94-
if (instance.IsVisible) return processId;
96+
if (GetWindow() is not { } window)
97+
continue;
98+
99+
if (window.IsVisible)
100+
return window.ProcessId;
101+
102+
if (window.ProcessId != processId)
103+
return processId;
95104
}
96105
return null;
97106
}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1+
using System.Runtime.InteropServices;
12
using Windows.Win32.Foundation;
23
using static Windows.Win32.PInvoke;
34

45
namespace Flarial.Launcher.Runtime.System;
56

6-
unsafe readonly struct NativeWindow
7+
unsafe readonly struct NativeWindow(HWND handle)
78
{
8-
readonly HWND _handle;
9-
10-
NativeWindow(HWND handle) => _handle = handle;
11-
12-
internal bool IsVisible => IsWindowVisible(_handle);
9+
readonly HWND _handle = handle;
1310

1411
internal void Switch() => SwitchToThisWindow(_handle, true);
1512

16-
internal uint ProcessId
17-
{
18-
get
19-
{
20-
uint processId;
21-
GetWindowThreadProcessId(_handle, &processId);
22-
return processId;
23-
}
24-
}
13+
internal bool IsVisible => IsWindowVisible(_handle);
14+
internal uint ProcessId { get { uint _; GetWindowThreadProcessId(_handle, &_); return _; } }
2515

2616
public static implicit operator NativeWindow(in HWND hwnd) => new(hwnd);
27-
2817
public static implicit operator HWND(in NativeWindow window) => window._handle;
2918
}

src/Flarial.Launcher/Flarial.Launcher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
55
<PlatformTarget>x64</PlatformTarget>
6-
<TargetFramework>net481</TargetFramework>
6+
<TargetFramework>net48</TargetFramework>
77

88
<UseWPF>true</UseWPF>
99
<ApplicationIcon>Resources\Application.ico</ApplicationIcon>

src/Windows.Win32/Windows.Win32.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<PlatformTarget>x64</PlatformTarget>
4-
<TargetFramework>net481</TargetFramework>
4+
<TargetFramework>net48</TargetFramework>
55

66
<Optimize>true</Optimize>
77
<PublishRelease>true</PublishRelease>

0 commit comments

Comments
 (0)