Can now "download" archives from the game folder. This is useful if installers need to repack game files, they can source the files from the main game archive and then build a new BSA modifying/adding files as needed

This commit is contained in:
Timothy Baldridge 2019-12-09 16:52:17 -07:00
parent 02803c84e2
commit ed41308f12
4 changed files with 112 additions and 1 deletions

View File

@ -9,6 +9,7 @@ namespace Wabbajack.Lib.Downloaders
{
public static readonly List<IDownloader> Downloaders = new List<IDownloader>()
{
new GameFileSourceDownloader(),
new MegaDownloader(),
new DropboxDownloader(),
new GoogleDriveDownloader(),

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 AbstractDownloadState GetDownloaderState(dynamic archiveINI)
{
var gameName = (string)archiveINI?.General?.gameName;
var gameFile = (string)archiveINI?.General?.gameFile;
var game = GameRegistry.GetByMO2ArchiveName(gameName);
if (gameFile == null || gameFile == null || game == null)
return null;
var path = game.GameLocation();
var filePath = Path.Combine(path, gameFile);
if (!File.Exists(filePath))
return null;
var hash = filePath.FileHashCached();
return new State
{
Game = GameRegistry.GetByMO2ArchiveName(gameName).Game,
GameFile = gameFile,
Hash = hash,
};
}
public void Prepare()
{
}
public class State : AbstractDownloadState
{
public Game Game { get; set; }
public string GameFile { get; set; }
public string Hash { get; set; }
internal string SourcePath => Path.Combine(Game.MetaData().GameLocation(), GameFile);
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
return true;
}
public override void Download(Archive a, string destination)
{
using(var src = File.OpenRead(SourcePath))
using (var dest = File.OpenWrite(destination))
{
var size = new FileInfo(SourcePath).Length;
src.CopyToWithStatus(size, dest, "Copying from Game folder");
}
}
public override bool Verify()
{
return File.Exists(SourcePath) && SourcePath.FileHash() == Hash;
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<GameFileSourceDownloader>();
}
public override string GetReportEntry(Archive a)
{
return $"* Game File {Game} - {GameFile}";
}
}
}
}

View File

@ -116,6 +116,7 @@
<Compile Include="CompilationSteps\IStackStep.cs" />
<Compile Include="CompilationSteps\PatchStockESMs.cs" />
<Compile Include="CompilationSteps\Serialization.cs" />
<Compile Include="Downloaders\GameFileSourceDownloader.cs" />
<Compile Include="Downloaders\LoversLabDownloader.cs" />
<Compile Include="Downloaders\SteamWorkshopDownloader.cs" />
<Compile Include="LibCefHelpers\Init.cs" />

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Alphaleonis.Win32.Filesystem;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
using Wabbajack.Common.StatusFeed;
@ -11,6 +12,7 @@ using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.Validation;
using File = Alphaleonis.Win32.Filesystem.File;
using Game = Wabbajack.Common.Game;
namespace Wabbajack.Test
{
@ -298,8 +300,32 @@ namespace Wabbajack.Test
Assert.AreEqual("eSIyd+KOG3s=", Utils.FileHash(filename));
Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
}
[TestMethod]
public void GameFileSourceDownload()
{
DownloadDispatcher.GetInstance<LoversLabDownloader>().Prepare();
var ini = $@"[General]
gameName={Game.SkyrimSpecialEdition.MetaData().MO2ArchiveName}
gameFile=Data/Update.esm";
var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());
Assert.IsNotNull(state);
var converted = state.ViaJSON();
Assert.IsTrue(converted.Verify());
var filename = Guid.NewGuid().ToString();
Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List<string>() }));
converted.Download(new Archive { Name = "Update.esm" }, filename);
Assert.AreEqual("/DLG/LjdGXI=", Utils.FileHash(filename));
CollectionAssert.AreEqual(File.ReadAllBytes(Path.Combine(Game.SkyrimSpecialEdition.MetaData().GameLocation(), "Data/Update.esm")), File.ReadAllBytes(filename));
}
}