2019-10-30 12:29:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
using Compression.BSA;
|
2019-10-31 02:24:42 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.CompilationSteps
|
|
|
|
|
{
|
2019-10-31 02:24:42 +00:00
|
|
|
|
public class DeconstructBSAs : ACompilationStep
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2019-10-31 02:24:42 +00:00
|
|
|
|
private readonly IEnumerable<string> _include_directly;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
private readonly List<ICompilationStep> _microstack;
|
|
|
|
|
private readonly List<ICompilationStep> _microstackWithInclude;
|
2019-11-11 20:03:27 +00:00
|
|
|
|
private readonly Compiler _mo2Compiler;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2019-11-03 16:45:49 +00:00
|
|
|
|
public DeconstructBSAs(ACompiler compiler) : base(compiler)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2019-11-11 20:03:27 +00:00
|
|
|
|
_mo2Compiler = (Compiler) compiler;
|
|
|
|
|
_include_directly = _mo2Compiler.ModInis.Where(kv =>
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
|
|
|
|
var general = kv.Value.General;
|
|
|
|
|
if (general.notes != null && general.notes.Contains(Consts.WABBAJACK_INCLUDE)) return true;
|
|
|
|
|
if (general.comments != null && general.comments.Contains(Consts.WABBAJACK_INCLUDE)) return true;
|
|
|
|
|
return false;
|
|
|
|
|
})
|
|
|
|
|
.Select(kv => $"mods\\{kv.Key}\\")
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
_microstack = new List<ICompilationStep>
|
|
|
|
|
{
|
2019-11-11 20:03:27 +00:00
|
|
|
|
new DirectMatch(_mo2Compiler),
|
|
|
|
|
new IncludePatches(_mo2Compiler),
|
|
|
|
|
new DropAll(_mo2Compiler)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_microstackWithInclude = new List<ICompilationStep>
|
|
|
|
|
{
|
2019-11-11 20:03:27 +00:00
|
|
|
|
new DirectMatch(_mo2Compiler),
|
|
|
|
|
new IncludePatches(_mo2Compiler),
|
|
|
|
|
new IncludeAll(_mo2Compiler)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
};
|
2019-10-31 02:24:42 +00:00
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2019-10-31 02:24:42 +00:00
|
|
|
|
public override IState GetState()
|
|
|
|
|
{
|
|
|
|
|
return new State();
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Directive Run(RawSourceFile source)
|
|
|
|
|
{
|
|
|
|
|
if (!Consts.SupportedBSAs.Contains(Path.GetExtension(source.Path).ToLower())) return null;
|
|
|
|
|
|
|
|
|
|
var defaultInclude = false;
|
|
|
|
|
if (source.Path.StartsWith("mods"))
|
|
|
|
|
if (_include_directly.Any(path => source.Path.StartsWith(path)))
|
|
|
|
|
defaultInclude = true;
|
|
|
|
|
|
2019-11-15 13:06:34 +00:00
|
|
|
|
var source_files = source.File.Children;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
|
|
|
|
var stack = defaultInclude ? _microstackWithInclude : _microstack;
|
|
|
|
|
|
|
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
|
|
2019-11-11 20:03:27 +00:00
|
|
|
|
var matches = source_files.PMap(e => _mo2Compiler.RunStack(stack, new RawSourceFile(e)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2019-11-15 13:06:34 +00:00
|
|
|
|
Path = Path.Combine(Consts.BSACreationDir, id, e.Name)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var match in matches)
|
|
|
|
|
{
|
2019-10-31 02:24:42 +00:00
|
|
|
|
if (match is IgnoredDirectly)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
Utils.Error($"File required for BSA {source.Path} creation doesn't exist: {match.To}");
|
2019-11-11 20:03:27 +00:00
|
|
|
|
_mo2Compiler.ExtraFiles.Add(match);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2019-10-31 02:24:42 +00:00
|
|
|
|
|
2019-10-30 12:29:06 +00:00
|
|
|
|
CreateBSA directive;
|
2019-11-16 00:01:37 +00:00
|
|
|
|
using (var bsa = BSADispatch.OpenRead(source.AbsolutePath))
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
|
|
|
|
directive = new CreateBSA
|
|
|
|
|
{
|
|
|
|
|
To = source.Path,
|
|
|
|
|
TempID = id,
|
|
|
|
|
State = bsa.State,
|
|
|
|
|
FileStates = bsa.Files.Select(f => f.State).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return directive;
|
|
|
|
|
}
|
2019-10-31 02:24:42 +00:00
|
|
|
|
|
|
|
|
|
[JsonObject("DeconstructBSAs")]
|
|
|
|
|
public class State : IState
|
|
|
|
|
{
|
2019-11-03 16:45:49 +00:00
|
|
|
|
public ICompilationStep CreateStep(ACompiler compiler)
|
2019-10-31 02:24:42 +00:00
|
|
|
|
{
|
|
|
|
|
return new DeconstructBSAs(compiler);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2019-10-31 02:24:42 +00:00
|
|
|
|
}
|