From 302b4c46e410a1f4473e38f8617ba7a69e16f673 Mon Sep 17 00:00:00 2001 From: JonnyOThan Date: Sun, 11 Feb 2024 11:05:11 -0500 Subject: [PATCH] remove the cloned attachnode when detaching a part --- .../BugFixes/ReRootCloneSurfaceAttach.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/KSPCommunityFixes/BugFixes/ReRootCloneSurfaceAttach.cs b/KSPCommunityFixes/BugFixes/ReRootCloneSurfaceAttach.cs index 40f4ee2..0389af7 100644 --- a/KSPCommunityFixes/BugFixes/ReRootCloneSurfaceAttach.cs +++ b/KSPCommunityFixes/BugFixes/ReRootCloneSurfaceAttach.cs @@ -1,4 +1,5 @@ -using HarmonyLib; +using Expansions.Missions.Editor; +using HarmonyLib; using System; using System.Collections.Generic; using System.Linq; @@ -31,6 +32,11 @@ protected override void ApplyPatches(List patches) PatchMethodType.Postfix, AccessTools.Method(typeof(Part), nameof(Part.FindAttachNode)), this)); + + patches.Add(new PatchInfo( + PatchMethodType.Postfix, + AccessTools.Method(typeof(EditorLogicBase), nameof(EditorLogicBase.clearAttachNodes)), + this)); } // In stock, this function is called after reversing a surface attachment during a re-root operation. @@ -81,5 +87,20 @@ static void Part_FindAttachNode_Postfix(Part __instance, string nodeId, ref Atta __result = newSrfAttachNode; } } + + // In the editor, when detaching parts, remove the extra attachnode we added + static void EditorLogicBase_clearAttachNodes_Postfix(Part part) + { + for (int i = 0; i < part.attachNodes.Count; i++) + { + AttachNode attachNode = part.attachNodes[i]; + + if (attachNode.id == CLONED_NODE_ID && attachNode.attachedPart.IsNullRef()) + { + part.attachNodes.RemoveAt(i); + return; + } + } + } } }