wabbajack/Wabbajack.Lib/Downloaders/GameFileSourceDownloader.cs

100 lines
3.0 KiB
C#
Raw Normal View History

2019-12-14 17:30:52 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2019-12-14 11:10:22 +00:00
using Alphaleonis.Win32.Filesystem;
using Wabbajack.Common;
using Wabbajack.Lib.Validation;
using File = Alphaleonis.Win32.Filesystem.File;
using Game = Wabbajack.Common.Game;
namespace Wabbajack.Lib.Downloaders
{
public class GameFileSourceDownloader : IDownloader
{
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
{
var gameName = (string)archiveINI?.General?.gameName;
var gameFile = (string)archiveINI?.General?.gameFile;
if (gameFile == null || gameFile == null)
return null;
var game = GameRegistry.GetByFuzzyName(gameName);
2019-12-14 11:10:22 +00:00
if (game == null) return null;
var path = game.GameLocation();
2020-03-25 22:30:43 +00:00
var filePath = path?.Combine(gameFile);
2019-12-14 17:30:52 +00:00
2020-03-25 22:30:43 +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 = game.Game,
2019-12-14 11:10:22 +00:00
GameFile = gameFile,
Hash = hash,
GameVersion = game.InstalledVersion
2019-12-14 11:10:22 +00:00
};
}
public async Task Prepare()
{
}
public class State : AbstractDownloadState
{
public Game Game { get; set; }
public string GameFile { get; set; }
2020-03-22 15:50:53 +00:00
public Hash Hash { get; set; }
public string GameVersion { get; set; }
2019-12-14 11:10:22 +00:00
2020-03-25 22:30:43 +00:00
internal AbsolutePath SourcePath => Game.MetaData().GameLocation().Value.Combine(GameFile);
2019-12-14 11:10:22 +00:00
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
{
2020-03-25 22:30:43 +00:00
using(var src = SourcePath.OpenRead())
using (var dest = destination.Create())
2019-12-14 11:10:22 +00:00
{
2020-03-25 22:30:43 +00:00
var size = SourcePath.Size;
2019-12-14 11:10:22 +00:00
src.CopyToWithStatus(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
}
}
}