wabbajack/Wabbajack.Lib/Downloaders/GameFileSourceDownloader.cs

105 lines
3.2 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using Newtonsoft.Json;
2019-12-14 11:10:22 +00:00
using Wabbajack.Common;
using Wabbajack.Common.Serialization.Json;
2019-12-14 11:10:22 +00:00
using Wabbajack.Lib.Validation;
using Game = Wabbajack.Common.Game;
namespace Wabbajack.Lib.Downloaders
{
public class GameFileSourceDownloader : IDownloader
{
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
2019-12-14 11:10:22 +00:00
{
var gameName = (string?)archiveINI?.General?.gameName;
var gameFile = (string?)archiveINI?.General?.gameFile;
2019-12-14 11:10:22 +00:00
if (gameName == null || gameFile == null)
2019-12-14 11:10:22 +00:00
return null;
2020-04-12 19:47:28 +00:00
if (!GameRegistry.TryGetByFuzzyName(gameName, out var game)) return null;
2019-12-14 11:10:22 +00:00
var path = game.TryGetGameLocation();
2020-03-25 22:30:43 +00:00
var filePath = path?.Combine(gameFile);
2019-12-14 17:30:52 +00:00
if (!(filePath?.Exists ?? false))
2019-12-14 11:10:22 +00:00
return null;
2020-03-25 22:30:43 +00:00
var fp = filePath.Value;
var hash = await fp.FileHashCachedAsync();
2019-12-14 11:10:22 +00:00
return new State(game.InstalledVersion)
2019-12-14 11:10:22 +00:00
{
Game = game.Game,
2020-03-28 20:42:45 +00:00
GameFile = (RelativePath)gameFile,
Hash = hash
2019-12-14 11:10:22 +00:00
};
}
public async Task Prepare()
{
}
[JsonName("GameFileSourceDownloader")]
2019-12-14 11:10:22 +00:00
public class State : AbstractDownloadState
{
public Game Game { get; set; }
2020-03-28 20:42:45 +00:00
public RelativePath GameFile { get; set; }
2020-03-22 15:50:53 +00:00
public Hash Hash { get; set; }
public string GameVersion { get; set; } = "";
public State(string gameVersion)
{
GameVersion = gameVersion;
}
2019-12-14 11:10:22 +00:00
public State()
{
}
[JsonIgnore]
internal AbsolutePath SourcePath => Game.MetaData().GameLocation().Combine(GameFile);
2019-12-14 11:10:22 +00:00
[JsonIgnore]
public override object[] PrimaryKey { get => new object[] {Game, GameVersion, GameFile}; }
2019-12-14 11:10:22 +00:00
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
return true;
}
2020-03-25 22:30:43 +00:00
public override async Task<bool> Download(Archive a, AbsolutePath destination)
2019-12-14 11:10:22 +00:00
{
await using var src = await SourcePath.OpenRead();
await using var dest = await destination.Create();
2020-03-29 20:42:45 +00:00
var size = SourcePath.Size;
await src.CopyToWithStatusAsync(size, dest, "Copying from Game folder");
return true;
2019-12-14 11:10:22 +00:00
}
public override async Task<bool> Verify(Archive a)
2019-12-14 11:10:22 +00:00
{
2020-03-25 22:30:43 +00:00
return SourcePath.Exists && await SourcePath.FileHashCachedAsync() == Hash;
2019-12-14 11:10:22 +00:00
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<GameFileSourceDownloader>();
}
public override string? GetManifestURL(Archive a)
{
return null;
}
public override string[] GetMetaIni()
{
return new[] {"[General]", $"gameName={Game.MetaData().MO2ArchiveName}", $"gameFile={GameFile}"};
}
2019-12-14 11:10:22 +00:00
}
}
}