use async IO and Paths api

Let's see how I can code in GitHub's editor
This commit is contained in:
Timothy Baldridge 2020-05-12 19:48:57 -06:00 committed by GitHub
parent 84b27b2a9c
commit 233013c495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,65 +1,65 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem; using Alphaleonis.Win32.Filesystem;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wabbajack.Common; using Wabbajack.Common;
namespace Wabbajack.Lib.CompilationSteps namespace Wabbajack.Lib.CompilationSteps
{ {
public class IncludeThisProfile : ACompilationStep public class IncludeThisProfile : ACompilationStep
{ {
private readonly IEnumerable<AbsolutePath> _correctProfiles; private readonly IEnumerable<AbsolutePath> _correctProfiles;
private MO2Compiler _mo2Compiler; private MO2Compiler _mo2Compiler;
public IncludeThisProfile(ACompiler compiler) : base(compiler) public IncludeThisProfile(ACompiler compiler) : base(compiler)
{ {
_mo2Compiler = (MO2Compiler) compiler; _mo2Compiler = (MO2Compiler) compiler;
_correctProfiles = _mo2Compiler.SelectedProfiles.Select(p => _mo2Compiler.MO2ProfileDir.Parent.Combine(p)).ToList(); _correctProfiles = _mo2Compiler.SelectedProfiles.Select(p => _mo2Compiler.MO2ProfileDir.Parent.Combine(p)).ToList();
} }
public override async ValueTask<Directive?> Run(RawSourceFile source) public override async ValueTask<Directive?> Run(RawSourceFile source)
{ {
if (!_correctProfiles.Any(p => source.AbsolutePath.InFolder(p))) if (!_correctProfiles.Any(p => source.AbsolutePath.InFolder(p)))
return null; return null;
var data = source.Path.FileName == Consts.ModListTxt var data = source.Path.FileName == Consts.ModListTxt
? await ReadAndCleanModlist(source.AbsolutePath) ? await ReadAndCleanModlist(source.AbsolutePath)
: await source.AbsolutePath.ReadAllBytesAsync(); : await source.AbsolutePath.ReadAllBytesAsync();
var e = source.EvolveTo<InlineFile>(); var e = source.EvolveTo<InlineFile>();
e.SourceDataID = await _compiler.IncludeFile(data); e.SourceDataID = await _compiler.IncludeFile(data);
return e; return e;
} }
public override IState GetState() public override IState GetState()
{ {
return new State(); return new State();
} }
private async Task<byte[]> ReadAndCleanModlist(AbsolutePath absolutePath) private async Task<byte[]> ReadAndCleanModlist(AbsolutePath absolutePath)
{ {
var alwaysEnabled = _mo2Compiler.ModInis.Where(f => IgnoreDisabledMods.IsAlwaysEnabled(f.Value)) var alwaysEnabled = _mo2Compiler.ModInis.Where(f => IgnoreDisabledMods.IsAlwaysEnabled(f.Value))
.Select(f => f.Key) .Select(f => f.Key)
.Distinct(); .Distinct();
var lines = File.ReadAllLines(absolutePath.ToString()).Where(l => var lines = (await absolutePath.ReadAllLinesAsync()).Where(l =>
{ {
return l.StartsWith("+") return l.StartsWith("+")
|| alwaysEnabled.Any(x => x.Equals(l.Substring(1))) || alwaysEnabled.Any(x => x.Equals(l.Substring(1)))
|| l.EndsWith("_separator"); || l.EndsWith("_separator");
}).ToArray(); }).ToArray();
return Encoding.UTF8.GetBytes(string.Join("\r\n", lines)); return Encoding.UTF8.GetBytes(string.Join("\r\n", lines));
} }
[JsonObject("IncludeThisProfile")] [JsonObject("IncludeThisProfile")]
public class State : IState public class State : IState
{ {
public ICompilationStep CreateStep(ACompiler compiler) public ICompilationStep CreateStep(ACompiler compiler)
{ {
return new IncludeThisProfile(compiler); return new IncludeThisProfile(compiler);
} }
} }
} }
} }