Skip to content

Commit

Permalink
Implement bugfix for #208 : PartBoundsIgnoreDisabledTransforms (#209)
Browse files Browse the repository at this point in the history
Implement KSP bugfix for #208
  • Loading branch information
gotmachine authored Apr 4, 2024
1 parent 19a1d6a commit 0ca268d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
5 changes: 5 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ##########################
Expand Down
125 changes: 125 additions & 0 deletions KSPCommunityFixes/BugFixes/PartBoundsIgnoreDisabledTransforms.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
}

protected override Version VersionMin => new Version(1, 12, 3);

protected override void ApplyPatches(List<PatchInfo> patches)
{
patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(PartGeometryUtil), nameof(PartGeometryUtil.GetPartRendererBounds)),
this));
}

static readonly List<Renderer> partRenderersBuffer = new List<Renderer>();

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<Renderer> rendererBuffer = new List<Renderer>();

static void GetRenderersRecursive(Transform parent, List<Renderer> renderers, List<string> 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<KerbalEVA>())
{
Transform result = part.partTransform.Find("model01");
if (result.IsNotNullOrDestroyed())
return result;
}

if (part.HasModuleImplementing<ModuleAsteroid>())
{
Transform result = part.partTransform.Find("Asteroid");
if (result.IsNotNullOrDestroyed())
return result;
}

if (part.HasModuleImplementing<ModuleComet>())
{
Transform result = part.partTransform.Find("Comet");
if (result.IsNotNullOrDestroyed())
return result;
}

return part.partTransform.Find("model");
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="BugFixes\EVAConstructionMass.cs" />
<Compile Include="BugFixes\InventoryPartMass.cs" />
<Compile Include="BugFixes\ModulePartVariantsNodePersistence.cs" />
<Compile Include="BugFixes\PartBoundsIgnoreDisabledTransforms.cs" />
<Compile Include="BugFixes\PropellantFlowDescription.cs" />
<Compile Include="BugFixes\LadderToggleableLight.cs" />
<Compile Include="BugFixes\MapSOCorrectWrapping.cs" />
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- [**RespawnDeadKerbals**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/104) [KSP 1.12.3 - 1.12.5]<br/>When respawning is enabled, starts the respawn timer for any dead kerbals (changing their state to "missing") when loading a save. This addresses stock bugs where kerbals could be set to dead even when respawning is enabled.
- [**ZeroCostTechNode**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/180) [KSP 1.12.3 - 1.12.5]<br/>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]<br/>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]<br/>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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0ca268d

Please sign in to comment.