diff --git a/GameData/KSPCommunityFixes/Settings.cfg b/GameData/KSPCommunityFixes/Settings.cfg index 50f81f8..e99d5dd 100644 --- a/GameData/KSPCommunityFixes/Settings.cfg +++ b/GameData/KSPCommunityFixes/Settings.cfg @@ -292,6 +292,10 @@ KSP_COMMUNITY_FIXES // speed up reading) at the cost of human readability. SkipIndentsOnSavesAndCraftFiles = false + // After using physics warp, Unity's max physics dt will never be returned to the value specified in + // game settings which can degrade performance in some cases + RestoreMaxPhysicsDT = true + // ########################## // Modding // ########################## diff --git a/KSPCommunityFixes/BugFixes/RestoreMaxPhysicsDT.cs b/KSPCommunityFixes/BugFixes/RestoreMaxPhysicsDT.cs new file mode 100644 index 0000000..d61b9d5 --- /dev/null +++ b/KSPCommunityFixes/BugFixes/RestoreMaxPhysicsDT.cs @@ -0,0 +1,30 @@ +using HarmonyLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace KSPCommunityFixes.BugFixes +{ + public class RestoreMaxPhysicsDT : BasePatch + { + protected override void ApplyPatches(List patches) + { + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(TimeWarp), "updateRate"), + this)); + } + + static void TimeWarp_updateRate_Prefix() + { + // when you use physics warp, it increases Time.fixedDeltaTime + // Unity will internally increase maximumDeltaTime to be at least as high as fixedDeltaTime + // But nothing in the KSP code will ever return maximumDeltaTime to the value from the settings when time warp is over + // Setting it just before TimeWarp sets fixedDeltaTime guarantees that all points of code will have the correct value + Time.maximumDeltaTime = GameSettings.PHYSICS_FRAME_DT_LIMIT; + } + } +} diff --git a/KSPCommunityFixes/KSPCommunityFixes.csproj b/KSPCommunityFixes/KSPCommunityFixes.csproj index 55f3cbd..77b4b69 100644 --- a/KSPCommunityFixes/KSPCommunityFixes.csproj +++ b/KSPCommunityFixes/KSPCommunityFixes.csproj @@ -104,6 +104,7 @@ +