Skip to content

Commit

Permalink
Merge pull request #109 from BUTR/dev
Browse files Browse the repository at this point in the history
v1.5.5
  • Loading branch information
Aragas authored May 1, 2024
2 parents d210465 + d0d802f commit 4c3bf47
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 3 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,29 @@ jobs:
run: |
mkdir bannerlord;
dotnet build src/${{matrix.project}}/${{matrix.project}}.csproj --configuration Release -p:GameConfiguration="${{matrix.configuration}}" -p:GameFolder="$PWD/bannerlord" -p:BuildShared="false" /nowarn:MSB4011;
env:
BANNERLORD_BUTR_COMPATIBILITY_SCORE_URL: ${{ secrets.BANNERLORD_BUTR_COMPATIBILITY_SCORE_URL }}
shell: pwsh

- name: Add GPU acceleration hint
run: |
# dotnet tool install -g Topten.nvpatch;
# $env:DOTNET_ROLL_FORWARD="Major";
dotnet tool install -g dotnet-script;
$fileNames = Get-ChildItem -Path "bannerlord/" -Recurse -Include *.exe;
foreach ($f in $fileNames) {
echo $f.FullName;
# nvpatch --enable $f.FullName;
dotnet script build/gpu.csx -- --enable-gpu "$f";
}
shell: pwsh

- name: Upload Bannerlord folder
uses: actions/upload-artifact@v4
with:
name: bannerlord-${{matrix.project}}-${{matrix.configuration}}
path: ./bannerlord/
env:
BANNERLORD_BUTR_COMPATIBILITY_SCORE_URL: ${{ secrets.BANNERLORD_BUTR_COMPATIBILITY_SCORE_URL }}

###########################
# NEXUSMODS #
Expand Down
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<!--Development Variables-->
<PropertyGroup>
<Version>1.5.4</Version>
<Version>1.5.5</Version>
<HarmonyVersion>2.2.2</HarmonyVersion>
<BUTRSharedVersion>3.0.0.139</BUTRSharedVersion>
<BUTRModuleManagerVersion>5.0.222</BUTRModuleManagerVersion>
Expand Down
279 changes: 279 additions & 0 deletions build/gpu.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
#r "nuget: PE-Sharp, 1.0.0"

using System.Collections.Generic;
using PE_Sharp.PEStatic;
using PE_Sharp.Exceptions;
using PE_Sharp.PEFormat;
using System.Reflection;
using System.Linq;
using System.IO;
using System;

