From 478d5196a6913ef4dd1099915856f710d67eea9f Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Wed, 13 Jan 2021 13:04:35 +0100 Subject: [PATCH] Improved app start up Fixed issues with extraction --- .../Converters/ListOfDLcToStringConverter.cs | 4 +- auto-creamapi/Services/CacheService.cs | 8 +-- auto-creamapi/Services/CreamConfigService.cs | 6 +- auto-creamapi/Services/CreamDllService.cs | 15 ++-- .../Services/DownloadCreamApiService.cs | 72 ++++++++++++------- auto-creamapi/ViewModels/DownloadViewModel.cs | 7 +- auto-creamapi/ViewModels/MainViewModel.cs | 27 ++++--- .../ViewModels/SearchResultViewModel.cs | 2 +- auto-creamapi/Views/MainView.xaml | 2 +- auto-creamapi/Views/SearchResultView.xaml.cs | 32 --------- 10 files changed, 84 insertions(+), 91 deletions(-) diff --git a/auto-creamapi/Converters/ListOfDLcToStringConverter.cs b/auto-creamapi/Converters/ListOfDLcToStringConverter.cs index aabfdd6..9904374 100644 --- a/auto-creamapi/Converters/ListOfDLcToStringConverter.cs +++ b/auto-creamapi/Converters/ListOfDLcToStringConverter.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.IO; @@ -19,6 +20,7 @@ namespace auto_creamapi.Converters protected override string Convert(ObservableCollection value, Type targetType, object parameter, CultureInfo culture) { + if (value == null) return ""; MyLogger.Log.Debug("ListOfDLcToStringConverter: Convert"); var dlcListToString = DlcListToString(value); return dlcListToString.GetType() == targetType ? dlcListToString : ""; @@ -52,7 +54,7 @@ namespace auto_creamapi.Converters return result; } - private static string DlcListToString(ObservableCollection value) + private static string DlcListToString(IEnumerable value) { var result = ""; //value.ForEach(x => result += $"{x}\n"); diff --git a/auto-creamapi/Services/CacheService.cs b/auto-creamapi/Services/CacheService.cs index 2928dd6..ca2af53 100644 --- a/auto-creamapi/Services/CacheService.cs +++ b/auto-creamapi/Services/CacheService.cs @@ -65,10 +65,10 @@ namespace auto_creamapi.Services var httpCall = client.GetAsync(SteamUri); var response = await httpCall.ConfigureAwait(false); var readAsStringAsync = response.Content.ReadAsStringAsync(); - var responseBody = await readAsStringAsync; + var responseBody = await readAsStringAsync.ConfigureAwait(false); MyLogger.Log.Information("Got content from API successfully. Writing to file..."); - await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8); + await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8).ConfigureAwait(false); var cacheString = responseBody; MyLogger.Log.Information("Cache written to file successfully."); return cacheString; @@ -141,12 +141,12 @@ namespace auto_creamapi.Services MyLogger.Log.Information("Get SteamDB App"); var httpCall = client.GetAsync(steamDbUri); - var response = await httpCall; + var response = await httpCall.ConfigureAwait(false); MyLogger.Log.Debug(httpCall.Status.ToString()); MyLogger.Log.Debug(response.EnsureSuccessStatusCode().ToString()); var readAsStringAsync = response.Content.ReadAsStringAsync(); - var responseBody = await readAsStringAsync; + var responseBody = await readAsStringAsync.ConfigureAwait(false); MyLogger.Log.Debug(readAsStringAsync.Status.ToString()); var parser = new HtmlParser(); diff --git a/auto-creamapi/Services/CreamConfigService.cs b/auto-creamapi/Services/CreamConfigService.cs index b331410..acd2a3e 100644 --- a/auto-creamapi/Services/CreamConfigService.cs +++ b/auto-creamapi/Services/CreamConfigService.cs @@ -38,7 +38,7 @@ namespace auto_creamapi.Services bool unlockAll, bool extraProtection, bool forceOffline, - ObservableCollection dlcList); + IEnumerable dlcList); public bool ConfigExists(); } @@ -47,7 +47,7 @@ namespace auto_creamapi.Services { private string _configFilePath; - public CreamConfig Config { get; set; } + public CreamConfig Config { get; private set; } public void Initialize() { @@ -144,7 +144,7 @@ namespace auto_creamapi.Services bool unlockAll, bool extraProtection, bool forceOffline, - ObservableCollection dlcList) + IEnumerable dlcList) { Config.AppId = appId; Config.Language = language; diff --git a/auto-creamapi/Services/CreamDllService.cs b/auto-creamapi/Services/CreamDllService.cs index d41c35a..9cee2df 100644 --- a/auto-creamapi/Services/CreamDllService.cs +++ b/auto-creamapi/Services/CreamDllService.cs @@ -103,16 +103,13 @@ namespace auto_creamapi.Services private static string GetHash(string filename) { - if (File.Exists(filename)) - { - using var md5 = MD5.Create(); - using var stream = File.OpenRead(filename); - return BitConverter - .ToString(md5.ComputeHash(stream)) - .Replace("-", string.Empty); - } + if (!File.Exists(filename)) return ""; + using var md5 = MD5.Create(); + using var stream = File.OpenRead(filename); + return BitConverter + .ToString(md5.ComputeHash(stream)) + .Replace("-", string.Empty); - return ""; } } } \ No newline at end of file diff --git a/auto-creamapi/Services/DownloadCreamApiService.cs b/auto-creamapi/Services/DownloadCreamApiService.cs index 0cc37f2..4998739 100644 --- a/auto-creamapi/Services/DownloadCreamApiService.cs +++ b/auto-creamapi/Services/DownloadCreamApiService.cs @@ -17,8 +17,6 @@ namespace auto_creamapi.Services { public interface IDownloadCreamApiService { - /*public void Initialize(); - public Task InitializeAsync();*/ public Task Download(string username, string password); public Task Extract(string filename); } @@ -26,10 +24,7 @@ namespace auto_creamapi.Services public class DownloadCreamApiService : IDownloadCreamApiService { private const string ArchivePassword = "cs.rin.ru"; - - //private string _filename; private readonly IMvxMessenger _messenger; - //private DownloadWindow _wnd; public DownloadCreamApiService(IMvxMessenger messenger) { @@ -50,23 +45,25 @@ namespace auto_creamapi.Services new KeyValuePair("login", "login") }); MyLogger.Log.Debug("Download: post login"); - var response1 = await client.PostAsync("https://cs.rin.ru/forum/ucp.php?mode=login", formContent); + var response1 = await client.PostAsync("https://cs.rin.ru/forum/ucp.php?mode=login", formContent) + .ConfigureAwait(false); MyLogger.Log.Debug($"Login Status Code: {response1.EnsureSuccessStatusCode().StatusCode.ToString()}"); var cookie = container.GetCookies(new Uri("https://cs.rin.ru/forum/ucp.php?mode=login")) .FirstOrDefault(c => c.Name.Contains("_sid")); MyLogger.Log.Debug($"Login Cookie: {cookie}"); - var response2 = await client.GetAsync("https://cs.rin.ru/forum/viewtopic.php?t=70576"); + var response2 = await client.GetAsync("https://cs.rin.ru/forum/viewtopic.php?t=70576") + .ConfigureAwait(false); MyLogger.Log.Debug( $"Download Page Status Code: {response2.EnsureSuccessStatusCode().StatusCode.ToString()}"); var content = response2.Content.ReadAsStringAsync(); - var contentResult = await content; + var contentResult = await content.ConfigureAwait(false); var expression = new Regex(".*\\/download\\/file\\.php\\?id=.*)\">(?.*)<\\/a>.*"); using var reader = new StringReader(contentResult); string line; var archiveFileList = new Dictionary(); - while ((line = await reader.ReadLineAsync()) != null) + while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null) { var match = expression.Match(line); // ReSharper disable once InvertIf @@ -80,7 +77,6 @@ namespace auto_creamapi.Services MyLogger.Log.Debug("Choosing first element from list..."); var (filename, url) = archiveFileList.FirstOrDefault(); - //filename = filename; if (File.Exists(filename)) { MyLogger.Log.Information($"{filename} already exists, skipping download..."); @@ -92,7 +88,7 @@ namespace auto_creamapi.Services x => _messenger.Publish(new ProgressMessage(this, "Downloading...", filename, x))); await using var fileStream = File.OpenWrite(filename); var task = client.GetAsync(url, fileStream, progress); - var response = await task; + await task.ConfigureAwait(false); if (task.IsCompletedSuccessfully) _messenger.Publish(new ProgressMessage(this, "Downloading...", filename, 1.0)); MyLogger.Log.Information("Download done."); @@ -107,23 +103,47 @@ namespace auto_creamapi.Services const string steamApi64Dll = "steam_api64.dll"; const string steamApiDll = "steam_api.dll"; MyLogger.Log.Information($@"Start extraction of ""{filename}""..."); - var expression1 = new Regex(@"nonlog_build\\steam_api(?:64)?\.dll"); - _messenger.Publish(new ProgressMessage(this, "Extracting...", filename, 1.0)); - SevenZipBase.SetLibraryPath(Path.Combine(cwd, "resources/7z.dll")); - using (var extractor = - new SevenZipExtractor(filename, ArchivePassword, InArchiveFormat.Rar) - {PreserveDirectoryStructure = false}) + try { - await extractor.ExtractFilesAsync(cwd, - $"{nonlogBuild}\\{steamApi64Dll}", - $"{nonlogBuild}\\{steamApiDll}"); + var nonlogBuildPath = Path.Combine(cwd, nonlogBuild); + if (Directory.Exists(nonlogBuildPath)) + Directory.Delete(nonlogBuildPath, true); + _messenger.Publish(new ProgressMessage(this, "Extracting...", filename, 1.0)); + SevenZipBase.SetLibraryPath(Path.Combine(cwd, "resources/7z.dll")); + using (var extractor = + new SevenZipExtractor(filename, ArchivePassword, InArchiveFormat.Rar) + {PreserveDirectoryStructure = false}) + { + await extractor.ExtractFilesAsync( + cwd, + $@"{nonlogBuild}\{steamApi64Dll}", + $@"{nonlogBuild}\{steamApiDll}" + ).ConfigureAwait(false); + } + + if (File.Exists(Path.Combine(nonlogBuildPath, steamApi64Dll))) + File.Move( + Path.Combine(cwd, nonlogBuild, steamApi64Dll), + Path.Combine(cwd, steamApi64Dll), + true + ); + + if (File.Exists(Path.Combine(nonlogBuildPath, steamApiDll))) + File.Move( + Path.Combine(nonlogBuildPath, steamApiDll), + Path.Combine(cwd, steamApiDll), + true + ); + + if (Directory.Exists(nonlogBuildPath)) + Directory.Delete(nonlogBuildPath, true); + MyLogger.Log.Information("Extraction done!"); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; } - if (File.Exists(Path.Combine(cwd, nonlogBuild, steamApi64Dll))) - File.Move(Path.Combine(cwd, nonlogBuild, steamApi64Dll), Path.Combine(cwd, steamApi64Dll)); - if (File.Exists(Path.Combine(cwd, nonlogBuild, steamApiDll))) - File.Move(Path.Combine(cwd, nonlogBuild, steamApiDll), Path.Combine(cwd, steamApiDll)); - Directory.Delete(Path.Combine(cwd, nonlogBuild)); - MyLogger.Log.Information("Extraction done!"); } } } \ No newline at end of file diff --git a/auto-creamapi/ViewModels/DownloadViewModel.cs b/auto-creamapi/ViewModels/DownloadViewModel.cs index 80ea09a..c580d8d 100644 --- a/auto-creamapi/ViewModels/DownloadViewModel.cs +++ b/auto-creamapi/ViewModels/DownloadViewModel.cs @@ -9,6 +9,7 @@ using MvvmCross.ViewModels; namespace auto_creamapi.ViewModels { + public class DownloadViewModel : MvxNavigationViewModel { private readonly IDownloadCreamApiService _download; @@ -63,18 +64,18 @@ namespace auto_creamapi.ViewModels public override async Task Initialize() { - await base.Initialize(); + await base.Initialize().ConfigureAwait(false); InfoLabel = "Please wait..."; FilenameLabel = ""; Progress = 0.0; var download = _download.Download(Secrets.ForumUsername, Secrets.ForumPassword); - var filename = await download; + var filename = await download.ConfigureAwait(false); /*var extract = _download.Extract(filename); await extract;*/ var extract = _download.Extract(filename); await extract.ConfigureAwait(false); _token.Dispose(); - await _navigationService.Close(this); + await _navigationService.Close(this).ConfigureAwait(false); } private void OnProgressMessage(ProgressMessage obj) diff --git a/auto-creamapi/ViewModels/MainViewModel.cs b/auto-creamapi/ViewModels/MainViewModel.cs index 3bda2a7..eed6b30 100644 --- a/auto-creamapi/ViewModels/MainViewModel.cs +++ b/auto-creamapi/ViewModels/MainViewModel.cs @@ -52,14 +52,16 @@ namespace auto_creamapi.ViewModels //_download = download; } - public override async Task Initialize() + public override async void Prepare() { + base.Prepare(); _config.Initialize(); - var tasks = new List {base.Initialize(), _cache.Initialize()}; + var tasks = new List {_cache.Initialize()}; if (!File.Exists("steam_api.dll") | !File.Exists("steam_api64.dll")) tasks.Add(_navigationService.Navigate()); + //tasks.Add(_navigationService.Navigate()); tasks.Add(_dll.Initialize()); - await Task.WhenAll(tasks); + await Task.WhenAll(tasks).ConfigureAwait(false); Languages = new ObservableCollection(Misc.DefaultLanguages); ResetForm(); UseSteamDb = true; @@ -67,11 +69,16 @@ namespace auto_creamapi.ViewModels Status = "Ready."; } + public override Task Initialize() + { + return base.Initialize(); + } + // // COMMANDS // // public IMvxCommand OpenFileCommand => new MvxAsyncCommand(OpenFile); - public IMvxCommand SearchCommand => new MvxAsyncCommand(async () => await Search()); //Command(Search); + public IMvxCommand SearchCommand => new MvxAsyncCommand(async () => await Search().ConfigureAwait(false)); //Command(Search); public IMvxCommand GetListOfDlcCommand => new MvxAsyncCommand(GetListOfDlc); @@ -110,7 +117,6 @@ namespace auto_creamapi.ViewModels { _gameName = value; RaisePropertyChanged(() => GameName); - //MyLogger.Log.Debug($"GameName: {value}"); } } @@ -132,7 +138,6 @@ namespace auto_creamapi.ViewModels { _lang = value; RaisePropertyChanged(() => Lang); - //MyLogger.Log.Debug($"Lang: {value}"); } } @@ -269,8 +274,8 @@ namespace auto_creamapi.ViewModels : -1; var s = index > -1 ? strings[index] : null; if (s != null) GameName = s; - await Search(); - await GetListOfDlc(); + await Search().ConfigureAwait(false); + await GetListOfDlc().ConfigureAwait(false); } Status = "Ready."; @@ -297,7 +302,7 @@ namespace auto_creamapi.ViewModels MainWindowEnabled = false; var navigate = _navigationService.Navigate, SteamApp>( _cache.GetListOfAppsByName(GameName)); - await navigate; + await navigate.ConfigureAwait(false); var navigateResult = navigate.Result; if (navigateResult != null) { @@ -306,7 +311,7 @@ namespace auto_creamapi.ViewModels } } - await GetListOfDlc(); + await GetListOfDlc().ConfigureAwait(false); } else { @@ -324,7 +329,7 @@ namespace auto_creamapi.ViewModels var app = new SteamApp {AppId = AppId, Name = GameName}; var task = _cache.GetListOfDlc(app, UseSteamDb, IgnoreUnknown); MainWindowEnabled = false; - var listOfDlc = await task; + var listOfDlc = await task.ConfigureAwait(false); if (task.IsCompletedSuccessfully) { listOfDlc.Sort((app1, app2) => app1.AppId.CompareTo(app2.AppId)); diff --git a/auto-creamapi/ViewModels/SearchResultViewModel.cs b/auto-creamapi/ViewModels/SearchResultViewModel.cs index 8e26c1f..17804ca 100644 --- a/auto-creamapi/ViewModels/SearchResultViewModel.cs +++ b/auto-creamapi/ViewModels/SearchResultViewModel.cs @@ -67,7 +67,7 @@ namespace auto_creamapi.ViewModels if (Selected != null) { MyLogger.Log.Information($"Successfully got app {Selected}"); - await _navigationService.Close(this, Selected); + await _navigationService.Close(this, Selected).ConfigureAwait(false); } } diff --git a/auto-creamapi/Views/MainView.xaml b/auto-creamapi/Views/MainView.xaml index 8048119..154e9f4 100644 --- a/auto-creamapi/Views/MainView.xaml +++ b/auto-creamapi/Views/MainView.xaml @@ -102,7 +102,7 @@ VerticalAlignment="Bottom" Width="108" Command="{Binding GetListOfDlcCommand}" Grid.Row="4" /> - + diff --git a/auto-creamapi/Views/SearchResultView.xaml.cs b/auto-creamapi/Views/SearchResultView.xaml.cs index a3a63cc..fe8f364 100644 --- a/auto-creamapi/Views/SearchResultView.xaml.cs +++ b/auto-creamapi/Views/SearchResultView.xaml.cs @@ -11,38 +11,6 @@ namespace auto_creamapi.Views public SearchResultView() { InitializeComponent(); - //DgApps.ItemsSource = list; } - - /*private void OK_OnClick(object sender, RoutedEventArgs e) - { - GetSelectedApp(); - } - - private void DgApps_OnMouseDoubleClick(object sender, MouseButtonEventArgs e) - { - GetSelectedApp(); - } - - private void Cancel_OnClick(object sender, RoutedEventArgs e) - { - Close(); - } - - private void GetSelectedApp() - { - if (Application.Current.MainWindow is MainWindow currentMainWindow) - { - var app = (SteamApp) DgApps.SelectedItem; - if (app != null) - { - MyLogger.Log.Information($"Successfully got app {app}"); - //currentMainWindow.Game.Text = app.Name; - //currentMainWindow.AppId.Text = app.AppId.ToString(); - } - } - - Close(); - }*/ } } \ No newline at end of file