Skip to content

Commit b1e6c4d

Browse files
committed
Avoid re-parsing ship effects config on every vessel creation (e.g. during RUD)
1 parent 2054525 commit b1e6c4d

3 files changed

Lines changed: 69 additions & 68 deletions

File tree

Source/RocketSoundEnhancement/RocketSoundEnhancement.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ private void Awake()
7474
private void Start()
7575
{
7676
Settings.Load();
77+
// TODO: eventually separate these; we don't need to Load every time we enter the flight scene!
7778
ShipEffectsConfig.Load();
79+
ShipEffectsConfig.Start();
7880

7981
ApplySettings();
8082

Source/RocketSoundEnhancement/ShipEffects.cs

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public static AerodynamicsFX AeroFX
2121
}
2222
}
2323

24-
public Dictionary<PhysicsControl, List<SoundLayer>> SoundLayerGroups = new Dictionary<PhysicsControl, List<SoundLayer>>();
2524
public Dictionary<string, AudioSource> Sources = new Dictionary<string, AudioSource>();
2625
public Dictionary<string, AirSimulationFilter> AirSimFilters = new Dictionary<string, AirSimulationFilter>();
2726

@@ -66,37 +65,15 @@ public bool Initialize()
6665
return true;
6766
}
6867

69-
if (ShipEffectsConfig.ShipEffectsConfigNode.Count > 0)
70-
{
71-
foreach (var node in ShipEffectsConfig.ShipEffectsConfigNode)
72-
{
73-
if (!Enum.TryParse(node.name, true, out PhysicsControl controlGroup)) continue;
74-
if (ignoreVessel && controlGroup != PhysicsControl.SONICBOOM) continue;
75-
76-
if (node.HasNode("SOUNDLAYER"))
77-
{
78-
var soundLayers = AudioUtility.CreateSoundLayerGroup(node.GetNodes("SOUNDLAYER"));
79-
if (soundLayers.Count == 0) continue;
80-
81-
if (SoundLayerGroups.ContainsKey(controlGroup)) { SoundLayerGroups[controlGroup].AddRange(soundLayers); continue; }
82-
83-
SoundLayerGroups.Add(controlGroup, soundLayers);
84-
}
85-
}
86-
}
87-
8868
audioParent = new GameObject($"ShipEffects_{vessel.vesselName}");
8969
audioParent.transform.rotation = vessel.transform.rotation;
9070
audioParent.transform.position = vessel.transform.position;
9171
audioParent.transform.parent = vessel.transform;
9272

93-
if (SoundLayerGroups.Count > 0)
73+
foreach (var soundLayerGroup in ShipEffectsConfig.SoundLayerGroups)
9474
{
95-
foreach (var soundLayerGroup in SoundLayerGroups)
96-
{
97-
var hasAirSimFilter = soundLayerGroup.Key != PhysicsControl.SONICBOOM;
98-
StartCoroutine(SetupAudioSources(soundLayerGroup.Value, hasAirSimFilter));
99-
}
75+
var hasAirSimFilter = soundLayerGroup.Key != PhysicsControl.SONICBOOM;
76+
StartCoroutine(SetupAudioSources(soundLayerGroup.Value, hasAirSimFilter));
10077
}
10178

10279
CacheVesselData();
@@ -280,40 +257,43 @@ private void LateUpdate()
280257
if (!HighLogic.LoadedSceneIsFlight || !initialized || gamePaused || noPhysics || ignoreVessel)
281258
return;
282259

283-
if (SoundLayerGroups.ContainsKey(PhysicsControl.SONICBOOM) && Settings.MachEffectsAmount > 0 && !MapView.MapCamera.isActiveAndEnabled)
260+
foreach (var soundLayerGroup in ShipEffectsConfig.SoundLayerGroups)
284261
{
285-
if (MachPass > Settings.MachEffectLowerLimit && !SonicBoomed)
262+
if (soundLayerGroup.Key == PhysicsControl.SONICBOOM)
286263
{
287-
SonicBoomed = true;
288-
if (vessel.mach > 1)
264+
if (Settings.MachEffectsAmount > 0 && !MapView.MapCamera.isActiveAndEnabled)
289265
{
290-
foreach (var soundLayer in SoundLayerGroups[PhysicsControl.SONICBOOM])
266+
if (MachPass > Settings.MachEffectLowerLimit && !SonicBoomed)
291267
{
292-
if (vessel.crewableParts == 0 && soundLayer.channel == FXChannel.Interior)
293-
continue;
268+
SonicBoomed = true;
269+
if (vessel.mach > 1)
270+
{
271+
foreach (var soundLayer in soundLayerGroup.Value)
272+
{
273+
if (vessel.crewableParts == 0 && soundLayer.channel == FXChannel.Interior)
274+
continue;
275+
276+
PlaySonicBoom(soundLayer);
277+
}
278+
}
279+
}
294280

295-
PlaySonicBoom(soundLayer);
281+
if (MachPass == Settings.MachEffectLowerLimit)
282+
{
283+
SonicBoomed = false;
296284
}
297285
}
298286
}
299-
300-
if (MachPass == Settings.MachEffectLowerLimit)
301-
{
302-
SonicBoomed = false;
303-
}
304-
}
305-
306-
foreach (var soundLayerGroup in SoundLayerGroups)
307-
{
308-
if (soundLayerGroup.Key == PhysicsControl.SONICBOOM) continue;
309-
310-
float rawControl = GetPhysicsController(soundLayerGroup.Key);
311-
foreach (var soundLayer in soundLayerGroup.Value)
287+
else
312288
{
313-
if (vessel.crewableParts == 0 && soundLayer.channel == FXChannel.Interior)
314-
continue;
289+
float rawControl = GetPhysicsController(soundLayerGroup.Key);
290+
foreach (var soundLayer in soundLayerGroup.Value)
291+
{
292+
if (vessel.crewableParts == 0 && soundLayer.channel == FXChannel.Interior)
293+
continue;
315294

316-
PlaySoundLayer(soundLayer, rawControl);
295+
PlaySoundLayer(soundLayer, rawControl);
296+
}
317297
}
318298
}
319299

