Fixed issues with extraction (migration from SharpCompress to Squid-Box.SevenZipSharp)

This commit is contained in:
Jeddunk 2021-01-04 15:43:27 +01:00
parent af8110e475
commit 2cfb7dc654
5 changed files with 47 additions and 36 deletions

View File

@ -45,17 +45,7 @@ namespace auto_creamapi.Services
string cacheString; string cacheString;
if (updateNeeded) if (updateNeeded)
{ {
MyLogger.Log.Information("Getting content from API..."); cacheString = await UpdateCache().ConfigureAwait(false);
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.");
} }
else else
{ {
@ -63,12 +53,27 @@ namespace auto_creamapi.Services
// ReSharper disable once MethodHasAsyncOverload // ReSharper disable once MethodHasAsyncOverload
cacheString = File.ReadAllText(CachePath); cacheString = File.ReadAllText(CachePath);
} }
var steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString); var steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString);
_cache = new HashSet<SteamApp>(steamApps.AppList.Apps); _cache = new HashSet<SteamApp>(steamApps.AppList.Apps);
MyLogger.Log.Information("Loaded cache into memory!"); MyLogger.Log.Information("Loaded cache into memory!");
} }
private static async Task<string> 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<SteamApp> GetListOfAppsByName(string name) public IEnumerable<SteamApp> GetListOfAppsByName(string name)
{ {
var listOfAppsByName = _cache.Search(x => x.Name) var listOfAppsByName = _cache.Search(x => x.Name)

View File

@ -4,15 +4,14 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using auto_creamapi.Messenger; using auto_creamapi.Messenger;
using auto_creamapi.Utils; using auto_creamapi.Utils;
using HttpProgress; using HttpProgress;
using MvvmCross.Plugin.Messenger; using MvvmCross.Plugin.Messenger;
using SharpCompress.Archives; using SevenZip;
using SharpCompress.Common;
using SharpCompress.Readers;
namespace auto_creamapi.Services namespace auto_creamapi.Services
{ {
@ -21,7 +20,7 @@ namespace auto_creamapi.Services
/*public void Initialize(); /*public void Initialize();
public Task InitializeAsync();*/ public Task InitializeAsync();*/
public Task<string> Download(string username, string password); public Task<string> Download(string username, string password);
public void Extract(string filename); public Task Extract(string filename);
} }
public class DownloadCreamApiService : IDownloadCreamApiService public class DownloadCreamApiService : IDownloadCreamApiService
@ -100,26 +99,30 @@ namespace auto_creamapi.Services
return filename; return filename;
} }
public void Extract(string filename) public async Task Extract(string filename)
{ {
MyLogger.Log.Debug("Extract"); 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}""..."); 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"); var expression1 = new Regex(@"nonlog_build\\steam_api(?:64)?\.dll");
_messenger.Publish(new ProgressMessage(this, "Extracting...", filename, 1.0)); _messenger.Publish(new ProgressMessage(this, "Extracting...", filename, 1.0));
foreach (var entry in archive.Entries) SevenZipBase.SetLibraryPath(Path.Combine(cwd, "resources/7z.dll"));
// ReSharper disable once InvertIf using (var extractor =
if (!entry.IsDirectory && expression1.IsMatch(entry.Key)) new SevenZipExtractor(filename, ArchivePassword, InArchiveFormat.Rar)
{ {PreserveDirectoryStructure = false})
MyLogger.Log.Debug(entry.Key); {
entry.WriteToDirectory(Directory.GetCurrentDirectory(), new ExtractionOptions await extractor.ExtractFilesAsync(cwd,
{ $"{nonlogBuild}\\{steamApi64Dll}",
ExtractFullPath = false, $"{nonlogBuild}\\{steamApiDll}");
Overwrite = true }
}); 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!"); MyLogger.Log.Information("Extraction done!");
} }
} }

View File

@ -71,7 +71,7 @@ namespace auto_creamapi.ViewModels
var filename = await download; var filename = await download;
/*var extract = _download.Extract(filename); /*var extract = _download.Extract(filename);
await extract;*/ await extract;*/
var extract = Task.Run(() => _download.Extract(filename)); var extract = _download.Extract(filename);
await extract.ConfigureAwait(false); await extract.ConfigureAwait(false);
_token.Dispose(); _token.Dispose();
await _navigationService.Close(this); await _navigationService.Close(this);

View File

@ -5,12 +5,12 @@
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>auto_creamapi</RootNamespace> <RootNamespace>auto_creamapi</RootNamespace>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<PackageVersion>2.1.3</PackageVersion> <PackageVersion>2.1.4</PackageVersion>
<Title>auto-creamapi</Title> <Title>auto-creamapi</Title>
<Authors>Jeddunk</Authors> <Authors>Jeddunk</Authors>
<Company>jeddunk.xyz</Company> <Company>jeddunk.xyz</Company>
<AssemblyVersion>2.1.3</AssemblyVersion> <AssemblyVersion>2.1.4</AssemblyVersion>
<FileVersion>2.1.3</FileVersion> <FileVersion>2.1.4</FileVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@ -29,7 +29,7 @@
<PackageReference Include="Serilog" Version="2.10.0" /> <PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" /> <PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="SharpCompress" Version="0.26.0" /> <PackageReference Include="Squid-Box.SevenZipSharp" Version="1.3.283" />
<PackageReference Include="SteamStorefrontAPI.NETStandard" Version="1.0.0" /> <PackageReference Include="SteamStorefrontAPI.NETStandard" Version="1.0.0" />
</ItemGroup> </ItemGroup>
@ -47,6 +47,9 @@
<Content Include="resources\CourierPrime-Regular.ttf"> <Content Include="resources\CourierPrime-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="resources\7z.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
</Project> </Project>

Binary file not shown.