Skip to content

Commit 1e446c9

Browse files
authoredSep 17, 2017
Merge pull request #229 from Kopernicus/rndfixer
Fix RnDFixer
2 parents e16b5e4 + b606b0f commit 1e446c9

File tree

2 files changed

+97
-53
lines changed

2 files changed

+97
-53
lines changed
 

‎Kopernicus/Kopernicus/Configuration/Body.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ public String cbNameLater
6363
}
6464
set
6565
{
66+
// Change the displayName
67+
generatedBody.celestialBody.bodyDisplayName = value;
68+
69+
// Set the NameChanger component
6670
if (!generatedBody.celestialBody.GetComponent<NameChanger>())
6771
{
6872
NameChanger changer = generatedBody.celestialBody.gameObject.AddComponent<NameChanger>();
6973
changer.oldName = name;
7074
changer.newName = value;
7175
}
7276
else
73-
generatedBody.celestialBody.gameObject.GetComponent<NameChanger>().newName = cbNameLater;
77+
generatedBody.celestialBody.gameObject.GetComponent<NameChanger>().newName = value;
7478
}
7579
}
7680

‎Kopernicus/Kopernicus/RuntimeUtility/RnDFixer.cs

+92-52
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,68 @@ void Start()
4444
{
4545
// FIX BODIES MOVED POSTSPAWN //
4646
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++)
4852
{
53+
CelestialBody cb = postSpawnBodies[i];
54+
4955
// Fix position if the body gets moved PostSpawn
5056
ConfigNode patch = cb.Get<ConfigNode>("orbitPatches");
5157
if (patch.GetValue("referenceBody") != null || patch.GetValue("semiMajorAxis") != null)
5258
{
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
5676
PSystemBody newParent = oldParent;
5777
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+
}
5984

60-
if (body != null && oldParent != null)
85+
// Check if PostSpawnOrbit changes semiMajorAxis
86+
if (body?.orbitDriver?.orbit?.semiMajorAxis == null)
6187
{
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;
8390
}
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;
84109
}
85110
}
86111

@@ -95,23 +120,29 @@ void Start()
95120

96121
// Create a list with body to hide and their parent
97122
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+
100131
if (body.Get<PropertiesLoader.RDVisibility>("hiddenRnD") == PropertiesLoader.RDVisibility.SKIP)
101132
{
102-
PSystemBody hidden = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, name);
133+
PSystemBody hidden = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, body.transform.name);
103134
if (hidden.children.Count == 0)
104135
{
105136
body.Set("hiddenRnd", PropertiesLoader.RDVisibility.HIDDEN);
106137
}
107138
else
108139
{
109-
PSystemBody parent = bodies.First(b => b.children.Contains(hidden));
140+
PSystemBody parent = bodies.FirstOrDefault(b => b.children.Contains(hidden));
110141
if (parent != null)
111142
{
112143
if (skipList.Any(b => b.Key == parent))
113144
{
114-
Int32 index = skipList.IndexOf(skipList.First(b => b.Key == parent));
145+
Int32 index = skipList.IndexOf(skipList.FirstOrDefault(b => b.Key == parent));
115146
skipList.Insert(index, new KeyValuePair<PSystemBody, PSystemBody>(hidden, parent));
116147
}
117148
else
@@ -121,8 +152,11 @@ void Start()
121152
}
122153
}
123154

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++)
125157
{
158+
KeyValuePair<PSystemBody, PSystemBody> pair = skipList[i];
159+
126160
// Get hidden body and parent
127161
PSystemBody hidden = pair.Key;
128162
PSystemBody parent = pair.Value;
@@ -137,20 +171,23 @@ void Start()
137171
parent.children.Remove(hidden);
138172
}
139173

140-
if (skipList.Count > 0)
174+
if (skipList?.Count > 0)
141175
{
142176
// Rebuild Archives
143177
AddPlanets();
144178

145179
// 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--)
147181
{
148182
PSystemBody hidden = skipList.ElementAt(i).Key;
149183
PSystemBody parent = skipList.ElementAt(i).Value;
150-
Int32 oldIndex = parent.children.IndexOf(hidden.children.First());
184+
Int32 oldIndex = parent.children.IndexOf(hidden.children.FirstOrDefault());
151185
parent.children.Insert(oldIndex, hidden);
152-
foreach (PSystemBody child in hidden.children)
186+
187+
for (int j = 0; j < hidden.children.Count; j++)
153188
{
189+
PSystemBody child = hidden.children[j];
190+
154191
if (parent.children.Contains(child))
155192
parent.children.Remove(child);
156193
}
@@ -160,11 +197,21 @@ void Start()
160197

161198

162199
// 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++)
165203
{
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+
166214
// Barycenter
167-
CelestialBody body = PSystemManager.Instance.localBodies.Find(b => b.transform.name == planetItem.label_planetName.text);
168215
if (body.Has("barycenter") || body.Has("notSelectable"))
169216
{
170217
planetItem.planet.SetActive(false);
@@ -192,24 +239,17 @@ void Start()
192239
planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineRight;
193240
}
194241
}
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-
}
203242
}
204243
}
205244

206245

246+
207247
void AddPlanets()
208248
{
209249
// 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();
213253

214254
// AddPlanets requires this list to be empty when triggered
215255
list.SetValue(RDAC, new Dictionary<String, List<RDArchivesController.Filter>>());
@@ -218,4 +258,4 @@ void AddPlanets()
218258
add.Invoke(RDAC, null);
219259
}
220260
}
221-
}
261+
}

0 commit comments

Comments
 (0)
Please sign in to comment.