2020-04-06 20:48:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Newtonsoft.Json;
|
2019-12-14 11:10:22 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
|
2019-12-14 11:10:22 +00:00
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
var gameName = (string?)archiveINI?.General?.gameName;
|
|
|
|
|
var gameFile = (string?)archiveINI?.General?.gameFile;
|
2019-12-14 11:10:22 +00:00
|
|
|
|
|
2020-04-09 20:20:34 +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
|
|
|
|
|
2020-04-09 18:57:02 +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
|
|
|
|
|
2020-04-09 20:20:34 +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
|
|
|
|
|
2020-04-09 20:20:34 +00:00
|
|
|
|
return new State(game.InstalledVersion)
|
2019-12-14 11:10:22 +00:00
|
|
|
|
{
|
2020-02-13 12:29:59 +00:00
|
|
|
|
Game = game.Game,
|
2020-03-28 20:42:45 +00:00
|
|
|
|
GameFile = (RelativePath)gameFile,
|
2020-04-09 20:20:34 +00:00
|
|
|
|
Hash = hash
|
2019-12-14 11:10:22 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Prepare()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[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; }
|
2020-05-02 21:09:29 +00:00
|
|
|
|
public string GameVersion { get; set; } = "";
|
2020-04-09 20:20:34 +00:00
|
|
|
|
|
|
|
|
|
public State(string gameVersion)
|
|
|
|
|
{
|
|
|
|
|
GameVersion = gameVersion;
|
|
|
|
|
}
|
2019-12-14 11:10:22 +00:00
|
|
|
|
|
2020-05-02 21:09:29 +00:00
|
|
|
|
public State()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-04-09 18:57:02 +00:00
|
|
|
|
internal AbsolutePath SourcePath => Game.MetaData().GameLocation().Combine(GameFile);
|
2019-12-14 11:10:22 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-01-03 17:00:57 +00:00
|
|
|
|
public override object[] PrimaryKey { get => new object[] {Game, GameVersion, GameFile}; }
|
2020-01-01 16:19:06 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2020-05-25 17:34:25 +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");
|
|
|
|
|
|
2020-01-18 19:57:26 +00:00
|
|
|
|
return true;
|
2019-12-14 11:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:55:55 +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>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public override string? GetManifestURL(Archive a)
|
2020-02-02 12:15:29 +00:00
|
|
|
|
{
|
2020-02-02 12:23:07 +00:00
|
|
|
|
return null;
|
2020-02-02 12:15:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
|
|
|
|
return new[] {"[General]", $"gameName={Game.MetaData().MO2ArchiveName}", $"gameFile={GameFile}"};
|
|
|
|
|
}
|
2020-05-02 21:09:29 +00:00
|
|
|
|
|
2019-12-14 11:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|