コード例 #1
0
        private bool ServerClientAiExists(PacketObj data)
        {
            MyEntity exists;
            var      packet = data.Packet;

            uint[] mIds;
            if (PlayerMIds.TryGetValue(packet.SenderId, out mIds) && mIds[(int)packet.PType] < packet.MId)
            {
                mIds[(int)packet.PType] = packet.MId;

                if (packet.PType == PacketType.ClientAiRemove && PlayerEntityIdInRange.ContainsKey(packet.SenderId))
                {
                    PlayerEntityIdInRange[packet.SenderId].Remove(packet.EntityId);
                }
                else if ((packet.PType == PacketType.ClientAiAdd))
                {
                    PlayerEntityIdInRange[packet.SenderId].Add(packet.EntityId);
                }
                else
                {
                    return(Error(data, Msg("SenderId not found")));
                }

                data.Report.PacketValid = true;
            }
            else
            {
                Log.Line($"ServerClientAiExists: MidsHasSenderId:{PlayerMIds.ContainsKey(packet.SenderId)} - entityExists:{MyEntities.TryGetEntityById(packet.EntityId, out exists, true)}({packet.EntityId}) - entityName:{exists?.DebugName} - entityMarked:{exists?.MarkedForClose} - midsNull:{mIds == null} - senderId:{packet.SenderId}");
            }


            return(true);
        }
コード例 #2
0
        internal void ProccessServerPacketsForClients()
        {
            if ((!IsServer || !MpActive))
            {
                Log.Line($"trying to process server packets on a non-server");
                return;
            }

            PacketsToClient.AddRange(PrunedPacketsToClient.Values);
            for (int i = 0; i < PacketsToClient.Count; i++)
            {
                var packetInfo = PacketsToClient[i];
                var bytes      = MyAPIGateway.Utilities.SerializeToBinary(packetInfo.Packet);
                if (packetInfo.SingleClient)
                {
                    MyModAPIHelper.MyMultiplayer.Static.SendMessageTo(ClientPacketId, bytes, packetInfo.Packet.SenderId, true);
                }
                else
                {
                    long entityId = packetInfo.Entity?.GetTopMostParent().EntityId ?? -1;

                    foreach (var p in Players.Values)
                    {
                        var notSender  = p.SteamUserId != packetInfo.Packet.SenderId;
                        var sendPacket = notSender && packetInfo.Entity == null;
                        if (!sendPacket && notSender)
                        {
                            if (PlayerEntityIdInRange.ContainsKey(p.SteamUserId))
                            {
                                if (PlayerEntityIdInRange[p.SteamUserId].Contains(entityId))
                                {
                                    sendPacket = true;
                                }
                                else
                                {
                                    GridAi rootAi;
                                    var    grid = packetInfo.Entity.GetTopMostParent() as MyCubeGrid;
                                    if (grid != null && GridToMasterAi.TryGetValue(grid, out rootAi) && PlayerEntityIdInRange[p.SteamUserId].Contains(rootAi.MyGrid.EntityId))
                                    {
                                        sendPacket = true;
                                    }
                                }
                            }
                        }

                        if (sendPacket)
                        {
                            MyModAPIHelper.MyMultiplayer.Static.SendMessageTo(ClientPacketId, bytes, p.SteamUserId, true);
                        }
                    }
                }
            }

            ServerPacketsForClientsClean();
        }