Skip to content

Commit

Permalink
Binding Redirects: Last attempt to hopefully get them right. Fix them…
Browse files Browse the repository at this point in the history
… in situations where the mods folder isn't located next to the actual framework DLLs
  • Loading branch information
MeFisto94 committed Oct 19, 2024
1 parent 328e325 commit c826b60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions BindingRedirects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public static void Setup(string exePath, string dllName)
}
}

public static void CopyRedirects(string sourceFile, string applicationPath, string applicationFile)
public static void CopyRedirects(string sourceFile, string frameworkDllFolder, string applicationFile)
{
File.Copy(sourceFile, Path.Combine(applicationPath, applicationFile + ".config"), true);
File.Copy(sourceFile, Path.Combine(frameworkDllFolder, applicationFile + ".config"), true);
}

// TODO: Merge XML files, but this may be non-trivial due to actual version conflicts, so rather make downstream frameworks supply the right config files.
Expand Down
38 changes: 21 additions & 17 deletions CommandLine/CliEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ public async Task InvokeAsync(string commandLine, IConsole? outputConsole)
}

protected virtual void LaunchGame(bool nonInteractive, string applicationPath, string frameworkDllName,
string? modsJsonPath, string? modsFolder, string commandLine, bool patchLargeAddressAware)
string? modsJsonPath, string? modsFolder, string commandLine, bool? setLargeAddressAware)
{
var profileFolder = PreLaunch(modsJsonPath, modsFolder);
var frameworkDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, frameworkDllName);
// actually, we need the framework folder but with the game name? This fixes binding redirects apparently.
SetupBindingRedirects(applicationPath, frameworkDllName);
SetupBindingRedirects(applicationPath, frameworkDllPath);
if (setLargeAddressAware.HasValue)
{
try
Expand All @@ -205,27 +206,16 @@ protected virtual void LaunchGame(bool nonInteractive, string applicationPath, s
}
}

var process = StartApplication(applicationPath, commandLine, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, frameworkDllName), profileFolder);
var process = StartApplication(applicationPath, commandLine, frameworkDllPath, profileFolder);
PostLaunch(process, profileFolder, nonInteractive);
}

protected virtual void SetupBindingRedirects(string applicationPath, string frameworkDllName)
{
var redirectFile = frameworkDllName + ".config";
if (!File.Exists(redirectFile))
{
// Fall back to the generic DLL
redirectFile = "Andraste.Payload.Generic.dll.config";
}

BindingRedirects.CopyRedirects(redirectFile, Directory.GetParent(applicationPath)!.FullName, Path.GetFileName(applicationPath));
}

protected virtual void MonitorGame(bool nonInteractive, string applicationPath, string frameworkDllName,
string? modsJsonPath, string modsFolder)
{
PreLaunch(modsJsonPath, modsFolder);
SetupBindingRedirects(applicationPath, frameworkDllName);
var frameworkDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, frameworkDllName);
SetupBindingRedirects(applicationPath, frameworkDllPath);
// TODO: PostLaunch
//PostLaunch();
}
Expand All @@ -234,11 +224,25 @@ protected virtual void AttachGame(bool nonInteractive, int pid, string framework
string? modsJsonPath, string modsFolder)
{
var profileFolder = PreLaunch(modsJsonPath, modsFolder);
var frameworkDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, frameworkDllName);
var process = Process.GetProcessById(pid);
// TODO: Does this MainModule work?
SetupBindingRedirects(process.MainModule!.FileName, frameworkDllName);
SetupBindingRedirects(process.MainModule!.FileName, frameworkDllPath);
PostLaunch(process, profileFolder, nonInteractive);
}

protected virtual void SetupBindingRedirects(string applicationPath, string frameworkDllPath)
{
var frameworkDllName = Path.GetFileName(frameworkDllPath);
var redirectFile = frameworkDllName + ".config";
if (!File.Exists(redirectFile))
{
// Fall back to the generic DLL
redirectFile = "Andraste.Payload.Generic.dll.config";
}

BindingRedirects.CopyRedirects(redirectFile, Directory.GetParent(frameworkDllPath)!.FullName, Path.GetFileName(applicationPath));
}

protected virtual string PreLaunch(string? modsJsonPath, string? modsFolder)
{
Expand Down

0 comments on commit c826b60

Please sign in to comment.