Skip to content

Commit 2a8b04c

Browse files
authored
[Assets] Prevent crash of the assets compiler when an assembly cannot be fully loaded. (stride3d#2144)
Fixes stride3d#2140
1 parent f650051 commit 2a8b04c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

sources/assets/Stride.Core.Assets/Compiler/AssetCompilerRegistry.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Reflection;
89
using Stride.Core;
@@ -181,7 +182,7 @@ private void RegisterAssembly(Assembly assembly)
181182
private void RegisterCompilersFromAssembly(Assembly assembly)
182183
{
183184
// Process Asset types.
184-
foreach (var type in assembly.GetTypes())
185+
foreach (var type in GetFullyLoadedTypes(assembly))
185186
{
186187
// Only process Asset types
187188
if (!typeof(IAssetCompiler).IsAssignableFrom(type) || !type.IsClass)
@@ -202,6 +203,21 @@ private void RegisterCompilersFromAssembly(Assembly assembly)
202203
log.Error($"Unable to instantiate compiler [{compilerAttribute.TypeName}]", ex);
203204
}
204205
}
206+
207+
// Taken from https://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes
208+
[DebuggerNonUserCode]
209+
IEnumerable<Type> GetFullyLoadedTypes(Assembly assembly)
210+
{
211+
try
212+
{
213+
return assembly.GetTypes();
214+
}
215+
catch (ReflectionTypeLoadException ex)
216+
{
217+
log.Warning($"Could not load all types from assembly {assembly.FullName}", ex);
218+
return ex.Types.Where(t => t != null);
219+
}
220+
}
205221
}
206222

207223
private void UnregisterAssembly(Assembly assembly)

0 commit comments

Comments
 (0)