From 6dc68de16903a2cd7185f1ec9c05d2f3aecd8229 Mon Sep 17 00:00:00 2001 From: Christopher Kaliszewski Date: Tue, 6 Dec 2022 11:41:01 -0500 Subject: [PATCH] Keep MS Edge and its web driver versions in sync --- PlexAutoIntroSkip.csproj | 1 + Program.cs | 20 +++++------- ProgramOptions.cs | 3 -- ReadMe.md | 5 +-- WebDriverManager.cs | 68 +++++++++++++++++++++++++--------------- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/PlexAutoIntroSkip.csproj b/PlexAutoIntroSkip.csproj index 7bef17d..f173547 100644 --- a/PlexAutoIntroSkip.csproj +++ b/PlexAutoIntroSkip.csproj @@ -15,6 +15,7 @@ + diff --git a/Program.cs b/Program.cs index 4962af4..fc076c5 100644 --- a/Program.cs +++ b/Program.cs @@ -40,7 +40,12 @@ public static void Main(string[] args) { var options = GetProgramOptions(args); - HandleConsoleWindow(options.ShowConsoleWindow); + // Was called using own process window? + var hWnd = Process.GetCurrentProcess().MainWindowHandle; + if (hWnd.ToInt32() != 0) + { + ShowWindow(hWnd, 0); + } var edgeOptions = new EdgeOptions { @@ -58,7 +63,7 @@ public static void Main(string[] args) try { - WebDriverManager.AutoUpdate(); + WebDriverManager.AutoInstall(); } catch (Exception exception) { @@ -91,17 +96,6 @@ public static void Main(string[] args) Process.GetProcessById(service.ProcessId).Kill(); } - private static void HandleConsoleWindow(bool showConsoleWindow) - { - var hWnd = Process.GetCurrentProcess().MainWindowHandle; - - // Hide console window and called using own process window? - if (showConsoleWindow == false && hWnd.ToInt32() != 0) - { - ShowWindow(hWnd, 0); - } - } - /// /// Run main program loop. /// diff --git a/ProgramOptions.cs b/ProgramOptions.cs index 9b81ed4..a7f99b7 100644 --- a/ProgramOptions.cs +++ b/ProgramOptions.cs @@ -4,9 +4,6 @@ namespace PlexAutoIntroSkip { public class ProgramOptions { - [Option('d', "debug", Required = false, HelpText = "Show console window.")] - public bool ShowConsoleWindow { get; set; } - [Option('w', "wait-time", Required = false, Default = 3000, HelpText = "Time to wait after Skip Button becomes visible before clicking.")] public int SkipButtonWaitTime { get; set; } diff --git a/ReadMe.md b/ReadMe.md index f14e9a3..bff6126 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -18,11 +18,8 @@ Plex has an great feature that allows users to [Skip TV Show Intros](https://sup ## Options -* `-d`, `--debug` **:** Show console window. -* `-m`, `--manual-driver` **:** Manually handle MS Edge Web Driver. - * If manually handling web driver the executable will need to be in environment's PATH or next to the `PlexAutoIntroSkip` executable. * `-w`, `--wait-time` **:** Time to wait, in milliseconds, after Skip Button becomes visible before clicking. - * **Default:** 2500 + * **Default:** 3000 ## Notes diff --git a/WebDriverManager.cs b/WebDriverManager.cs index 7c2a2f7..871366c 100644 --- a/WebDriverManager.cs +++ b/WebDriverManager.cs @@ -1,59 +1,77 @@ +using System; using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Net; using System.Text.RegularExpressions; +using Microsoft.Win32; namespace PlexAutoIntroSkip { public static class WebDriverManager { + /// + /// MS Edge web driver executable path. + /// public static readonly string MsEdgeDriverFilePath = Path.GetFullPath(@"./edgedriver_win64/msedgedriver.exe"); public static readonly string MsEdgeDriverDirectoryName = "edgedriver_win64"; public static readonly string MsEdgeDriverFileName = "msedgedriver.exe"; - private static readonly string _stableChannelLatestVersionAddress = "https://msedgedriver.azureedge.net/LATEST_STABLE"; - private static readonly string _stableChannelAddress = "https://msedgedriver.azureedge.net/<>/edgedriver_win64.zip"; - private static readonly Regex _msEdgeDriverVersionRegEx = new(@"(\d+\.\d+\.\d+)"); + private static readonly Regex _versionRegEx = new(@"^(\d+\.\d+\.\d+)"); - public static void AutoUpdate() + /// + /// Automatically download and update MS Edge web driver. + /// + public static void AutoInstall() { - var webClient = new WebClient(); - var httpStream = webClient.OpenRead(_stableChannelLatestVersionAddress); - var streamReader = new StreamReader(httpStream); - var latestVersion = streamReader.ReadLine(); - - streamReader.Close(); - httpStream.Close(); + var msEdgeVersion = GetMsEdgeVersion(); + var webDriverVersion = File.Exists(MsEdgeDriverFilePath) ? GetMsEdgeDriverVersion() : string.Empty; - var currentVersion = string.Empty; - if (File.Exists(MsEdgeDriverFilePath)) + if (_versionRegEx.Match(msEdgeVersion).Value != webDriverVersion) { - currentVersion = GetMsEdgeDriverVersion(MsEdgeDriverFilePath); - } + var webClient = new WebClient(); - if (currentVersion != _msEdgeDriverVersionRegEx.Match(latestVersion).Value) - { var downloadFileName = $"./{MsEdgeDriverDirectoryName}.zip"; - webClient.DownloadFile(_stableChannelAddress.Replace("<>", latestVersion), downloadFileName); + webClient.DownloadFile(_stableChannelAddress.Replace("<>", msEdgeVersion), downloadFileName); ZipFile.ExtractToDirectory(downloadFileName, MsEdgeDriverDirectoryName, overwriteFiles: true); + + webClient.Dispose(); } + } - webClient.Dispose(); + /// + /// Get current version of installed MS Edge. + /// + /// Version of MS Edge. + private static string GetMsEdgeVersion() + { + if (OperatingSystem.IsWindows()) + { + return Registry.GetValue( + @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon", "version", null).ToString(); + } + else + { + throw new Exception("Unable to determine current version of MS Edge on non-Windows platforms."); + } } - private static string GetMsEdgeDriverVersion(string msEdgeDriverPath) + /// + /// Get current version of downloaded MS Edge web driver. + /// + /// Version of MS Edge web driver. + private static string GetMsEdgeDriverVersion() { var msEdgeDriver = new Process { StartInfo = new ProcessStartInfo { - FileName = msEdgeDriverPath, + FileName = MsEdgeDriverFilePath, Arguments = "--version", UseShellExecute = false, RedirectStandardOutput = true, @@ -62,12 +80,10 @@ private static string GetMsEdgeDriverVersion(string msEdgeDriverPath) }; msEdgeDriver.Start(); - while (msEdgeDriver.StandardOutput.EndOfStream == false) - { - return _msEdgeDriverVersionRegEx.Match(msEdgeDriver.StandardOutput.ReadLine()).Value; - } - return string.Empty; + return msEdgeDriver.StandardOutput.EndOfStream + ? string.Empty + : _versionRegEx.Match(msEdgeDriver.StandardOutput.ReadLine()).Value; } } }