Skip to content

Commit

Permalink
Merge pull request #34 from ccloli/feature/restart-detect
Browse files Browse the repository at this point in the history
Check user id to determine if Steam has been restarted
  • Loading branch information
ErisApps authored Jul 1, 2023
2 parents 4f8359e + 74dbd98 commit 4555df6
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions LegacyInstaller/SteamProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public SteamShortcuts Shortcuts
public readonly string InstallDir;
public readonly string SteamExecutable;

private readonly string[] MainWindowTitles = new[] { "Steam", "Servers", "Friends", "Default IME" };

public SteamProcess(string installDir)
{
InstallDir = installDir;
Expand Down Expand Up @@ -73,7 +71,6 @@ await Task.Run(() =>


private const int MainWindowPollTime = 500;
private const int WM_GETTEXT = 0x000D;

[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
Expand All @@ -84,29 +81,23 @@ await Task.Run(() =>

public async Task WaitForMainWindow()
{
List<string> windowTitles = new List<string>();
while (!MainWindowTitles.All(title => windowTitles.Contains(title)))
// when Steam restarts, the user id still remains
while (Utilities.GetCurrentSteamUser() != 0)
{
if (Process == null)
continue;

await Task.Delay(MainWindowPollTime);
}
// when Steam logs in, the user id is reset to 0
while (Utilities.GetCurrentSteamUser() == 0)
{
if (Process == null)
continue;

await Task.Run(() =>
{
Process.Refresh();
var handles = new List<IntPtr>();
foreach (ProcessThread thread in Process.Threads)
EnumThreadWindows(thread.Id,
(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
foreach (var handle in handles)
{
StringBuilder message = new StringBuilder(1000);
SendMessage(handle, WM_GETTEXT, message.Capacity, message);
if (!string.IsNullOrEmpty(message.ToString()))
windowTitles.Add(message.ToString());
}
});
await Task.Delay(MainWindowPollTime);
}
// when login is successful and window is loaded, the user id is set
}
}
}

0 comments on commit 4555df6

Please sign in to comment.