/// <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);
                }
            }
        }
        /// <summary>
        /// Convert a byte array to a ConfigNode.
        /// If anything goes wrong it will return null.
        /// </summary>
        private static ConfigNode ConvertByteArrayToConfigNode(byte[] data, int numBytes)
        {
            ConfigNode node;

            try
            {
                node = ConfigNodeSerializer.Deserialize(data, numBytes);
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing strategy configNode: {e}");
                return(null);
            }

            if (node == null)
            {
                LunaLog.LogError("[LMP]: Error, the strategy configNode was null.");
                return(null);
            }

            if (!node.HasValue("isActive"))
            {
                LunaLog.LogError("[LMP]: Error, the strategy configNode is invalid (isActive missing).");
                return(null);
            }

            return(node);
        }
Esempio n. 3
0
        public ProtoVessel CreateProtoVessel()
        {
            var configNode = ConfigNodeSerializer.Deserialize(RawData, NumBytes);

            if (configNode == null || VesselCommon.VesselHasNaNPosition(configNode))
            {
                LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}");
                VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel");
                VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel");
                return(null);
            }

            var newProto = VesselSerializer.CreateSafeProtoVesselFromConfigNode(configNode, VesselId);

            if (newProto == null)
            {
                LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}");
                VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel");
                VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel");
                return(null);
            }

            if (VesselCommon.ProtoVesselHasInvalidParts(newProto))
            {
                return(null);
            }

            return(newProto);
        }
Esempio n. 4
0
        public void HandleMessage(IServerMessageBase msg)
        {
            if (!(msg.Data is ScenarioBaseMsgData msgData))
            {
                return;
            }

            if (msgData.ScenarioMessageType == ScenarioMessageType.Data)
            {
                var data = (ScenarioDataMsgData)msgData;
                for (var i = 0; i < data.ScenarioCount; i++)
                {
                    var scenarioNode = ConfigNodeSerializer.Deserialize(data.ScenariosData[i].Data, data.ScenariosData[i].NumBytes);
                    if (scenarioNode != null)
                    {
                        var entry = new ScenarioEntry
                        {
                            ScenarioName = data.ScenariosData[i].Module,
                            ScenarioNode = scenarioNode
                        };
                        System.ScenarioQueue.Enqueue(entry);
                    }
                    else
                    {
                        LunaLog.LogError($"[LMP]: Scenario data has been lost for {data.ScenariosData[i].Module}");
                        ScreenMessages.PostScreenMessage($"Scenario data has been lost for {data.ScenariosData[i].Module}", 5f, ScreenMessageStyle.UPPER_CENTER);
                    }
                }
                MainSystem.NetworkState = ClientState.ScenariosSynced;
            }
        }
        /// <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);
                    }
                }
            });
        }
