11using System . Collections ;
22using System . Collections . Generic ;
3+ using System . Runtime . InteropServices ;
34using RocketSoundEnhancement . AudioFilters ;
45using RocketSoundEnhancement . Unity ;
56using 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}
0 commit comments