wabbajack/Wabbajack.CLI/Verbs/DownloadCef.cs

114 lines
4.0 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
2021-10-23 16:51:17 +00:00
using System.Runtime.InteropServices;
2021-09-27 12:42:46 +00:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Tar;
using Microsoft.Extensions.Logging;
using Wabbajack.CLI.DTOs;
using Wabbajack.Downloaders;
using Wabbajack.DTOs;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Version = System.Version;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.CLI.Verbs;
public class DownloadCef : IVerb
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly DownloadDispatcher _dispatcher;
private readonly FileExtractor.FileExtractor _fileExtractor;
private readonly HttpClient _httpClient;
private readonly ILogger<DownloadCef> _logger;
public DownloadCef(ILogger<DownloadCef> logger, DownloadDispatcher dispatcher,
FileExtractor.FileExtractor fileExtractor, HttpClient httpClient)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger = logger;
_dispatcher = dispatcher;
_fileExtractor = fileExtractor;
_httpClient = httpClient;
}
public Command MakeCommand()
{
var command = new Command("download-cef");
command.Add(new Option<AbsolutePath>(new[] {"-f", "-folder"}, "Path to Wabbajack"));
command.Add(new Option<bool>(new[] {"--force"}, "Force the download even if the output already exists"));
command.Description = "Downloads CEF into this folder";
command.Handler = CommandHandler.Create(Run);
return command;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task<int> Run(AbsolutePath folder, bool force = false)
{
if (folder == default) folder = KnownFolders.EntryPoint;
var cefNet = folder.Combine("CefNet.dll");
if (!cefNet.FileExists())
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger.LogError("Cannot find CefNet.dll in {folder}", folder);
return 1;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
var version = Version.Parse(FileVersionInfo.GetVersionInfo(cefNet.ToString()).FileVersion!);
var downloadVersion = $"{version.Major}.{version.Minor}";
var runtime = RuntimeInformation.RuntimeIdentifier;
if (folder.Combine("libcef.dll").FileExists() && !force)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Not downloading, cef already exists");
return 0;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Downloading Cef version {version} for {runtime}", downloadVersion, runtime);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var versions = await CefCDNResponse.Load(_httpClient);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var findSource = versions.FindSource(downloadVersion);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var fileUri = new Uri($"https://cef-builds.spotifycdn.com/{findSource.Name}");
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var parsed = _dispatcher.Parse(fileUri);
var tempFile = folder.Combine(findSource.Name);
await _dispatcher.Download(new Archive {State = parsed!}, tempFile, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
{
_logger.LogInformation("Extracting {file}", tempFile);
await using var istream = tempFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
await using var bzip2 = new BZip2InputStream(istream);
await using var tar = new TarInputStream(bzip2, Encoding.UTF8);
var prefix = tempFile.FileName.WithoutExtension().WithoutExtension().Combine("Release");
var fullPrefix = prefix.RelativeTo(folder);
while (true)
{
var entry = tar.GetNextEntry();
if (entry == null) break;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var path = entry.Name.ToRelativePath();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
if (path.InFolder(prefix) && entry.Size > 0)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var outputPath = path.RelativeTo(folder).RelativeTo(fullPrefix).RelativeTo(folder);
outputPath.Parent.CreateDirectory();
_logger.LogInformation("Extracting {FileName} to {Folder}", outputPath.FileName,
outputPath.RelativeTo(folder));
await using var os = outputPath.Open(FileMode.Create, FileAccess.Write);
tar.CopyEntryContents(os);
2021-09-27 12:42:46 +00:00
}
}
}
2021-10-23 16:51:17 +00:00
tempFile.Delete();
return 0;
2021-09-27 12:42:46 +00:00
}
}