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;
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<SteamApps>(cacheString);
_cache = new HashSet<SteamApp>(steamApps.AppList.Apps);
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)
{
var listOfAppsByName = _cache.Search(x => x.Name)

View File

@ -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<string> 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!");
}
}

View File

@ -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);

View File

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

Binary file not shown.