fix(sverm): the Ravager costume no longer keeps a live Humanoid

The BepInEx log showed "Can't remove Humanoid (Script) because
CharacterDrop (Script) depends on it" right after the Ravager template
was built from SeekerBrute. The strip was one pass in component order,
and Unity refuses to destroy a component another still requires, so
the Brute's Humanoid survived on the costume and on every copy a
player wore: the leftover that once broke the hammer for every piece.

ModelBorrow.StripBehaviours destroys only what nothing remaining
requires through [RequireComponent], round after round, and logs any
survivor by name. Used by the Ravager template, by ModelBorrow.Attach
and by the Skeleton's rise costume, which had the same single pass.
This commit is contained in:
kanym 2026-10-17 19:42:14 +02:00
commit 5cf1ae7fb0
3 changed files with 67 additions and 12 deletions

View file

@ -96,10 +96,7 @@ namespace RacesOfValheim
// Everything that is not a Renderer or a Transform. Order matters for the same reason it did // Everything that is not a Renderer or a Transform. Order matters for the same reason it did
// on the Aswid's lights: behaviours first, then the things they hold references to. // on the Aswid's lights: behaviours first, then the things they hold references to.
foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren<MonoBehaviour>(true)) StripBehaviours(copy, label);
{
if (behaviour != null) Object.DestroyImmediate(behaviour);
}
foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true)) foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true))
{ {
if (collider != null) Object.DestroyImmediate(collider); if (collider != null) Object.DestroyImmediate(collider);
@ -119,6 +116,67 @@ namespace RacesOfValheim
return holder; return holder;
} }
/// <summary>
/// Destroys every MonoBehaviour under <paramref name="root"/>, dependents first.
///
/// A single pass in component order is not enough, and the log said so: "Can't remove Humanoid
/// (Script) because CharacterDrop (Script) depends on it". Unity refuses to destroy a component
/// another one still requires through [RequireComponent], so the pass that reached Humanoid
/// before CharacterDrop left a live Humanoid on the Sverm's Ravager costume - on every player
/// wearing it - which is the leftover that once broke the hammer for every piece (see
/// SvermHive.MouthCandidates). Each round here destroys only what nothing remaining requires,
/// and rounds repeat until nothing is left or nothing more can go.
/// </summary>
public static void StripBehaviours(GameObject root, string label)
{
if (root == null) return;
for (int round = 0; round < 16; round++)
{
MonoBehaviour[] left = root.GetComponentsInChildren<MonoBehaviour>(true);
if (left.Length == 0) return;
int destroyed = 0;
foreach (MonoBehaviour behaviour in left)
{
if (behaviour == null || IsRequired(behaviour)) continue;
Object.DestroyImmediate(behaviour);
destroyed++;
}
if (destroyed == 0) break;
}
foreach (MonoBehaviour survivor in root.GetComponentsInChildren<MonoBehaviour>(true))
{
if (survivor != null)
{
Jotunn.Logger.LogWarning($"[{label}] '{survivor.GetType().Name}' on '{survivor.name}' could not be stripped from a borrowed model.");
}
}
}
/// <summary>True if another component on the same GameObject names this one's type in a
/// [RequireComponent].</summary>
private static bool IsRequired(Component target)
{
System.Type type = target.GetType();
foreach (Component other in target.GetComponents<Component>())
{
if (other == null || other == target) continue;
foreach (object attribute in other.GetType().GetCustomAttributes(typeof(RequireComponent), true))
{
RequireComponent require = (RequireComponent)attribute;
if (Requires(require.m_Type0, type) || Requires(require.m_Type1, type) || Requires(require.m_Type2, type))
{
return true;
}
}
}
return false;
}
private static bool Requires(System.Type required, System.Type type) => required != null && required.IsAssignableFrom(type);
/// <summary> /// <summary>
/// How tall the borrowed model actually turned out, in the holder's parent's space. Used to stack /// How tall the borrowed model actually turned out, in the holder's parent's space. Used to stack
/// one borrowed model on top of another without a magic offset. /// one borrowed model on top of another without a magic offset.

View file

@ -125,10 +125,8 @@ namespace RacesOfValheim
// Everything that is not a Renderer, a Transform or the Animator. Animator is a Behaviour, // Everything that is not a Renderer, a Transform or the Animator. Animator is a Behaviour,
// not a MonoBehaviour, so the first loop never touches it. // not a MonoBehaviour, so the first loop never touches it.
foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren<MonoBehaviour>(true)) // Dependents first, so a Humanoid held down by CharacterDrop cannot survive on the copy.
{ ModelBorrow.StripBehaviours(copy, "Skeleton");
if (behaviour != null) Object.DestroyImmediate(behaviour);
}
foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true)) foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true))
{ {
if (collider != null) Object.DestroyImmediate(collider); if (collider != null) Object.DestroyImmediate(collider);

View file

@ -103,10 +103,9 @@ namespace RacesOfValheim
// Everything that is not a Renderer or a Transform. Animator is a Behaviour, not a // Everything that is not a Renderer or a Transform. Animator is a Behaviour, not a
// MonoBehaviour, so it is never touched by the first loop and survives on purpose - // MonoBehaviour, so it is never touched by the first loop and survives on purpose -
// it is the one live thing this costume is allowed to keep. // it is the one live thing this costume is allowed to keep.
foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren<MonoBehaviour>(true)) // Dependents first: a single pass left the Brute's Humanoid on the costume, because
{ // CharacterDrop requires it and came later. See ModelBorrow.StripBehaviours.
if (behaviour != null) Object.DestroyImmediate(behaviour); ModelBorrow.StripBehaviours(copy, "Sverm");
}
foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true)) foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true))
{ {
if (collider != null) Object.DestroyImmediate(collider); if (collider != null) Object.DestroyImmediate(collider);