@@ -21,20 +21,20 @@ public partial class NetworkManager : IEnumerable<ISatellite>
2121 public event Action < ISatellite , NetworkLink < ISatellite > > OnLinkRemove = delegate { } ;
2222
2323 public Dictionary < Guid , CelestialBody > Planets { get ; private set ; }
24- public ISatellite [ ] GroundStations { get { return RTSettings . Instance . GroundStations ; } }
24+ public Dictionary < Guid , ISatellite > GroundStations { get ; private set ; }
2525 public Dictionary < Guid , List < NetworkLink < ISatellite > > > Graph { get ; private set ; }
2626
27- public int Count { get { return RTCore . Instance . Satellites . Count + GroundStations . Length ; } }
27+ public int Count { get { return RTCore . Instance . Satellites . Count + GroundStations . Count ; } }
2828
2929 public static Guid ActiveVesselGuid = new Guid ( RTSettings . Instance . ActiveVesselGuid ) ;
3030
3131 public ISatellite this [ Guid guid ]
3232 {
3333 get
3434 {
35- if ( guid == ActiveVesselGuid )
36- return RTCore . Instance . Satellites [ FlightGlobals . ActiveVessel ] ;
37- return RTCore . Instance . Satellites [ guid ] ;
35+ return RTCore . Instance . Satellites [ guid ] ??
36+ ( ( guid == ActiveVesselGuid ) ? RTCore . Instance . Satellites [ FlightGlobals . ActiveVessel ] : null ) ??
37+ ( GroundStations . ContainsKey ( guid ) ? GroundStations [ guid ] : null ) ;
3838 }
3939 }
4040
@@ -56,16 +56,27 @@ public List<NetworkRoute<ISatellite>> this[ISatellite sat]
5656 public NetworkManager ( )
5757 {
5858 Graph = new Dictionary < Guid , List < NetworkLink < ISatellite > > > ( ) ;
59- Planets = new Dictionary < Guid , CelestialBody > ( ) ;
6059
60+ // Load all planets into a dictionary;
61+ Planets = new Dictionary < Guid , CelestialBody > ( ) ;
6162 foreach ( CelestialBody cb in FlightGlobals . Bodies )
6263 {
6364 Planets [ cb . Guid ( ) ] = cb ;
6465 }
6566
66- foreach ( var sat in GroundStations )
67+ // Load all ground stations into a dictionary;
68+ GroundStations = new Dictionary < Guid , ISatellite > ( ) ;
69+ foreach ( ISatellite sat in RTSettings . Instance . GroundStations )
6770 {
68- OnSatelliteRegister ( sat ) ;
71+ try
72+ {
73+ GroundStations . Add ( sat . Guid , sat ) ;
74+ OnSatelliteRegister ( sat ) ;
75+ }
76+ catch ( Exception e ) // Already exists.
77+ {
78+ RTLog . Notify ( "A ground station cannot be loaded: " + e . Message ) ;
79+ }
6980 }
7081
7182 RTCore . Instance . Satellites . OnRegister += OnSatelliteRegister ;
@@ -86,7 +97,7 @@ public void Dispose()
8697 public void FindPath ( ISatellite start , IEnumerable < ISatellite > commandStations )
8798 {
8899 var paths = new List < NetworkRoute < ISatellite > > ( ) ;
89- foreach ( ISatellite root in commandStations . Concat ( GroundStations ) . Where ( r => r != start ) )
100+ foreach ( ISatellite root in commandStations . Concat ( GroundStations . Values ) . Where ( r => r != start ) )
90101 {
91102 paths . Add ( NetworkPathfinder . Solve ( start , root , FindNeighbors , RangeModelExtensions . DistanceTo , RangeModelExtensions . DistanceTo ) ) ;
92103 }
@@ -154,7 +165,7 @@ public void OnPhysicsUpdate()
154165 foreach ( VesselSatellite s in RTCore . Instance . Satellites . Concat ( RTCore . Instance . Satellites ) . Skip ( mTickIndex ) . Take ( takeCount ) )
155166 {
156167 UpdateGraph ( s ) ;
157- //RTLog.Debug ("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
168+ //("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
158169 if ( s . SignalProcessor . VesselLoaded || HighLogic . LoadedScene == GameScenes . TRACKSTATION )
159170 {
160171 FindPath ( s , commandStations ) ;
@@ -183,7 +194,7 @@ private void OnSatelliteRegister(ISatellite s)
183194
184195 public IEnumerator < ISatellite > GetEnumerator ( )
185196 {
186- return RTCore . Instance . Satellites . Cast < ISatellite > ( ) . Concat ( GroundStations ) . GetEnumerator ( ) ;
197+ return RTCore . Instance . Satellites . Cast < ISatellite > ( ) . Concat ( GroundStations . Values ) . GetEnumerator ( ) ;
187198 }
188199
189200 IEnumerator IEnumerable . GetEnumerator ( )
@@ -192,23 +203,21 @@ IEnumerator IEnumerable.GetEnumerator()
192203 }
193204 }
194205
195- public sealed class MissionControlSatellite : ISatellite
206+ public sealed class MissionControlSatellite : ISatellite , IPersistenceLoad
196207 {
197- public static Guid Guid = new Guid ( "5105f5a9d62841c6ad4b21154e8fc488" ) ;
198-
199208 /* Config Node parameters */
209+ [ Persistent ] private String Guid = new Guid ( "5105f5a9d62841c6ad4b21154e8fc488" ) . ToString ( ) ;
200210 [ Persistent ] private String Name = "Mission Control" ;
201211 [ Persistent ] private double Latitude = - 0.1313315f ;
202212 [ Persistent ] private double Longitude = - 74.59484f ;
203213 [ Persistent ] private double Height = 75.0f ;
204214 [ Persistent ] private int Body = 1 ;
205- [ Persistent ( collectionIndex = "ANTENNA" ) ]
206- private MissionControlAntenna [ ] Antennas = new MissionControlAntenna [ ] { new MissionControlAntenna ( ) } ;
215+ [ Persistent ( collectionIndex = "ANTENNA" ) ] private MissionControlAntenna [ ] Antennas = new MissionControlAntenna [ ] { new MissionControlAntenna ( ) } ;
207216
208217 bool ISatellite . Powered { get { return true ; } }
209218 bool ISatellite . Visible { get { return true ; } }
210219 String ISatellite . Name { get { return Name ; } set { Name = value ; } }
211- Guid ISatellite . Guid { get { return Guid ; } }
220+ Guid ISatellite . Guid { get { return mGuid ; } }
212221 Vector3d ISatellite . Position { get { return FlightGlobals . Bodies [ Body ] . GetWorldSurfacePosition ( Latitude , Longitude , Height ) ; } }
213222 bool ISatellite . IsCommandStation { get { return true ; } }
214223 bool ISatellite . HasLocalControl { get { return false ; } }
@@ -217,6 +226,17 @@ public sealed class MissionControlSatellite : ISatellite
217226
218227 void ISatellite . OnConnectionRefresh ( List < NetworkRoute < ISatellite > > route ) { }
219228
229+ private Guid mGuid ;
230+
231+ void IPersistenceLoad . PersistenceLoad ( )
232+ {
233+ foreach ( var antenna in Antennas )
234+ {
235+ antenna . Parent = this ;
236+ }
237+ mGuid = new Guid ( Guid ) ;
238+ }
239+
220240 public override String ToString ( )
221241 {
222242 return Name ;
0 commit comments