Skip to content

Commit

Permalink
chore: fix some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Aug 22, 2024
1 parent beae9c9 commit 480a1b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
37 changes: 16 additions & 21 deletions DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ internal class DownloadTask : IDisposable {
private ConcurrentDeque<Measurement> Entries { get; } = new();
private Util.SentryTransaction? Transaction { get; set; }
private bool SupportsHardLinks { get; set; }

/// <summary>
/// A mapping of existing file paths to their hashes.
/// </summary>
private IReadOnlyDictionary<string, string>? ExistingPathHashes { get; set; }
private IReadOnlyDictionary<string, string> ExistingPathHashes { get; set; } = new Dictionary<string, string>();

/// <summary>
/// A mapping of existing hashes to their file paths.
/// </summary>
private IReadOnlyDictionary<string, string>? ExistingHashPaths { get; set; }
private IReadOnlyDictionary<string, string> ExistingHashPaths { get; set; } = new Dictionary<string, string>();

/// <summary>
/// A list of files expected by the group jsons made by this task. These
Expand Down Expand Up @@ -168,6 +170,7 @@ private async Task Run() {
this.VariantName = info.Variant.Name;
this.GenerateModDirectoryPath(info);
await this.TestHardLinks();
await this.HashExistingFiles();
await this.DownloadFiles(info);
await this.ConstructModPack(info);
await this.AddMod(info);
Expand Down Expand Up @@ -320,7 +323,7 @@ private async Task HashExistingFiles() {

// path => hash
var hashes = new ConcurrentDictionary<string, string>();
var allFiles = DirectoryHelper.GetFilesRecursive(this.ModDirectory).ToList();
var allFiles = DirectoryHelper.GetFilesRecursive(filesPath).ToList();

this.StateDataMax = (uint) allFiles.Count;

Expand Down Expand Up @@ -410,8 +413,6 @@ private IEnumerable<Task> DownloadNormalFiles(IDownloadTask_GetVersion_NeededFil
return neededFiles.Files.Files
.Select(pair => Task.Run(async () => {
var (hash, files) = pair;
// FIXME: here and batched: this does not account for duplicated ui files.
// this ends up always redownloading ui files that are duped
var outputPaths = GetOutputPaths(files);
using (await SemaphoreGuard.WaitAsync(Plugin.DownloadSemaphore, this.CancellationToken.Token)) {
Expand Down Expand Up @@ -451,7 +452,7 @@ private IEnumerable<Task> DownloadBatchedFiles(IDownloadTask_GetVersion_NeededFi
// find which files from this batch we already have a hash for
var toDuplicate = new HashSet<string>();
foreach (var hash in batchedFiles.Keys) {
if (!this.ExistingHashPaths!.TryGetValue(hash, out var path)) {
if (!this.ExistingHashPaths.TryGetValue(hash, out var path)) {
continue;
}
Expand All @@ -461,7 +462,7 @@ private IEnumerable<Task> DownloadBatchedFiles(IDownloadTask_GetVersion_NeededFi
// sort files in batch by offset, removing already-downloaded files
var listOfFiles = batchedFiles
.Select(pair => (Hash: pair.Key, Info: pair.Value))
.Where(pair => !this.ExistingHashPaths!.ContainsKey(pair.Hash))
.Where(pair => !this.ExistingHashPaths.ContainsKey(pair.Hash))
.OrderBy(pair => pair.Info.Offset).ToList();
if (listOfFiles.Count > 0) {
Expand Down Expand Up @@ -561,7 +562,7 @@ await Plugin.Resilience.ExecuteAsync(
continue;
}
if (!this.ExistingPathHashes!.TryGetValue(path, out var hash)) {
if (!this.ExistingPathHashes.TryGetValue(path, out var hash)) {
throw new Exception("missing hash for file to duplicate");
}
Expand Down Expand Up @@ -597,11 +598,8 @@ StateCounter counter
});

// construct the request
using var req = new HttpRequestMessage(HttpMethod.Get, uri) {
Headers = {
Range = rangeHeader,
},
};
using var req = new HttpRequestMessage(HttpMethod.Get, uri);
req.Headers.Range = rangeHeader;

// send the request
using var resp = await Plugin.Client.SendAsync2(req, HttpCompletionOption.ResponseHeadersRead, this.CancellationToken.Token);
Expand Down Expand Up @@ -754,6 +752,7 @@ private async Task DuplicateFile(string filesDir, IEnumerable<string> outputPath
return;
}

// ReSharper disable once AccessToModifiedClosure
Plugin.Resilience.Execute(() => File.Move(path, dest));
path = dest;
return;
Expand Down Expand Up @@ -788,7 +787,7 @@ private void RemoveOldFiles() {
using var span = this.Transaction?.StartChild(nameof(this.RemoveOldFiles));

this.State = State.RemovingOldFiles;
this.SetStateData(0, 1);
this.SetStateData(0, 0);

// find old, normal files no longer being used to remove
var filesPath = Path.Join(this.PenumbraModPath, "files");
Expand Down Expand Up @@ -842,7 +841,7 @@ private async Task DownloadFile(Uri baseUri, string filesPath, string[] outputPa
}

// find an existing path that has this hash
if (this.ExistingHashPaths!.TryGetValue(hash, out var validPath)) {
if (this.ExistingHashPaths.TryGetValue(hash, out var validPath)) {
goto Duplicate;
}

Expand Down Expand Up @@ -1006,15 +1005,13 @@ private async Task<DefaultMod> ConstructDefaultMod(IDownloadTask_GetVersion info
Manipulations = ManipTokensForOption(info.NeededFiles.Manipulations.FirstOrDefault(group => group.Name == null)?.Options, null),
FileSwaps = info.DefaultOption?.FileSwaps.Swaps ?? [],
};
foreach (var (hash, files) in info.NeededFiles.Files.Files) {
foreach (var files in info.NeededFiles.Files.Files.Values) {
foreach (var file in files) {
if (file[0] != null || file[1] != null) {
continue;
}

var gamePath = file[2]!;
var outputPath = file[3];

var replacedPath = this.SupportsHardLinks
? GetReplacedPath(file)
: GetReplacedPath(files[0]);
Expand Down Expand Up @@ -1136,7 +1133,6 @@ private async Task<List<ModGroup>> ConstructGroups(IDownloadTask_GetVersion info
var group = file[0] ?? DefaultFolder;
var option = file[1] ?? DefaultFolder;
var gamePath = file[2]!;
var outputPath = file[3];

if (!pathsMap.TryGetValue(group, out var groupMap)) {
groupMap = [];
Expand Down Expand Up @@ -1165,7 +1161,6 @@ private async Task<List<ModGroup>> ConstructGroups(IDownloadTask_GetVersion info
var groupName = file[0]!;
var optionName = file[1]!;
var gamePath = file[2]!;
var outputPath = file[3];

var modGroup = modGroups[groupName];
if (modGroup is not StandardModGroup standard) {
Expand Down Expand Up @@ -1503,7 +1498,7 @@ private async Task DuplicateUiFiles(DefaultMod defaultMod, List<ModGroup> modGro
}

// then find any uniquely referenced more than once
foreach (var (gamePath, outputPathCounts) in references) {
foreach (var outputPathCounts in references.Values) {
foreach (var (joinedOutputPath, (refs, updatePathActions)) in outputPathCounts) {
var outputPath = joinedOutputPath[6..];
if (refs < 2) {
Expand Down
10 changes: 6 additions & 4 deletions Ui/NotificationProgressManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private State UpdateNotif(IActiveNotification notif, DownloadTask task) {
if (task.PackageName is { } packageName) {
sb.Append(packageName);

if (task.VariantName is { } variantName) {
if (task.VariantName is { } variantName) {
if (variantName != Consts.DefaultVariant || !this.Plugin.Config.HideDefaultVariant) {
sb.Append(" (");
sb.Append(variantName);
Expand All @@ -149,9 +149,11 @@ private State UpdateNotif(IActiveNotification notif, DownloadTask task) {
? error == null
? $"{state.Name()}"
: $"{state.Name()} ({error.GetType().Name})"
: sMax == 0
? $"{state.Name()} ({sData:N0}){speed}"
: $"{state.Name()} ({sData:N0} / {sMax:N0}){speed}";
: sMax == 0 && sData == 0
? $"{state.Name()}"
: sMax == 0
? $"{state.Name()} ({sData:N0}){speed}"
: $"{state.Name()} ({sData:N0} / {sMax:N0}){speed}";
notif.Progress = sMax == 0
? 0
: (float) sData / sMax;
Expand Down

0 comments on commit 480a1b1

Please sign in to comment.