Update ChangeDownload.cs to use Path code. Update MEGADownloader to not try and heal archives

This commit is contained in:
Timothy Baldridge 2020-07-12 21:50:12 -06:00
parent 145b5b01f3
commit d52a09094b
4 changed files with 113 additions and 111 deletions

View File

@ -180,6 +180,7 @@ namespace Wabbajack.CLI
Console.WriteLine(msg); Console.WriteLine(msg);
else else
Console.Write(msg); Console.Write(msg);
Console.Out.Flush();
} }
internal static ExitCode Exit(string msg, ExitCode code) internal static ExitCode Exit(string msg, ExitCode code)

View File

@ -19,19 +19,27 @@ namespace Wabbajack.CLI.Verbs
{ {
[IsDirectory(CustomMessage = "Downloads folder %1 does not exist!")] [IsDirectory(CustomMessage = "Downloads folder %1 does not exist!")]
[Option("input", Required = true, HelpText = "Input folder containing the downloads you want to move")] [Option("input", Required = true, HelpText = "Input folder containing the downloads you want to move")]
public string Input { get; set; } = ""; public string _input { get; set; } = "";
public AbsolutePath Input => (AbsolutePath)_input;
[IsDirectory(Create = true)] [IsDirectory(Create = true)]
[Option("output", Required = true, HelpText = "Output folder the downloads should be transferred to")] [Option("output", Required = true, HelpText = "Output folder the downloads should be transferred to")]
public string Output { get; set; } = ""; public string _output { get; set; } = "";
public AbsolutePath Output => (AbsolutePath)_output;
[IsFile(CustomMessage = "Modlist file %1 does not exist!")] [IsFile(CustomMessage = "Modlist file %1 does not exist!")]
[Option("modlist", Required = true, HelpText = "The Modlist, can either be a .wabbajack or a modlist.txt file")] [Option("modlist", Required = true, HelpText = "The Modlist, can either be a .wabbajack or a modlist.txt file")]
public string Modlist { get; set; } = ""; public string _modlist { get; set; } = "";
public AbsolutePath ModList => (AbsolutePath)_modlist;
[Option("mods", Required = false, [Option("mods", Required = false,
HelpText = "Mods folder location if the provided modlist file is an MO2 modlist.txt")] HelpText = "Mods folder location if the provided modlist file is an MO2 modlist.txt")]
public string Mods { get; set; } = ""; public string _mods { get; set; } = "";
public AbsolutePath Mods => (AbsolutePath)_mods;
[Option("copy", Default = true, HelpText = "Whether to copy the files", SetName = "copy")] [Option("copy", Default = true, HelpText = "Whether to copy the files", SetName = "copy")]
public bool Copy { get; set; } public bool Copy { get; set; }
@ -45,32 +53,63 @@ namespace Wabbajack.CLI.Verbs
[Option("meta", Default = true, HelpText = "Whether to also transfer the meta file for the archive")] [Option("meta", Default = true, HelpText = "Whether to also transfer the meta file for the archive")]
public bool IncludeMeta { get; set; } public bool IncludeMeta { get; set; }
private struct TransferFile private interface ITransferFile
{ {
public readonly string Input; public Task PerformCopy();
public readonly string Output; public AbsolutePath Output { get; }
public readonly bool IsMeta; }
public TransferFile(string input, string output, bool isMeta = false) private struct FileCopy : ITransferFile
{
private AbsolutePath _src;
private AbsolutePath _dest;
public FileCopy(AbsolutePath src, AbsolutePath dest)
{ {
Input = input; _src = src;
Output = output; _dest = dest;
IsMeta = isMeta;
} }
public async Task PerformCopy()
{
CLIUtils.Log($"Copying {_src} to {_dest}");
await _src.CopyToAsync(_dest);
}
public AbsolutePath Output => _dest;
}
private struct StringCopy : ITransferFile
{
private string _src;
private AbsolutePath _dest;
public StringCopy(string src, AbsolutePath dest)
{
_src = src;
_dest = dest;
}
public async Task PerformCopy()
{
CLIUtils.Log($"Writing data to {_src}");
await _dest.WriteAllTextAsync(_src);
}
public AbsolutePath Output => _dest;
} }
protected override async Task<ExitCode> Run() protected override async Task<ExitCode> Run()
{ {
var modListPath = (AbsolutePath)Modlist; if (ModList.Extension != Consts.ModListExtension && ModList.FileName != (RelativePath)"modlist.txt")
if (modListPath.Extension != Consts.ModListExtension && modListPath.FileName != (RelativePath)"modlist.txt") return CLIUtils.Exit($"The file {ModList} is not a valid modlist file!", ExitCode.BadArguments);
return CLIUtils.Exit($"The file {Modlist} is not a valid modlist file!", ExitCode.BadArguments);
if (Copy && Move) if (Copy && Move)
return CLIUtils.Exit("You can't set both copy and move flags!", ExitCode.BadArguments); return CLIUtils.Exit("You can't set both copy and move flags!", ExitCode.BadArguments);
var isModlist = modListPath.Extension == Consts.ModListExtension; var isModlist = ModList.Extension == Consts.ModListExtension;
var list = new List<TransferFile>(); var list = new List<ITransferFile>();
if (isModlist) if (isModlist)
{ {
@ -78,7 +117,7 @@ namespace Wabbajack.CLI.Verbs
try try
{ {
modlist = AInstaller.LoadFromFile(modListPath); modlist = AInstaller.LoadFromFile(ModList);
} }
catch (Exception e) catch (Exception e)
{ {
@ -92,30 +131,40 @@ namespace Wabbajack.CLI.Verbs
CLIUtils.Log($"Modlist contains {modlist.Archives.Count} archives."); CLIUtils.Log($"Modlist contains {modlist.Archives.Count} archives.");
using var queue = new WorkQueue();
CLIUtils.Log($"Hashing Downloads (this may take some time)");
var hashes = (await Input.EnumerateFiles().PMap(queue, async f =>
{
CLIUtils.Log($"Hashing {f}");
return (f, await f.FileHashCachedAsync());
}))
.GroupBy(d => d.Item2)
.ToDictionary(d => d.Key, d => d.First().f);
modlist.Archives.Do(a => modlist.Archives.Do(a =>
{ {
var inputPath = Path.Combine(Input, a.Name); if (!hashes.TryGetValue(a.Hash, out var inputPath))
var outputPath = Path.Combine(Output, a.Name);
if (!File.Exists(inputPath))
{ {
CLIUtils.Log($"File {inputPath} does not exist, skipping."); CLIUtils.Log($"Archive not found for hash {a.Hash}");
return; return;
} }
var outputPath = Output.Combine(a.Name);
CLIUtils.Log($"Adding {inputPath} to the transfer list."); CLIUtils.Log($"Adding {inputPath} to the transfer list.");
list.Add(new TransferFile(inputPath, outputPath)); list.Add(new FileCopy(inputPath, outputPath));
var metaInputPath = Path.Combine(inputPath, ".meta"); var metaInputPath = inputPath.WithExtension(Consts.MetaFileExtension);
var metaOutputPath = Path.Combine(outputPath, ".meta"); var metaOutputPath = outputPath.WithExtension(Consts.MetaFileExtension);
if (File.Exists(metaInputPath)) if (metaInputPath.Exists)
{ {
CLIUtils.Log($"Found meta file {metaInputPath}"); CLIUtils.Log($"Found meta file {metaInputPath}");
if (IncludeMeta) if (IncludeMeta)
{ {
CLIUtils.Log($"Adding {metaInputPath} to the transfer list."); CLIUtils.Log($"Adding {metaInputPath} to the transfer list.");
list.Add(new TransferFile(metaInputPath, metaOutputPath)); list.Add(new FileCopy(metaInputPath, metaOutputPath));
} }
else else
{ {
@ -134,7 +183,7 @@ namespace Wabbajack.CLI.Verbs
} }
CLIUtils.Log("Adding meta from archive info the transfer list"); CLIUtils.Log("Adding meta from archive info the transfer list");
list.Add(new TransferFile(a.Meta, metaOutputPath, true)); list.Add(new StringCopy(a.Meta, metaOutputPath));
} }
else else
{ {
@ -145,16 +194,16 @@ namespace Wabbajack.CLI.Verbs
} }
else else
{ {
if (!Directory.Exists(Mods)) if (!Mods.Exists)
return CLIUtils.Exit($"Mods directory {Mods} does not exist!", ExitCode.BadArguments); return CLIUtils.Exit($"Mods directory {Mods} does not exist!", ExitCode.BadArguments);
CLIUtils.Log($"Reading modlist.txt from {Modlist}"); CLIUtils.Log($"Reading modlist.txt from {ModList}");
string[] modlist = File.ReadAllLines(Modlist); var modlist = await ModList.ReadAllLinesAsync();
if (modlist == null || modlist.Length == 0) if (modlist == null || !modlist.Any())
return CLIUtils.Exit($"Provided modlist.txt file at {Modlist} is empty or could not be read!", ExitCode.BadArguments); return CLIUtils.Exit($"Provided modlist.txt file at {ModList} is empty or could not be read!", ExitCode.BadArguments);
var mods = modlist.Where(s => s.StartsWith("+")).Select(s => s.Substring(1)).ToHashSet(); var mods = modlist.Where(s => s.StartsWith("+")).Select(s => s.Substring(1)).Select(f => (RelativePath)f).ToHashSet();
if (mods.Count == 0) if (mods.Count == 0)
return CLIUtils.Exit("Counted mods from modlist.txt are 0!", ExitCode.BadArguments); return CLIUtils.Exit("Counted mods from modlist.txt are 0!", ExitCode.BadArguments);
@ -163,19 +212,19 @@ namespace Wabbajack.CLI.Verbs
var downloads = new HashSet<string>(); var downloads = new HashSet<string>();
Directory.EnumerateDirectories(Mods, "*", SearchOption.TopDirectoryOnly) Mods.EnumerateDirectories(recursive:false)
.Where(d => mods.Contains(Path.GetRelativePath(Path.GetDirectoryName(d), d))) .Where(d => mods.Contains(d.Parent.FileName))
.Do(d => .Do(async d =>
{ {
var meta = Path.Combine(d, "meta.ini"); var meta = d.Combine("meta.ini");
if (!File.Exists(meta)) if (!meta.Exists)
{ {
CLIUtils.Log($"Mod meta file {meta} does not exist, skipping"); CLIUtils.Log($"Mod meta file {meta} does not exist, skipping");
return; return;
} }
string[] ini = File.ReadAllLines(meta); var ini = await meta.ReadAllLinesAsync();
if (ini == null || ini.Length == 0) if (ini == null || !ini.Any())
{ {
CLIUtils.Log($"Mod meta file {meta} could not be read or is empty!"); CLIUtils.Log($"Mod meta file {meta} could not be read or is empty!");
return; return;
@ -192,26 +241,26 @@ namespace Wabbajack.CLI.Verbs
CLIUtils.Log($"Found {downloads.Count} installationFiles from mod metas."); CLIUtils.Log($"Found {downloads.Count} installationFiles from mod metas.");
Directory.EnumerateFiles(Input, "*", SearchOption.TopDirectoryOnly) Input.EnumerateFiles()
.Where(f => downloads.Contains(Path.GetFileNameWithoutExtension(f))) .Where(f => downloads.Contains(f.FileNameWithoutExtension.ToString()))
.Do(f => .Do(f =>
{ {
CLIUtils.Log($"Found archive {f}"); CLIUtils.Log($"Found archive {f}");
var outputPath = Path.Combine(Output, Path.GetFileName(f)); var outputPath = Output.Combine(f.FileName);
CLIUtils.Log($"Adding {f} to the transfer list"); CLIUtils.Log($"Adding {f} to the transfer list");
list.Add(new TransferFile(f, outputPath)); list.Add(new FileCopy(f, outputPath));
var metaInputPath = Path.Combine(f, ".meta"); var metaInputPath = f.WithExtension(Consts.MetaFileExtension);
if (File.Exists(metaInputPath)) if (metaInputPath.Exists)
{ {
CLIUtils.Log($"Found meta file for {f} at {metaInputPath}"); CLIUtils.Log($"Found meta file for {f} at {metaInputPath}");
if (IncludeMeta) if (IncludeMeta)
{ {
var metaOutputPath = Path.Combine(outputPath, ".meta"); var metaOutputPath = outputPath.WithExtension(Consts.MetaFileExtension);
CLIUtils.Log($"Adding {metaInputPath} to the transfer list."); CLIUtils.Log($"Adding {metaInputPath} to the transfer list.");
list.Add(new TransferFile(metaInputPath, metaOutputPath)); list.Add(new FileCopy(metaInputPath, metaOutputPath));
} }
else else
{ {
@ -229,19 +278,20 @@ namespace Wabbajack.CLI.Verbs
var success = 0; var success = 0;
var failed = 0; var failed = 0;
var skipped = 0; var skipped = 0;
list.Do(f => list.Do(async f =>
{ {
if (File.Exists(f.Output)) if (f.Output.Exists)
{ {
if (Overwrite) if (Overwrite)
{ {
CLIUtils.Log($"Output file {f.Output} already exists, it will be overwritten"); CLIUtils.Log($"Output file {f.Output} already exists, it will be overwritten");
if (f.IsMeta || Move) if (f is StringCopy || Move)
{ {
CLIUtils.Log($"Deleting file at {f.Output}"); CLIUtils.Log($"Deleting file at {f.Output}");
try try
{ {
File.Delete(f.Output); if (f.Output.Exists)
f.Output.Delete();
} }
catch (Exception e) catch (Exception e)
{ {
@ -258,51 +308,7 @@ namespace Wabbajack.CLI.Verbs
} }
} }
if (f.IsMeta) await f.PerformCopy();
{
CLIUtils.Log($"Writing meta data to {f.Output}");
try
{
File.WriteAllText(f.Output, f.Input, Encoding.UTF8);
success++;
}
catch (Exception e)
{
CLIUtils.Log($"Error while writing meta data to {f.Output}!\n{e}");
failed++;
}
}
else
{
if (Copy)
{
CLIUtils.Log($"Copying file {f.Input} to {f.Output}");
try
{
File.Copy(f.Input, f.Output, Overwrite ? CopyOptions.None : CopyOptions.FailIfExists, CopyMoveProgressHandler, null);
success++;
}
catch (Exception e)
{
CLIUtils.Log($"Error while copying file {f.Input} to {f.Output}!\n{e}");
failed++;
}
}
else if(Move)
{
CLIUtils.Log($"Moving file {f.Input} to {f.Output}");
try
{
File.Move(f.Input, f.Output, Overwrite ? MoveOptions.ReplaceExisting : MoveOptions.None, CopyMoveProgressHandler, null);
success++;
}
catch (Exception e)
{
CLIUtils.Log($"Error while moving file {f.Input} to {f.Output}!\n{e}");
failed++;
}
}
}
}); });
CLIUtils.Log($"Skipped transfers: {skipped}"); CLIUtils.Log($"Skipped transfers: {skipped}");
@ -311,15 +317,5 @@ namespace Wabbajack.CLI.Verbs
return 0; return 0;
} }
private static CopyMoveProgressResult CopyMoveProgressHandler(long totalfilesize, long totalbytestransferred, long streamsize, long streambytestransferred, int streamnumber, CopyMoveProgressCallbackReason callbackreason, object userdata)
{
Console.Write($"\r{' ', 100}");
Console.Write(totalfilesize == totalbytestransferred
? "\rTransfer complete!\n"
: $"\rTotal Size: {totalfilesize}, Transferred: {totalbytestransferred}");
return CopyMoveProgressResult.Continue;
}
} }
} }

View File

@ -212,7 +212,7 @@ TOP:
} }
public async Task<(Archive? Archive, TempFile NewFile)> FindUpgrade(Archive a) public virtual async Task<(Archive? Archive, TempFile NewFile)> FindUpgrade(Archive a)
{ {
var tmpFile = new TempFile(); var tmpFile = new TempFile();

View File

@ -193,6 +193,11 @@ namespace Wabbajack.Lib.Downloaders
return false; return false;
} }
} }
public override async Task<(Archive? Archive, TempFile NewFile)> FindUpgrade(Archive a)
{
return default;
}
} }
public ReactiveCommand<Unit, Unit> TriggerLogin { get; } public ReactiveCommand<Unit, Unit> TriggerLogin { get; }