@@ -44,43 +44,68 @@ void Start()
44
44
{
45
45
// FIX BODIES MOVED POSTSPAWN //
46
46
Boolean postSpawnChanges = false ;
47
- foreach ( CelestialBody cb in PSystemManager . Instance . localBodies . Where ( b => b . Has ( "orbitPatches" ) ) )
47
+
48
+ // Replaced 'foreach' with 'for' to improve performance
49
+ var postSpawnBodies = PSystemManager . Instance ? . localBodies ? . FindAll ( b => b . Has ( "orbitPatches" ) ) ;
50
+
51
+ for ( int i = 0 ; i < postSpawnBodies ? . Count ; i ++ )
48
52
{
53
+ CelestialBody cb = postSpawnBodies [ i ] ;
54
+
49
55
// Fix position if the body gets moved PostSpawn
50
56
ConfigNode patch = cb . Get < ConfigNode > ( "orbitPatches" ) ;
51
57
if ( patch . GetValue ( "referenceBody" ) != null || patch . GetValue ( "semiMajorAxis" ) != null )
52
58
{
53
- // Get the body, the old parent and the new parent
54
- PSystemBody body = PSystemManager . Instance . systemPrefab . GetComponentsInChildren < PSystemBody > ( true ) . First ( b => b . name == name ) ;
55
- PSystemBody oldParent = PSystemManager . Instance . systemPrefab . GetComponentsInChildren < PSystemBody > ( true ) . First ( b => b . children . Contains ( body ) ) ;
59
+ // Get the body
60
+ PSystemBody body = PSystemManager . Instance ? . systemPrefab ? . GetComponentsInChildren < PSystemBody > ( true ) ? . FirstOrDefault ( b => b . name == cb . transform . name ) ;
61
+ if ( body == null )
62
+ {
63
+ Debug . Log ( "[Kopernicus]: RnDFixer: Could not find PSystemBody => " + cb . transform . name ) ;
64
+ continue ;
65
+ }
66
+
67
+ // Get the parent
68
+ PSystemBody oldParent = PSystemManager . Instance ? . systemPrefab ? . GetComponentsInChildren < PSystemBody > ( true ) ? . FirstOrDefault ( b => b . children . Contains ( body ) ) ;
69
+ if ( oldParent == null )
70
+ {
71
+ Debug . Log ( "[Kopernicus]: RnDFixer: Could not find referenceBody of CelestialBody => " + cb . transform . name ) ;
72
+ continue ;
73
+ }
74
+
75
+ // Check if PostSpawnOrbit changes referenceBody
56
76
PSystemBody newParent = oldParent ;
57
77
if ( patch . GetValue ( "referenceBody" ) != null )
58
- newParent = PSystemManager . Instance . systemPrefab . GetComponentsInChildren < PSystemBody > ( true ) . First ( b => b . name == patch . GetValue ( "referenceBody" ) ) ;
78
+ newParent = PSystemManager . Instance ? . systemPrefab ? . GetComponentsInChildren < PSystemBody > ( true ) . FirstOrDefault ( b => b . name == patch . GetValue ( "referenceBody" ) ) ;
79
+ if ( oldParent == null )
80
+ {
81
+ Debug . Log ( "[Kopernicus]: RnDFixer: Could not find PostSpawn referenceBody of CelestialBody => " + cb . transform . name ) ;
82
+ newParent = oldParent ;
83
+ }
59
84
60
- if ( body != null && oldParent != null )
85
+ // Check if PostSpawnOrbit changes semiMajorAxis
86
+ if ( body ? . orbitDriver ? . orbit ? . semiMajorAxis == null )
61
87
{
62
- // If there is no new SMA it means only the parent changed
63
- NumericParser < Double > newSMA = body . orbitDriver . orbit . semiMajorAxis ;
64
- if ( patch . GetValue ( "semiMajorAxis" ) != null )
65
- newSMA . SetFromString ( patch . GetValue ( "semiMajorAxis" ) ) ;
66
-
67
- // Count how many children comes before our body in the newParent.child list
68
- Int32 index = 0 ;
69
- foreach ( PSystemBody child in newParent . children )
70
- {
71
- if ( child . orbitDriver . orbit . semiMajorAxis < newSMA . value )
72
- index ++ ;
73
- }
74
-
75
- // Add the body as child for the new parent and remove it for the old parent
76
- if ( index > newParent . children . Count )
77
- newParent . children . Add ( body ) ;
78
- else
79
- newParent . children . Insert ( index , body ) ;
80
-
81
- oldParent . children . Remove ( body ) ;
82
- postSpawnChanges = true ;
88
+ Debug . Log ( "[Kopernicus]: RnDFixer: Could not find PostSpawn semiMajorAxis of CelestialBody => " + cb . transform . name ) ;
89
+ continue ;
83
90
}
91
+ NumericParser < Double > newSMA = body . orbitDriver . orbit . semiMajorAxis ;
92
+ if ( patch . GetValue ( "semiMajorAxis" ) != null )
93
+ newSMA . SetFromString ( patch . GetValue ( "semiMajorAxis" ) ) ;
94
+
95
+ // Remove the body from oldParent.children
96
+ oldParent . children . Remove ( body ) ;
97
+
98
+ // Find the index of the body in newParent.children
99
+ Int32 index = newParent . children . FindAll ( c => c . orbitDriver . orbit . semiMajorAxis < newSMA . value ) . Count ;
100
+
101
+ // Add the body to newParent.children
102
+ if ( index > newParent . children . Count )
103
+ newParent . children . Add ( body ) ;
104
+ else
105
+ newParent . children . Insert ( index , body ) ;
106
+
107
+ // Signal that the system has PostSpawn changes
108
+ postSpawnChanges = true ;
84
109
}
85
110
}
86
111
@@ -95,23 +120,29 @@ void Start()
95
120
96
121
// Create a list with body to hide and their parent
97
122
PSystemBody [ ] bodies = PSystemManager . Instance . systemPrefab . GetComponentsInChildren < PSystemBody > ( true ) ;
98
- foreach ( CelestialBody body in PSystemManager . Instance . localBodies . Where ( b => b . Has ( "hiddenRnD" ) ) )
99
- {
123
+
124
+ // Replaced 'foreach' with 'for' to improve performance
125
+ CelestialBody [ ] hideBodies = PSystemManager . Instance ? . localBodies ? . Where ( b => b . Has ( "hiddenRnD" ) ) . ToArray ( ) ;
126
+
127
+ for ( int i = 0 ; i < hideBodies ? . Length ; i ++ )
128
+ {
129
+ CelestialBody body = hideBodies [ i ] ;
130
+
100
131
if ( body . Get < PropertiesLoader . RDVisibility > ( "hiddenRnD" ) == PropertiesLoader . RDVisibility . SKIP )
101
132
{
102
- PSystemBody hidden = Utility . FindBody ( PSystemManager . Instance . systemPrefab . rootBody , name ) ;
133
+ PSystemBody hidden = Utility . FindBody ( PSystemManager . Instance . systemPrefab . rootBody , body . transform . name ) ;
103
134
if ( hidden . children . Count == 0 )
104
135
{
105
136
body . Set ( "hiddenRnd" , PropertiesLoader . RDVisibility . HIDDEN ) ;
106
137
}
107
138
else
108
139
{
109
- PSystemBody parent = bodies . First ( b => b . children . Contains ( hidden ) ) ;
140
+ PSystemBody parent = bodies . FirstOrDefault ( b => b . children . Contains ( hidden ) ) ;
110
141
if ( parent != null )
111
142
{
112
143
if ( skipList . Any ( b => b . Key == parent ) )
113
144
{
114
- Int32 index = skipList . IndexOf ( skipList . First ( b => b . Key == parent ) ) ;
145
+ Int32 index = skipList . IndexOf ( skipList . FirstOrDefault ( b => b . Key == parent ) ) ;
115
146
skipList . Insert ( index , new KeyValuePair < PSystemBody , PSystemBody > ( hidden , parent ) ) ;
116
147
}
117
148
else
@@ -121,8 +152,11 @@ void Start()
121
152
}
122
153
}
123
154
124
- foreach ( KeyValuePair < PSystemBody , PSystemBody > pair in skipList )
155
+ // Replaced 'foreach' with 'for' to improve performance
156
+ for ( int i = 0 ; i < skipList . Count ; i ++ )
125
157
{
158
+ KeyValuePair < PSystemBody , PSystemBody > pair = skipList [ i ] ;
159
+
126
160
// Get hidden body and parent
127
161
PSystemBody hidden = pair . Key ;
128
162
PSystemBody parent = pair . Value ;
@@ -137,20 +171,23 @@ void Start()
137
171
parent . children . Remove ( hidden ) ;
138
172
}
139
173
140
- if ( skipList . Count > 0 )
174
+ if ( skipList ? . Count > 0 )
141
175
{
142
176
// Rebuild Archives
143
177
AddPlanets ( ) ;
144
178
145
179
// Undo the changes to the PSystem
146
- for ( Int32 i = skipList . Count ; i > 0 ; i = i - 1 )
180
+ for ( Int32 i = skipList . Count - 1 ; i > - 1 ; i -- )
147
181
{
148
182
PSystemBody hidden = skipList . ElementAt ( i ) . Key ;
149
183
PSystemBody parent = skipList . ElementAt ( i ) . Value ;
150
- Int32 oldIndex = parent . children . IndexOf ( hidden . children . First ( ) ) ;
184
+ Int32 oldIndex = parent . children . IndexOf ( hidden . children . FirstOrDefault ( ) ) ;
151
185
parent . children . Insert ( oldIndex , hidden ) ;
152
- foreach ( PSystemBody child in hidden . children )
186
+
187
+ for ( int j = 0 ; j < hidden . children . Count ; j ++ )
153
188
{
189
+ PSystemBody child = hidden . children [ j ] ;
190
+
154
191
if ( parent . children . Contains ( child ) )
155
192
parent . children . Remove ( child ) ;
156
193
}
@@ -160,11 +197,21 @@ void Start()
160
197
161
198
162
199
// RDVisibility = HIDDEN // RDVisibility = NOICON //
163
- // Loop through the Container
164
- foreach ( RDPlanetListItemContainer planetItem in Resources . FindObjectsOfTypeAll < RDPlanetListItemContainer > ( ) )
200
+ // Loop through the Containers
201
+ var containers = Resources . FindObjectsOfTypeAll < RDPlanetListItemContainer > ( ) . Where ( i => i . label_planetName . text != "Planet name" ) . ToArray ( ) ;
202
+ for ( int i = 0 ; i < containers ? . Count ( ) ; i ++ )
165
203
{
204
+ RDPlanetListItemContainer planetItem = containers [ i ] ;
205
+
206
+ // The label text is set from the CelestialBody's displayName
207
+ CelestialBody body = PSystemManager . Instance ? . localBodies ? . FirstOrDefault ( b => b . bodyDisplayName . Replace ( "^N" , "" ) == planetItem . label_planetName . text ) ;
208
+ if ( body == null )
209
+ {
210
+ Debug . Log ( "[Kopernicus]: RnDFixer: Could not find CelestialBody for the label => " + planetItem . label_planetName . text ) ;
211
+ continue ;
212
+ }
213
+
166
214
// Barycenter
167
- CelestialBody body = PSystemManager . Instance . localBodies . Find ( b => b . transform . name == planetItem . label_planetName . text ) ;
168
215
if ( body . Has ( "barycenter" ) || body . Has ( "notSelectable" ) )
169
216
{
170
217
planetItem . planet . SetActive ( false ) ;
@@ -192,24 +239,17 @@ void Start()
192
239
planetItem . label_planetName . alignment = TextAlignmentOptions . MidlineRight ;
193
240
}
194
241
}
195
-
196
- // namechanges
197
- if ( FindObjectsOfType < NameChanger > ( ) . Count ( n => n . oldName == planetItem . label_planetName . text ) != 0 && ! planetItem . label_planetName . name . EndsWith ( "NAMECHANGER" ) )
198
- {
199
- NameChanger changer = FindObjectsOfType < NameChanger > ( ) . First ( n => n . oldName == planetItem . label_planetName . text ) ;
200
- planetItem . label_planetName . text = changer . newName ;
201
- planetItem . label_planetName . name += "NAMECHANGER" ;
202
- }
203
242
}
204
243
}
205
244
206
245
246
+
207
247
void AddPlanets ( )
208
248
{
209
249
// Stuff needed for AddPlanets
210
- FieldInfo list = typeof ( RDArchivesController ) . GetFields ( BindingFlags . Instance | BindingFlags . NonPublic ) . Skip ( 7 ) . First ( ) ;
211
- MethodInfo add = typeof ( RDArchivesController ) . GetMethod ( "AddPlanets" ) ;
212
- var RDAC = Resources . FindObjectsOfTypeAll < RDArchivesController > ( ) . First ( ) ;
250
+ FieldInfo list = typeof ( RDArchivesController ) . GetFields ( BindingFlags . Instance | BindingFlags . NonPublic ) . Skip ( 7 ) . FirstOrDefault ( ) ;
251
+ MethodInfo add = typeof ( RDArchivesController ) . GetMethods ( BindingFlags . Instance | BindingFlags . NonPublic ) ? . Skip ( 26 ) ? . FirstOrDefault ( ) ;
252
+ var RDAC = Resources . FindObjectsOfTypeAll < RDArchivesController > ( ) . FirstOrDefault ( ) ;
213
253
214
254
// AddPlanets requires this list to be empty when triggered
215
255
list . SetValue ( RDAC , new Dictionary < String , List < RDArchivesController . Filter > > ( ) ) ;
@@ -218,4 +258,4 @@ void AddPlanets()
218
258
add . Invoke ( RDAC , null ) ;
219
259
}
220
260
}
221
- }
261
+ }
0 commit comments