void Start() { // FIX BODIES MOVED POSTSPAWN // Boolean postSpawnChanges = false; // Replaced 'foreach' with 'for' to improve performance CelestialBody[] postSpawnBodies = PSystemManager.Instance?.localBodies?.Where(b => b.Has("orbitPatches"))?.ToArray(); for (Int32 i = 0; i < postSpawnBodies?.Length; i++) { CelestialBody cb = postSpawnBodies[i]; // Fix position if the body gets moved PostSpawn ConfigNode patch = cb.Get <ConfigNode>("orbitPatches"); if (patch.GetValue("referenceBody") != null || patch.GetValue("semiMajorAxis") != null) { // Get the body PSystemBody body = PSystemManager.Instance?.systemPrefab?.GetComponentsInChildren <PSystemBody>(true)?.FirstOrDefault(b => b.name == cb.transform.name); if (body == null) { Debug.Log("[Kopernicus]: RnDFixer: Could not find PSystemBody => " + cb.transform.name); continue; } // Get the parent PSystemBody oldParent = PSystemManager.Instance?.systemPrefab?.GetComponentsInChildren <PSystemBody>(true)?.FirstOrDefault(b => b.children.Contains(body)); if (oldParent == null) { Debug.Log("[Kopernicus]: RnDFixer: Could not find referenceBody of CelestialBody => " + cb.transform.name); continue; } // Check if PostSpawnOrbit changes referenceBody PSystemBody newParent = oldParent; if (patch.GetValue("referenceBody") != null) { newParent = PSystemManager.Instance?.systemPrefab?.GetComponentsInChildren <PSystemBody>(true).FirstOrDefault(b => b.name == patch.GetValue("referenceBody")); } if (oldParent == null) { Debug.Log("[Kopernicus]: RnDFixer: Could not find PostSpawn referenceBody of CelestialBody => " + cb.transform.name); newParent = oldParent; } // Check if PostSpawnOrbit changes semiMajorAxis if (body?.orbitDriver?.orbit?.semiMajorAxis == null) { Debug.Log("[Kopernicus]: RnDFixer: Could not find PostSpawn semiMajorAxis of CelestialBody => " + cb.transform.name); continue; } NumericParser <Double> newSMA = body.orbitDriver.orbit.semiMajorAxis; if (patch.GetValue("semiMajorAxis") != null) { newSMA.SetFromString(patch.GetValue("semiMajorAxis")); } // Remove the body from oldParent.children oldParent.children.Remove(body); // Find the index of the body in newParent.children Int32 index = newParent.children.FindAll(c => c.orbitDriver.orbit.semiMajorAxis < newSMA.Value).Count; // Add the body to newParent.children if (index > newParent.children.Count) { newParent.children.Add(body); } else { newParent.children.Insert(index, body); } // Signal that the system has PostSpawn changes postSpawnChanges = true; } } // Rebuild Archives if (postSpawnChanges) { AddPlanets(); } // RDVisibility = HIDDEN // RDVisibility = SKIP // // Create a list with body to hide and their parent List <KeyValuePair <PSystemBody, KeyValuePair <PSystemBody, Int32> > > hideList = new List <KeyValuePair <PSystemBody, KeyValuePair <PSystemBody, Int32> > >(); // Create a list with body to skip and their parent List <KeyValuePair <PSystemBody, PSystemBody> > skipList = new List <KeyValuePair <PSystemBody, PSystemBody> >(); // Replaced 'foreach' with 'for' to improve performance PSystemBody[] bodies = PSystemManager.Instance.systemPrefab.GetComponentsInChildren <PSystemBody>(true); CelestialBody[] hideBodies = PSystemManager.Instance?.localBodies?.Where(cb => cb.Has("hiddenRnD"))?.ToArray(); for (Int32 i = 0; i < hideBodies?.Length; i++) { CelestialBody body = hideBodies[i]; PropertiesLoader.RDVisibility visibility = body.Get <PropertiesLoader.RDVisibility>("hiddenRnD"); if (visibility == PropertiesLoader.RDVisibility.HIDDEN || visibility == PropertiesLoader.RDVisibility.SKIP) { PSystemBody hidden = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, body.transform.name); PSystemBody parent = bodies.FirstOrDefault(b => b.children.Contains(hidden)); if (parent != null) { // Hide if (hidden.children.Count == 0 || visibility == PropertiesLoader.RDVisibility.HIDDEN) { body.Set("hiddenRnd", PropertiesLoader.RDVisibility.HIDDEN); hideList.Add(new KeyValuePair <PSystemBody, KeyValuePair <PSystemBody, Int32> >(hidden, new KeyValuePair <PSystemBody, Int32>(parent, 0))); } // Skip else { if (skipList.Any(b => b.Key == parent)) { Int32 index = skipList.IndexOf(skipList.FirstOrDefault(b => b.Key == parent)); skipList.Insert(index, new KeyValuePair <PSystemBody, PSystemBody>(hidden, parent)); } else { skipList.Add(new KeyValuePair <PSystemBody, PSystemBody>(hidden, parent)); } } } } } // Skip bodies for (Int32 i = 0; i < skipList.Count; i++) { KeyValuePair <PSystemBody, PSystemBody> pair = skipList[i]; // Get hidden body and parent PSystemBody hidden = pair.Key; PSystemBody parent = pair.Value; // Find where the hidden body is Int32 index = parent.children.IndexOf(hidden); // Remove the hidden body from its parent's children list so it won't show up when clicking the parent parent.children.Remove(hidden); // Put its children in its place parent.children.InsertRange(index, hidden.children); } // Hide bodies for (Int32 i = 0; i < hideList.Count; i++) { KeyValuePair <PSystemBody, KeyValuePair <PSystemBody, Int32> > pair = hideList[i]; // Get hidden body and parent PSystemBody hidden = pair.Key; PSystemBody parent = bodies.FirstOrDefault(b => b.children.Contains(hidden)); // Find where the hidden body is Int32 index = parent.children.IndexOf(hidden); // Remove the hidden body from its parent's children list so it won't show up when clicking the parent parent.children.Remove(hidden); // Save the position in the hideList hideList[i] = new KeyValuePair <PSystemBody, KeyValuePair <PSystemBody, Int32> >(hidden, new KeyValuePair <PSystemBody, Int32>(parent, index)); } // Apply changes and revert to the original PSystem if (hideList.Count > 0 || skipList?.Count > 0) { // Rebuild Archives AddPlanets(); // Undo the changes to the PSystem (hide) for (Int32 i = hideList.Count - 1; i > -1; i--) { PSystemBody hidden = hideList[i].Key; PSystemBody parent = hideList[i].Value.Key; Int32 oldIndex = hideList[i].Value.Value; parent.children.Insert(oldIndex, hidden); } // Undo the changes to the PSystem (skip) for (Int32 i = skipList.Count - 1; i > -1; i--) { PSystemBody hidden = skipList[i].Key; PSystemBody parent = skipList[i].Value; Int32 oldIndex = parent.children.IndexOf(hidden.children.FirstOrDefault()); parent.children.Insert(oldIndex, hidden); for (Int32 j = 0; j < hidden.children.Count; j++) { PSystemBody child = hidden.children[j]; if (parent.children.Contains(child)) { parent.children.Remove(child); } } } } // RDVisibility = NOICON // Kill Rotation // // Loop through the Containers RDPlanetListItemContainer[] containers = Resources.FindObjectsOfTypeAll <RDPlanetListItemContainer>().Where(i => i.label_planetName.text != "Planet name").ToArray(); for (Int32 i = 0; i < containers?.Count(); i++) { RDPlanetListItemContainer planetItem = containers[i]; // The label text is set from the CelestialBody's displayName CelestialBody body = PSystemManager.Instance?.localBodies?.FirstOrDefault(cb => cb.transform.name == planetItem.name); if (body == null) { Debug.Log("[Kopernicus]: RnDFixer: Could not find CelestialBody for the label => " + planetItem.name); continue; } // Barycenter if (body.Has("barycenter") || !body.Get("selectable", true)) { planetItem.planet.SetActive(false); planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineLeft; } // RD Visibility if (body.Has("hiddenRnD")) { PropertiesLoader.RDVisibility visibility = body.Get <PropertiesLoader.RDVisibility>("hiddenRnD"); if (visibility == PropertiesLoader.RDVisibility.NOICON) { planetItem.planet.SetActive(false); planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineLeft; } else { planetItem.planet.SetActive(true); planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineRight; } } // Add planetItems to 'RnDRotationKill' if (planetItem?.planet?.transform?.rotation == null) { continue; } if (body.Has("RnDRotation") ? !body.Get <Boolean>("RnDRotation") : body?.scaledBody?.GetComponentInChildren <SunCoronas>(true) != null) { RnDRotationKill.Add(planetItem); } } }
void Start() { // FIX BODIES MOVED POSTSPAWN // bool postSpawnChanges = false; foreach (CelestialBody cb in PSystemManager.Instance.localBodies.Where(b => b.Has("orbitPatches"))) { // Fix position if the body gets moved PostSpawn ConfigNode patch = cb.Get <ConfigNode>("orbitPatches"); if (patch.GetValue("referenceBody") != null || patch.GetValue("semiMajorAxis") != null) { // Get the body, the old parent and the new parent PSystemBody body = PSystemManager.Instance.systemPrefab.GetComponentsInChildren <PSystemBody>(true).First(b => b.name == name); PSystemBody oldParent = PSystemManager.Instance.systemPrefab.GetComponentsInChildren <PSystemBody>(true).First(b => b.children.Contains(body)); PSystemBody newParent = oldParent; if (patch.GetValue("referenceBody") != null) { newParent = PSystemManager.Instance.systemPrefab.GetComponentsInChildren <PSystemBody>(true).First(b => b.name == patch.GetValue("referenceBody")); } if (body != null && oldParent != null) { // If there is no new SMA it means only the parent changed NumericParser <double> newSMA = body.orbitDriver.orbit.semiMajorAxis; if (patch.GetValue("semiMajorAxis") != null) { newSMA.SetFromString(patch.GetValue("semiMajorAxis")); } // Count how many children comes before our body in the newParent.child list int index = 0; foreach (PSystemBody child in newParent.children) { if (child.orbitDriver.orbit.semiMajorAxis < newSMA.value) { index++; } } // Add the body as child for the new parent and remove it for the old parent if (index > newParent.children.Count) { newParent.children.Add(body); } else { newParent.children.Insert(index, body); } oldParent.children.Remove(body); postSpawnChanges = true; } } } // Rebuild Archives if (postSpawnChanges) { AddPlanets(); } // RDVisibility = SKIP // List <KeyValuePair <PSystemBody, PSystemBody> > skipList = new List <KeyValuePair <PSystemBody, PSystemBody> >(); // Create a list with body to hide and their parent PSystemBody[] bodies = PSystemManager.Instance.systemPrefab.GetComponentsInChildren <PSystemBody>(true); foreach (CelestialBody body in PSystemManager.Instance.localBodies.Where(b => b.Has("hiddenRnD"))) { if (body.Get <PropertiesLoader.RDVisibility>("hiddenRnD") == PropertiesLoader.RDVisibility.SKIP) { PSystemBody hidden = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, name); if (hidden.children.Count == 0) { body.Set("hiddenRnd", PropertiesLoader.RDVisibility.HIDDEN); } else { PSystemBody parent = bodies.First(b => b.children.Contains(hidden)); if (parent != null) { if (skipList.Any(b => b.Key == parent)) { int index = skipList.IndexOf(skipList.First(b => b.Key == parent)); skipList.Insert(index, new KeyValuePair <PSystemBody, PSystemBody>(hidden, parent)); } else { skipList.Add(new KeyValuePair <PSystemBody, PSystemBody>(hidden, parent)); } } } } } foreach (KeyValuePair <PSystemBody, PSystemBody> pair in skipList) { // Get hidden body and parent PSystemBody hidden = pair.Key; PSystemBody parent = pair.Value; // Find where the hidden body is int index = parent.children.IndexOf(hidden); // Put its children in its place parent.children.InsertRange(index, hidden.children); // Remove the hidden body from its parent's children list so it won't show up when clicking the parent parent.children.Remove(hidden); } if (skipList.Count > 0) { // Rebuild Archives AddPlanets(); // Undo the changes to the PSystem for (int i = skipList.Count; i > 0; i = i - 1) { PSystemBody hidden = skipList.ElementAt(i).Key; PSystemBody parent = skipList.ElementAt(i).Value; int oldIndex = parent.children.IndexOf(hidden.children.First()); parent.children.Insert(oldIndex, hidden); foreach (PSystemBody child in hidden.children) { if (parent.children.Contains(child)) { parent.children.Remove(child); } } } } // RDVisibility = HIDDEN // RDVisibility = NOICON // // Loop through the Container foreach (RDPlanetListItemContainer planetItem in Resources.FindObjectsOfTypeAll <RDPlanetListItemContainer>()) { // Barycenter CelestialBody body = PSystemManager.Instance.localBodies.Find(b => b.transform.name == planetItem.label_planetName.text); if (body.Has("barycenter") || body.Has("notSelectable")) { planetItem.planet.SetActive(false); planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineLeft; } // RD Visibility if (body.Has("hiddenRnD")) { PropertiesLoader.RDVisibility visibility = body.Get <PropertiesLoader.RDVisibility>("hiddenRnD"); if (visibility == PropertiesLoader.RDVisibility.NOICON) { planetItem.planet.SetActive(false); planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineLeft; } else if (visibility == PropertiesLoader.RDVisibility.HIDDEN) { planetItem.gameObject.SetActive(false); planetItem.Hide(); planetItem.HideChildren(); } else { planetItem.planet.SetActive(true); planetItem.label_planetName.alignment = TextAlignmentOptions.MidlineRight; } } // namechanges if (FindObjectsOfType <NameChanger>().Count(n => n.oldName == planetItem.label_planetName.text) != 0 && !planetItem.label_planetName.name.EndsWith("NAMECHANGER")) { NameChanger changer = FindObjectsOfType <NameChanger>().First(n => n.oldName == planetItem.label_planetName.text); planetItem.label_planetName.text = changer.newName; planetItem.label_planetName.name += "NAMECHANGER"; } } }