/// <summary>
        /// Unpacks commands and handles arguments
        /// </summary>
        /// <param name="msg">Data chunck recived from the network</param>
        private void HandleIncomingPacket(byte[] msg)
        {
            try
            {
                Command cmd = MyAPIGateway.Utilities.SerializeFromBinary <Command>(msg);

                if (!string.IsNullOrWhiteSpace(cmd.Message) && NetworkType == NetworkTypes.Client && MyAPIGateway.Session != null)
                {
                    MyAPIGateway.Utilities.ShowMessage(ModName, cmd.Message);
                }

                if (cmd != null)
                {
                    OnCommandRecived?.Invoke(cmd.SteamId, cmd.CommandString, cmd.Data);
                }

                if (cmd.CommandString == null)
                {
                    cmd.CommandString = string.Empty;
                }

                string command = cmd.CommandString.Split(' ')[0];

                if (NetworkCommands.ContainsKey(command))
                {
                    NetworkCommands[command]?.Invoke(cmd.SteamId, cmd.CommandString, cmd.Data);
                }
            }
            catch (Exception e)
            {
                MyLog.Default.Error($"[NetworkAPI] Failed to unpack message:\n{e.ToString()}");
            }
        }
Пример #2
0
        /// <summary>
        /// Unpacks commands and handles arguments
        /// </summary>
        /// <param name="msg">Data chunck recived from the network</param>
        private void HandleIncomingPacket(byte[] msg)
        {
            try
            {
                Command cmd = MyAPIGateway.Utilities.SerializeFromBinary <Command>(msg);

                if (LogNetworkTraffic)
                {
                    MyLog.Default.Info($"[NetworkAPI] ----- TRANSMISSION RECIEVED -----");
                    MyLog.Default.Info($"[NetworkAPI] Type: {((cmd.IsProperty) ? "Property" : $"Command ID: {cmd.CommandString}")}, {(cmd.IsCompressed ? "Compressed, " : "")}From: {cmd.SteamId} ");
                }

                if (cmd.IsCompressed)
                {
                    cmd.Data         = MyCompression.Decompress(cmd.Data);
                    cmd.IsCompressed = false;
                }

                if (cmd.IsProperty)
                {
                    NetSync <object> .RouteMessage(MyAPIGateway.Utilities.SerializeFromBinary <SyncData>(cmd.Data), cmd.SteamId, cmd.Timestamp);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(cmd.Message))
                    {
                        if (!MyAPIGateway.Utilities.IsDedicated)
                        {
                            if (MyAPIGateway.Session != null)
                            {
                                MyAPIGateway.Utilities.ShowMessage(ModName, cmd.Message);
                            }
                        }

                        if (MyAPIGateway.Multiplayer.IsServer)
                        {
                            SendCommand(null, cmd.Message);
                        }
                    }

                    if (cmd.CommandString != null)
                    {
                        OnCommandRecived?.Invoke(cmd.SteamId, cmd.CommandString, cmd.Data, new DateTime(cmd.Timestamp));

                        string command = cmd.CommandString.Split(' ')[0];

                        if (NetworkCommands.ContainsKey(command))
                        {
                            NetworkCommands[command]?.Invoke(cmd.SteamId, cmd.CommandString, cmd.Data, new DateTime(cmd.Timestamp));
                        }
                    }
                }

                if (LogNetworkTraffic)
                {
                    MyLog.Default.Info($"[NetworkAPI] ----- END -----");
                }
            }
            catch (Exception e)
            {
                MyLog.Default.Error($"[NetworkAPI] Failure in message processing:\n{e.ToString()}");
            }
        }