From 0ca268d439f632b936578650b115bdaf20765712 Mon Sep 17 00:00:00 2001 From: gotmachine <24925209+gotmachine@users.noreply.github.com> Date: Fri, 5 Apr 2024 00:06:24 +0200 Subject: [PATCH] Implement bugfix for #208 : PartBoundsIgnoreDisabledTransforms (#209) Implement KSP bugfix for #208 --- GameData/KSPCommunityFixes/Settings.cfg | 5 + .../PartBoundsIgnoreDisabledTransforms.cs | 125 ++++++++++++++++++ KSPCommunityFixes/KSPCommunityFixes.csproj | 1 + README.md | 3 + 4 files changed, 134 insertions(+) create mode 100644 KSPCommunityFixes/BugFixes/PartBoundsIgnoreDisabledTransforms.cs diff --git a/GameData/KSPCommunityFixes/Settings.cfg b/GameData/KSPCommunityFixes/Settings.cfg index 1446837..d5ceb7d 100644 --- a/GameData/KSPCommunityFixes/Settings.cfg +++ b/GameData/KSPCommunityFixes/Settings.cfg @@ -217,6 +217,11 @@ KSP_COMMUNITY_FIXES // vessel was spawned directly from the editor or loaded from the saved game. ModulePartVariantsNodePersistence = true + // Fix disabled renderers by mesh switchers (B9PartSwitch...) still being considered for part + // bounds evaluation, resulting in various issues like parts not being occluded from drag in + // cargo bays, wrong vessel size being reported, etc... + PartBoundsIgnoreDisabledTransforms = true + // ########################## // Obsolete bugfixes // ########################## diff --git a/KSPCommunityFixes/BugFixes/PartBoundsIgnoreDisabledTransforms.cs b/KSPCommunityFixes/BugFixes/PartBoundsIgnoreDisabledTransforms.cs new file mode 100644 index 0000000..0d40ffc --- /dev/null +++ b/KSPCommunityFixes/BugFixes/PartBoundsIgnoreDisabledTransforms.cs @@ -0,0 +1,125 @@ +using HarmonyLib; +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace KSPCommunityFixes +{ + public class PartBoundsIgnoreDisabledTransforms : BasePatch + { + static PartBoundsIgnoreDisabledTransforms() + { + PartGeometryUtil.disabledVariantGOs = new List(); + } + + protected override Version VersionMin => new Version(1, 12, 3); + + protected override void ApplyPatches(List patches) + { + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(PartGeometryUtil), nameof(PartGeometryUtil.GetPartRendererBounds)), + this)); + } + + static readonly List partRenderersBuffer = new List(); + + static bool PartGeometryUtil_GetPartRendererBounds_Prefix(Part p, out Bounds[] __result) + { + PartGeometryUtil.disabledVariantGOs.Clear(); + + // config defined ignores + for (int i = p.partRendererBoundsIgnore.Count; i-- > 0;) + PartGeometryUtil.disabledVariantGOs.Add(p.partRendererBoundsIgnore[i]); + + // stock variant switcher + if (p.variants != null) + { + PartVariant selectedVariant = p.variants.SelectedVariant; + for (int i = selectedVariant.InfoGameObjects.Count; i-- > 0;) + { + PartGameObjectInfo info = selectedVariant.InfoGameObjects[i]; + if (!info.Status) + PartGeometryUtil.disabledVariantGOs.Add(info.Name); + } + } + + Transform modelTransform = GetPartModelTransform(p); + + try + { + GetRenderersRecursive(modelTransform, partRenderersBuffer, PartGeometryUtil.disabledVariantGOs); + + __result = new Bounds[partRenderersBuffer.Count]; + for (int i = __result.Length; i-- > 0;) + __result[i] = partRenderersBuffer[i].bounds; + } + finally + { + partRenderersBuffer.Clear(); + } + + return false; + } + + static readonly List rendererBuffer = new List(); + + static void GetRenderersRecursive(Transform parent, List renderers, List excludedGOs) + { + // note : this will result in a slight change in behavior vs stock + // as this will exclude childs as well, wereas stock will still include them. + // But stock behavior could be classified as a bug... + if (excludedGOs.Contains(parent.name)) + return; + + try + { + parent.GetComponents(rendererBuffer); + for (int i = rendererBuffer.Count; i-- > 0;) + { + Renderer r = rendererBuffer[i]; + Type rType = r.GetType(); + if (rType == typeof(MeshRenderer) || rType == typeof(SkinnedMeshRenderer)) + renderers.Add(r); + } + } + finally + { + rendererBuffer.Clear(); + } + + for (int i = parent.childCount; i-- > 0;) + { + Transform child = parent.GetChild(i); + if (child.gameObject.activeSelf) + GetRenderersRecursive(child, renderers, excludedGOs); + } + } + + static Transform GetPartModelTransform(Part part) + { + if (part.HasModuleImplementing()) + { + Transform result = part.partTransform.Find("model01"); + if (result.IsNotNullOrDestroyed()) + return result; + } + + if (part.HasModuleImplementing()) + { + Transform result = part.partTransform.Find("Asteroid"); + if (result.IsNotNullOrDestroyed()) + return result; + } + + if (part.HasModuleImplementing()) + { + Transform result = part.partTransform.Find("Comet"); + if (result.IsNotNullOrDestroyed()) + return result; + } + + return part.partTransform.Find("model"); + } + } +} diff --git a/KSPCommunityFixes/KSPCommunityFixes.csproj b/KSPCommunityFixes/KSPCommunityFixes.csproj index 9ba9fd2..5ccec49 100644 --- a/KSPCommunityFixes/KSPCommunityFixes.csproj +++ b/KSPCommunityFixes/KSPCommunityFixes.csproj @@ -107,6 +107,7 @@ + diff --git a/README.md b/README.md index 5fe8777..5402df8 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ User options are available from the "ESC" in-game settings menu :
Fixes a bug where parts in tech nodes that have 0 science cost would become unusable. - [**ModulePartVariantsNodePersistence**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/179) [KSP 1.12.3 - 1.12.5]
Fixes an issue with ModulePartVariants where attachnodes would use their default state when resuming flight on a vessel from a saved game. This would lead to different behavior in part joints and flexibility between initial launch and loading a save. +- [**PartBoundsIgnoreDisabledTransforms**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/208) [KSP 1.12.3 - 1.12.5]
Fix disabled renderers by mesh switchers (B9PartSwitch...) still being considered for part bounds evaluation, resulting in various issues like parts not being occluded from drag in cargo bays, wrong vessel size being reported, etc... #### Quality of Life tweaks @@ -192,6 +193,8 @@ If doing so in the `Debug` configuration and if your KSP install is modified to ##### 1.35.0 - New KSP performance patch : [**OptimizedModuleRaycasts**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/216) [KSP 1.12.3 - 1.12.5] : Improve engine exhaust damage and solar panel line of sight raycasts performance by avoiding extra physics state synchronization and caching solar panels scaled space raycasts results. - New KSP QoL/performance patch : [**OptionalMakingHistoryDLCFeatures**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/218) [KSP 1.12.3 - 1.12.5] : Allow to disable the Making History DLC mission editor and additional launch sites features to decrease memory usage and increase loading speed. The Making History parts will still be available. Can be toggled from the KSPCF in-game settings (requires a restart), or from a MM patch (see `Settings.cfg`) +- New KSP bugfix : [**PartBoundsIgnoreDisabledTransforms**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/208) [KSP 1.12.3 - 1.12.5] : Fix disabled renderers by mesh switchers (B9PartSwitch...) still being considered for part bounds evaluation, resulting in various issues like parts not being occluded from drag in cargo bays, wrong vessel size being reported, etc... +- **BetterUndoRedo** : Fixed "too much undoing" when undoing offset/rotate editor actions, and other incoherent behavior (see [related issue](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/206)) - **FastLoader** : Improved DDS loading performance by avoiding an extra copy of the DDS data ##### 1.34.1