2727 */
2828
2929using System ;
30+ using System . Collections . Generic ;
3031using System . Linq ;
3132using UnityEngine ;
3233
@@ -53,10 +54,13 @@ public void run()
5354
5455 try
5556 {
56- var evaParts = PartLoader . LoadedPartsList . Where ( p => p . name . Contains ( "kerbalEVA" ) ) ;
57- foreach ( var evaPart in evaParts )
57+ IEnumerable < AvailablePart > evaParts = PartLoader . LoadedPartsList . Where ( p => p . name . Contains ( "kerbalEVA" ) ) ;
58+ foreach ( AvailablePart evaPart in evaParts )
5859 {
59- EvaAddLifeSupport ( evaPart ) ;
60+ if ( evaPart . partPrefab != null && evaPart . partPrefab . Resources != null )
61+ {
62+ EvaAddLifeSupport ( evaPart ) ;
63+ }
6064 }
6165 }
6266 catch ( Exception ex )
@@ -86,11 +90,19 @@ public void ChangeValues()
8690 private void EvaAddLifeSupport ( AvailablePart part )
8791 {
8892 Part prefabPart = part . partPrefab ;
89-
93+ if ( prefabPart == null )
94+ {
95+ this . Log ( "Part " + part . name + " has no partPrefab" ) ;
96+ return ;
97+ }
9098 this . Log ( "Adding resources to " + part . name + "/" + prefabPart . partInfo . title ) ;
9199
92100 EvaAddPartModule ( prefabPart ) ;
93101 if ( HighLogic . CurrentGame == null ) return ;
102+ if ( prefabPart . Resources == null || ! prefabPart . Resources . IsValid || prefabPart . SimulationResources == null || ! prefabPart . SimulationResources . IsValid )
103+ {
104+ prefabPart . SetupResources ( ) ;
105+ }
94106 EvaAddResource ( prefabPart , HighLogic . CurrentGame . Parameters . CustomParams < TAC_SettingsParms_Sec2 > ( ) . EvaElectricityConsumptionRate , globalSettings . Electricity , false ) ;
95107 EvaAddResource ( prefabPart , HighLogic . CurrentGame . Parameters . CustomParams < TAC_SettingsParms_Sec2 > ( ) . FoodConsumptionRate , globalSettings . Food , false ) ;
96108 EvaAddResource ( prefabPart , HighLogic . CurrentGame . Parameters . CustomParams < TAC_SettingsParms_Sec2 > ( ) . WaterConsumptionRate , globalSettings . Water , false ) ;
@@ -111,20 +123,13 @@ private void EvaAddPartModule(Part part)
111123 {
112124 ConfigNode node = new ConfigNode ( "MODULE" ) ;
113125 node . AddValue ( "name" , "LifeSupportModule" ) ;
114- int c = part . Modules . Count ;
115- bool Found = false ;
116- for ( int mI = 0 ; mI < c ; ++ mI )
126+ node . AddValue ( "moduleName" , "LifeSupportModule" ) ;
127+ if ( part . FindModuleImplementing < LifeSupportModule > ( ) == null )
117128 {
118- if ( part . Modules [ mI ] . moduleName == "LifeSupportModule" )
119- {
120- Found = true ;
121- break ;
122- }
123- }
124- if ( ! Found )
125129 part . AddModule ( node ) ;
130+ }
126131
127- this . LogWarning ( "The expected exception did not happen when adding the Life Support part module to the EVA!" ) ;
132+ // this.LogWarning("The expected exception did not happen when adding the Life Support part module to " + part.partInfo.name + "-" + part.partInfo.title );
128133 }
129134 catch ( Exception ex )
130135 {
0 commit comments