private void FindBestSubtype()
{
// First try to identify subtype by name
if (!string.IsNullOrEmpty(currentSubtypeName))
{
int index = subtypes.FindIndex(subtype => subtype.Name == currentSubtypeName);
if (index != -1)
{
currentSubtypeIndex = index;
return;
}
else
{
LogError($"Cannot find subtype named '{currentSubtypeName}'");
}
}
// Now try to use index
if (subtypes.ValidIndex(currentSubtypeIndex))
{
currentSubtypeName = CurrentSubtype.Name;
return;
}
if (ManagesResources)
{
// Now use resources
// This finds all the managed resources that currently exist on teh part
string[] resourcesOnPart = ManagedResourceNames.Intersect(part.Resources.Select(resource => resource.resourceName)).ToArray();
#if DEBUG
LogInfo($"Managed resources found on part: [{string.Join(", ", resourcesOnPart)}]");
#endif
// If any of the part's current resources are managed, look for a subtype which has all of the managed resources (and all of its resources exist)
// Otherwise, look for a structural subtype (no resources)
if (resourcesOnPart.Any())
{
currentSubtypeIndex = subtypes.FindIndex(subtype => subtype.HasTank && subtype.ResourceNames.SameElementsAs(resourcesOnPart));
LogInfo($"Inferred subtype based on part's resources: '{CurrentSubtype.Name}'");
}
else
{
currentSubtypeIndex = subtypes.FindIndex(subtype => !subtype.HasTank);
}
}
// No useful way to determine correct subtype, just pick first
if (!subtypes.ValidIndex(currentSubtypeIndex))
currentSubtypeIndex = 0;
currentSubtypeName = CurrentSubtype.Name;
}