mirror of
https://github.com/AdvDebug/Brovan
synced 2026-08-09 11:57:17 +00:00
Replace editbin postbuild with GuardCF task
Reworked the post-build CFG handling in `Brovan.csproj` by replacing the Windows batch `editbin.exe` flow with an inline MSBuild `RoslynCodeTaskFactory` task (`DisableGuardCF`). The new task directly edits the built `.exe` PE header to clear the GuardCF bit when present, removing the dependency on VsDevCmd/editbin availability.
This commit is contained in:
+35
-2
@@ -20,8 +20,41 @@
|
||||
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="$([MSBuild]::IsOSPlatform('Windows'))">
|
||||
<Exec Command="@echo off
:: Detect if this is a Linux target build
set "RID=$(RuntimeIdentifier)"
set "IS_LINUX=0"
if not "%25RID%25"=="" (
 echo %25RID%25 | findstr /i "linux" >nul 2>&1
 if not errorlevel 1 set "IS_LINUX=1"
)

:: Skip editbin entirely for Linux builds
if "%25IS_LINUX%25"=="1" (
 echo Skipping editbin for Linux build.
 exit /b 0
)

:: Get the target path (which is the dll)
set "ORIGINAL_PATH=$(TargetPath)"
:: Replace the target path dll extension with an exe extension to pass it to editbin later.
set "MODIFIED_PATH=%25ORIGINAL_PATH:.dll=.exe%25"
echo Calling VsDevCmd.bat...
call "$(DevEnvDir)..\Tools\VsDevCmd.bat" >nul 2>&1
if errorlevel 1 (
 echo Warning: Couldn't execute visual studio developer command prompt.
 exit /b 0
)
:: Check if editbin.exe is now available
where editbin.exe >nul 2>&1
if errorlevel 1 (
 echo Warning: editbin.exe not found in environment. CFG won't be disabled, Brovan will try to disable it at runtime.
) else (
 :: Disable CFG using editbin.exe
 editbin.exe /GUARD:NO %25MODIFIED_PATH%25 >nul 2>&1
 if errorlevel 1 (
 echo editbin.exe failed to run, CFG won't be disabled so Brovan will try to disable it at runtime.
 ) else (
 echo Disabled CFG using editbin.exe successfully.
 )
)
exit /b 0" />
|
||||
<UsingTask TaskName="DisableGuardCF" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)/Microsoft.Build.Tasks.Core.dll">
|
||||
<ParameterGroup>
|
||||
<FilePath ParameterType="System.String" Required="true" />
|
||||
</ParameterGroup>
|
||||
<Task>
|
||||
<Using Namespace="System" />
|
||||
<Using Namespace="System.IO" />
|
||||
<Code Type="Fragment" Language="cs"><![CDATA[
|
||||
byte[] Image = File.ReadAllBytes(FilePath);
|
||||
if (Image.Length < 0x40 || Image[0] != (byte)'M' || Image[1] != (byte)'Z')
|
||||
return true;
|
||||
|
||||
int PeHeaderOffset = BitConverter.ToInt32(Image, 0x3C);
|
||||
|
||||
int DllCharacteristicsOffset = PeHeaderOffset + 4 + 20 + 0x46;
|
||||
if (PeHeaderOffset <= 0 || DllCharacteristicsOffset + 2 > Image.Length ||
|
||||
Image[PeHeaderOffset] != (byte)'P' || Image[PeHeaderOffset + 1] != (byte)'E')
|
||||
return true;
|
||||
|
||||
const ushort GuardCF = 0x4000;
|
||||
ushort DllCharacteristics = BitConverter.ToUInt16(Image, DllCharacteristicsOffset);
|
||||
if ((DllCharacteristics & GuardCF) == 0)
|
||||
return true;
|
||||
|
||||
DllCharacteristics &= unchecked((ushort)~GuardCF);
|
||||
Image[DllCharacteristicsOffset] = (byte)DllCharacteristics;
|
||||
Image[DllCharacteristicsOffset + 1] = (byte)(DllCharacteristics >> 8);
|
||||
File.WriteAllBytes(FilePath, Image);
|
||||
Log.LogMessage(MessageImportance.High, "Cleared the GuardCF flag in " + Path.GetFileName(FilePath) + " so Unicorn can run without a CFG-disabling restart.");
|
||||
]]></Code>
|
||||
</Task>
|
||||
</UsingTask>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="Exists('$(TargetDir)$(TargetName).exe')">
|
||||
<DisableGuardCF FilePath="$(TargetDir)$(TargetName).exe" />
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user