From 2984f3046af8fef094fcdc9a28ea9b434101e748 Mon Sep 17 00:00:00 2001 From: Aragas Date: Thu, 17 Mar 2022 20:09:57 +0300 Subject: [PATCH] Fixed crash at launch when an incompatible module is used --- build/common.props | 2 +- changelog.txt | 4 +++ .../Interceptor/InterceptorFeature.cs | 27 ++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/build/common.props b/build/common.props index 84ca134..f63a912 100644 --- a/build/common.props +++ b/build/common.props @@ -3,7 +3,7 @@ - 1.7.3 + 1.7.4 2.0.4 2.0.0.78 3.0.94 diff --git a/changelog.txt b/changelog.txt index c88af1f..8f49429 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,8 @@ --------------------------------------------------------------------------------------------------- +Version: 1.7.4 +Game Versions: e1.5.0,e1.5.1,e1.5.2,e1.5.3,e1.5.4,e1.5.5,e1.5.6,e1.5.7,e1.5.8,e1.5.9,e1.5.10,e1.6.0,e1.6.1,e1.6.2,e1.6.3,e1.6.4,e1.6.5,e1.7.0,e1.7.1,e1.7.2 +* Fixed crash at launch when an incompatible module is used +--------------------------------------------------------------------------------------------------- Version: 1.7.3 Game Versions: e1.5.0,e1.5.1,e1.5.2,e1.5.3,e1.5.4,e1.5.5,e1.5.6,e1.5.7,e1.5.8,e1.5.9,e1.5.10,e1.6.0,e1.6.1,e1.6.2,e1.6.3,e1.6.4,e1.6.5,e1.7.0,e1.7.1,e1.7.2 * Added option to disable binary compatibility check diff --git a/src/Bannerlord.BUTRLoader/Features/Interceptor/InterceptorFeature.cs b/src/Bannerlord.BUTRLoader/Features/Interceptor/InterceptorFeature.cs index c1963c7..1c17bf9 100644 --- a/src/Bannerlord.BUTRLoader/Features/Interceptor/InterceptorFeature.cs +++ b/src/Bannerlord.BUTRLoader/Features/Interceptor/InterceptorFeature.cs @@ -15,10 +15,29 @@ internal static class InterceptorFeature private delegate void OnInitializeSubModulesPrefixDelegate(); private delegate void OnLoadSubModulesPostfixDelegate(); - private static IEnumerable GetInterceptorTypes() => AppDomain.CurrentDomain.GetAssemblies() - .Where(asm => !asm.IsDynamic && asm.CodeBase.Contains("Modules")) - .SelectMany(asm => asm.DefinedTypes.Where(type => type.GetCustomAttributes().Any(att => - string.Equals(att.GetType().FullName, typeof(BUTRLoaderInterceptorAttribute).FullName, StringComparison.Ordinal)))); + private static IEnumerable GetInterceptorTypes() + { + static bool CheckType(Type type) => type.GetCustomAttributes() + .Any(att => string.Equals(att.GetType().FullName, typeof(BUTRLoaderInterceptorAttribute).FullName, StringComparison.Ordinal)); + + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(asm => !asm.IsDynamic && asm.CodeBase.Contains("Modules"))) + { + IEnumerable enumerable; + try + { + var types = assembly.GetTypes(); // Force type resolution + enumerable = types.Where(CheckType); + } + catch (ReflectionTypeLoadException) + { + enumerable = Enumerable.Empty(); // ignore the incompatibility, not our problem + } + foreach (var type in enumerable) + { + yield return type; + } + } + } public static void Enable(Harmony harmony) {