Ejemplo n.º 1
0
        /// <summary>
        /// Load a vessel into the game
        /// </summary>
        public IEnumerator LoadVessel(VesselProtoUpdate vesselProto)
        {
            var currentProto = CreateSafeProtoVesselFromConfigNode(vesselProto.VesselNode, vesselProto.VesselId);

            if (ProtoVesselValidationsPassed(currentProto))
            {
                RegisterServerAsteriodIfVesselIsAsteroid(currentProto);

                //VesselRemoveSystem.Singleton.KillVessel(vessel, false, true);

                var vessel = FlightGlobals.Vessels.FirstOrDefault(v => v.id == currentProto.vesselID);
                if (vessel != null)
                {
                    vessel.protoVessel = currentProto;
                    vesselProto.Loaded = true;
                    yield break;
                }

                FixProtoVesselFlags(currentProto);
                var result = LoadVesselIntoGame(currentProto);

                yield return(null); //Resume on next frame

                if (result)
                {
                    FinishVesselLoading(currentProto);
                    vesselProto.Loaded = true;
                }
            }
        }
        /// <summary>
        /// In this method we get the new vessel data and set it to the dictionary of all the player vessels.
        /// We set it as UNLOADED as perhaps vessel data has changed.
        /// </summary>
        private static void HandleVesselProtoData(byte[] vesselData, Guid vesselId)
        {
            UniverseSyncCache.QueueToCache(vesselData);
            var vesselNode = ConfigNodeSerializer.Deserialize(vesselData);
            var configGuid = vesselNode?.GetValue("pid");

            if (!string.IsNullOrEmpty(configGuid) && vesselId == Common.ConvertConfigStringToGuid(configGuid))
            {
                var vesselProtoUpdate = new VesselProtoUpdate(vesselNode, vesselId);
                if (vesselProtoUpdate.ProtoVessel == null)
                {
                    return;
                }

                if (System.AllPlayerVessels.ContainsKey(vesselId))
                {
                    //Vessel exists so replace it
                    System.AllPlayerVessels[vesselId] = vesselProtoUpdate;
                }
                else
                {
                    System.AllPlayerVessels.TryAdd(vesselId, vesselProtoUpdate);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// In this method we get the new vessel data and set it to the dictionary of all the player vessels.
        /// We set it as UNLOADED as perhaps vessel data has changed.
        /// </summary>
        public void HandleVesselProtoData(byte[] vesselData, Guid vesselId)
        {
            TaskFactory.StartNew(() =>
            {
                UniverseSyncCache.QueueToCache(vesselData);
                var vesselNode = ConfigNodeSerializer.Deserialize(vesselData);
                if (vesselNode != null && vesselId == Common.ConvertConfigStringToGuid(vesselNode.GetValue("pid")))
                {
                    var vesselProtoUpdate = new VesselProtoUpdate(vesselNode, vesselId);
                    if (vesselProtoUpdate.ProtoVessel == null)
                    {
                        return;
                    }

                    if (!AllPlayerVessels.TryGetValue(vesselId, out var existingProtoData))
                    {
                        AllPlayerVessels.TryAdd(vesselId, vesselProtoUpdate);
                    }
                    else if (VesselCommon.ProtoVesselHasChanges(existingProtoData.ProtoVessel, vesselProtoUpdate.ProtoVessel))
                    {
                        //Vessel exists and contain changes so replace it
                        AllPlayerVessels.TryUpdate(vesselId, vesselProtoUpdate, existingProtoData);
                    }
                }
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reloads an existing vessel into the game
        /// Bear in mind that this method works in just 1 frame and
        /// it won't reload the vessel unless the parts have changed.
        /// </summary>
        public void ReloadVessel(VesselProtoUpdate vesselProto)
        {
            var vessel = FlightGlobals.Vessels.FirstOrDefault(v => v.id == vesselProto.VesselId);

            if (vessel != null)
            {
                var currentProto = CreateSafeProtoVesselFromConfigNode(vesselProto.VesselNode, vesselProto.VesselId);
                if (currentProto.protoPartSnapshots.Count != vessel.Parts.Count)
                {
                    currentProto.latitude  = vessel.latitude;
                    currentProto.longitude = vessel.longitude;
                    currentProto.altitude  = vessel.altitude;
                    currentProto.rotation  = vessel.vesselTransform.rotation;
                    currentProto.position  = vessel.vesselTransform.position;

                    VesselRemoveSystem.Singleton.UnloadVessel(vessel);

                    FixProtoVesselFlags(currentProto);
                    var result = LoadVesselIntoGame(currentProto);

                    if (result)
                    {
                        FinishVesselLoading(currentProto);
                    }
                }
            }

            vesselProto.Loaded = true;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Updates the vesselProto from the dictionary in a thread safe manner
 /// </summary>
 private void UpdateVesselProtoInDictionary(VesselProtoUpdate vesselProto)
 {
     AllPlayerVessels.TryGetValue(vesselProto.VesselId, out var existingVesselProto);
     if (existingVesselProto != null)
     {
         AllPlayerVessels.TryUpdate(vesselProto.VesselId, vesselProto, existingVesselProto);
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Load a vessel into the game
 /// </summary>
 ///
 public void LoadVessel(VesselProtoUpdate vesselProto)
 {
     try
     {
         LoadVesselImpl(vesselProto);
     }
     catch (Exception e)
     {
         LunaLog.LogError($"[LMP]: Error loading vessel: {e}");
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Performs the operation of actually loading the vessel into the game.  Does not handle errors.
        /// </summary>
        /// <param name="vesselProto"></param>
        private static void LoadVesselImpl(VesselProtoUpdate vesselProto)
        {
            if (ProtoVesselValidationsPassed(vesselProto.ProtoVessel))
            {
                RegisterServerAsteriodIfVesselIsAsteroid(vesselProto.ProtoVessel);

                if (FlightGlobals.FindVessel(vesselProto.VesselId) == null)
                {
                    FixProtoVesselFlags(vesselProto.ProtoVessel);
                    vesselProto.Loaded = LoadVesselIntoGame(vesselProto.ProtoVessel);
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Load a vessel into the game
        /// </summary>
        public void LoadVessel(VesselProtoUpdate vesselProto)
        {
            var currentProto = CreateSafeProtoVesselFromConfigNode(vesselProto.VesselNode, vesselProto.VesselId);

            if (ProtoVesselValidationsPassed(currentProto))
            {
                RegisterServerAsteriodIfVesselIsAsteroid(currentProto);

                if (FlightGlobals.FindVessel(vesselProto.VesselId) == null)
                {
                    FixProtoVesselFlags(currentProto);
                    vesselProto.Loaded = LoadVesselIntoGame(currentProto);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Reloads an existing vessel into the game
        /// Bear in mind that this method won't reload the vessel unless the part count has changed.
        /// </summary>
        public void ReloadVessel(VesselProtoUpdate vesselProto)
        {
            var vessel = FlightGlobals.Vessels.FirstOrDefault(v => v.id == vesselProto.VesselId);

            if (vessel != null)
            {
                var currentProto = CreateSafeProtoVesselFromConfigNode(vesselProto.VesselNode, vesselProto.VesselId);
                if (currentProto.protoPartSnapshots.Count != vessel.BackupVessel().protoPartSnapshots.Count)
                {
                    VesselRemoveSystem.Singleton.UnloadVessel(vessel);
                    LoadVessel(vesselProto);
                }
            }

            vesselProto.Loaded = true;
        }
        /// <summary>
        /// Here we receive the vessel list msg from the server.We rty to get the vessels from the cache and if
        /// it fails or we don't have it in the cache we request that vessel info to the server.
        /// </summary>
        private static void HandleVesselList(VesselListReplyMsgData messageData)
        {
            var serverVessels    = new List <string>(messageData.Vessels);
            var cacheObjects     = new List <string>(UniverseSyncCache.GetCachedObjects());
            var requestedObjects = new List <string>();

            foreach (var serverVessel in serverVessels)
            {
                if (cacheObjects.Contains(serverVessel))
                {
                    //Try to get it from cache...
                    var vesselBytes = UniverseSyncCache.GetFromCache(serverVessel);
                    var vesselNode  = ConfigNodeSerializer.Deserialize(vesselBytes);
                    if (vesselNode != null)
                    {
                        var vesselId = Common.ConvertConfigStringToGuid(vesselNode.GetValue("pid"));
                        if (vesselBytes.Length != 0 && vesselId != Guid.Empty)
                        {
                            var update = new VesselProtoUpdate(vesselNode, vesselId);
                            if (update.ProtoVessel != null)
                            {
                                System.AllPlayerVessels.TryAdd(vesselId, update);
                            }
                        }
                        else
                        {
                            LunaLog.LogError($"[LMP]: Cached object {serverVessel} is damaged");
                            requestedObjects.Add(serverVessel);
                        }
                    }
                }
                else
                {
                    requestedObjects.Add(serverVessel);
                }
            }

            //Request the vessel data that we don't have.
            NetworkSender.QueueOutgoingMessage(MessageFactory.CreateNew <VesselCliMsg>
                                                   (new VesselsRequestMsgData {
                RequestList = requestedObjects.ToArray()
            }));
        }
        private static void HandleVesselProtoData(byte[] vesselData, string vesselId, int subspace)
        {
            UniverseSyncCache.Singleton.QueueToCache(vesselData);
            var vesselNode = ConfigNodeSerializer.Singleton.Deserialize(vesselData);

            if (vesselNode != null)
            {
                var configGuid = vesselNode.GetValue("pid");
                if (!string.IsNullOrEmpty(configGuid) && vesselId == Common.ConvertConfigStringToGuidString(configGuid))
                {
                    var vesselProtoUpdate = new VesselProtoUpdate
                    {
                        VesselId   = new Guid(vesselId),
                        VesselNode = vesselNode,
                        Loaded     = false
                    };

                    var oldVessel = System.AllPlayerVessels.FirstOrDefault(v => v.VesselId.ToString() == vesselId);
                    if (oldVessel != null)
                    {
                        //Vessel exists so replace it
                        var index = System.AllPlayerVessels.IndexOf(oldVessel);
                        System.AllPlayerVessels[index] = vesselProtoUpdate;
                    }
                    else
                    {
                        System.AllPlayerVessels.Add(vesselProtoUpdate);
                    }
                }
                else
                {
                    Debug.LogError($"[LMP]: Failed to load vessel {vesselId}!");
                }
            }
            else
            {
                Debug.LogError($"[LMP]: Failed to load vessel {vesselId}!");
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Load a vessel into the game in 2 frames
        /// </summary>
        public IEnumerator LoadVessel(VesselProtoUpdate vesselProto)
        {
            var currentProto = CreateSafeProtoVesselFromConfigNode(vesselProto.VesselNode, vesselProto.VesselId);

            if (ProtoVesselValidationsPassed(currentProto))
            {
                RegisterServerAsteriodIfVesselIsAsteroid(currentProto);

                if (FlightGlobals.FindVessel(vesselProto.VesselId) == null)
                {
                    FixProtoVesselFlags(currentProto);
                    var result = LoadVesselIntoGame(currentProto);

                    yield return(null); //Resume on next frame

                    if (result)
                    {
                        FinishVesselLoading(currentProto);
                        vesselProto.Loaded = true;
                    }
                }
            }
        }
Ejemplo n.º 13
0
 public VesselProtoUpdate(VesselProtoUpdate protoUpdate)
 {
     VesselId    = protoUpdate.VesselId;
     ProtoVessel = protoUpdate.ProtoVessel;
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Reloads an existing vessel into the game
        /// Bear in mind that this method won't reload the vessel unless the part count has changed.
        /// </summary>
        public bool ReloadVesselIfChanged(VesselProtoUpdate vesselProto)
        {
            try
            {
                var vessel = FlightGlobals.Vessels.FirstOrDefault(v => v.id == vesselProto.VesselId);
                if (vessel != null)
                {
                    //Load the existing target, if any.  We will use this to reset the target to the newly loaded vessel, if the vessel we're reloading is the one that is targeted.
                    var currentTarget = FlightGlobals.fetch.VesselTarget;

                    var vesselLoaded = false;
                    //TODO: Is BackupVessel() needed or can we just look at the protoVessel?
                    if (vesselProto.ProtoVessel.protoPartSnapshots.Count != vessel.BackupVessel().protoPartSnapshots.Count)
                    {
                        //If targeted, unloading the vessel will cause the target to be lost.  We'll have to reset it later.
                        SystemsContainer.Get <VesselRemoveSystem>().UnloadVessel(vessel);
                        LoadVesselImpl(vesselProto);
                        vesselLoaded = true;
                    }

                    //TODO: Handle when it's the active vessel for the FlightGlobals as well.  If you delete the active vessel, it ends badly.  Very badly.

                    //We do want to actually compare by reference--we want to see if the vessel object we're unloading and reloading is the one that's targeted.  If so, we need to
                    //reset the target to the new instance of the vessel
                    if (vesselLoaded && currentTarget?.GetVessel() == vessel)
                    {
                        //Fetch the new vessel information for the same vessel ID, as the unload/load creates a new game object, and we need to refer to the new one for the target
                        var newVessel = FlightGlobals.Vessels.FirstOrDefault(v => v.id == vesselProto.VesselId);

                        //Record the time immediately before calling SetVesselTarget
                        var currentTime = Time.realtimeSinceStartup;
                        FlightGlobals.fetch.SetVesselTarget(newVessel, true);

                        var messagesToRemove = new List <ScreenMessage>();
                        //Remove the "Target:" message created by SetVesselTarget
                        foreach (var message in ScreenMessages.Instance.ActiveMessages)
                        {
                            //If the message started on or after the SetVesselTarget call time, remove it, as it's the target message created by SetVesselTarget
                            if (message.startTime >= currentTime)
                            {
                                messagesToRemove.Add(message);
                            }
                        }

                        foreach (var message in messagesToRemove)
                        {
                            ScreenMessages.RemoveMessage(message);
                        }
                    }
                    return(vesselLoaded);
                }
                else
                {
                    LoadVesselImpl(vesselProto);
                    return(true);
                }
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error reloading vessel: {e}");
                return(false);
            }
        }