wabbajack/Wabbajack.Compiler/CompilationSteps/IncludeThisProfile.cs

62 lines
2.0 KiB
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.DTOs;
using Wabbajack.DTOs.Directives;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Compiler.CompilationSteps;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public class IncludeThisProfile : ACompilationStep
{
private readonly IEnumerable<AbsolutePath> _correctProfiles;
private readonly MO2Compiler _mo2Compiler;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public IncludeThisProfile(ACompiler compiler) : base(compiler)
{
_mo2Compiler = (MO2Compiler) compiler;
2022-09-27 03:02:39 +00:00
if (!compiler.Settings.IsMO2Modlist)
{
Disabled = true;
return;
}
_correctProfiles = _mo2Compiler._settings.AllProfiles
2021-10-23 16:51:17 +00:00
.Select(p => _mo2Compiler.MO2ProfileDir.Parent.Combine(p)).ToList();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override async ValueTask<Directive?> Run(RawSourceFile source)
{
if (!_correctProfiles.Any(p => source.AbsolutePath.InFolder(p)))
return null;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var data = source.Path.FileName == Consts.ModListTxt
? await ReadAndCleanModlist(source.AbsolutePath)
: await source.AbsolutePath.ReadAllBytesAsync();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var e = source.EvolveTo<InlineFile>();
e.SourceDataID = await _compiler.IncludeFile(data);
return e;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
private async Task<byte[]> ReadAndCleanModlist(AbsolutePath absolutePath)
{
var alwaysEnabledMods = _compiler._settings.AlwaysEnabled
.Where(f => f.InFolder(Consts.MO2ModFolderName))
.Where(f => f.Level > 1)
.Select(f => f.GetPart(1))
.ToHashSet();
2021-10-23 16:51:17 +00:00
var lines = await absolutePath.ReadAllLinesAsync()
.Where(l =>
2021-09-27 12:42:46 +00:00
{
var modName = l[1..].Trim();
2021-09-27 12:42:46 +00:00
return l.StartsWith("+")
|| alwaysEnabledMods.Contains(modName)
2021-09-27 12:42:46 +00:00
|| l.EndsWith("_separator");
}).ToList();
2021-10-23 16:51:17 +00:00
return Encoding.UTF8.GetBytes(string.Join(Consts.LineSeparator, lines));
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}