Esempio n. 6
0
        public void HandleMessage(IMessageData messageData)
        {
            var msgData = messageData as ScenarioBaseMsgData;

            if (msgData?.ScenarioMessageType == ScenarioMessageType.Data)
            {
                var data = ((ScenarioDataMsgData)messageData).ScenarioNameData;
                foreach (var scenario in data)
                {
                    var scenarioNode = ConfigNodeSerializer.Deserialize(scenario.Value);
                    if (scenarioNode != null)
                    {
                        var entry = new ScenarioEntry
                        {
                            ScenarioName = scenario.Key,
                            ScenarioNode = scenarioNode
                        };
                        System.ScenarioQueue.Enqueue(entry);
                    }
                    else
                    {
                        LunaLog.LogError($"[LMP]: Scenario data has been lost for {scenario.Key}");
                        ScreenMessages.PostScreenMessage($"Scenario data has been lost for {scenario.Key}", 5f, ScreenMessageStyle.UPPER_CENTER);
                    }
                }
                MainSystem.NetworkState = ClientState.ScneariosSynced;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Convert a byte array to a ConfigNode and then to a ProgressNode.
        /// If anything goes wrong it will return null.
        /// </summary>
        /// <param name="data">The byte array that represents the configNode</param>
        /// <param name="numBytes">The length of the byte array</param>
        /// <param name="progressNodeId">The Id of the ProgressNode</param>
        /// <returns></returns>
        private static ProgressNode ConvertByteArrayToAchievement(byte[] data, int numBytes, string progressNodeId)
        {
            ConfigNode node;

            try
            {
                node = ConfigNodeSerializer.Deserialize(data, numBytes);
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing achievement configNode: {e}");
                return(null);
            }

            if (node == null)
            {
                LunaLog.LogError("[LMP]: Error, the achievement configNode was null.");
                return(null);
            }

            ProgressNode achievement;

            try
            {
                achievement = new ProgressNode(progressNodeId, false);
                achievement.Load(node);
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing achievement: {e}");
                return(null);
            }

            return(achievement);
        }
        /// <summary>
        /// This method uses a lot of memory so try to call it as less as possible and only when needed
        /// </summary>
        public void DeserializeVesselBytes()
        {
            lock (_vesselDataSyncLock)
            {
                _needToDeserializeData = false;
                var newVesselNode = ConfigNodeSerializer.Deserialize(_vesselData, _numBytes);
                if (!VesselCommon.VesselHasNaNPosition(newVesselNode))
                {                
                    //In case there's a deserialization error skip it and keep the older node
                    _vesselNode = newVesselNode;
                }
                if (_vesselNode == null)
                {
                    LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}");
                    VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel");
                    VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel");
                    return;
                }

                var newProto = VesselSerializer.CreateSafeProtoVesselFromConfigNode(_vesselNode, VesselId);

                //In case there's a deserialization error skip it and keep the older proto
                if (newProto != null)
                {
                    HasInvalidParts = VesselCommon.ProtoVesselHasInvalidParts(newProto);

                    if (newProto.vesselID != VesselId)
                    {
                        LunaLog.LogError($"Tried to update the Vessel with a proto from a different vessel ID. Proto: {newProto.vesselID} CorrectId: {VesselId}");
                    }
                    else
                    {
                        _deserializedProtoVessel = newProto;
                    }
                }

                //If protovessel is still null then unfortunately we must remove that vessel as the server sent us a bad vessel
                if (_deserializedProtoVessel == null)
                {
                    LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}");
                    VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel");
                    VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel");
                }
                else
                {
                    _vesselParts.Clear();
                    foreach (var protoPart in _deserializedProtoVessel.protoPartSnapshots)
                    {
                        _vesselParts.TryAdd(protoPart.flightID, protoPart);
                    }
                }
            }
        }
        /// <summary>
        /// Appends the received kerbal to the dictionary
        /// </summary>
        private static void ProcessKerbal(byte[] kerbalData, int numBytes)
        {
            var kerbalNode = ConfigNodeSerializer.Deserialize(kerbalData, numBytes);

            if (kerbalNode != null)
            {
                System.KerbalsToProcess.Enqueue(kerbalNode);
            }
            else
            {
                LunaLog.LogError("[LMP]: Failed to load kerbal!");
            }
        }
        /// <summary>
        /// Just load the received kerbal into game
        /// </summary>
        /// <param name="messageData"></param>
        private static void HandleKerbalProto(KerbalProtoMsgData messageData)
        {
            var kerbalNode = ConfigNodeSerializer.Deserialize(messageData.KerbalData);

            if (kerbalNode != null)
            {
                System.LoadKerbal(kerbalNode);
            }
            else
            {
                LunaLog.LogError("[LMP]: Failed to load kerbal!");
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Deserialize a byte array into a protovessel
        /// </summary>
        public static ProtoVessel DeserializeVessel(byte[] data, int numBytes)
        {
            try
            {
                var vesselNode = ConfigNodeSerializer.Deserialize(data, numBytes);
                var configGuid = vesselNode?.GetValue("pid");

                return(CreateSafeProtoVesselFromConfigNode(vesselNode, new Guid(configGuid)));
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing vessel: {e}");
                return(null);
            }
        }
        /// <summary>
        /// Convert a byte array to a ConfigNode and then to a ScienceSubject.
        /// If anything goes wrong it will return null.
        /// </summary>
        private static ScienceSubject ConvertByteArrayToScienceSubject(byte[] data, int numBytes)
        {
            var node = new ConfigNode("Science");

            try
            {
                node.AddData(ConfigNodeSerializer.Deserialize(data, numBytes));
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing science subject configNode: {e}");
                return(null);
            }

            return(new ScienceSubject(node));
        }
Esempio n. 13
0
        private static void QueueScenarioBytes(string scenarioModule, byte[] scenarioData, int numBytes)
        {
            var scenarioNode = ConfigNodeSerializer.Deserialize(scenarioData, numBytes);

            if (scenarioNode != null)
            {
                var entry = new ScenarioEntry
                {
                    ScenarioModule = scenarioModule,
                    ScenarioNode   = scenarioNode
                };
                System.ScenarioQueue.Enqueue(entry);
            }
            else
            {
                LunaLog.LogError($"[LMP]: Scenario data has been lost for {scenarioModule}");
            }
        }
        /// <summary>
        /// We store all the kerbals in the KerbalProtoQueue dictionary so later once the game starts we load them
        /// </summary>
        /// <param name="messageData"></param>
        private static void HandleKerbalReply(KerbalReplyMsgData messageData)
        {
            foreach (var kerbal in messageData.KerbalsData)
            {
                var kerbalNode = ConfigNodeSerializer.Deserialize(kerbal.Value);
                if (kerbalNode != null)
                {
                    System.KerbalQueue.Enqueue(kerbalNode);
                }
                else
                {
                    LunaLog.LogError("[LMP]: Failed to load kerbal!");
                }
            }

            LunaLog.Log("[LMP]: Kerbals Synced!");
            MainSystem.NetworkState = ClientState.KerbalsSynced;
        }
        /// <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()
            }));
        }
        /// <summary>
        /// Just load the received kerbal into game
        /// </summary>
        /// <param name="messageData"></param>
        private static void HandleKerbalProto(KerbalProtoMsgData messageData)
        {
            var kerbalNode = ConfigNodeSerializer.Deserialize(messageData.KerbalData);

            if (kerbalNode != null)
            {
                if (MainSystem.NetworkState < ClientState.TimeLocked)
                {
                    System.KerbalQueue.Enqueue(kerbalNode);
                }
                else
                {
                    System.LoadKerbal(kerbalNode);
                }
            }
            else
            {
                LunaLog.LogError("[LMP]: Failed to load kerbal!");
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Convert a byte array to a ConfigNode and then to a ScienceSubject.
        /// If anything goes wrong it will return null.
        /// </summary>
        private static ScienceSubject ConvertByteArrayToScienceSubject(byte[] data, int numBytes)
        {
            ConfigNode node;

            try
            {
                node = ConfigNodeSerializer.Deserialize(data, numBytes);
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing science subject configNode: {e}");
                return(null);
            }

            if (node == null)
            {
                LunaLog.LogError("[LMP]: Error, the science subject configNode was null.");
                return(null);
            }

            return(new ScienceSubject(node.GetNode("Science")));
        }
Esempio n. 18
0
        /// <summary>
        /// Convert a byte array to a ConfigNode and then to a Contract.
        /// If anything goes wrong it will return null.
        /// </summary>
        private static Contract ConvertByteArrayToContract(byte[] data, int numBytes)
        {
            ConfigNode node;

            try
            {
                node = ConfigNodeSerializer.Deserialize(data, numBytes);
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing contract configNode: {e}");
                return(null);
            }

            if (node == null)
            {
                LunaLog.LogError("[LMP]: Error, the contract configNode was null.");
                return(null);
            }

            Contract contract;

            try
            {
                var value = node.GetValue("type");
                node.RemoveValues("type");
                var contractType = ContractSystem.GetContractType(value);
                contract = Contract.Load((Contract)Activator.CreateInstance(contractType), node);
            }
            catch (Exception e)
            {
                LunaLog.LogError($"[LMP]: Error while deserializing contract: {e}");
                return(null);
            }

            return(contract);
        }