From 2f065fdd629787c0b59afd033db95763526c50dc Mon Sep 17 00:00:00 2001 From: "@Noxis" Date: Tue, 29 Sep 2020 12:51:11 +0300 Subject: [PATCH 1/7] Test startup args parsing --- WowUp.WPF/App.xaml.cs | 67 ++++++++++++++++++++++-- WowUp.WPF/Properties/launchSettings.json | 8 +++ WowUp.WPF/Utilities/StartupOptions.cs | 22 ++++++++ WowUp.WPF/WowUp.WPF.csproj | 1 + 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 WowUp.WPF/Properties/launchSettings.json create mode 100644 WowUp.WPF/Utilities/StartupOptions.cs diff --git a/WowUp.WPF/App.xaml.cs b/WowUp.WPF/App.xaml.cs index 88e013fd..673539b6 100644 --- a/WowUp.WPF/App.xaml.cs +++ b/WowUp.WPF/App.xaml.cs @@ -1,7 +1,11 @@ -using Microsoft.Extensions.DependencyInjection; +using CommandLine; +using Microsoft.Extensions.DependencyInjection; using Serilog; using System; +using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -10,6 +14,7 @@ using WowUp.Common.Services.Contracts; using WowUp.WPF.AddonProviders; using WowUp.WPF.AddonProviders.Contracts; using WowUp.WPF.Enums; +using WowUp.WPF.Extensions; using WowUp.WPF.Repositories; using WowUp.WPF.Repositories.Contracts; using WowUp.WPF.Services; @@ -32,6 +37,9 @@ namespace WowUp.WPF private readonly ServiceProvider _serviceProvider; private readonly IAnalyticsService _analyticsService; + private readonly ISessionService _sessionService; + private readonly IAddonService _addonService; + private bool _startSilent; [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] @@ -61,14 +69,67 @@ namespace WowUp.WPF _serviceProvider = serviceCollection.BuildServiceProvider(); _analyticsService = _serviceProvider.GetRequiredService(); + _sessionService = _serviceProvider.GetRequiredService(); + _addonService = _serviceProvider.GetRequiredService(); } + async void TestParsed(StartupOptions options) + { + if (options.ClientType != Common.Enums.WowClientType.None) + _sessionService.SelectedClientType = options.ClientType; + if (options.Update) + { + var addons = await _addonService.GetAddons(_sessionService.SelectedClientType); + var addonsToUpdate = addons.Where(x => x.CanUpdate() || x.CanInstall() && !x.IsIgnored || options.Force); + await addonsToUpdate.ForEachAsync(2, async x => + await _addonService.InstallAddon(x.Id + //Without these lines addons are updated as expected but the view stays unrefreshed + //If I uncomment them, installation is not working + + //,(s,e) => { + // if (s == Common.Enums.AddonInstallState.Complete) + // _addonService.UpdateAddon(x); + //} + ) + ); + } + if (options.Silent) + { + _startSilent = true; + } + if (options.InputURLs.Any()) + { + await options.InputURLs.ForEachAsync(2, async x => + { + var potentialAddon = await _addonService.GetAddonByUri(new Uri(x), _sessionService.SelectedClientType); + if (potentialAddon != null) + await _addonService.InstallAddon(potentialAddon, _sessionService.SelectedClientType + //,(s,e) => { + // if (s == Common.Enums.AddonInstallState.Complete) + // _addonService.UpdateAddon(); + //} + ); + }); + } + + } + void TestNotParsed(IEnumerable errors) + { + Debug.WriteLine(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray())); + Current.Shutdown(); + } protected override void OnStartup(StartupEventArgs e) { + var args = Environment.GetCommandLineArgs().Skip(1); + Parser.Default.ParseArguments(args).WithParsed(TestParsed).WithNotParsed(TestNotParsed); + HandleSingleInstance(); - var mainWindow = _serviceProvider.GetRequiredService(); - mainWindow.Show(); + if(!_startSilent) + { + var mainWindow = _serviceProvider.GetRequiredService(); + mainWindow.Show(); + } } protected override void OnExit(ExitEventArgs e) diff --git a/WowUp.WPF/Properties/launchSettings.json b/WowUp.WPF/Properties/launchSettings.json new file mode 100644 index 00000000..df584f42 --- /dev/null +++ b/WowUp.WPF/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "WowUp.WPF": { + "commandName": "Project", + "commandLineArgs": "-u" + } + } +} \ No newline at end of file diff --git a/WowUp.WPF/Utilities/StartupOptions.cs b/WowUp.WPF/Utilities/StartupOptions.cs new file mode 100644 index 00000000..11a2b995 --- /dev/null +++ b/WowUp.WPF/Utilities/StartupOptions.cs @@ -0,0 +1,22 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.Text; +using WowUp.Common.Enums; + +namespace WowUp.WPF.Utilities +{ + public class StartupOptions + { + [Option(shortName: 'i', longName: "install", HelpText = "Specify addon urls to install them")] + public IEnumerable InputURLs { get; set; } + [Option(shortName: 's', longName: "silent", HelpText = "Start application minimized")] + public bool Silent { get; set; } + [Option(shortName: 'u', longName: "update", HelpText = "Update non-ignored addons on startup")] + public bool Update { get; set; } + [Option(shortName: 'f', longName: "force", HelpText = "Update all addons on startup")] + public bool Force { get; set; } + [Option(shortName: 'c', longName: "client", HelpText = "Specify client version to use", Default = WowClientType.None)] + public WowClientType ClientType { get; set; } + } +} diff --git a/WowUp.WPF/WowUp.WPF.csproj b/WowUp.WPF/WowUp.WPF.csproj index 375bdce6..d72f582b 100644 --- a/WowUp.WPF/WowUp.WPF.csproj +++ b/WowUp.WPF/WowUp.WPF.csproj @@ -54,6 +54,7 @@ + From 9af531b44ff1223c5b2296f490dad61ea68fdc3d Mon Sep 17 00:00:00 2001 From: "@Noxis" Date: Thu, 1 Oct 2020 15:13:30 +0300 Subject: [PATCH 2/7] Startup args test 2 --- WowUp.WPF/App.xaml.cs | 62 ++------------------- WowUp.WPF/Models/WowUp/StartupOptions.cs | 19 +++++++ WowUp.WPF/Properties/launchSettings.json | 2 +- WowUp.WPF/Services/SessionService.cs | 29 +++++++++- WowUp.WPF/Utilities/StartupHelper.cs | 24 ++++++++ WowUp.WPF/Utilities/StartupOptions.cs | 22 -------- WowUp.WPF/ViewModels/MainWindowViewModel.cs | 19 ++++++- 7 files changed, 94 insertions(+), 83 deletions(-) create mode 100644 WowUp.WPF/Models/WowUp/StartupOptions.cs create mode 100644 WowUp.WPF/Utilities/StartupHelper.cs delete mode 100644 WowUp.WPF/Utilities/StartupOptions.cs diff --git a/WowUp.WPF/App.xaml.cs b/WowUp.WPF/App.xaml.cs index 673539b6..a1c58b2d 100644 --- a/WowUp.WPF/App.xaml.cs +++ b/WowUp.WPF/App.xaml.cs @@ -15,6 +15,7 @@ using WowUp.WPF.AddonProviders; using WowUp.WPF.AddonProviders.Contracts; using WowUp.WPF.Enums; using WowUp.WPF.Extensions; +using WowUp.WPF.Models.WowUp; using WowUp.WPF.Repositories; using WowUp.WPF.Repositories.Contracts; using WowUp.WPF.Services; @@ -37,9 +38,6 @@ namespace WowUp.WPF private readonly ServiceProvider _serviceProvider; private readonly IAnalyticsService _analyticsService; - private readonly ISessionService _sessionService; - private readonly IAddonService _addonService; - private bool _startSilent; [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] @@ -69,67 +67,15 @@ namespace WowUp.WPF _serviceProvider = serviceCollection.BuildServiceProvider(); _analyticsService = _serviceProvider.GetRequiredService(); - _sessionService = _serviceProvider.GetRequiredService(); - _addonService = _serviceProvider.GetRequiredService(); - } - - async void TestParsed(StartupOptions options) - { - if (options.ClientType != Common.Enums.WowClientType.None) - _sessionService.SelectedClientType = options.ClientType; - if (options.Update) - { - var addons = await _addonService.GetAddons(_sessionService.SelectedClientType); - var addonsToUpdate = addons.Where(x => x.CanUpdate() || x.CanInstall() && !x.IsIgnored || options.Force); - await addonsToUpdate.ForEachAsync(2, async x => - await _addonService.InstallAddon(x.Id - //Without these lines addons are updated as expected but the view stays unrefreshed - //If I uncomment them, installation is not working - - //,(s,e) => { - // if (s == Common.Enums.AddonInstallState.Complete) - // _addonService.UpdateAddon(x); - //} - ) - ); - } - if (options.Silent) - { - _startSilent = true; - } - if (options.InputURLs.Any()) - { - await options.InputURLs.ForEachAsync(2, async x => - { - var potentialAddon = await _addonService.GetAddonByUri(new Uri(x), _sessionService.SelectedClientType); - if (potentialAddon != null) - await _addonService.InstallAddon(potentialAddon, _sessionService.SelectedClientType - //,(s,e) => { - // if (s == Common.Enums.AddonInstallState.Complete) - // _addonService.UpdateAddon(); - //} - ); - }); - } - - } - void TestNotParsed(IEnumerable errors) - { - Debug.WriteLine(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray())); - Current.Shutdown(); } protected override void OnStartup(StartupEventArgs e) { - var args = Environment.GetCommandLineArgs().Skip(1); - Parser.Default.ParseArguments(args).WithParsed(TestParsed).WithNotParsed(TestNotParsed); + StartupHelper.SetOptions(); HandleSingleInstance(); - if(!_startSilent) - { - var mainWindow = _serviceProvider.GetRequiredService(); - mainWindow.Show(); - } + var mainWindow = _serviceProvider.GetRequiredService(); + mainWindow.Show(); } protected override void OnExit(ExitEventArgs e) diff --git a/WowUp.WPF/Models/WowUp/StartupOptions.cs b/WowUp.WPF/Models/WowUp/StartupOptions.cs new file mode 100644 index 00000000..ed91187d --- /dev/null +++ b/WowUp.WPF/Models/WowUp/StartupOptions.cs @@ -0,0 +1,19 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using WowUp.Common.Enums; + +namespace WowUp.WPF.Models.WowUp +{ + public class StartupOptions + { + [Option(shortName: 'i', longName: "install", HelpText = "Specify addon URLs to install them")] + public IEnumerable InputURLs { get; set; } + [Option(shortName: 'm', longName: "minimized", HelpText = "Start the application minimized")] + public bool Minimized { get; set; } + [Option(shortName: 'q', longName: "quit", HelpText = "Exit the application after auto-updates")] + public bool Quit { get; set; } + [Option(shortName: 'c', longName: "client", HelpText = "Specify client version to use", Default = WowClientType.None)] + public WowClientType ClientType { get; set; } + } +} diff --git a/WowUp.WPF/Properties/launchSettings.json b/WowUp.WPF/Properties/launchSettings.json index df584f42..8ebf388f 100644 --- a/WowUp.WPF/Properties/launchSettings.json +++ b/WowUp.WPF/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "WowUp.WPF": { "commandName": "Project", - "commandLineArgs": "-u" + "commandLineArgs": "-q" } } } \ No newline at end of file diff --git a/WowUp.WPF/Services/SessionService.cs b/WowUp.WPF/Services/SessionService.cs index 07085e25..a38a9acb 100644 --- a/WowUp.WPF/Services/SessionService.cs +++ b/WowUp.WPF/Services/SessionService.cs @@ -4,10 +4,14 @@ using System; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Windows; +using System.Windows.Threading; using WowUp.Common.Enums; using WowUp.Common.Models; using WowUp.Common.Models.Events; +using WowUp.WPF.Extensions; using WowUp.WPF.Services.Contracts; +using WowUp.WPF.Utilities; namespace WowUp.WPF.Services { @@ -114,7 +118,10 @@ namespace WowUp.WPF.Services public void AppLoaded() { - if(_updateCheckTimer == null) + if (StartupHelper.StartupOptions.ClientType != WowClientType.None) + SelectedClientType = StartupHelper.StartupOptions.ClientType; + + if (_updateCheckTimer == null) { _updateCheckTimer = new Timer(_ => UpdateCheckTimerElapsed(), null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(60)); } @@ -123,6 +130,21 @@ namespace WowUp.WPF.Services { _autoUpdateCheckTimer = new Timer(_ => ProcessAutoUpdates(), null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(60)); } + + //if (StartupHelper.StartupOptions.InputURLs.Any()) + //{ + // await StartupHelper.StartupOptions.InputURLs.ForEachAsync(2, async x => + // { + // var potentialAddon = await _addonService.GetAddonByUri(new Uri(x), SelectedClientType); + // if (potentialAddon != null) + // await _addonService.InstallAddon(potentialAddon, SelectedClientType + // //,(s,e) => { + // // if (s == Common.Enums.AddonInstallState.Complete) + // // _addonService.UpdateAddon(); + // //} + // ); + // }); + //} } public void SetContextText(object requestor, string text) @@ -144,6 +166,11 @@ namespace WowUp.WPF.Services { TaskbarIcon.ShowBalloonTip("WowUp", $"Automatically updated {updateCount} addons.", TaskbarIcon.Icon, true); } + + if (StartupHelper.StartupOptions.Quit) + { + await Application.Current.Dispatcher.BeginInvoke(() => { Application.Current.Shutdown(); }, DispatcherPriority.ApplicationIdle); + } } private void SetContextText(string text) diff --git a/WowUp.WPF/Utilities/StartupHelper.cs b/WowUp.WPF/Utilities/StartupHelper.cs new file mode 100644 index 00000000..f4dc88ee --- /dev/null +++ b/WowUp.WPF/Utilities/StartupHelper.cs @@ -0,0 +1,24 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using WowUp.WPF.Models.WowUp; + +namespace WowUp.WPF.Utilities +{ + public static class StartupHelper + { + public static void SetOptions() + { + var args = Environment.GetCommandLineArgs().Skip(1); + Parser.Default.ParseArguments(args) + .WithParsed( + options => StartupOptions = options) + .WithNotParsed( + errors => Debug.WriteLine(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray()))); + } + public static StartupOptions StartupOptions { get; private set; } + } +} diff --git a/WowUp.WPF/Utilities/StartupOptions.cs b/WowUp.WPF/Utilities/StartupOptions.cs deleted file mode 100644 index 11a2b995..00000000 --- a/WowUp.WPF/Utilities/StartupOptions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using CommandLine; -using System; -using System.Collections.Generic; -using System.Text; -using WowUp.Common.Enums; - -namespace WowUp.WPF.Utilities -{ - public class StartupOptions - { - [Option(shortName: 'i', longName: "install", HelpText = "Specify addon urls to install them")] - public IEnumerable InputURLs { get; set; } - [Option(shortName: 's', longName: "silent", HelpText = "Start application minimized")] - public bool Silent { get; set; } - [Option(shortName: 'u', longName: "update", HelpText = "Update non-ignored addons on startup")] - public bool Update { get; set; } - [Option(shortName: 'f', longName: "force", HelpText = "Update all addons on startup")] - public bool Force { get; set; } - [Option(shortName: 'c', longName: "client", HelpText = "Specify client version to use", Default = WowClientType.None)] - public WowClientType ClientType { get; set; } - } -} diff --git a/WowUp.WPF/ViewModels/MainWindowViewModel.cs b/WowUp.WPF/ViewModels/MainWindowViewModel.cs index 8f728bc2..be85086a 100644 --- a/WowUp.WPF/ViewModels/MainWindowViewModel.cs +++ b/WowUp.WPF/ViewModels/MainWindowViewModel.cs @@ -1,13 +1,16 @@ -using Hardcodet.Wpf.TaskbarNotification; +using CommandLine; +using Hardcodet.Wpf.TaskbarNotification; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Threading; using WowUp.Common.Services.Contracts; using WowUp.WPF.Entities; using WowUp.WPF.Extensions; +using WowUp.WPF.Models.WowUp; using WowUp.WPF.Repositories.Contracts; using WowUp.WPF.Services.Contracts; using WowUp.WPF.Utilities; @@ -196,6 +199,13 @@ namespace WowUp.WPF.ViewModels public void OnSourceInitialized(Window window) { + if (StartupHelper.StartupOptions.Minimized) + { + window.Hide(); + window.ShowInTaskbar = false; + window.IsVisibleChanged += Window_IsVisibleChanged; + } + var windowPref = _preferenceRepository.FindByKey(WindowPlacementKey); var windowStatePref = _preferenceRepository.FindByKey(WindowStateKey); if (windowPref == null) @@ -223,6 +233,13 @@ namespace WowUp.WPF.ViewModels } } + private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) + { + var window = (Window)sender; + window.IsVisibleChanged -= Window_IsVisibleChanged; + window.ShowInTaskbar = true; + } + public void OnClosing(Window window) { var placement = window.GetPlacement(); From d51627463c85df50e858e23b017d28172718f80b Mon Sep 17 00:00:00 2001 From: "@Noxis" Date: Thu, 1 Oct 2020 16:45:39 +0300 Subject: [PATCH 3/7] Startup args parsing pull candidate --- WowUp.WPF/Properties/launchSettings.json | 3 +- WowUp.WPF/Services/SessionService.cs | 51 +++++++++++++-------- WowUp.WPF/ViewModels/MainWindowViewModel.cs | 2 +- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/WowUp.WPF/Properties/launchSettings.json b/WowUp.WPF/Properties/launchSettings.json index 8ebf388f..35e73a83 100644 --- a/WowUp.WPF/Properties/launchSettings.json +++ b/WowUp.WPF/Properties/launchSettings.json @@ -1,8 +1,7 @@ { "profiles": { "WowUp.WPF": { - "commandName": "Project", - "commandLineArgs": "-q" + "commandName": "Project" } } } \ No newline at end of file diff --git a/WowUp.WPF/Services/SessionService.cs b/WowUp.WPF/Services/SessionService.cs index a38a9acb..f51a421d 100644 --- a/WowUp.WPF/Services/SessionService.cs +++ b/WowUp.WPF/Services/SessionService.cs @@ -116,9 +116,37 @@ namespace WowUp.WPF.Services SessionChanged?.Invoke(this, new SessionEventArgs(_sessionState)); } - public void AppLoaded() + public async void AppLoaded() { - if (StartupHelper.StartupOptions.ClientType != WowClientType.None) + if (StartupHelper.StartupOptions?.InputURLs.Any() == true) + { + await StartupHelper.StartupOptions.InputURLs.ForEachAsync(2, async x => + { + PotentialAddon potentialAddon = null; + try + { + potentialAddon = await _addonService.GetAddonByUri(new Uri(x), SelectedClientType); + } + catch + { + MessageBox.Show($"Failed to import addon by URI: {x}"); + } + if (potentialAddon != null) + { + try + { + await _addonService.InstallAddon(potentialAddon, SelectedClientType); + } + catch + { + MessageBox.Show($"Failed to install addon {potentialAddon.Name}"); + } + } + + }); + } + + if (StartupHelper.StartupOptions != null && StartupHelper.StartupOptions.ClientType != WowClientType.None) SelectedClientType = StartupHelper.StartupOptions.ClientType; if (_updateCheckTimer == null) @@ -131,20 +159,7 @@ namespace WowUp.WPF.Services _autoUpdateCheckTimer = new Timer(_ => ProcessAutoUpdates(), null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(60)); } - //if (StartupHelper.StartupOptions.InputURLs.Any()) - //{ - // await StartupHelper.StartupOptions.InputURLs.ForEachAsync(2, async x => - // { - // var potentialAddon = await _addonService.GetAddonByUri(new Uri(x), SelectedClientType); - // if (potentialAddon != null) - // await _addonService.InstallAddon(potentialAddon, SelectedClientType - // //,(s,e) => { - // // if (s == Common.Enums.AddonInstallState.Complete) - // // _addonService.UpdateAddon(); - // //} - // ); - // }); - //} + } public void SetContextText(object requestor, string text) @@ -167,9 +182,9 @@ namespace WowUp.WPF.Services TaskbarIcon.ShowBalloonTip("WowUp", $"Automatically updated {updateCount} addons.", TaskbarIcon.Icon, true); } - if (StartupHelper.StartupOptions.Quit) + if (StartupHelper.StartupOptions?.Quit == true) { - await Application.Current.Dispatcher.BeginInvoke(() => { Application.Current.Shutdown(); }, DispatcherPriority.ApplicationIdle); + await Application.Current.Dispatcher.BeginInvoke(() => { Application.Current.Shutdown(); }, DispatcherPriority.SystemIdle); } } diff --git a/WowUp.WPF/ViewModels/MainWindowViewModel.cs b/WowUp.WPF/ViewModels/MainWindowViewModel.cs index be85086a..f5530437 100644 --- a/WowUp.WPF/ViewModels/MainWindowViewModel.cs +++ b/WowUp.WPF/ViewModels/MainWindowViewModel.cs @@ -199,7 +199,7 @@ namespace WowUp.WPF.ViewModels public void OnSourceInitialized(Window window) { - if (StartupHelper.StartupOptions.Minimized) + if (StartupHelper.StartupOptions?.Minimized == true) { window.Hide(); window.ShowInTaskbar = false; From dc0467d31baeb02ded37114677d21723fbf08ed6 Mon Sep 17 00:00:00 2001 From: "@Noxis" Date: Fri, 2 Oct 2020 08:10:07 +0300 Subject: [PATCH 4/7] Some polishes --- WowUp.WPF/App.xaml.cs | 12 +++--------- WowUp.WPF/Models/WowUp/StartupOptions.cs | 1 - WowUp.WPF/Utilities/StartupHelper.cs | 6 ++---- WowUp.WPF/ViewModels/MainWindowViewModel.cs | 3 --- 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/WowUp.WPF/App.xaml.cs b/WowUp.WPF/App.xaml.cs index a1c58b2d..ae4a2d75 100644 --- a/WowUp.WPF/App.xaml.cs +++ b/WowUp.WPF/App.xaml.cs @@ -1,11 +1,7 @@ -using CommandLine; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Serilog; using System; -using System.Collections.Generic; -using System.Diagnostics; using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -14,8 +10,6 @@ using WowUp.Common.Services.Contracts; using WowUp.WPF.AddonProviders; using WowUp.WPF.AddonProviders.Contracts; using WowUp.WPF.Enums; -using WowUp.WPF.Extensions; -using WowUp.WPF.Models.WowUp; using WowUp.WPF.Repositories; using WowUp.WPF.Repositories.Contracts; using WowUp.WPF.Services; @@ -70,10 +64,10 @@ namespace WowUp.WPF } protected override void OnStartup(StartupEventArgs e) { - StartupHelper.SetOptions(); - HandleSingleInstance(); + StartupHelper.SetOptions(); + var mainWindow = _serviceProvider.GetRequiredService(); mainWindow.Show(); } diff --git a/WowUp.WPF/Models/WowUp/StartupOptions.cs b/WowUp.WPF/Models/WowUp/StartupOptions.cs index ed91187d..b9853dfd 100644 --- a/WowUp.WPF/Models/WowUp/StartupOptions.cs +++ b/WowUp.WPF/Models/WowUp/StartupOptions.cs @@ -1,5 +1,4 @@ using CommandLine; -using System; using System.Collections.Generic; using WowUp.Common.Enums; diff --git a/WowUp.WPF/Utilities/StartupHelper.cs b/WowUp.WPF/Utilities/StartupHelper.cs index f4dc88ee..2bd64422 100644 --- a/WowUp.WPF/Utilities/StartupHelper.cs +++ b/WowUp.WPF/Utilities/StartupHelper.cs @@ -1,9 +1,7 @@ using CommandLine; +using Serilog; using System; -using System.Collections.Generic; -using System.Diagnostics; using System.Linq; -using System.Text; using WowUp.WPF.Models.WowUp; namespace WowUp.WPF.Utilities @@ -17,7 +15,7 @@ namespace WowUp.WPF.Utilities .WithParsed( options => StartupOptions = options) .WithNotParsed( - errors => Debug.WriteLine(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray()))); + errors => Log.Error(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray()))); } public static StartupOptions StartupOptions { get; private set; } } diff --git a/WowUp.WPF/ViewModels/MainWindowViewModel.cs b/WowUp.WPF/ViewModels/MainWindowViewModel.cs index cf163a2b..a391a2f4 100644 --- a/WowUp.WPF/ViewModels/MainWindowViewModel.cs +++ b/WowUp.WPF/ViewModels/MainWindowViewModel.cs @@ -1,4 +1,3 @@ -using CommandLine; using Hardcodet.Wpf.TaskbarNotification; using Microsoft.Extensions.DependencyInjection; using System; @@ -7,10 +6,8 @@ using System.Linq; using System.Windows; using System.Windows.Controls; using WowUp.Common.Enums; -using WowUp.Common.Services.Contracts; using WowUp.WPF.Entities; using WowUp.WPF.Extensions; -using WowUp.WPF.Models.WowUp; using WowUp.WPF.Repositories.Contracts; using WowUp.WPF.Services.Contracts; using WowUp.WPF.Utilities; From 93ec7c1ddf1e083908a150ea56b8a63a81241770 Mon Sep 17 00:00:00 2001 From: "@Noxis" Date: Fri, 2 Oct 2020 08:14:12 +0300 Subject: [PATCH 5/7] Delete launchSettings.json --- WowUp.WPF/Properties/launchSettings.json | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 WowUp.WPF/Properties/launchSettings.json diff --git a/WowUp.WPF/Properties/launchSettings.json b/WowUp.WPF/Properties/launchSettings.json deleted file mode 100644 index 35e73a83..00000000 --- a/WowUp.WPF/Properties/launchSettings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "profiles": { - "WowUp.WPF": { - "commandName": "Project" - } - } -} \ No newline at end of file From 66f84902132cba8170cf242220073af7f5b3615f Mon Sep 17 00:00:00 2001 From: "@Noxis" Date: Fri, 2 Oct 2020 08:16:23 +0300 Subject: [PATCH 6/7] Some more polishes --- WowUp.WPF/Services/SessionService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WowUp.WPF/Services/SessionService.cs b/WowUp.WPF/Services/SessionService.cs index f51a421d..8ff64dbe 100644 --- a/WowUp.WPF/Services/SessionService.cs +++ b/WowUp.WPF/Services/SessionService.cs @@ -118,6 +118,9 @@ namespace WowUp.WPF.Services public async void AppLoaded() { + if (StartupHelper.StartupOptions != null && StartupHelper.StartupOptions.ClientType != WowClientType.None) + SelectedClientType = StartupHelper.StartupOptions.ClientType; + if (StartupHelper.StartupOptions?.InputURLs.Any() == true) { await StartupHelper.StartupOptions.InputURLs.ForEachAsync(2, async x => @@ -146,9 +149,6 @@ namespace WowUp.WPF.Services }); } - if (StartupHelper.StartupOptions != null && StartupHelper.StartupOptions.ClientType != WowClientType.None) - SelectedClientType = StartupHelper.StartupOptions.ClientType; - if (_updateCheckTimer == null) { _updateCheckTimer = new Timer(_ => UpdateCheckTimerElapsed(), null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(60)); From 58cc93d801958f07a0ba8f51464a7207fa5d7016 Mon Sep 17 00:00:00 2001 From: jliddev Date: Fri, 2 Oct 2020 13:55:12 -0500 Subject: [PATCH 7/7] Move parsing to App. Minor style tweaks. --- WowUp.WPF/App.xaml.cs | 21 +++++- WowUp.WPF/Services/SessionService.cs | 75 ++++++++++++--------- WowUp.WPF/Utilities/StartupHelper.cs | 22 ------ WowUp.WPF/ViewModels/MainWindowViewModel.cs | 12 +--- 4 files changed, 63 insertions(+), 67 deletions(-) delete mode 100644 WowUp.WPF/Utilities/StartupHelper.cs diff --git a/WowUp.WPF/App.xaml.cs b/WowUp.WPF/App.xaml.cs index ae4a2d75..c56146f3 100644 --- a/WowUp.WPF/App.xaml.cs +++ b/WowUp.WPF/App.xaml.cs @@ -1,7 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; +using CommandLine; +using Microsoft.Extensions.DependencyInjection; using Serilog; using System; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -10,6 +12,7 @@ using WowUp.Common.Services.Contracts; using WowUp.WPF.AddonProviders; using WowUp.WPF.AddonProviders.Contracts; using WowUp.WPF.Enums; +using WowUp.WPF.Models.WowUp; using WowUp.WPF.Repositories; using WowUp.WPF.Repositories.Contracts; using WowUp.WPF.Services; @@ -40,6 +43,8 @@ namespace WowUp.WPF [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd); + public static StartupOptions StartupOptions { get; private set; } + public App() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler); @@ -56,6 +61,8 @@ namespace WowUp.WPF Log.Information($"Starting {AppUtilities.CurrentVersion}"); + ParseCommandLineArgs(); + var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); @@ -66,8 +73,6 @@ namespace WowUp.WPF { HandleSingleInstance(); - StartupHelper.SetOptions(); - var mainWindow = _serviceProvider.GetRequiredService(); mainWindow.Show(); } @@ -160,5 +165,15 @@ namespace WowUp.WPF //there is already another instance running! Current.Shutdown(); } + + private void ParseCommandLineArgs() + { + var args = Environment.GetCommandLineArgs().Skip(1); + Parser.Default.ParseArguments(args) + .WithParsed( + options => StartupOptions = options) + .WithNotParsed( + errors => Log.Error(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray()))); + } } } diff --git a/WowUp.WPF/Services/SessionService.cs b/WowUp.WPF/Services/SessionService.cs index 8ff64dbe..e607475a 100644 --- a/WowUp.WPF/Services/SessionService.cs +++ b/WowUp.WPF/Services/SessionService.cs @@ -11,7 +11,6 @@ using WowUp.Common.Models; using WowUp.Common.Models.Events; using WowUp.WPF.Extensions; using WowUp.WPF.Services.Contracts; -using WowUp.WPF.Utilities; namespace WowUp.WPF.Services { @@ -58,7 +57,6 @@ namespace WowUp.WPF.Services StatusText = string.Empty, UpdaterReady = false }; - } private async void UpdateCheckTimerElapsed() @@ -118,37 +116,13 @@ namespace WowUp.WPF.Services public async void AppLoaded() { - if (StartupHelper.StartupOptions != null && StartupHelper.StartupOptions.ClientType != WowClientType.None) - SelectedClientType = StartupHelper.StartupOptions.ClientType; - - if (StartupHelper.StartupOptions?.InputURLs.Any() == true) + if (App.StartupOptions != null && App.StartupOptions.ClientType != WowClientType.None) { - await StartupHelper.StartupOptions.InputURLs.ForEachAsync(2, async x => - { - PotentialAddon potentialAddon = null; - try - { - potentialAddon = await _addonService.GetAddonByUri(new Uri(x), SelectedClientType); - } - catch - { - MessageBox.Show($"Failed to import addon by URI: {x}"); - } - if (potentialAddon != null) - { - try - { - await _addonService.InstallAddon(potentialAddon, SelectedClientType); - } - catch - { - MessageBox.Show($"Failed to install addon {potentialAddon.Name}"); - } - } - - }); + SelectedClientType = App.StartupOptions.ClientType; } + await ProcessInputUrls(); + if (_updateCheckTimer == null) { _updateCheckTimer = new Timer(_ => UpdateCheckTimerElapsed(), null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(60)); @@ -158,8 +132,6 @@ namespace WowUp.WPF.Services { _autoUpdateCheckTimer = new Timer(_ => ProcessAutoUpdates(), null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(60)); } - - } public void SetContextText(object requestor, string text) @@ -173,6 +145,41 @@ namespace WowUp.WPF.Services SetContextText(text); } + private async Task ProcessInputUrls() + { + if (!App.StartupOptions?.InputURLs.Any() ?? false) + { + return; + } + + await App.StartupOptions.InputURLs.ForEachAsync(2, async x => + { + PotentialAddon potentialAddon = null; + try + { + potentialAddon = await _addonService.GetAddonByUri(new Uri(x), SelectedClientType); + } + catch + { + MessageBox.Show($"Failed to import addon by URI: {x}"); + return; + } + + if (potentialAddon != null) + { + try + { + await _addonService.InstallAddon(potentialAddon, SelectedClientType); + } + catch + { + MessageBox.Show($"Failed to install addon {potentialAddon.Name}"); + } + } + + }); + } + private async void ProcessAutoUpdates() { var updateCount = await _addonService.ProcessAutoUpdates(); @@ -182,8 +189,10 @@ namespace WowUp.WPF.Services TaskbarIcon.ShowBalloonTip("WowUp", $"Automatically updated {updateCount} addons.", TaskbarIcon.Icon, true); } - if (StartupHelper.StartupOptions?.Quit == true) + if (App.StartupOptions?.Quit == true) { + // Artificial delay to allow notification to fire. + await Task.Delay(3000); await Application.Current.Dispatcher.BeginInvoke(() => { Application.Current.Shutdown(); }, DispatcherPriority.SystemIdle); } } diff --git a/WowUp.WPF/Utilities/StartupHelper.cs b/WowUp.WPF/Utilities/StartupHelper.cs deleted file mode 100644 index 2bd64422..00000000 --- a/WowUp.WPF/Utilities/StartupHelper.cs +++ /dev/null @@ -1,22 +0,0 @@ -using CommandLine; -using Serilog; -using System; -using System.Linq; -using WowUp.WPF.Models.WowUp; - -namespace WowUp.WPF.Utilities -{ - public static class StartupHelper - { - public static void SetOptions() - { - var args = Environment.GetCommandLineArgs().Skip(1); - Parser.Default.ParseArguments(args) - .WithParsed( - options => StartupOptions = options) - .WithNotParsed( - errors => Log.Error(string.Join("\r\n", errors.Select(x => x.ToString()).ToArray()))); - } - public static StartupOptions StartupOptions { get; private set; } - } -} diff --git a/WowUp.WPF/ViewModels/MainWindowViewModel.cs b/WowUp.WPF/ViewModels/MainWindowViewModel.cs index a391a2f4..a0c4228e 100644 --- a/WowUp.WPF/ViewModels/MainWindowViewModel.cs +++ b/WowUp.WPF/ViewModels/MainWindowViewModel.cs @@ -219,6 +219,7 @@ namespace WowUp.WPF.ViewModels return; } + Application.Current.MainWindow.ShowInTaskbar = true; Application.Current.MainWindow.Show(); Application.Current.MainWindow.WindowState = WindowState.Normal; Application.Current.MainWindow.Activate(); @@ -255,11 +256,11 @@ namespace WowUp.WPF.ViewModels public void OnSourceInitialized(Window window) { - if (StartupHelper.StartupOptions?.Minimized == true) + if (App.StartupOptions?.Minimized == true) { window.Hide(); window.ShowInTaskbar = false; - window.IsVisibleChanged += Window_IsVisibleChanged; + window.WindowState = WindowState.Minimized; } var windowPref = _preferenceRepository.FindByKey(WindowPlacementKey); @@ -289,13 +290,6 @@ namespace WowUp.WPF.ViewModels } } - private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) - { - var window = (Window)sender; - window.IsVisibleChanged -= Window_IsVisibleChanged; - window.ShowInTaskbar = true; - } - public void OnClosing(Window window) { var placement = window.GetPlacement();