Skip to content

Commit 36646b3

Browse files
authored
Fix #307: guard against partmodules defined in multiple assemblies (#… (#310)
Fix #307: guard against partmodules defined in multiple assemblies (#308)
1 parent 3106805 commit 36646b3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

KSPCommunityFixes/BugFixes/ModuleIndexingMismatch.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,31 @@ protected override void ApplyPatches()
4747

4848
AddPatch(PatchType.Transpiler, typeof(ShipConstruct), "LoadShip", new Type[] { typeof(ConfigNode), typeof(uint), typeof(bool), typeof(string).MakeByRefType() });
4949

50-
Type partModuleType = typeof(PartModule);
5150
Type multiModuleType = typeof(IMultipleModuleInPart);
5251

5352
// note : do not use AppDomain.CurrentDomain.GetAssemblies(), as in case some plugin was missing
5453
// a reference, that missing reference assembly will be included even though it is invalid, leading
5554
// to a ReflectionTypeLoadException when doing anything with it such as calling GetTypes()
55+
56+
// note: it's important that this logic mirrors AssemblyLoader.GetTypeByName since that is what's
57+
// used to instantiate a PartModule from a cfg file.
5658
foreach (AssemblyLoader.LoadedAssembly loadedAssembly in AssemblyLoader.loadedAssemblies)
5759
{
58-
foreach (Type type in loadedAssembly.assembly.GetTypes())
59-
{
60-
if (partModuleType.IsAssignableFrom(type))
61-
{
62-
if (type == partModuleType)
63-
continue;
60+
var partModules = loadedAssembly.types.GetValueOrDefault(typeof(PartModule));
6461

65-
if (!type.IsAbstract && !type.IsInterface)
66-
allModuleTypes.Add(type.Name, type);
62+
if (partModules == null) continue;
6763

64+
foreach (Type type in partModules)
65+
{
66+
if (allModuleTypes.TryAdd(type.Name, type))
67+
{
6868
if (multiModuleType.IsAssignableFrom(type))
6969
multiModules.Add(type.Name);
7070
}
71+
else
72+
{
73+
Debug.LogWarning($"[KSPCF:ModuleIndexingMismatch] Found additional PartModule named '{type.Name}' in assembly '{loadedAssembly.assembly.GetName().Name}'. Original was in '{allModuleTypes[type.Name].Assembly.GetName().Name}'");
74+
}
7175
}
7276
}
7377

0 commit comments

Comments
 (0)