2019-10-30 12:29:06 +00:00
using System.IO ;
2019-12-04 01:26:26 +00:00
using System.Threading.Tasks ;
2019-10-31 02:24:42 +00:00
using Newtonsoft.Json ;
2019-10-30 12:29:06 +00:00
using Wabbajack.Common ;
using File = Alphaleonis . Win32 . Filesystem . File ;
using Path = Alphaleonis . Win32 . Filesystem . Path ;
namespace Wabbajack.Lib.CompilationSteps
{
2019-10-31 02:24:42 +00:00
public class PatchStockESMs : ACompilationStep
2019-10-30 12:29:06 +00:00
{
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 PatchStockESMs ( 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
}
2019-12-04 01:26:26 +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
var filename = source . Path . FileName ;
var gameFile = _mo2Compiler . GamePath . Combine ( ( RelativePath ) "Data" , filename ) ;
2019-10-30 12:29:06 +00:00
if ( ! Consts . GameESMs . Contains ( filename ) | | ! source . Path . StartsWith ( "mods\\" ) | |
2020-03-25 12:47:25 +00:00
! gameFile . Exists ) return null ;
2019-10-31 02:24:42 +00:00
2019-10-30 12:29:06 +00:00
Utils . Log (
2020-01-13 21:11:07 +00:00
$"An ESM named {filename} was found in a mod that shares a name with one of the core game ESMs, it is assumed this is a cleaned ESM and it will be binary patched" ) ;
2019-10-30 12:29:06 +00:00
var result = source . EvolveTo < CleanedESM > ( ) ;
2019-11-15 13:06:34 +00:00
result . SourceESMHash = _compiler . VFS . Index . ByRootPath [ gameFile ] . Hash ;
2019-10-30 12:29:06 +00:00
Utils . Status ( $"Generating patch of {filename}" ) ;
2020-02-05 05:17:12 +00:00
await using ( var ms = new MemoryStream ( ) )
2019-10-30 12:29:06 +00:00
{
2020-03-25 12:47:25 +00:00
await Utils . CreatePatch ( await gameFile . ReadAllBytesAsync ( ) , await source . AbsolutePath . ReadAllBytesAsync ( ) , ms ) ;
2019-10-30 12:29:06 +00:00
var data = ms . ToArray ( ) ;
2020-03-25 12:47:25 +00:00
result . SourceDataID = await _compiler . IncludeFile ( data ) ;
2019-10-30 12:29:06 +00:00
Utils . Log ( $"Generated a {data.Length} byte patch for {filename}" ) ;
}
2019-10-31 02:24:42 +00:00
2019-10-30 12:29:06 +00:00
return result ;
2019-10-31 02:24:42 +00:00
}
public override IState GetState ( )
{
return new State ( ) ;
}
2019-10-30 12:29:06 +00:00
2019-10-31 02:24:42 +00:00
[JsonObject("PatchStockESMs")]
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 PatchStockESMs ( compiler ) ;
}
2019-10-30 12:29:06 +00:00
}
}
2019-12-04 01:26:26 +00:00
}