public async void BrowserDownload(GameBananaRecord record) { DownloadWindow downloadWindow = new DownloadWindow(record); downloadWindow.ShowDialog(); if (downloadWindow.YesNo) { string downloadUrl = null; string fileName = null; if (record.Files.Count == 1) { downloadUrl = record.Files[0].DownloadUrl; fileName = record.Files[0].FileName; } else if (record.Files.Count > 1) { UpdateFileBox fileBox = new UpdateFileBox(record.Files, record.Title); fileBox.Activate(); fileBox.ShowDialog(); downloadUrl = fileBox.chosenFileUrl; fileName = fileBox.chosenFileName; } if (downloadUrl != null && fileName != null) { await DownloadFile(downloadUrl, fileName, new Progress <DownloadProgress>(ReportUpdateProgress), CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Token)); if (!cancelled) { await ExtractFile(fileName, record); } } } }
public DownloadWindow(GameBananaRecord record) { InitializeComponent(); DownloadText.Text = $"{record.Title}\nSubmitted by {record.Owner.Name}"; var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = record.Image; bitmap.EndInit(); Preview.Source = bitmap; }
private async Task ExtractFile(string fileName, GameBananaRecord record) { await Task.Run(() => { string _ArchiveSource = $@"{assemblyLocation}/Downloads/{fileName}"; string _ArchiveType = Path.GetExtension(fileName); string ArchiveDestination = $@"{assemblyLocation}/Mods/{string.Concat(record.Title.Split(Path.GetInvalidFileNameChars()))}"; // Find a unique destination if it already exists var counter = 2; while (Directory.Exists(ArchiveDestination)) { ArchiveDestination = $@"{assemblyLocation}/Mods/{string.Concat(record.Title.Split(Path.GetInvalidFileNameChars()))} ({counter})"; ++counter; } if (File.Exists(_ArchiveSource)) { try { if (Path.GetExtension(_ArchiveSource).Equals(".7z", StringComparison.InvariantCultureIgnoreCase)) { using (var archive = SevenZipArchive.Open(_ArchiveSource)) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(ArchiveDestination, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } } else { using (Stream stream = File.OpenRead(_ArchiveSource)) using (var reader = ReaderFactory.Open(stream)) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { reader.WriteEntryToDirectory(ArchiveDestination, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } } } if (!File.Exists($@"{ArchiveDestination}/mod.json")) { Metadata metadata = new Metadata(); metadata.submitter = record.Owner.Name; metadata.description = record.Description; metadata.preview = record.Image; metadata.homepage = record.Link; metadata.avi = record.Owner.Avatar; metadata.upic = record.Owner.Upic; metadata.cat = record.CategoryName; metadata.caticon = record.Category.Icon; metadata.lastupdate = record.DateUpdated; string metadataString = JsonSerializer.Serialize(metadata, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText($@"{ArchiveDestination}/mod.json", metadataString); } } catch (Exception e) { MessageBox.Show($"Couldn't extract {fileName}: {e.Message}", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } } // Check if folder output folder exists, if not nothing was extracted if (!Directory.Exists(ArchiveDestination)) { MessageBox.Show($"Didn't extract {fileName} due to improper format", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } else { // Only delete if successfully extracted File.Delete(_ArchiveSource); } }); }