wabbajack/Wabbajack.Lib/CompilationSteps/IncludeStubbedConfigfiles.cs

58 lines
2.4 KiB
C#
Raw Normal View History

2020-10-22 00:43:02 +00:00
using System;
using System.Text;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using Newtonsoft.Json;
using Wabbajack.Common;
namespace Wabbajack.Lib.CompilationSteps
{
public class IncludeStubbedConfigFiles : ACompilationStep
{
private readonly MO2Compiler _mo2Compiler;
public IncludeStubbedConfigFiles(ACompiler compiler) : base(compiler)
{
_mo2Compiler = (MO2Compiler) compiler;
}
public override async ValueTask<Directive?> Run(RawSourceFile source)
{
2020-03-25 12:47:25 +00:00
return Consts.ConfigFileExtensions.Contains(source.Path.Extension) ? await RemapFile(source) : null;
}
private async Task<Directive?> RemapFile(RawSourceFile source)
{
2020-03-25 12:47:25 +00:00
var data = await source.AbsolutePath.ReadAllTextAsync();
var originalData = data;
2020-10-22 00:43:02 +00:00
data = RemapData(_mo2Compiler, data);
if (data == originalData)
return null;
var result = source.EvolveTo<RemappedInlineFile>();
2020-03-25 22:30:43 +00:00
result.SourceDataID = await _compiler.IncludeFile(Encoding.UTF8.GetBytes(data));
return result;
}
2020-10-22 00:43:02 +00:00
public static string RemapData(ACompiler compiler, string data)
{
var gamePath = compiler.GamePath.Normalize();
data = data.Replace(gamePath, Consts.GAME_PATH_MAGIC_BACK, StringComparison.InvariantCultureIgnoreCase);
data = data.Replace(gamePath.Replace("\\", "\\\\"), Consts.GAME_PATH_MAGIC_DOUBLE_BACK, StringComparison.InvariantCultureIgnoreCase);
data = data.Replace(gamePath.Replace("\\", "/"), Consts.GAME_PATH_MAGIC_FORWARD, StringComparison.InvariantCultureIgnoreCase);
var sourcePath = compiler.SourcePath.Normalize();
data = data.Replace(sourcePath, Consts.MO2_PATH_MAGIC_BACK);
data = data.Replace(sourcePath.Replace("\\", "\\\\"), Consts.MO2_PATH_MAGIC_DOUBLE_BACK);
data = data.Replace(sourcePath.Replace("\\", "/"), Consts.MO2_PATH_MAGIC_FORWARD);
var downloadsPath = compiler.DownloadsPath.Normalize();
data = data.Replace(downloadsPath, Consts.DOWNLOAD_PATH_MAGIC_BACK);
data = data.Replace(downloadsPath.Replace("\\", "\\\\"),
Consts.DOWNLOAD_PATH_MAGIC_DOUBLE_BACK);
data = data.Replace(downloadsPath.Replace("\\", "/"), Consts.DOWNLOAD_PATH_MAGIC_FORWARD);
return data;
}
}
}