Skip to content

Commit 53fc08b

Browse files
committed
Simplify readychecker (#1457)
* Fix windows ready checker (Closes #1456) * Add extra debugging line * Extra debug info
1 parent 4077777 commit 53fc08b

File tree

2 files changed

+2
-82
lines changed

2 files changed

+2
-82
lines changed

Daybreak.Core/Services/ApplicationLauncher/ApplicationLauncher.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ is null
439439
return (default, []);
440440
}
441441

442+
scopedLogger.LogDebug("Waiting for Guild Wars to be ready...");
442443
var sw = Stopwatch.StartNew();
443444
var isReady = await this.guildWarsReadyChecker.WaitForReady(
444445
process,

Daybreak.Windows/Services/ApplicationLauncher/GuildWarsReadyChecker.cs

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
using System.Core.Extensions;
44
using System.Diagnostics;
55
using System.Extensions.Core;
6-
using System.Runtime.InteropServices;
7-
using System.Text;
8-
using static Daybreak.Windows.Utils.NativeMethods;
96

107
namespace Daybreak.Windows.Services.ApplicationLauncher;
118

@@ -17,8 +14,6 @@ internal sealed class GuildWarsReadyChecker(
1714
ILogger<GuildWarsReadyChecker> logger
1815
) : IGuildWarsReadyChecker
1916
{
20-
private const double LaunchMemoryThreshold = 200000000;
21-
2217
private readonly ILogger<GuildWarsReadyChecker> logger = logger.ThrowIfNull();
2318

2419
public async Task<bool> WaitForReady(Process process, TimeSpan timeout, CancellationToken cancellationToken)
@@ -41,87 +36,11 @@ public async Task<bool> WaitForReady(Process process, TimeSpan timeout, Cancella
4136
continue;
4237
}
4338

44-
var windows = GetRootWindowsOfProcess(process.Id)
45-
.Select(root => (root, GetChildWindows(root)))
46-
.SelectMany(tuple =>
47-
{
48-
tuple.Item2.Add(tuple.root);
49-
return tuple.Item2;
50-
})
51-
.Select(GetWindowTitle).ToList();
52-
53-
/*
54-
* Detect when the game window has shown. Because both the updater and the game window are called Guild Wars,
55-
* we need to look at the other windows created by the process. Especially, we need to detect the input windows
56-
* to check when the game is ready to accept input
57-
*/
58-
if (!windows.Contains("Guild Wars Reforged"))
59-
{
60-
continue;
61-
}
62-
63-
var virtualMemory = process.VirtualMemorySize64;
64-
if (virtualMemory < LaunchMemoryThreshold)
65-
{
66-
continue;
67-
}
68-
39+
scopedLogger.LogInformation("Guild Wars process is ready and running");
6940
return true;
7041
}
7142

7243
scopedLogger.LogError("Timed out waiting for Guild Wars to be ready");
7344
return false;
7445
}
75-
76-
private static List<IntPtr> GetRootWindowsOfProcess(int pid)
77-
{
78-
var rootWindows = GetChildWindows(IntPtr.Zero);
79-
var dsProcRootWindows = new List<IntPtr>();
80-
foreach (IntPtr hWnd in rootWindows)
81-
{
82-
_ = GetWindowThreadProcessId(hWnd, out var lpdwProcessId);
83-
if (lpdwProcessId == pid)
84-
dsProcRootWindows.Add(hWnd);
85-
}
86-
87-
return dsProcRootWindows;
88-
}
89-
90-
private static List<IntPtr> GetChildWindows(IntPtr parent)
91-
{
92-
var result = new List<IntPtr>();
93-
var listHandle = GCHandle.Alloc(result);
94-
try
95-
{
96-
var childProc = new Win32Callback(EnumWindow);
97-
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
98-
}
99-
finally
100-
{
101-
if (listHandle.IsAllocated)
102-
listHandle.Free();
103-
}
104-
105-
return result;
106-
}
107-
108-
private static string GetWindowTitle(IntPtr hwnd)
109-
{
110-
var titleLength = GetWindowTextLength(hwnd);
111-
var titleBuffer = new StringBuilder(titleLength);
112-
_ = GetWindowText(hwnd, titleBuffer, titleLength + 1);
113-
return titleBuffer.ToString();
114-
}
115-
116-
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
117-
{
118-
var gch = GCHandle.FromIntPtr(pointer);
119-
if (gch.Target is not List<IntPtr> list)
120-
{
121-
return false;
122-
}
123-
124-
list.Add(handle);
125-
return true;
126-
}
12746
}

0 commit comments

Comments
 (0)