2019-10-30 12:29:06 +00:00
using System.IO ;
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
{
public PatchStockESMs ( Compiler compiler ) : base ( compiler )
{
}
public override Directive Run ( RawSourceFile source )
{
var filename = Path . GetFileName ( source . Path ) ;
var gameFile = Path . Combine ( _compiler . GamePath , "Data" , filename ) ;
if ( ! Consts . GameESMs . Contains ( filename ) | | ! source . Path . StartsWith ( "mods\\" ) | |
! File . Exists ( gameFile ) ) return null ;
2019-10-31 02:24:42 +00:00
2019-10-30 12:29:06 +00:00
Utils . Log (
$"A ESM named {filename} was found in a mod that shares a name with a core game ESMs, it is assumed this is a cleaned ESM and it will be binary patched." ) ;
var result = source . EvolveTo < CleanedESM > ( ) ;
result . SourceESMHash = _compiler . VFS . Lookup ( gameFile ) . Hash ;
Utils . Status ( $"Generating patch of {filename}" ) ;
using ( var ms = new MemoryStream ( ) )
{
Utils . CreatePatch ( File . ReadAllBytes ( gameFile ) , File . ReadAllBytes ( source . AbsolutePath ) , ms ) ;
var data = ms . ToArray ( ) ;
result . SourceDataID = _compiler . IncludeFile ( data ) ;
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
{
public ICompilationStep CreateStep ( Compiler compiler )
{
return new PatchStockESMs ( compiler ) ;
}
2019-10-30 12:29:06 +00:00
}
}
2019-10-31 02:24:42 +00:00
}