From 1fb9e061a3334d492540fdb4c67dd8b3c5a47dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 26 Aug 2020 21:11:27 +0200 Subject: [PATCH] fix: wait for main nostale window to open --- NosTaleGfless/NostaleLauncher.cs | 29 +++++++++++++++++++++++++++++ NosTaleGfless/User32.cs | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 NosTaleGfless/User32.cs diff --git a/NosTaleGfless/NostaleLauncher.cs b/NosTaleGfless/NostaleLauncher.cs index 518db315f8101494f9577ea2c430807fb5e5ef7f..710efc2b4b1867a335a03cb83efedf7822acf366 100644 --- a/NosTaleGfless/NostaleLauncher.cs +++ b/NosTaleGfless/NostaleLauncher.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; +using System.Data; using System.Diagnostics; using System.IO; using System.Threading; @@ -27,6 +29,33 @@ namespace NosTaleGfless NostaleProcess process = RunProcess(account, nostalePath); process.Initialized = ProcessPipeServer(process, sessionToken).GetAwaiter().GetResult(); + if (process.Initialized) + { + while (true) + { + List handles = new List(); + + // wait for main window open + User32.EnumDesktopWindows(IntPtr.Zero, (hWnd, lParam) => + { + User32.GetWindowThreadProcessId(hWnd, out uint processId); + if (processId == process.ProcessId) + { + handles.Add(hWnd); + } + + return true; + }, IntPtr.Zero); + + if (handles.Count > 6) + { + break; + } + + Task.Delay(50).GetAwaiter().GetResult(); + } + } + return process; } }); diff --git a/NosTaleGfless/User32.cs b/NosTaleGfless/User32.cs new file mode 100644 index 0000000000000000000000000000000000000000..e46beedb6c1ed1e035b33f4145b0458181d49436 --- /dev/null +++ b/NosTaleGfless/User32.cs @@ -0,0 +1,28 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace NosTaleGfless +{ + public class User32 + { + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool IsWindowVisible(IntPtr hWnd); + + [DllImport("user32.dll", EntryPoint = "GetWindowText", + ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] + public static extern int GetWindowText(IntPtr hWnd, + StringBuilder lpWindowText, int nMaxCount); + + [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", + ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] + public static extern bool EnumDesktopWindows(IntPtr hDesktop, + EnumDelegate lpEnumCallbackFunction, IntPtr lParam); + + [DllImport("user32.dll", SetLastError=true)] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); + + public delegate bool EnumDelegate(IntPtr hWnd, int lParam); + } +} \ No newline at end of file