2020-10-22 00:43:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
2019-12-04 01:26:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
|
using Wabbajack.DTOs.Directives;
|
|
|
|
|
using Wabbajack.Paths.IO;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
namespace Wabbajack.Compiler.CompilationSteps
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
|
|
|
|
public class IncludeStubbedConfigFiles : ACompilationStep
|
|
|
|
|
{
|
2019-11-18 00:17:06 +00:00
|
|
|
|
private readonly MO2Compiler _mo2Compiler;
|
2019-11-11 20:03:27 +00:00
|
|
|
|
|
2019-11-03 16:45:49 +00:00
|
|
|
|
public IncludeStubbedConfigFiles(ACompiler compiler) : base(compiler)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2019-11-18 00:17:06 +00:00
|
|
|
|
_mo2Compiler = (MO2Compiler) compiler;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 18:14:05 +00:00
|
|
|
|
public override async ValueTask<Directive?> Run(RawSourceFile source)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return Consts.ConfigFileExtensions.Contains(source.Path.Extension) ? await RemapFile(source) : null;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2020-04-09 18:14:05 +00:00
|
|
|
|
private async Task<Directive?> RemapFile(RawSourceFile source)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
var data = await source.AbsolutePath.ReadAllTextAsync();
|
2019-10-30 12:29:06 +00:00
|
|
|
|
var originalData = data;
|
|
|
|
|
|
2020-10-22 00:43:02 +00:00
|
|
|
|
data = RemapData(_mo2Compiler, data);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
|
|
|
|
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));
|
2019-10-30 12:29:06 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-10-22 00:43:02 +00:00
|
|
|
|
|
|
|
|
|
public static string RemapData(ACompiler compiler, string data)
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var gamePath = compiler._locator.GameLocation(compiler._settings.Game).ToString();
|
2020-10-22 00:43:02 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var sourcePath = compiler._settings.Source.ToString();
|
2021-07-09 23:52:00 +00:00
|
|
|
|
data = data.Replace(sourcePath, Consts.MO2_PATH_MAGIC_BACK, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
data = data.Replace(sourcePath.Replace("\\", "\\\\"), Consts.MO2_PATH_MAGIC_DOUBLE_BACK, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
data = data.Replace(sourcePath.Replace("\\", "/"), Consts.MO2_PATH_MAGIC_FORWARD, StringComparison.InvariantCultureIgnoreCase);
|
2020-10-22 00:43:02 +00:00
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var downloadsPath = compiler._settings.Source.ToString();
|
2021-07-09 23:52:00 +00:00
|
|
|
|
data = data.Replace(downloadsPath, Consts.DOWNLOAD_PATH_MAGIC_BACK, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
data = data.Replace(downloadsPath.Replace("\\", "\\\\"), Consts.DOWNLOAD_PATH_MAGIC_DOUBLE_BACK, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
data = data.Replace(downloadsPath.Replace("\\", "/"), Consts.DOWNLOAD_PATH_MAGIC_FORWARD, StringComparison.InvariantCultureIgnoreCase);
|
2020-10-22 00:43:02 +00:00
|
|
|
|
return data;
|
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2019-12-04 01:26:26 +00:00
|
|
|
|
}
|