diff --git a/auto-creamapi/Services/CacheService.cs b/auto-creamapi/Services/CacheService.cs index 41a990d..486a08a 100644 --- a/auto-creamapi/Services/CacheService.cs +++ b/auto-creamapi/Services/CacheService.cs @@ -45,17 +45,7 @@ namespace auto_creamapi.Services string cacheString; if (updateNeeded) { - MyLogger.Log.Information("Getting content from API..."); - var client = new HttpClient(); - var httpCall = client.GetAsync(SteamUri); - var response = await httpCall.ConfigureAwait(false); - var readAsStringAsync = response.Content.ReadAsStringAsync(); - var responseBody = await readAsStringAsync; - MyLogger.Log.Information("Got content from API successfully. Writing to file..."); - - await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8); - cacheString = responseBody; - MyLogger.Log.Information("Cache written to file successfully."); + cacheString = await UpdateCache().ConfigureAwait(false); } else { @@ -63,12 +53,27 @@ namespace auto_creamapi.Services // ReSharper disable once MethodHasAsyncOverload cacheString = File.ReadAllText(CachePath); } - var steamApps = JsonSerializer.Deserialize(cacheString); _cache = new HashSet(steamApps.AppList.Apps); MyLogger.Log.Information("Loaded cache into memory!"); } + private static async Task UpdateCache() + { + MyLogger.Log.Information("Getting content from API..."); + var client = new HttpClient(); + var httpCall = client.GetAsync(SteamUri); + var response = await httpCall.ConfigureAwait(false); + var readAsStringAsync = response.Content.ReadAsStringAsync(); + var responseBody = await readAsStringAsync; + MyLogger.Log.Information("Got content from API successfully. Writing to file..."); + + await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8); + var cacheString = responseBody; + MyLogger.Log.Information("Cache written to file successfully."); + return cacheString; + } + public IEnumerable GetListOfAppsByName(string name) { var listOfAppsByName = _cache.Search(x => x.Name) diff --git a/auto-creamapi/Services/DownloadCreamApiService.cs b/auto-creamapi/Services/DownloadCreamApiService.cs index 34126f1..0cc37f2 100644 --- a/auto-creamapi/Services/DownloadCreamApiService.cs +++ b/auto-creamapi/Services/DownloadCreamApiService.cs @@ -4,15 +4,14 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using auto_creamapi.Messenger; using auto_creamapi.Utils; using HttpProgress; using MvvmCross.Plugin.Messenger; -using SharpCompress.Archives; -using SharpCompress.Common; -using SharpCompress.Readers; +using SevenZip; namespace auto_creamapi.Services { @@ -21,7 +20,7 @@ namespace auto_creamapi.Services /*public void Initialize(); public Task InitializeAsync();*/ public Task Download(string username, string password); - public void Extract(string filename); + public Task Extract(string filename); } public class DownloadCreamApiService : IDownloadCreamApiService @@ -100,26 +99,30 @@ namespace auto_creamapi.Services return filename; } - public void Extract(string filename) + public async Task Extract(string filename) { MyLogger.Log.Debug("Extract"); + var cwd = Directory.GetCurrentDirectory(); + const string nonlogBuild = "nonlog_build"; + const string steamApi64Dll = "steam_api64.dll"; + const string steamApiDll = "steam_api.dll"; MyLogger.Log.Information($@"Start extraction of ""{filename}""..."); - var options = new ReaderOptions {Password = ArchivePassword}; - var archive = ArchiveFactory.Open(filename, options); var expression1 = new Regex(@"nonlog_build\\steam_api(?:64)?\.dll"); _messenger.Publish(new ProgressMessage(this, "Extracting...", filename, 1.0)); - foreach (var entry in archive.Entries) - // ReSharper disable once InvertIf - if (!entry.IsDirectory && expression1.IsMatch(entry.Key)) - { - MyLogger.Log.Debug(entry.Key); - entry.WriteToDirectory(Directory.GetCurrentDirectory(), new ExtractionOptions - { - ExtractFullPath = false, - Overwrite = true - }); - } - + 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}"); + } + 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!"); } } diff --git a/auto-creamapi/ViewModels/DownloadViewModel.cs b/auto-creamapi/ViewModels/DownloadViewModel.cs index ef83b23..80ea09a 100644 --- a/auto-creamapi/ViewModels/DownloadViewModel.cs +++ b/auto-creamapi/ViewModels/DownloadViewModel.cs @@ -71,7 +71,7 @@ namespace auto_creamapi.ViewModels var filename = await download; /*var extract = _download.Extract(filename); await extract;*/ - var extract = Task.Run(() => _download.Extract(filename)); + var extract = _download.Extract(filename); await extract.ConfigureAwait(false); _token.Dispose(); await _navigationService.Close(this); diff --git a/auto-creamapi/auto-creamapi.csproj b/auto-creamapi/auto-creamapi.csproj index 1d0cc07..19da18a 100644 --- a/auto-creamapi/auto-creamapi.csproj +++ b/auto-creamapi/auto-creamapi.csproj @@ -5,12 +5,12 @@ netcoreapp3.1 auto_creamapi true - 2.1.3 + 2.1.4 auto-creamapi Jeddunk jeddunk.xyz - 2.1.3 - 2.1.3 + 2.1.4 + 2.1.4 @@ -29,7 +29,7 @@ - + @@ -47,6 +47,9 @@ PreserveNewest + + PreserveNewest + \ No newline at end of file diff --git a/auto-creamapi/resources/7z.dll b/auto-creamapi/resources/7z.dll new file mode 100644 index 0000000..b32d7bf Binary files /dev/null and b/auto-creamapi/resources/7z.dll differ