public override void OnLoad(ConfigNode node) { if (node.GetNodes("InhabitedBody").Count() == 0) { node = DefaultConfiguration(); } foreach (var i in node.GetNodes("InhabitedBody")) { string name = i.GetValue("name"); CelestialBody body = FlightGlobals.Bodies.FirstOrDefault(x => x.name == name); int seed = int.Parse(i.GetValue("seed")); ThreadDispatcher.QueueToWorker(() => { KSPPlanet planet = LoadPlanet(i, body, seed); lock (planets) { planets.Add(planet.Body, planet); } }); } ThreadDispatcher.QueueToWorker(() => ThreadDispatcher.QueueToMainThread(() => PhysicsReady = true)); }
public void Update() { KSPPlanet CurrentPlanet; planets.TryGetValue(FlightGlobals.currentMainBody ?? FlightGlobals.Bodies.FirstOrDefault(x => x.name == "Kerbin"), out CurrentPlanet); if (CurrentPlanet != LastPlanet) { Utils.Log("Planet changed: {0} => {1}", LastPlanet == null ? "(null)" : LastPlanet.Body.name, CurrentPlanet == null ? "(null)" : CurrentPlanet.Body.name); LastPlanet = CurrentPlanet; } if (CurrentPlanet != null) { if (FlightGlobals.ActiveVessel == null) { CurrentPlanet.UpdatePosition(Coordinates.KSC); } else { CurrentPlanet.UpdatePosition(new Coordinates(FlightGlobals.ActiveVessel.latitude * Math.PI / 180, FlightGlobals.ActiveVessel.longitude * Math.PI / 180)); } } ThreadDispatcher.DequeueFromWorker(20); }
KSPPlanet LoadPlanet(ConfigNode node, CelestialBody body, int seed) { // TODO: safe to use ConfigNode in a worker thread? KSPPlanet planet = null; /* * string filename = CacheDirectory + "/" + name + ".planet"; * bool FromCache = true; * * try * { * BinaryFormatter formatter = new BinaryFormatter(); * using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) * { * planet = (KSPPlanet)formatter.Deserialize(stream); * } * } * catch(Exception e) * { * Utils.Log(string.Format("Cannot load {0} from cache: {1}", name, e.Message)); * FromCache = false; * }*/ planet = new KSPPlanet(body, seed); /*if (!FromCache) * { * try * { * using (Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) * { * BinaryFormatter formatter = new BinaryFormatter(); * formatter.Serialize(stream, planet); * Utils.Log(string.Format("Saved {0} to cache", name)); * } * } * catch (Exception e) * { * Utils.Log(string.Format("Cannot save {0} to cache: {1}", name, e.Message)); * } * }*/ planet.Load(node); return(planet); }
KSPPlanet LoadPlanet(ConfigNode node, CelestialBody body, int seed) { // TODO: safe to use ConfigNode in a worker thread? KSPPlanet planet = null; /* string filename = CacheDirectory + "/" + name + ".planet"; bool FromCache = true; try { BinaryFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { planet = (KSPPlanet)formatter.Deserialize(stream); } } catch(Exception e) { Utils.Log(string.Format("Cannot load {0} from cache: {1}", name, e.Message)); FromCache = false; }*/ planet = new KSPPlanet(body, seed); /*if (!FromCache) { try { using (Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, planet); Utils.Log(string.Format("Saved {0} to cache", name)); } } catch (Exception e) { Utils.Log(string.Format("Cannot save {0} to cache: {1}", name, e.Message)); } }*/ planet.Load(node); return planet; }
public void Update() { KSPPlanet CurrentPlanet; planets.TryGetValue(FlightGlobals.currentMainBody ?? FlightGlobals.Bodies.FirstOrDefault(x => x.name == "Kerbin"), out CurrentPlanet); if (CurrentPlanet != LastPlanet) { Utils.Log("Planet changed: {0} => {1}", LastPlanet == null ? "(null)" : LastPlanet.Body.name, CurrentPlanet == null ? "(null)" : CurrentPlanet.Body.name); LastPlanet = CurrentPlanet; } if (CurrentPlanet != null) { if (FlightGlobals.ActiveVessel == null) CurrentPlanet.UpdatePosition(Coordinates.KSC); else CurrentPlanet.UpdatePosition(new Coordinates(FlightGlobals.ActiveVessel.latitude * Math.PI / 180, FlightGlobals.ActiveVessel.longitude * Math.PI / 180)); } ThreadDispatcher.DequeueFromWorker(20); }