Skip to content

Commit

Permalink
Implement bugfix for #208
Browse files Browse the repository at this point in the history
  • Loading branch information
gotmachine committed Mar 1, 2024
1 parent e7cd641 commit 4ddcdec
Show file tree
Hide file tree
Showing 4 changed files with 136 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(parent.GetChild(i), 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
5 changes: 5 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 @@ -188,6 +189,10 @@ If doing so in the `Debug` configuration and if your KSP install is modified to

### Changelog

##### next

- New KSP bugfix : [**PartBoundsIgnoreDisabledTransforms**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/208) : 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...

##### 1.34.1
- Disable BetterEditorUndoRedo when TweakScale/L is installed due to introducing a bug with part attachments in the editor.

Expand Down

0 comments on commit 4ddcdec

Please sign in to comment.