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;
|
2020-03-26 23:02:15 +00:00
|
|
|
|
using MessagePack;
|
2019-12-14 11:10:22 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2020-02-13 12:29:59 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-02-13 12:29:59 +00:00
|
|
|
|
Game = game.Game,
|
2020-03-28 20:42:45 +00:00
|
|
|
|
GameFile = (RelativePath)gameFile,
|
2019-12-14 11:10:22 +00:00
|
|
|
|
Hash = hash,
|
2020-02-13 12:29:59 +00:00
|
|
|
|
GameVersion = game.InstalledVersion
|
2019-12-14 11:10:22 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Prepare()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[MessagePackObject]
|
2019-12-14 11:10:22 +00:00
|
|
|
|
public class State : AbstractDownloadState
|
|
|
|
|
{
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[Key(0)]
|
2019-12-14 11:10:22 +00:00
|
|
|
|
public Game Game { get; set; }
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[Key(1)]
|
2020-03-28 20:42:45 +00:00
|
|
|
|
public RelativePath GameFile { get; set; }
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[Key(2)]
|
2020-03-22 15:50:53 +00:00
|
|
|
|
public Hash Hash { get; set; }
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[Key(3)]
|
2020-01-03 17:00:57 +00:00
|
|
|
|
public string GameVersion { get; set; }
|
2019-12-14 11:10:22 +00:00
|
|
|
|
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[IgnoreMember]
|
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
|
|
|
|
|
2020-03-26 23:02:15 +00:00
|
|
|
|
[IgnoreMember]
|
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-03-29 20:42:45 +00:00
|
|
|
|
await using var src = SourcePath.OpenRead();
|
|
|
|
|
await using var dest = destination.Create();
|
|
|
|
|
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-02-02 12:23:07 +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}"};
|
|
|
|
|
}
|
2019-12-14 11:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|