Prioritize the shallowest path for the same assembly name in the default providers (#980)

Change the behaviors of the default providers so if multiple assemblies with the same name exists, but in different directories, ignore any that are located deeper.
This commit is contained in:
aldelaro5 authored and GitHub committed 2024-09-10 14:48:48 +02:00
1 parent cdeaad7868
commit 726b68e52b
2 files changed
+46 -2

No files matched your search

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx.Logging;
using BepInEx.Preloader.Core.Patching;
@@ -19,7 +20,28 @@ internal class BepInExPatcherProvider : BasePatcherProvider
{
try
{
AssemblyLocationsByFilename.Add(Path.GetFileNameWithoutExtension(dll), Path.GetDirectoryName(dll));
var filename = Path.GetFileNameWithoutExtension(dll);
var foundDirectory = Path.GetDirectoryName(dll);
// Prioritize the shallowest path of each assembly name
if (AssemblyLocationsByFilename.TryGetValue(filename, out var existingDirectory))
{
int levelExistingDirectory = existingDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
int levelFoundDirectory = foundDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
bool shallowerPathFound = levelExistingDirectory > levelFoundDirectory;
Log.LogWarning($"Found duplicate assemblies filenames: {filename} was found at {foundDirectory} " +
$"while it exists already at {AssemblyLocationsByFilename[filename]}. " +
$"Only the {(shallowerPathFound ? "first" : "second")} will be examined and resolved");
if (levelExistingDirectory > levelFoundDirectory)
AssemblyLocationsByFilename[filename] = foundDirectory;
}
else
{
AssemblyLocationsByFilename.Add(filename, foundDirectory);
}
loadContexts.Add(new BepInExPatcherLoadContext
{
AssemblyHash = File.GetLastWriteTimeUtc(dll).ToString("O"),
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx.Logging;
@@ -18,7 +19,28 @@ internal class BepInExPluginProvider : BasePluginProvider
{
try
{
AssemblyLocationsByFilename.Add(Path.GetFileNameWithoutExtension(dll), Path.GetDirectoryName(dll));
var filename = Path.GetFileNameWithoutExtension(dll);
var foundDirectory = Path.GetDirectoryName(dll);
// Prioritize the shallowest path of each assembly name
if (AssemblyLocationsByFilename.TryGetValue(filename, out var existingDirectory))
{
int levelExistingDirectory = existingDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
int levelFoundDirectory = foundDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
bool shallowerPathFound = levelExistingDirectory > levelFoundDirectory;
Logger.LogWarning($"Found duplicate assemblies filenames: {filename} was found at {foundDirectory} " +
$"while it exists already at {AssemblyLocationsByFilename[filename]}. " +
$"Only the {(shallowerPathFound ? "first" : "second")} will be examined and resolved");
if (levelExistingDirectory > levelFoundDirectory)
AssemblyLocationsByFilename[filename] = foundDirectory;
}
else
{
AssemblyLocationsByFilename.Add(filename, foundDirectory);
}
loadContexts.Add(new BepInExPluginLoadContext
{
AssemblyHash = File.GetLastWriteTimeUtc(dll).ToString("O"),