From 5cf1ae7fb0a4e30a8a30027821c09b3aa33e5c63 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:42:14 +0200 Subject: [PATCH] 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. --- JotunnModStub/ModelBorrow.cs | 66 +++++++++++++++++++++++-- JotunnModStub/Skeleton/SkeletonRise.cs | 6 +-- JotunnModStub/Sverm/SvermRavagerForm.cs | 7 ++- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/JotunnModStub/ModelBorrow.cs b/JotunnModStub/ModelBorrow.cs index 20b2ded..1bd8fd2 100644 --- a/JotunnModStub/ModelBorrow.cs +++ b/JotunnModStub/ModelBorrow.cs @@ -96,10 +96,7 @@ namespace RacesOfValheim // 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. - foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren(true)) - { - if (behaviour != null) Object.DestroyImmediate(behaviour); - } + StripBehaviours(copy, label); foreach (Collider collider in copy.GetComponentsInChildren(true)) { if (collider != null) Object.DestroyImmediate(collider); @@ -119,6 +116,67 @@ namespace RacesOfValheim return holder; } + /// + /// Destroys every MonoBehaviour under , 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. + /// + public static void StripBehaviours(GameObject root, string label) + { + if (root == null) return; + + for (int round = 0; round < 16; round++) + { + MonoBehaviour[] left = root.GetComponentsInChildren(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(true)) + { + if (survivor != null) + { + Jotunn.Logger.LogWarning($"[{label}] '{survivor.GetType().Name}' on '{survivor.name}' could not be stripped from a borrowed model."); + } + } + } + + /// True if another component on the same GameObject names this one's type in a + /// [RequireComponent]. + private static bool IsRequired(Component target) + { + System.Type type = target.GetType(); + foreach (Component other in target.GetComponents()) + { + 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); + /// /// 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. diff --git a/JotunnModStub/Skeleton/SkeletonRise.cs b/JotunnModStub/Skeleton/SkeletonRise.cs index d44cd75..7e9827b 100644 --- a/JotunnModStub/Skeleton/SkeletonRise.cs +++ b/JotunnModStub/Skeleton/SkeletonRise.cs @@ -125,10 +125,8 @@ namespace RacesOfValheim // 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. - foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren(true)) - { - if (behaviour != null) Object.DestroyImmediate(behaviour); - } + // Dependents first, so a Humanoid held down by CharacterDrop cannot survive on the copy. + ModelBorrow.StripBehaviours(copy, "Skeleton"); foreach (Collider collider in copy.GetComponentsInChildren(true)) { if (collider != null) Object.DestroyImmediate(collider); diff --git a/JotunnModStub/Sverm/SvermRavagerForm.cs b/JotunnModStub/Sverm/SvermRavagerForm.cs index f41b7f0..d9b21a7 100644 --- a/JotunnModStub/Sverm/SvermRavagerForm.cs +++ b/JotunnModStub/Sverm/SvermRavagerForm.cs @@ -103,10 +103,9 @@ namespace RacesOfValheim // 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 - // it is the one live thing this costume is allowed to keep. - foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren(true)) - { - if (behaviour != null) Object.DestroyImmediate(behaviour); - } + // Dependents first: a single pass left the Brute's Humanoid on the costume, because + // CharacterDrop requires it and came later. See ModelBorrow.StripBehaviours. + ModelBorrow.StripBehaviours(copy, "Sverm"); foreach (Collider collider in copy.GetComponentsInChildren(true)) { if (collider != null) Object.DestroyImmediate(collider);