Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential new KSP bugfix : ReRootPreserveSurfaceAttach #142

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Expand Up @@ -168,6 +168,10 @@ KSP_COMMUNITY_FIXES
// ladder is retracted, and implements manual control of the light.
LadderToggleableLight = true

// Disable the stock behavior of altering surface attachment nodes on re-rooting, which is
// unnecessary and doesn't work correctly, leading to permanently borked attachement nodes.
ReRootPreserveSurfaceAttach = true

// ##########################
// Obsolete bugfixes
// ##########################
Expand Down
139 changes: 139 additions & 0 deletions KSPCommunityFixes/BugFixes/ReRootPreserveSurfaceAttach.cs
@@ -0,0 +1,139 @@
// see https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/142

// #define REROOT_DEBUG_MODULE

using HarmonyLib;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

#if REROOT_DEBUG_MODULE
using UnityEngine;
#endif

namespace KSPCommunityFixes.BugFixes
{
class ReRootPreserveSurfaceAttach : BasePatch
{
protected override void ApplyPatches(List<PatchInfo> patches)
{
patches.Add(new PatchInfo(
PatchMethodType.Transpiler,
AccessTools.Method(typeof(Part), nameof(Part.SetHierarchyRoot)),
this));
}

// skip the portion of that method that alter surface nodes position/orientation on re-rooting,
// by returning after the recursive SetHierarchyRoot() call.
private static IEnumerable<CodeInstruction> Part_SetHierarchyRoot_Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo m_Part_SetHierarchyRoot = AccessTools.Method(typeof(Part), nameof(Part.SetHierarchyRoot));

foreach (CodeInstruction instruction in instructions)
{
yield return instruction;
if (instruction.opcode == OpCodes.Callvirt && ReferenceEquals(instruction.operand, m_Part_SetHierarchyRoot))
yield return new CodeInstruction(OpCodes.Ret);
}
}
}

