Example #1
0
        public static Vessel GetVesselById(Guid id, bool useCache = true)
        {
            // Fast resolve if it's the active vessel, which is probably the most likely case.
            if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == id)
            {
                return(FlightGlobals.ActiveVessel);
            }

            // Check cache.
            if (useCache)
            {
                var item = vesselCache.GetValueOrDefault(id, null);
                if (item != null)
                {
                    if (item.IsAlive)
                    {
                        return((Vessel)item.Target);
                    }
                    vesselCache.Remove(id);
                }
            }

            Vessel vessel = FlightGlobals.Vessels.Find(v => v.id == id);

            if (vessel != null && useCache)
            {
                vesselCache[id] = new WeakReference(vessel);
            }
            return(vessel);
        }