Skip to content

Commit

Permalink
Fix DoubleCurve to no longer flatten the first key's tangent.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanKell committed Jul 26, 2022
1 parent 728204a commit 5f64961
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ KSP_COMMUNITY_FIXES
// causing loss of vessel effects audio and random volume or left/right channel weirdness.
LostSoundAfterSceneSwitch = true
// Fix DoubleCurve flattening the tangents of the first keyframe regardless of whether tangents are supplied.
DoubleCurvePreserveTangents = true
// Fix recovery of EVAing kerbals either causing their inventory to be recovered twice or the
// science data they carry not being recovered, depending on the EVA kerbal variant/suit.
EVAKerbalRecovery = true
Expand Down
40 changes: 40 additions & 0 deletions KSPCommunityFixes/BugFixes/DoubleCurvePreserveTangents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using HarmonyLib;
using System.Reflection.Emit;

namespace KSPCommunityFixes.BugFixes
{
class DoubleCurvePreserveTangents : BasePatch
{
protected override Version VersionMin => new Version(1, 8, 0);

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

static IEnumerable<CodeInstruction> DoubleCurve_RecomputeTangents_Transpiler(IEnumerable<CodeInstruction> instructions)
{
// The existing function has a test if ( count == 1 ) and, if true, it
// will flatten the tangents of the key regardless of if it is
// set to autotangent or not. Since the tangents of a single-key
// curve don't matter, let's just make the test always false,
// by making it if ( count == -1 ).
List<CodeInstruction> code = new List<CodeInstruction>(instructions);
for (int i = 1; i < code.Count; ++i)
{
if (code[i].opcode == OpCodes.Ldc_I4_1 && code[i-1].opcode == OpCodes.Ldloc_1)
{
code[i].opcode = OpCodes.Ldc_I4_M1;
break;
}
}

return code;
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="BasePatch.cs" />
<Compile Include="BugFixes\AsteroidInfiniteMining.cs" />
<Compile Include="BugFixes\EnginePlateAirstreamShieldedTopPart.cs" />
<Compile Include="BugFixes\DoubleCurvePreserveTangents.cs" />
<Compile Include="Performance\CommNetThrottling.cs" />
<Compile Include="Performance\DisableMapUpdateInFlight.cs" />
<Compile Include="Performance\MemoryLeaks.cs" />
Expand Down

0 comments on commit 5f64961

Please sign in to comment.