#if REROOT_DEBUG_MODULE
public class SrfAttachInfo : PartModule
{
[KSPField(guiActive = true, guiActiveEditor = true)]
public string attachInfo;

[KSPField(guiActive = true, guiActiveEditor = true, guiName = "srf node visual debug")]
[UI_Toggle]
public bool attachVisualDebug = true;

void Update()
{
attachInfo = "\n";

if (part.srfAttachNode != null)
attachInfo += "srf: " + part.srfAttachNode.position + " / " + part.srfAttachNode.orientation + "\n";

for (int i = 0; i < part.attachNodes.Count; i++)
{
attachInfo += $"{part.attachNodes[i].id}: {part.attachNodes[i].position} / {part.attachNodes[i].orientation}\n";
}
}

private void OnRenderObject()
{
if (!attachVisualDebug)
return;

if (!part.attachRules.srfAttach)
return;

Vector3 pos = part.transform.rotation * part.srfAttachNode.position + part.transform.position;
Vector3 dir = part.transform.TransformDirection(part.srfAttachNode.orientation);

Camera cam = GetActiveCam();
GLStart();
DrawPoint(cam, pos, Color.green);
DrawRay(cam, pos, dir * 0.5f, Color.red);
GLEnd();
}

private static Material _material;

private static Material Material
{
get
{
if (_material == null)
_material = new Material(Shader.Find("Hidden/Internal-Colored"));
return _material;
}
}

private static void GLStart()
{
GL.PushMatrix();
Material.SetPass(0);
GL.LoadPixelMatrix();
GL.Begin(GL.LINES);
}

private static void GLEnd()
{
GL.End();
GL.PopMatrix();
}


private static Camera GetActiveCam()
{
Camera cam;
if (HighLogic.LoadedSceneIsEditor)
cam = EditorLogic.fetch.editorCamera;
else if (HighLogic.LoadedSceneIsFlight)
cam = MapView.MapIsEnabled ? PlanetariumCamera.Camera : FlightCamera.fetch.mainCamera;
else
cam = Camera.main;
return cam;
}

private static void DrawRay(Camera cam, Vector3 origin, Vector3 direction, Color color)
{
Vector3 screenPoint1 = cam.WorldToScreenPoint(origin);
Vector3 screenPoint2 = cam.WorldToScreenPoint(origin + direction);

GL.Color(color);
GL.Vertex3(screenPoint1.x, screenPoint1.y, 0);
GL.Vertex3(screenPoint2.x, screenPoint2.y, 0);
}

private static void DrawPoint(Camera cam, Vector3 position, Color color)
{
DrawRay(cam, position + Vector3.up * 0.1f, -Vector3.up * 0.2f, color);
DrawRay(cam, position + Vector3.right * 0.1f, -Vector3.right * 0.2f, color);
DrawRay(cam, position + Vector3.forward * 0.1f, -Vector3.forward * 0.2f, color);
}
}
#endif
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Expand Up @@ -105,6 +105,7 @@
<Compile Include="BugFixes\CorrectDragForFlags.cs" />
<Compile Include="BugFixes\LadderToggleableLight.cs" />
<Compile Include="BugFixes\MapSOCorrectWrapping.cs" />
<Compile Include="BugFixes\ReRootPreserveSurfaceAttach.cs" />
<Compile Include="BugFixes\UpgradeBugs.cs" />
<Compile Include="BugFixes\PartTooltipUpgradesApplyToSubstituteParts.cs" />
<Compile Include="BugFixes\CometMiningNotRemovingMass.cs" />
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -74,6 +74,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- **[ChutePhantomSymmetry](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/107)** [KSP 1.10.0 - 1.12.5]<br/>Fix spread angle still being applied after decoupling symmetry-placed parachutes.
- **[CorrectDragForFlags](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/126)** [KSP 1.12.3 - 1.12.5]<br/>Fix the "panel" variants of the flag parts using a single drag cube, causing excessive drag for the smaller options.
- **LadderToggleableLight** [KSP 1.8.0 - 1.12.5]<br/>Fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active even when the ladder is retracted, and implements manual control of the light.
- [**ReRootPreserveSurfaceAttach**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/142) [KSP 1.8.0 - 1.12.5]<br/>Disable the stock behavior of altering surface attachment nodes on re-rooting, a questionable QoL feature that doesn't work correctly, leading to permanently borked attachement nodes.

#### Quality of Life tweaks

Expand Down Expand Up @@ -172,6 +173,7 @@ If doing so in the `Debug` configuration and if your KSP install is modified to
### Changelog

##### 1.28.0
- New KSP bugfix : [**ReRootPreserveSurfaceAttach**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/142) [KSP 1.8.0 - 1.12.5], disable the stock behavior of altering surface attachment nodes on re-rooting, a questionable QoL feature that doesn't work correctly, leading to permanently borked attachement nodes.
- New API/modding patch : **BetterDDSSupport** [KSP 1.12.3 - 1.12.5] (actually part of the **FastLoader** patch), implement support of loading additional DDS formats.
- New performance patch : [**DisableHiddenPortraits**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/84) [KSP 1.8.0 - 1.12.5], prevent non-visible crew portraits from being rendered after a switch back from the map view (and other cases), causing a significant perf hit when there are many kerbals in the vessel.
- New performance/bugfix patch : [**DragCubeGeneration**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/137) [KSP 1.12.0 - 1.12.5], faster and more reliable implementation of drag cube generation. Improves overall loading times (both game load and scene/vessel/ship load times), prevent occasional lag spikes (in the editor mostly) and fix some issues causing incorrect drag cubes to be generated (notable examples are the stock inflatable heat shield, the 1.25m and 2.5m nose cones and the Mainsail shroud). Note that by design, this patch results in a small deviation from the stock behavior for buyoancy, aerodynamics and thermodynamics, as the generated drag cubes will be slightly different.
Expand Down