Skip to content

Commit 8c44227

Browse files
committed
Only import newest version of an assembly if multiple present in AppDomain
1 parent 2ad962c commit 8c44227

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

ScriptLoader/MonoCompiler.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Reflection;
56
using System.Reflection.Emit;
67
using System.Text;
8+
using BepInEx;
79
using Mono.CSharp;
810

911
namespace ScriptLoader
@@ -101,14 +103,33 @@ SeekableStreamReader GetFile(SourceFile file)
101103
return ass.Builder;
102104
}
103105

106+
private static AssemblyName ParseName(string fullName)
107+
{
108+
try
109+
{
110+
return new AssemblyName(fullName);
111+
}
112+
catch (Exception)
113+
{
114+
return null;
115+
}
116+
}
117+
104118
private static void ImportAppdomainAssemblies(Action<Assembly> import)
105119
{
106-
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
120+
// In some cases there could be multiple versions of the same assembly loaded
121+
// In that case we decide to simply load only the latest one as it's easiest to handle
122+
var dedupedAssemblies = AppDomain.CurrentDomain.GetAssemblies()
123+
.Select(a => new { ass = a, name = ParseName(a.FullName) })
124+
.Where(a => a.name != null)
125+
.GroupBy(a => a.name.Name)
126+
.Select(g => g.OrderByDescending(a => a.name.Version))
127+
.First();
128+
foreach (var ass in dedupedAssemblies)
107129
{
108-
var name = assembly.GetName().Name;
109-
if (StdLib.Contains(name) || compiledAssemblies.Contains(name))
130+
if (StdLib.Contains(ass.name.Name) || compiledAssemblies.Contains(ass.name.Name))
110131
continue;
111-
import(assembly);
132+
import(ass.ass);
112133
}
113134
}
114135

0 commit comments

Comments
 (0)