class Program
{
private static readonly string ExePath = AppDomain.CurrentDomain.BaseDirectory, ConfigPath = ExePath + "/config";

private const string SectionName = ".patchPE";//.nvpatch

private static readonly ExportSymbol[] GpuSymbols = {
new("NvOptimusEnablement"),
new("AmdPowerXpressRequestHighPerformance")
};

public static unsafe int Main(IList<string> args2)
{
var args = args2.ToArray();

// Local vars
List<ExportSymbol> exportSymbols = new();
var inFile = string.Empty;
var statusMode = false;
var enableGpuMode = false;
var disableGpuMode = false;
var enableMode = false;
var disableMode = false;
var quietYesMode = false;
var quietNoMode = false;

// Work out input/output file names
if (args.Length == 0)
{
ShowLogo();
Console.WriteLine("type '--help' or '/help' to get help");
return 0;
}

if (args.Length >= 1)
{
if (PEUtils.IsSwitch(args[0], out var name, out var value))
{
switch (name)
{
case "enable-gpu":
enableGpuMode = true;
break;
case "disable-gpu":
disableGpuMode = true;
break;
case "enable":
enableMode = true;
break;
case "disable":
disableMode = true;
break;
case "status":
statusMode = true;
break;
case "quiet-yes":
quietYesMode = true;
break;
case "quiet-no":
quietNoMode = true;
break;
case "help":
ShowLogo();
ShowHelp();
return 0;
case "version":
ShowLogo();
return 0;
}

if (name != "version" && name != "help" && !quietNoMode && !quietYesMode && args.Length == 1)
{
var exc = "only '--version' '--help' --'quite-no' ";
exc += "'--quite-yes' have one options";
throw new ArgException(exc);
}
}
else
{
var exc = $"don´t exist this option '{args[0]}'";
throw new ArgException(exc);
}
}
if (args.Length >= 2)
{
if (!enableMode && !disableMode && !statusMode
&& !enableGpuMode && !disableGpuMode)
{
var exc = "only '--enableMode' '--disableMode' '--enableGpuMode' ";
exc += "'--disableGpuMode' '--statusMode' have two or more options";
throw new ArgException(exc);
}

Console.WriteLine("DEU BOM 01"); //AAAAAAH
inFile = args[1];
if (enableGpuMode || disableGpuMode || statusMode)
exportSymbols.AddRange(GpuSymbols);
else if ((enableMode || disableMode) && args.Length == 2)
throw new ArgException("'--enableMode' '--disableMode' need more than three options");

}
if (args.Length >= 3)
{
if (enableMode || disableMode)
{
var expArgs = args[2..];
foreach (var exp in expArgs)
exportSymbols.Add(new(exp));
}
else
{
var exc = "only '--enableMode' '--disableMode' have more than three options";
throw new ArgException(exc);
}
}

if (enableGpuMode) enableMode = true;
if (disableGpuMode) disableMode = true;

// config file
var quietMode = false;
if (File.Exists(ConfigPath))
{
var options = File.ReadAllBytes(ConfigPath);
if (options.Length > 0)
{
if (options[0] == '0')
{
quietMode = false;
}
else if (options[0] == '1')
{
Console.WriteLine("DEU BOM 04"); //AAAAAAH
quietMode = true;
}
}
else
{
quietMode = false;
}
}
else
{
Console.WriteLine("DEU BOM 02"); //AAAAAAH
quietMode = false;
}

// Read the file
var pe = PEFileUtils.GetPEFile(inFile);

// Get (or create) the export table)
var exports = new PEExportTable(pe) ?? throw new NullException("exports");

// Just check it?
if (statusMode)
{
foreach (var symbol in exportSymbols)
{
var export = exports.Find(symbol.Name);
if (export == null)
{
Console.WriteLine($"Module doesn't export {symbol.Name} symbol");
}
else
{
var value = *(uint*)pe.GetRVA(export.RVA);
Console.WriteLine($"Module exports {symbol.Name} symbol as 0x{value:X8}");
}
}
return 0;
}

// Are all the symbols already present, update existing entries
if (exportSymbols.All(x => exports.Find(x.Name) != null))
{
foreach (var symbol in exportSymbols)
{
var exportsEntry = exports.Find(symbol.Name) ?? throw new NullException("exportsEntry");
*(uint*)pe.GetRVA(exportsEntry.RVA) = enableMode ? ExportSymbol.EnableValue : ExportSymbol.DisableValue;
}
}
else
{
if (pe.FindSection(SectionName) != null)
{
throw new InvalidOperationException($"Can't patch as some symbols are missing and {SectionName} section has already been created");
}

// Create a new section into which we'll write the changes
var newSection = pe.AddSection();
newSection.Name = SectionName;
newSection.Characteristics = SectionFlags.InitializedData | SectionFlags.MemRead;

// Setup the module name
exports.ModuleName = Path.GetFileName(args[1]);

// Create entres
foreach (var symbol in exportSymbols)
{
// Add export table entry
exports.Add(new PEExportTable.Entry()
{
Ordinal = exports.GetNextOrdinal(),
Name = symbol.Name,
RVA = newSection.CurrentRVA,
});

// Write it's value
newSection.OutputStream.Write((uint)(enableMode ? 1 : 0));
}

// Write the new exports table
var newExportDD = exports.Write(newSection);

// Patch the data directories with the new export table
pe.DataDirectories[(int)DataDirectoryIndex.ExportTable] = newExportDD;
}

// Clear the checksum (just in case)
pe.WindowsHeader.CheckSum = 0;

// Rewrite the file
pe.Write(inFile);
pe.Dispose();

if (!quietMode)
Console.WriteLine("OK");

Console.WriteLine("DEU BOM 03"); //AAAAAAH

return 0;
}

static void ShowLogo()
{
var show = $"""
EditBinPE v{Assembly.GetExecutingAssembly().GetName().Version}
PE-Sharp v{PEUtils.GetVersion()}
Copyright ©2023 Gabriel Frigo Software. All Rights Reserved

""";
Console.WriteLine(show);
}

static void ShowHelp()
{
var help = """
Usage: BinPeEx <options> [<inputfile.exe>] [<exp..>]

Adds, updates or queries the export symbols 'NvOptimusEnablement'
and 'AmdPowerXpressRequestHighPerformance' in an existing .exe

Options:
--enable-gpu <input> sets GPU export symbols to 1 (adding if missing)
--disable-gpu <input> sets GPU export symbols to 0 (if it exists)
--enable <input> <exp..> sets export symbols to 1 (adding if missing)
--disable <input> <exp..> sets export symbols to 0 (if it exists)
--status <input> shows the current export symbols status
--help show this help, or help for a command
--version show version information
--quite-yes
--quite-no
""";
Console.WriteLine(help);
}
}

Program.Main(Args);
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 1.5.5
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.1.6,v1.2.x
* BETA Release!
* Added support for GPU Acceleration Hints
---------------------------------------------------------------------------------------------------
Version: 1.5.4
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.1.6,v1.2.x
* BETA Release!
Expand Down
File renamed without changes.

0 comments on commit 4c3bf47

Please sign in to comment.