diff --git a/BepInEx.PatcherProvider/BepInExPatcherProvider.cs b/BepInEx.PatcherProvider/BepInExPatcherProvider.cs index 5ae4555..7685874 100644 --- a/BepInEx.PatcherProvider/BepInExPatcherProvider.cs +++ b/BepInEx.PatcherProvider/BepInExPatcherProvider.cs @@ -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"), diff --git a/BepInEx.PluginProvider/BepInExPluginProvider.cs b/BepInEx.PluginProvider/BepInExPluginProvider.cs index 6520974..6f134ab 100644 --- a/BepInEx.PluginProvider/BepInExPluginProvider.cs +++ b/BepInEx.PluginProvider/BepInExPluginProvider.cs @@ -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"),