public static OrbitGenerator Create(ConfigNode configNode, CelestialBody defaultBody, OrbitGeneratorFactory factory) { OrbitGenerator obGenerator = new OrbitGenerator(); bool valid = true; int index = 0; foreach (ConfigNode child in ConfigNodeUtil.GetChildNodes(configNode)) { DataNode dataNode = new DataNode("ORBIT_" + index++, factory.dataNode, factory); try { ConfigNodeUtil.SetCurrentDataNode(dataNode); OrbitData obData = new OrbitData(child.name); // Get settings that differ by type if (child.name == "FIXED_ORBIT") { valid &= ConfigNodeUtil.ValidateMandatoryChild(child, "ORBIT", factory); obData.orbit = new OrbitSnapshot(ConfigNodeUtil.GetChildNode(child, "ORBIT")).Load(); } else if (child.name == "RANDOM_ORBIT") { valid &= ConfigNodeUtil.ParseValue <OrbitType>(child, "type", x => obData.orbitType = x, factory); valid &= ConfigNodeUtil.ParseValue <int>(child, "count", x => obData.count = x, factory, 1, x => Validation.GE(x, 1)); } else { throw new ArgumentException("Unrecognized orbit node: '" + child.name + "'"); } // Get target body valid &= ConfigNodeUtil.ParseValue <CelestialBody>(child, "targetBody", x => obData.orbit.referenceBody = x, factory, defaultBody, Validation.NotNull); // Add to the list obGenerator.orbits.Add(obData); } finally { ConfigNodeUtil.SetCurrentDataNode(factory.dataNode); } } allOrbitGenerators.Add(obGenerator); return(valid ? obGenerator : null); }
public static SpawnVessel Create(ConfigNode configNode, CelestialBody defaultBody, SpawnVesselFactory factory) { SpawnVessel spawnVessel = new SpawnVessel(); bool valid = true; int index = 0; foreach (ConfigNode child in ConfigNodeUtil.GetChildNodes(configNode, "VESSEL")) { DataNode dataNode = new DataNode("VESSEL_" + index++, factory.dataNode, factory); try { ConfigNodeUtil.SetCurrentDataNode(dataNode); VesselData vessel = new VesselData(); // Get name if (child.HasValue("name")) { vessel.name = child.GetValue("name"); } // Get paths vessel.craftURL = ConfigNodeUtil.ParseValue <string>(child, "craftURL"); vessel.flagURL = ConfigNodeUtil.ParseValue <string>(child, "flagURL", (string)null); vessel.vesselType = ConfigNodeUtil.ParseValue <VesselType>(child, "vesselType", VesselType.Ship); // Get celestial body valid &= ConfigNodeUtil.ParseValue <CelestialBody>(child, "targetBody", x => vessel.body = x, factory, defaultBody, Validation.NotNull); // Get landed stuff if (child.HasValue("lat") && child.HasValue("lon")) { valid &= ConfigNodeUtil.ParseValue <double>(child, "lat", x => vessel.latitude = x, factory); valid &= ConfigNodeUtil.ParseValue <double>(child, "lon", x => vessel.longitude = x, factory); valid &= ConfigNodeUtil.ParseValue <double?>(child, "alt", x => vessel.altitude = x, factory, (double?)null); vessel.landed = true; } // Get orbit else { valid &= ConfigNodeUtil.ValidateMandatoryChild(child, "ORBIT", factory); vessel.orbit = new OrbitSnapshot(ConfigNodeUtil.GetChildNode(child, "ORBIT")).Load(); vessel.orbit.referenceBody = vessel.body; } // Get additional flags valid &= ConfigNodeUtil.ParseValue <bool>(child, "owned", x => vessel.owned = x, factory, false); // Handle the CREW nodes foreach (ConfigNode crewNode in ConfigNodeUtil.GetChildNodes(child, "CREW")) { int count = 1; valid &= ConfigNodeUtil.ParseValue <int>(crewNode, "count", x => count = x, factory, 1); for (int i = 0; i < count; i++) { CrewData cd = new CrewData(); // Read crew details valid &= ConfigNodeUtil.ParseValue <string>(crewNode, "name", x => cd.name = x, factory, (string)null); valid &= ConfigNodeUtil.ParseValue <bool>(crewNode, "addToRoster", x => cd.addToRoster = x, factory, true); // Add the record vessel.crew.Add(cd); } } // Add to the list spawnVessel.vessels.Add(vessel); } finally { ConfigNodeUtil.SetCurrentDataNode(factory.dataNode); } } return(valid ? spawnVessel : null); }
public static SpawnKerbal Create(ConfigNode configNode, CelestialBody defaultBody, SpawnKerbalFactory factory) { SpawnKerbal spawnKerbal = new SpawnKerbal(); bool valid = true; int index = 0; foreach (ConfigNode child in ConfigNodeUtil.GetChildNodes(configNode, "KERBAL")) { DataNode dataNode = new DataNode("KERBAL_" + index++, factory.dataNode, factory); try { ConfigNodeUtil.SetCurrentDataNode(dataNode); KerbalData kerbal = new KerbalData(); // Get name if (child.HasValue("name")) { kerbal.name = child.GetValue("name"); } // Get celestial body valid &= ConfigNodeUtil.ParseValue <CelestialBody>(child, "targetBody", x => kerbal.body = x, factory, defaultBody, Validation.NotNull); // Get landed stuff if (child.HasValue("lat") && child.HasValue("lon") || child.HasValue("pqsCity")) { kerbal.landed = true; if (child.HasValue("pqsCity")) { string pqsCityStr = null; valid &= ConfigNodeUtil.ParseValue <string>(child, "pqsCity", x => pqsCityStr = x, factory); if (pqsCityStr != null) { try { kerbal.pqsCity = kerbal.body.GetComponentsInChildren <PQSCity>(true).Where(pqs => pqs.name == pqsCityStr).First(); } catch (Exception e) { LoggingUtil.LogError(typeof(WaypointGenerator), "Couldn't load PQSCity with name '" + pqsCityStr + "'"); LoggingUtil.LogException(e); valid = false; } } valid &= ConfigNodeUtil.ParseValue <Vector3d>(child, "pqsOffset", x => kerbal.pqsOffset = x, factory, new Vector3d()); } else { valid &= ConfigNodeUtil.ParseValue <double>(child, "lat", x => kerbal.latitude = x, factory); valid &= ConfigNodeUtil.ParseValue <double>(child, "lon", x => kerbal.longitude = x, factory); } } // Get orbit else if (child.HasNode("ORBIT")) { kerbal.orbit = new OrbitSnapshot(ConfigNodeUtil.GetChildNode(child, "ORBIT")).Load(); kerbal.orbit.referenceBody = kerbal.body; } else { // Will error valid &= ConfigNodeUtil.ValidateMandatoryChild(child, "ORBIT", factory); } valid &= ConfigNodeUtil.ParseValue <double?>(child, "alt", x => kerbal.altitude = x, factory, (double?)null); // Get additional flags valid &= ConfigNodeUtil.ParseValue <bool>(child, "owned", x => kerbal.owned = x, factory, false); valid &= ConfigNodeUtil.ParseValue <bool>(child, "addToRoster", x => kerbal.addToRoster = x, factory, true); // Add to the list spawnKerbal.kerbals.Add(kerbal); } finally { ConfigNodeUtil.SetCurrentDataNode(factory.dataNode); } } return(valid ? spawnKerbal : null); }