Source/RocketSoundEnhancement/ShipEffectsConfig.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
11
using KSP.UI.Screens;
2+
using System;
23
using System.Collections.Generic;
4+
using UnityEngine;
35

46
namespace RocketSoundEnhancement
57
{
68
public static class ShipEffectsConfig
79
{
810
public static bool MuteStockAeroSounds = false;
11+
private static AudioClip nextStageClip;
12+
private static AudioClip cannotSeparateClip;
913

10-
private static List<ConfigNode> _shipEffectsNodes = new List<ConfigNode>();
11-
public static List<ConfigNode> ShipEffectsConfigNode
14+
public static Dictionary<PhysicsControl, List<SoundLayer>> SoundLayerGroups;
15+
16+
public static void Load()
1217
{
13-
get
18+
if (SoundLayerGroups != null) return;
19+
SoundLayerGroups = new Dictionary<PhysicsControl, List<SoundLayer>>();
20+
21+
foreach (var configNode in GameDatabase.Instance.GetConfigNodes("SHIPEFFECTS_SOUNDLAYERS"))
1422
{
15-
if (_shipEffectsNodes.Count == 0)
23+
if (configNode.HasValue("MuteStockAeroSounds"))
24+
bool.TryParse(configNode.GetValue("MuteStockAeroSounds"), out MuteStockAeroSounds);
25+
if (configNode.HasValue("nextStageClip"))
26+
nextStageClip = GameDatabase.Instance.GetAudioClip(configNode.GetValue("nextStageClip"));
27+
if (configNode.HasValue("cannotSeparateClip"))
28+
cannotSeparateClip = GameDatabase.Instance.GetAudioClip(configNode.GetValue("cannotSeparateClip"));
29+
30+
foreach (var node in configNode.GetNodes())
1631
{
17-
foreach (var configNode in GameDatabase.Instance.GetConfigNodes("SHIPEFFECTS_SOUNDLAYERS"))
18-
{
19-
_shipEffectsNodes.AddRange(configNode.GetNodes());
20-
}
21-
}
32+
if (!Enum.TryParse(node.name, true, out PhysicsControl controlGroup)) continue;
33+
if (!node.HasNode("SOUNDLAYER")) continue;
34+
35+
var soundLayers = AudioUtility.CreateSoundLayerGroup(node.GetNodes("SOUNDLAYER"));
36+
if (soundLayers.Count == 0) continue;
2237

23-
return _shipEffectsNodes;
38+
if (SoundLayerGroups.ContainsKey(controlGroup))
39+
SoundLayerGroups[controlGroup].AddRange(soundLayers);
40+
else
41+
SoundLayerGroups.Add(controlGroup, soundLayers);
42+
}
2443
}
2544
}
2645

27-
public static void Load()
46+
public static void Start()
2847
{
29-
foreach (var configNode in GameDatabase.Instance.GetConfigNodes("SHIPEFFECTS_SOUNDLAYERS"))
48+
if (nextStageClip != null)
3049
{
31-
if (configNode.HasValue("MuteStockAeroSounds"))
32-
bool.TryParse(configNode.GetValue("MuteStockAeroSounds"), out MuteStockAeroSounds);
33-
if (configNode.HasValue("nextStageClip"))
34-
StageManager.Instance.nextStageClip = GameDatabase.Instance.GetAudioClip(configNode.GetValue("nextStageClip"));
35-
if (configNode.HasValue("cannotSeparateClip"))
36-
StageManager.Instance.cannotSeparateClip = GameDatabase.Instance.GetAudioClip(configNode.GetValue("cannotSeparateClip"));
50+
StageManager.Instance.nextStageClip = nextStageClip;
51+
}
52+
53+
if (cannotSeparateClip != null)
54+
{
55+
StageManager.Instance.cannotSeparateClip = cannotSeparateClip;
3756
}
3857
}
3958
}

0 commit comments

Comments
 (0)