Skip to content

Commit a86ea57

Browse files
Scene switch perf (#39)
* perf: Minimal change to avoid iterating over every PART node * chore: Move defaults to module constructors * perf: Move config parsing to the LOADING scene This moves the entire config node parsing process to happen only during part compilation. * look up module prefab by index * add a comment for future concerns * more use of moduleindex --------- Co-authored-by: Phantomical <[email protected]>
1 parent 96b3ca3 commit a86ea57

File tree

9 files changed

+169
-113
lines changed

9 files changed

+169
-113
lines changed

Source/RocketSoundEnhancement/AudioUtility.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,6 @@ public static class AudioUtility
7070
public static AnimationCurve SmoothControl = AnimationCurve.EaseInOut(0f, 0.04f, 1f, 1f);
7171
public static string RSETag = "RSE";
7272

73-
public static ConfigNode GetConfigNode(string partInfoName, string moduleName, string moduleID = "")
74-
{
75-
var configs = GameDatabase.Instance.GetConfigs("PART");
76-
77-
foreach (var configNode in configs)
78-
{
79-
if (configNode.name.Replace("_", ".") == partInfoName)
80-
{
81-
if (moduleID == "")
82-
{
83-
return Array.FindAll(configNode.config.GetNodes("MODULE"), x => x.GetValue("name") == moduleName).FirstOrDefault();
84-
}
85-
else
86-
{
87-
return Array.FindAll(configNode.config.GetNodes("MODULE"), x => x.GetValue("name") == moduleName && x.GetValue("moduleID") == moduleID).FirstOrDefault();
88-
}
89-
}
90-
}
91-
return null;
92-
}
93-
9473
public static List<SoundLayer> CreateSoundLayerGroup(ConfigNode[] groupNodes)
9574
{
9675
var group = new List<SoundLayer>();

Source/RocketSoundEnhancement/PartModules/RSE_Coupler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ public class RSE_Coupler : RSE_Module
1111
private bool isDecoupler;
1212
private bool hasDecoupled;
1313

14+
public RSE_Coupler()
15+
{
16+
EnableLowpassFilter = true;
17+
EnableDistortionFilter = true;
18+
}
19+
1420
public override void OnStart(StartState state)
1521
{
1622
if (state == StartState.Editor || state == StartState.None)
1723
return;
1824

19-
EnableLowpassFilter = true;
20-
EnableDistortionFilter = true;
2125
base.OnStart(state);
2226

2327
if(part.GetComponent<ModuleDecouplerBase>()) {

Source/RocketSoundEnhancement/PartModules/RSE_Engines.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ public class RSE_Engines : RSE_Module
1212
public Dictionary<string, int> sharedSoundLayers = new Dictionary<string, int>();
1313
private List<ModuleEngines> engineModules = new List<ModuleEngines>();
1414

15-
public override void OnStart(StartState state)
15+
public RSE_Engines()
1616
{
17-
if(state == StartState.Editor || state == StartState.None)
18-
return;
19-
2017
EnableLowpassFilter = true;
2118
EnableCombFilter = true;
2219
EnableDistortionFilter = true;
20+
}
21+
22+
public override void OnStart(StartState state)
23+
{
24+
if (state == StartState.Editor || state == StartState.None)
25+
return;
26+
2327
base.OnStart(state);
2428

2529
var soundLayersCache = new HashSet<string>();

Source/RocketSoundEnhancement/PartModules/RSE_KerbalEVA.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ namespace RocketSoundEnhancement.PartModules
55
{
66
public class RSE_KerbalEVA : RSE_Module
77
{
8+
public RSE_KerbalEVA()
9+
{
10+
EnableLowpassFilter = true;
11+
}
12+
813
public override void OnStart(StartState state)
914
{
1015
if (state == StartState.Editor || state == StartState.None)
1116
return;
1217

13-
EnableLowpassFilter = true;
1418
base.OnStart(state);
1519

1620
Initialized = true;

Source/RocketSoundEnhancement/PartModules/RSE_Module.cs

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
34
using RocketSoundEnhancement.AudioFilters;
45
using RocketSoundEnhancement.Unity;
56
using UnityEngine;
@@ -17,7 +18,6 @@ public class RSE_Module : PartModule
1718

1819
public GameObject AudioParent { get; protected set; }
1920

20-
public ConfigNode PartConfigNode;
2121
public bool PrepareSoundLayers = true;
2222
public bool Initialized;
2323
public bool GamePaused;
@@ -53,51 +53,9 @@ public override void OnStart(StartState state)
5353
{
5454
string partParentName = part.name + "_" + this.moduleName;
5555
AudioParent = AudioUtility.CreateAudioParent(part, partParentName);
56-
PartConfigNode = AudioUtility.GetConfigNode(part.partInfo.name, this.moduleName);
57-
58-
if (!float.TryParse(PartConfigNode.GetValue("volume"), out Volume)) Volume = 1;
59-
if (!float.TryParse(PartConfigNode.GetValue("DopplerFactor"), out DopplerFactor)) DopplerFactor = 0.5f;
60-
61-
if (PartConfigNode.HasNode("AIRSIMULATION"))
62-
{
63-
var node = PartConfigNode.GetNode("AIRSIMULATION");
64-
65-
if (node.HasValue("EnableCombFilter")) bool.TryParse(node.GetValue("EnableCombFilter"), out EnableCombFilter);
66-
if (node.HasValue("EnableLowpassFilter")) bool.TryParse(node.GetValue("EnableLowpassFilter"), out EnableLowpassFilter);
67-
if (node.HasValue("EnableDistortionFilter")) bool.TryParse(node.GetValue("EnableDistortionFilter"), out EnableDistortionFilter);
68-
69-
if (node.HasValue("UpdateMode")) node.TryGetEnum("UpdateMode", ref AirSimUpdateMode, AirSimulationUpdate.Full);
70-
71-
if (node.HasValue("FarLowpass")) FarLowpass = float.Parse(node.GetValue("FarLowpass"));
72-
if (node.HasValue("MaxCombDelay")) MaxCombDelay = float.Parse(node.GetValue("MaxCombDelay"));
73-
if (node.HasValue("MaxCombMix")) MaxCombMix = float.Parse(node.GetValue("MaxCombMix"));
74-
if (node.HasValue("MaxDistortion")) MaxDistortion = float.Parse(node.GetValue("MaxDistortion"));
75-
if (node.HasValue("AngleHighpass")) AngleHighpass = float.Parse(node.GetValue("AngleHighpass"));
76-
}
77-
78-
UseAirSimulation = !(!EnableLowpassFilter && !EnableCombFilter && !EnableDistortionFilter);
7956

8057
if (PrepareSoundLayers)
8158
{
82-
foreach (var node in PartConfigNode.GetNodes())
83-
{
84-
var soundLayers = AudioUtility.CreateSoundLayerGroup(node.GetNodes("SOUNDLAYER"));
85-
if (soundLayers.Count == 0) continue;
86-
87-
var groupName = node.name;
88-
89-
if(node.name == "SOUNDLAYERGROUP")
90-
{
91-
groupName = node.GetValue("name");
92-
}
93-
94-
if (SoundLayerGroups.ContainsKey(groupName)) { SoundLayerGroups[groupName].AddRange(soundLayers); continue; }
95-
96-
SoundLayerGroups.Add(groupName, soundLayers);
97-
}
98-
99-
SoundLayers = AudioUtility.CreateSoundLayerGroup(PartConfigNode.GetNodes("SOUNDLAYER"));
100-
10159
if (SoundLayerGroups.Count > 0)
10260
{
10361
foreach (var soundLayerGroup in SoundLayerGroups)
@@ -263,7 +221,7 @@ public void PlaySoundLayer(SoundLayer soundLayer, float control, float volume, b
263221

264222
if (source.isPlaying && soundLayer.loop)
265223
source.volume = 0;
266-
224+
267225
return;
268226
}
269227

@@ -338,5 +296,66 @@ public virtual void OnDestroy()
338296
}
339297
Destroy(AudioParent);
340298
}
299+
300+
public override void OnLoad(ConfigNode node)
301+
{
302+
base.OnLoad(node);
303+
304+
// This likely doesn't work correctly with B9PS which can call OnLoad at unexpected times
305+
if (part?.partInfo?.partPrefab != null)
306+
{
307+
int moduleIndex = part.Modules.IndexOf(this);
308+
var prefab = part.partInfo.partPrefab.modules[moduleIndex] as RSE_Module;
309+
310+
SoundLayerGroups = prefab.SoundLayerGroups;
311+
SoundLayers = prefab.SoundLayers;
312+
return;
313+
}
314+
315+
// Do the actual parsing during part compilation.
316+
317+
node.TryGetValue("volume", ref Volume);
318+
node.TryGetValue("DopplerFactor", ref DopplerFactor);
319+
320+
ConfigNode sim = null;
321+
if (node.TryGetNode("AIRSIMULATION", ref sim))
322+
{
323+
sim.TryGetValue("EnableCombFilter", ref EnableCombFilter);
324+
sim.TryGetValue("EnableLowpassFilter", ref EnableLowpassFilter);
325+
sim.TryGetValue("EnableDistortionFilter", ref EnableDistortionFilter);
326+
327+
sim.TryGetEnum("UpdateMode", ref AirSimUpdateMode, AirSimulationUpdate.Full);
328+
329+
sim.TryGetValue("FarLowpass", ref FarLowpass);
330+
sim.TryGetValue("MaxCombDelay", ref MaxCombDelay);
331+
sim.TryGetValue("MaxCombMix", ref MaxCombMix);
332+
sim.TryGetValue("MaxDistortion", ref MaxDistortion);
333+
sim.TryGetValue("AngleHighpass", ref AngleHighpass);
334+
}
335+
336+
UseAirSimulation = !(!EnableLowpassFilter && !EnableCombFilter && !EnableDistortionFilter);
337+
338+
if (PrepareSoundLayers)
339+
{
340+
foreach (var lnode in node.GetNodes())
341+
{
342+
var soundLayers = AudioUtility.CreateSoundLayerGroup(lnode.GetNodes("SOUNDLAYER"));
343+
if (soundLayers.Count == 0) continue;
344+
345+
var groupName = lnode.name;
346+
347+
if (lnode.name == "SOUNDLAYERGROUP")
348+
{
349+
groupName = lnode.GetValue("name");
350+
}
351+
352+
if (SoundLayerGroups.ContainsKey(groupName)) { SoundLayerGroups[groupName].AddRange(soundLayers); continue; }
353+
354+
SoundLayerGroups.Add(groupName, soundLayers);
355+
}
356+
357+
SoundLayers = AudioUtility.CreateSoundLayerGroup(node.GetNodes("SOUNDLAYER"));
358+
}
359+
}
341360
}
342361
}

Source/RocketSoundEnhancement/PartModules/RSE_RCS.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ public class RSE_RCS : RSE_Module
77
{
88
private ModuleRCSFX moduleRCSFX;
99

10+
public RSE_RCS()
11+
{
12+
EnableLowpassFilter = true;
13+
}
14+
15+
1016
public override void OnStart(StartState state)
1117
{
1218
if (state == StartState.Editor || state == StartState.None)
1319
return;
1420

15-
EnableLowpassFilter = true;
1621
base.OnStart(state);
1722

1823
moduleRCSFX = part.Modules.GetModule<ModuleRCSFX>();

Source/RocketSoundEnhancement/PartModules/RSE_RotorEngines.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ public class RSE_RotorEngines : RSE_Module
2424
private ModuleResourceIntake resourceIntake;
2525
private int childPartsCount = 0;
2626

27+
public RSE_RotorEngines()
28+
{
29+
EnableLowpassFilter = true;
30+
EnableCombFilter = true;
31+
EnableDistortionFilter = true;
32+
}
33+
2734
public override void OnStart(StartState state)
2835
{
2936
if(state == StartState.Editor || state == StartState.None)
3037
return;
3138

32-
EnableLowpassFilter = true;
33-
EnableCombFilter = true;
34-
EnableDistortionFilter = true;
3539
base.OnStart(state);
3640

3741
rotorModule = part.GetComponent<ModuleRoboticServoRotor>();
@@ -58,8 +62,8 @@ public void SetupBlades()
5862
childPartsCount = childParts.Count;
5963
foreach (var childPart in childParts)
6064
{
61-
var configNode = GameDatabase.Instance.GetConfigs("PART").FirstOrDefault(x => x.name.Replace("_", ".") == childPart.partInfo.name);
62-
var propConfig = configNode.config.GetNode("RSE_Propellers");
65+
var configNode = childPart.partInfo.partConfig;
66+
var propConfig = configNode.GetNode("RSE_Propellers");
6367

6468
if (propConfig != null)
6569
{

Source/RocketSoundEnhancement/PartModules/RSE_Wheels.cs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,18 @@ public class RSE_Wheels : RSE_Module
2323
private float wheelSpeed = 0;
2424
private float slipDisplacement = 0;
2525

26+
public RSE_Wheels()
27+
{
28+
EnableLowpassFilter = true;
29+
}
30+
2631
public override void OnStart(StartState state)
2732
{
2833
if (state == StartState.Editor || state == StartState.None)
2934
return;
3035

31-
EnableLowpassFilter = true;
3236
base.OnStart(state);
3337

34-
if (PartConfigNode.HasNode("Motor"))
35-
{
36-
ConfigNode offLoadVolumeScaleNode;
37-
if ((offLoadVolumeScaleNode = PartConfigNode.GetNode("Motor").GetNode("offLoadVolumeScale")) != null && offLoadVolumeScaleNode.HasValues())
38-
{
39-
foreach (ConfigNode.Value node in offLoadVolumeScaleNode.values)
40-
{
41-
string soundLayerName = node.name;
42-
float value = float.Parse(node.value);
43-
44-
if (offLoadVolumeScale.ContainsKey(soundLayerName))
45-
{
46-
offLoadVolumeScale[soundLayerName] = value;
47-
continue;
48-
}
49-
50-
offLoadVolumeScale.Add(soundLayerName, value);
51-
}
52-
}
53-
}
54-
5538
moduleWheel = part.GetComponent<ModuleWheelBase>();
5639
moduleMotor = part.GetComponent<ModuleWheelMotor>();
5740
moduleDeploy = part.GetComponent<ModuleWheelDeployment>();
@@ -188,5 +171,42 @@ public override void FixedUpdate()
188171

189172
slipDisplacement = Mathf.Sqrt(x * x + y * y);
190173
}
174+
175+
public override void OnLoad(ConfigNode node)
176+
{
177+
base.OnLoad(node);
178+
179+
if (part?.partInfo?.partPrefab != null)
180+
{
181+
int moduleIndex = part.modules.IndexOf(this);
182+
var prefab = part.partInfo.partPrefab.modules[moduleIndex] as RSE_Wheels;
183+
offLoadVolumeScale = prefab.offLoadVolumeScale;
184+
return;
185+
}
186+
187+
ConfigNode motor = null;
188+
if (node.TryGetNode("Motor", ref motor))
189+
{
190+
ConfigNode offLoadVolumeScaleNode = motor.GetNode("offLoadVolumeScale");
191+
192+
if (offLoadVolumeScaleNode != null && offLoadVolumeScaleNode.HasValue())
193+
{
194+
foreach (ConfigNode.Value vnode in offLoadVolumeScaleNode.values)
195+
{
196+
string soundLayerName = vnode.name;
197+
float value = float.Parse(vnode.value);
198+
199+
if (offLoadVolumeScale.ContainsKey(soundLayerName))
200+
{
201+
offLoadVolumeScale[soundLayerName] = value;
202+
continue;
203+
}
204+
205+
offLoadVolumeScale.Add(soundLayerName, value);
206+
}
207+
}
208+
}
209+
}
210+
191211
}
192212
}

0 commit comments

Comments
 (0)