Skip to content
This repository has been archived by the owner on May 15, 2022. It is now read-only.

Commit

Permalink
Use UIAutomation to detect when the friends list opens
Browse files Browse the repository at this point in the history
When it opens, check for an update to the friends css.

Track whether or not the css is in the process of updating, and if a new file is detected in this time wait until update is complete to process.

This should hopefully eliminate cases where the css would update while Steam is running and the patcher would miss it. Further testing needed.
  • Loading branch information
PhantomGamers committed Jun 12, 2019
1 parent c9dc5f3 commit a35ab97
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
56 changes: 51 additions & 5 deletions Program.cs
Expand Up @@ -18,6 +18,7 @@
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Automation;

namespace SteamFriendsPatcher
{
Expand Down Expand Up @@ -58,6 +59,8 @@ internal class Program
public static FileSystemWatcher crashWatcher;
public static FileStream cacheLock;
public static bool scannerExists = false;
public static bool friendslistWatcherExists = false;
public static bool updatePending = false;

private static List<string> pendingCacheFiles = new List<string>();

Expand Down Expand Up @@ -295,12 +298,13 @@ private static byte[] PrependFile(byte[] file)

public static bool GetLatestFriendsCSS(bool force = false)
{
if (DateTime.Now.Subtract(friendscssage).TotalMinutes < 5.0 && !force)
if ((DateTime.Now.Subtract(friendscssage).TotalMinutes < 5.0 && !force) || updatePending)
{
return true;
}
lock (GetFriendsCSSLock)
{
updatePending = true;
Print("Checking for latest friends.css...");
using (var wc = new WebClient())
{
Expand All @@ -317,6 +321,7 @@ public static bool GetLatestFriendsCSS(bool force = false)
if (!string.IsNullOrEmpty(etag) && !string.IsNullOrEmpty(friendscssetag) && etag == friendscssetag)
{
Print("friends.css is already up to date.");
updatePending = false;
return true;
}

Expand Down Expand Up @@ -354,6 +359,7 @@ public static bool GetLatestFriendsCSS(bool force = false)
friendscssage = DateTime.Now;
friendscssetag = etag;
Print("Successfully downloaded latest friends.css");
updatePending = false;
return true;
}
}
Expand All @@ -372,12 +378,14 @@ public static bool GetLatestFriendsCSS(bool force = false)
}
*/
Print("Failed to download friends.css", LogLevel.Error);
updatePending = false;
return false;
}
catch (WebException we)
{
Print("Failed to download friends.css.", LogLevel.Error);
Print(we.ToString(), LogLevel.Error);
updatePending = false;
return false;
}
}
Expand Down Expand Up @@ -432,7 +440,6 @@ public static void ToggleCacheScanner(bool isEnabled)
cacheWatcher.EnableRaisingEvents = isEnabled;
crashWatcher.EnableRaisingEvents = isEnabled;
scannerExists = isEnabled;
Print("Cache Watcher " + (isEnabled ? "Started" : "Stopped") + ".");
if (!isEnabled)
{
cacheLock.Dispose();
Expand All @@ -441,6 +448,9 @@ public static void ToggleCacheScanner(bool isEnabled)
File.Delete(Path.Combine(steamCacheDir, "tmp.lock"));
}
}
Automation.RemoveAllEventHandlers();
friendslistWatcherExists = false;
Print("Cache Watcher " + (isEnabled ? "Started" : "Stopped") + ".");
Main.ToggleButtons(true);
return;
}
Expand Down Expand Up @@ -471,6 +481,8 @@ public static void ToggleCacheScanner(bool isEnabled)
return;
}

StartFriendsListWatcher();

StartCrashScanner();

cacheWatcher = new FileSystemWatcher
Expand All @@ -497,7 +509,7 @@ public static void ToggleCacheScanner(bool isEnabled)

private static void CacheWatcher_Changed(object sender, FileSystemEventArgs e)
{
if (pendingCacheFiles.Contains(e.Name) || friendscss == null || new System.IO.FileInfo(e.FullPath).Length != friendscss.Length)
if (pendingCacheFiles.Contains(e.Name) || (!updatePending && (friendscss == null || new System.IO.FileInfo(e.FullPath).Length != friendscss.Length)))
return;
pendingCacheFiles.Add(e.Name);
Thread t = new Thread(new ParameterizedThreadStart(ProcessCacheFile));
Expand All @@ -506,6 +518,10 @@ private static void CacheWatcher_Changed(object sender, FileSystemEventArgs e)

private static void ProcessCacheFile(object obj)
{
while (updatePending)
{
Task.Delay(TimeSpan.FromMilliseconds(20)).Wait();
}
FileSystemEventArgs e = (FileSystemEventArgs)obj;
Print($"New file found: {e.Name}", LogLevel.Debug);
DateTime lastAccess, lastWrite;
Expand Down Expand Up @@ -644,6 +660,7 @@ private static void StartCrashScanner()
};
crashWatcher.Created += new FileSystemEventHandler(CrashWatcher_Event);
crashWatcher.Changed += new FileSystemEventHandler(CrashWatcher_Event);
crashWatcher.Deleted += new FileSystemEventHandler(CrashWatcher_Event);

crashWatcher.EnableRaisingEvents = true;

Expand All @@ -652,8 +669,37 @@ private static void StartCrashScanner()

private static void CrashWatcher_Event(object sender, FileSystemEventArgs e)
{
Print("Steam start detected.", LogLevel.Debug);
GetLatestFriendsCSS();
if (e.ChangeType == WatcherChangeTypes.Changed || e.ChangeType == WatcherChangeTypes.Created)
{
Print("Steam start detected.", LogLevel.Debug);
GetLatestFriendsCSS();
if (scannerExists)
{
StartFriendsListWatcher();
}
}
else if (e.ChangeType == WatcherChangeTypes.Deleted)
{
Print("Steam graceful shutdown detected.", LogLevel.Debug);
friendslistWatcherExists = false;
Automation.RemoveAllEventHandlers();
}
}

private static void StartFriendsListWatcher()
{
if (!friendslistWatcherExists && Process.GetProcessesByName("Steam").FirstOrDefault() != null)
{
friendslistWatcherExists = true;
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, (sender, e) =>
{
var element = sender as AutomationElement;
if (element.Current.ClassName == "SDL_app")
{
GetLatestFriendsCSS();
}
});
}
}

public static void ClearSteamCache()
Expand Down
2 changes: 2 additions & 0 deletions SteamFriendsPatcher.csproj
Expand Up @@ -80,6 +80,8 @@
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="UIAutomationClient" />
<Reference Include="UIAutomationTypes" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down

0 comments on commit a35ab97

Please sign in to comment.