wabbajack/Wabbajack.CLI/Verbs/HashGameFiles.cs
2021-01-09 19:44:59 +01:00

61 lines
2.2 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using CommandLine;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack.CLI.Verbs
{
[Verb("hash-game-files", HelpText = "Hashes a game's files for inclusion in the public github repo")]
public class HashGamefiles : AVerb
{
[Option('o', "output", Required = true, HelpText = @"Output folder in which the file will be placed")]
public string OutputFolder { get; set; } = "";
private AbsolutePath _outputFolder => (AbsolutePath)OutputFolder;
[Option('g', "game", Required = true, HelpText = @"WJ Game to index")]
public string Game { get; set; } = "";
private Game _game => GameRegistry.GetByFuzzyName(Game).Game;
protected override async Task<ExitCode> Run()
{
var version = _game.MetaData().InstalledVersion;
var file = _outputFolder.Combine(_game.ToString(), version).WithExtension(new Extension(".json"));
file.Parent.CreateDirectory();
using var queue = new WorkQueue();
var gameLocation = _game.MetaData().GameLocation();
Utils.Log($"Hashing files for {_game} {version}");
var indexed = (await gameLocation
.EnumerateFiles()
.PMap(queue, async f =>
{
var hash = await f.FileHashCachedAsync();
if (hash == null) return null;
return new Archive(new GameFileSourceDownloader.State
{
Game = _game,
GameFile = f.RelativeTo(gameLocation),
Hash = hash.Value,
GameVersion = version
})
{
Name = f.FileName.ToString(),
Hash = hash.Value,
Size = f.Size
};
})).NotNull().ToArray();
Utils.Log($"Found and hashed {indexed.Length} files");
await indexed.ToJsonAsync(file, prettyPrint: true);
return ExitCode.Ok;
}
}
}