Skip to content

Commit

Permalink
New KSP bugfix (issue #42) : Fix audio source not being centered/alig…
Browse files Browse the repository at this point in the history
…ned with the current vessel after scene switches, causing loss of vessel effects audio and random volume or left/right channel weirdness.

Thanks to @ensou04 for uncovering the cause of this bug.
  • Loading branch information
gotmachine committed Jan 30, 2023
1 parent 18e152b commit 502f00c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ KSP_COMMUNITY_FIXES
// PQSLandControl definition.
ScatterDistribution = true
// Fix audio source not being centered/aligned with the current vessel after scene switches,
// causing loss of vessel effects audio and random volume or left/right channel weirdness.
LostSoundAfterSceneSwitch = true
GetPotentialTorqueFixes = true
BetterSAS = true
Expand Down
65 changes: 65 additions & 0 deletions KSPCommunityFixes/BugFixes/LostSoundAfterSceneSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;

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

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

patches.Add(new PatchInfo(
PatchMethodType.Transpiler,
AccessTools.Method(typeof(FlightCamera), nameof(FlightCamera.DisableCamera), new[] {typeof(bool)}),
this));
}

static IEnumerable<CodeInstruction> FlightCamera_EnableCamera_Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo setParentOriginal = AccessTools.Method(typeof(Transform), nameof(Transform.SetParent), new[] {typeof(Transform)});
MethodInfo setParentReplacement = AccessTools.Method(typeof(Transform), nameof(Transform.SetParent), new[] { typeof(Transform), typeof(bool) });
List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Callvirt && ReferenceEquals(code[i].operand, setParentOriginal))
{
code[i].operand = setParentReplacement;
code.Insert(i, new CodeInstruction(OpCodes.Ldc_I4_0));
}
}


return code;
}

static IEnumerable<CodeInstruction> FlightCamera_DisableCamera_Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo setParentOriginal = AccessTools.Method(typeof(Transform), nameof(Transform.SetParent), new[] { typeof(Transform) });
MethodInfo setParentReplacement = AccessTools.Method(typeof(Transform), nameof(Transform.SetParent), new[] { typeof(Transform), typeof(bool) });

List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Callvirt && ReferenceEquals(code[i].operand, setParentOriginal))
{
code[i].operand = setParentReplacement;
code.Insert(i, new CodeInstruction(OpCodes.Ldc_I4_0));
}
}

return code;
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="BugFixes\ExtendedDeployableParts.cs" />
<Compile Include="BugFixes\DeltaVHideWhenDisabled.cs" />
<Compile Include="BugFixes\KerbalTooltipMaxSustainedG.cs" />
<Compile Include="BugFixes\LostSoundAfterSceneSwitch.cs" />
<Compile Include="BugFixes\GetPotentialTorqueFixes.cs" />
<Compile Include="BugFixes\PackedPartsRotation.cs" />
<Compile Include="BugFixes\PartListTooltipIconSpin.cs" />
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- **AsteroidSpawnerUniqueFlightId** [KSP 1.8.0 - 1.12.3]<br/>Fix the asteroid/comet spawner generating non-unique `Part.flightId` identifiers. This has a few minor side effects in stock (mainly incorrect science bonuses), but this field is heavily relied upon by various mods and this can cause major issues for them.
- **PartListTooltipIconSpin** [KSP 1.8.0 - 1.12.3]<br/> Fix editor tooltip part icons not spinning anymore after hovering on a greyed out surface attachable only part while the editor is empty.
- **[ScatterDistribution](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/41)** [KSP 1.8.0 - 1.12.3]<br/>Fix incorrect terrain scatter distribution when a partial longitude range is defined in the PQSLandControl definition.
- **LostSoundAfterSceneSwitch** [KSP 1.12.0 - 1.12.3]<br/> Fix audio source not being centered/aligned with the current vessel after scene switches, causing loss of vessel effects audio and random volume or left/right channel weirdness.

#### Quality of Life tweaks

Expand Down Expand Up @@ -126,6 +127,8 @@ If doing so in the `Debug` configuration and if your KSP install is modified to

##### 1.15.0
- New KSP bugfix : [ScatterDistribution](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/41) (credit to @R-T-B)
- New KSP bugfix : LostSoundAfterSceneSwitch (credit to @ensou04)
- Fixed KerbalInventoryPersistence patch not being applied on KSP 1.12.3

##### 1.14.1
- Fix KSPCF [issue #39](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/39) : AltimeterHorizontalPosition patch causes state inconsistencies with vessel filters.
Expand Down

0 comments on commit 502f00c

Please sign in to comment.