Skip to content

Commit 726b68e

Browse files
authored
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.
1 parent cdeaad7 commit 726b68e

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

BepInEx.PatcherProvider/BepInExPatcherProvider.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Reflection;
56
using BepInEx.Logging;
67
using BepInEx.Preloader.Core.Patching;
@@ -19,7 +20,28 @@ public override IList<IPluginLoadContext> GetPatchers()
1920
{
2021
try
2122
{
22-
AssemblyLocationsByFilename.Add(Path.GetFileNameWithoutExtension(dll), Path.GetDirectoryName(dll));
23+
var filename = Path.GetFileNameWithoutExtension(dll);
24+
var foundDirectory = Path.GetDirectoryName(dll);
25+
26+
// Prioritize the shallowest path of each assembly name
27+
if (AssemblyLocationsByFilename.TryGetValue(filename, out var existingDirectory))
28+
{
29+
int levelExistingDirectory = existingDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
30+
int levelFoundDirectory = foundDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
31+
32+
bool shallowerPathFound = levelExistingDirectory > levelFoundDirectory;
33+
Log.LogWarning($"Found duplicate assemblies filenames: {filename} was found at {foundDirectory} " +
34+
$"while it exists already at {AssemblyLocationsByFilename[filename]}. " +
35+
$"Only the {(shallowerPathFound ? "first" : "second")} will be examined and resolved");
36+
37+
if (levelExistingDirectory > levelFoundDirectory)
38+
AssemblyLocationsByFilename[filename] = foundDirectory;
39+
}
40+
else
41+
{
42+
AssemblyLocationsByFilename.Add(filename, foundDirectory);
43+
}
44+
2345
loadContexts.Add(new BepInExPatcherLoadContext
2446
{
2547
AssemblyHash = File.GetLastWriteTimeUtc(dll).ToString("O"),

BepInEx.PluginProvider/BepInExPluginProvider.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Reflection;
56
using BepInEx.Logging;
67

@@ -18,7 +19,28 @@ public override IList<IPluginLoadContext> GetPlugins()
1819
{
1920
try
2021
{
21-
AssemblyLocationsByFilename.Add(Path.GetFileNameWithoutExtension(dll), Path.GetDirectoryName(dll));
22+
var filename = Path.GetFileNameWithoutExtension(dll);
23+
var foundDirectory = Path.GetDirectoryName(dll);
24+
25+
// Prioritize the shallowest path of each assembly name
26+
if (AssemblyLocationsByFilename.TryGetValue(filename, out var existingDirectory))
27+
{
28+
int levelExistingDirectory = existingDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
29+
int levelFoundDirectory = foundDirectory?.Count(x => x == Path.DirectorySeparatorChar) ?? 0;
30+
31+
bool shallowerPathFound = levelExistingDirectory > levelFoundDirectory;
32+
Logger.LogWarning($"Found duplicate assemblies filenames: {filename} was found at {foundDirectory} " +
33+
$"while it exists already at {AssemblyLocationsByFilename[filename]}. " +
34+
$"Only the {(shallowerPathFound ? "first" : "second")} will be examined and resolved");
35+
36+
if (levelExistingDirectory > levelFoundDirectory)
37+
AssemblyLocationsByFilename[filename] = foundDirectory;
38+
}
39+
else
40+
{
41+
AssemblyLocationsByFilename.Add(filename, foundDirectory);
42+
}
43+
2244
loadContexts.Add(new BepInExPluginLoadContext
2345
{
2446
AssemblyHash = File.GetLastWriteTimeUtc(dll).ToString("O"),

0 commit comments

Comments
 (0)