Skip to content

Commit e72c3c1

Browse files
committed
add MinorPerfTweaks patch
1 parent f62ccb2 commit e72c3c1

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

GameData/KSPCommunityFixes/Settings.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ KSP_COMMUNITY_FIXES
448448
// strut position tracking...
449449
PartSystemsFastUpdate = true
450450
451+
// Various small performance patches (volume normalizer, eva module checks)
452+
MinorPerfTweaks = true
453+
451454
// General micro-optimization of FlightIntegrator and VesselPrecalculate, significantely increase
452455
// framerate in large part count situations.
453456
FlightPerf = true

KSPCommunityFixes/KSPCommunityFixes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<Compile Include="Modding\ReflectionTypeLoadExceptionHandler.cs" />
204204
<Compile Include="Performance\FewerSaves.cs" />
205205
<Compile Include="Performance\ConfigNodePerf.cs" />
206+
<Compile Include="Performance\MinorPerfTweaks.cs" />
206207
<Compile Include="Performance\ModuleDockingNodeFindOtherNodesFaster.cs" />
207208
<Compile Include="Performance\OptimizedModuleRaycasts.cs" />
208209
<Compile Include="Performance\PQSCoroutineLeak.cs" />
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace KSPCommunityFixes.Performance
6+
{
7+
internal class MinorPerfTweaks : BasePatch
8+
{
9+
protected override Version VersionMin => new Version(1, 12, 3);
10+
11+
protected override void ApplyPatches()
12+
{
13+
AddPatch(PatchType.Override, typeof(Part), nameof(Part.isKerbalEVA));
14+
15+
AddPatch(PatchType.Override, typeof(VolumeNormalizer), nameof(VolumeNormalizer.Update));
16+
}
17+
18+
// Called (sometimes multiple times) in Part.FixedUpdate()
19+
public static bool Part_isKerbalEVA_Override(Part part)
20+
{
21+
part.cachedModules ??= new Dictionary<Type, PartModule>(10);
22+
23+
if (!part.cachedModules.TryGetValue(typeof(KerbalEVA), out PartModule module))
24+
{
25+
if (part.modules == null)
26+
return false;
27+
28+
List<PartModule> modules = part.modules.modules;
29+
int moduleCount = modules.Count;
30+
for (int i = 0; i < moduleCount; i++)
31+
{
32+
if (modules[i] is KerbalEVA)
33+
{
34+
module = modules[i];
35+
break;
36+
}
37+
}
38+
39+
part.cachedModules[typeof(KerbalEVA)] = module;
40+
}
41+
42+
return module.IsNotNullRef();
43+
}
44+
45+
// setting AudioListener.volume is actually quite costly (0.7% of the frame time),
46+
// so avoid setting it when the value hasn't actually changed...
47+
private static void VolumeNormalizer_Update_Override(VolumeNormalizer vn)
48+
{
49+
float newVolume;
50+
if (GameSettings.SOUND_NORMALIZER_ENABLED)
51+
{
52+
vn.threshold = GameSettings.SOUND_NORMALIZER_THRESHOLD;
53+
vn.sharpness = GameSettings.SOUND_NORMALIZER_RESPONSIVENESS;
54+
AudioListener.GetOutputData(vn.samples, 0);
55+
vn.level = 0f;
56+
57+
for (int i = 0; i < vn.sampleCount; i += 1 + GameSettings.SOUND_NORMALIZER_SKIPSAMPLES)
58+
vn.level = Mathf.Max(vn.level, Mathf.Abs(vn.samples[i]));
59+
60+
if (vn.level > vn.threshold)
61+
newVolume = vn.threshold / vn.level;
62+
else
63+
newVolume = 1f;
64+
65+
newVolume = Mathf.Lerp(AudioListener.volume, newVolume * GameSettings.MASTER_VOLUME, vn.sharpness * Time.deltaTime);
66+
}
67+
else
68+
{
69+
newVolume = Mathf.Lerp(AudioListener.volume, GameSettings.MASTER_VOLUME, vn.sharpness * Time.deltaTime);
70+
}
71+
72+
if (newVolume != vn.volume)
73+
AudioListener.volume = newVolume;
74+
75+
vn.volume = newVolume;
76+
}
77+
}
78+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
138138
- [**ModuleDockingNodeFindOtherNodesFaster**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Faster lookup of other docking nodes.
139139
- [**CollisionEnhancerFastUpdate**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Optimization of the `CollisionEnhancer` component (responsible for part to terrain collision detection).
140140
- [**PartSystemsFastUpdate**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Optimization of various flight scene auxiliary subsystems : temperature gauges, highlighter, strut position tracking...
141+
- [**MinorPerfTweaks**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Various small performance patches (volume normalizer, eva module checks)
141142
- [**FasterPartFindTransform**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/255) [KSP 1.12.3 - 1.12.5]<br/>Faster, and minimal GC alloc relacements for the Part FindModelTransform* and FindHeirarchyTransform* methods.
142143

143144
#### API and modding tools

0 commit comments

Comments
 (0)