wabbajack/Wabbajack.Lib/CompilationSteps/DeconstructBSAs.cs

124 lines
4.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using Compression.BSA;
using Newtonsoft.Json;
using Wabbajack.Common;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed.Errors;
2020-02-05 05:17:12 +00:00
using Wabbajack.VirtualFileSystem;
namespace Wabbajack.Lib.CompilationSteps
{
public class DeconstructBSAs : ACompilationStep
{
2020-04-27 21:32:19 +00:00
private readonly IEnumerable<RelativePath> _includeDirectly;
2020-02-05 05:17:12 +00:00
private readonly Func<VirtualFile, List<ICompilationStep>> _microstack;
private readonly Func<VirtualFile, List<ICompilationStep>> _microstackWithInclude;
private readonly MO2Compiler _mo2Compiler;
public DeconstructBSAs(ACompiler compiler) : base(compiler)
{
_mo2Compiler = (MO2Compiler) compiler;
2019-11-21 15:51:57 +00:00
_includeDirectly = _mo2Compiler.ModInis.Where(kv =>
{
var general = kv.Value.General;
2020-04-27 21:32:19 +00:00
if (general.notes != null && (general.notes.Contains(Consts.WABBAJACK_INCLUDE) || general.notes.Contains(Consts.WABBAJACK_NOMATCH_INCLUDE))) return true;
if (general.comments != null && (general.notes.Contains(Consts.WABBAJACK_INCLUDE) || general.notes.Contains(Consts.WABBAJACK_NOMATCH_INCLUDE))) return true;
return false;
})
2020-04-27 21:32:19 +00:00
.Select(kv => kv.Key.RelativeTo(_mo2Compiler.MO2Folder))
.ToList();
2020-02-05 05:17:12 +00:00
_microstack = bsa => new List<ICompilationStep>
{
new DirectMatch(_mo2Compiler),
2020-02-05 05:17:12 +00:00
new IncludePatches(_mo2Compiler, bsa),
new DropAll(_mo2Compiler)
};
2020-02-05 05:17:12 +00:00
_microstackWithInclude = bsa => new List<ICompilationStep>
{
new DirectMatch(_mo2Compiler),
2020-02-05 05:17:12 +00:00
new IncludePatches(_mo2Compiler, bsa),
new IncludeAll(_mo2Compiler)
};
}
public override IState GetState()
{
return new State();
}
public override async ValueTask<Directive?> Run(RawSourceFile source)
{
2020-03-25 22:30:43 +00:00
if (!Consts.SupportedBSAs.Contains(source.Path.Extension)) return null;
var defaultInclude = false;
2020-03-25 22:30:43 +00:00
if (source.Path.RelativeTo(_mo2Compiler.MO2Folder).InFolder(_mo2Compiler.MO2Folder.Combine(Consts.MO2ModFolderName)))
2019-11-21 15:51:57 +00:00
if (_includeDirectly.Any(path => source.Path.StartsWith(path)))
defaultInclude = true;
if (source.AbsolutePath.Size >= (long) 2 << 31)
{
await using var bsa = BSADispatch.OpenRead(source.AbsolutePath);
if (bsa.State is BSAStateObject)
{
Utils.Error(
$"BSA {source.AbsolutePath.FileName} is over 2GB in size, very few programs (Including Wabbajack) can create BSA files this large without causing CTD issues." +
$"Please re-compress this BSA into a more manageable size.");
}
}
2019-11-21 15:51:57 +00:00
var sourceFiles = source.File.Children;
2020-02-05 05:17:12 +00:00
var stack = defaultInclude ? _microstackWithInclude(source.File) : _microstack(source.File);
var id = Guid.NewGuid().ToString();
2020-04-27 21:32:19 +00:00
Func<Task>? _cleanup = null;
if (defaultInclude)
{
_cleanup = await source.File.Context.Stage(source.File.Children);
}
2020-04-15 11:53:49 +00:00
var matches = await sourceFiles.PMap(_mo2Compiler.Queue, e => _mo2Compiler.RunStack(stack, new RawSourceFile(e, Consts.BSACreationDir.Combine((RelativePath)id, (RelativePath)e.Name))));
foreach (var match in matches)
{
if (match is IgnoredDirectly)
Utils.ErrorThrow(new UnconvertedError($"File required for BSA {source.Path} creation doesn't exist: {match.To}"));
_mo2Compiler.ExtraFiles.Add(match);
}
CreateBSA directive;
await using (var bsa = BSADispatch.OpenRead(source.AbsolutePath))
{
2020-04-10 01:29:53 +00:00
directive = new CreateBSA(
state: bsa.State,
items: bsa.Files.Select(f => f.State).ToList())
{
To = source.Path,
2020-03-25 22:30:43 +00:00
TempID = (RelativePath)id,
};
}
2020-04-27 21:32:19 +00:00
if (_cleanup != null)
await _cleanup();
return directive;
}
[JsonObject("DeconstructBSAs")]
public class State : IState
{
public ICompilationStep CreateStep(ACompiler compiler)
{
return new DeconstructBSAs(compiler);
}
}
}
2019-12-04 04:12:08 +00:00
}