Beispiel #1
0
        /// <summary>
        /// Reads a number of bits from the buffer and inserts them to a new NetBuffer instance
        /// </summary>
        public static IReadMessage ExtractBits(this IReadMessage originalBuffer, int numberOfBits)
        {
            var buffer = new ReadWriteMessage();

            for (int i = 0; i < numberOfBits; i++)
            {
                bool bit = originalBuffer.ReadBoolean();
                buffer.Write(bit);
            }
            buffer.BitPosition = 0;

            return(buffer);
        }
        public void WriteSpawnData(IWriteMessage msg, UInt16 entityId, bool restrictMessageSize)
        {
            if (GameMain.Server == null)
            {
                return;
            }

            int msgLength = msg.LengthBytes;

            msg.Write(Info == null);
            msg.Write(entityId);
            msg.Write(SpeciesName);
            msg.Write(seed);

            if (Removed)
            {
                msg.Write(0.0f);
                msg.Write(0.0f);
            }
            else
            {
                msg.Write(WorldPosition.X);
                msg.Write(WorldPosition.Y);
            }

            msg.Write(Enabled);

            //character with no characterinfo (e.g. some monster)
            if (Info == null)
            {
                TryWriteStatus(msg);
                return;
            }

            Client ownerClient = GameMain.Server.ConnectedClients.Find(c => c.Character == this);

            if (ownerClient != null)
            {
                msg.Write(true);
                msg.Write(ownerClient.ID);
            }
            else if (GameMain.Server.Character == this)
            {
                msg.Write(true);
                msg.Write((byte)0);
            }
            else
            {
                msg.Write(false);
            }

            msg.Write((byte)TeamID);
            msg.Write(this is AICharacter);
            msg.Write(info.SpeciesName);
            info.ServerWrite(msg);

            msg.Write((byte)CampaignInteractionType);

            // Current order
            if (info.CurrentOrder != null)
            {
                msg.Write(true);
                msg.Write((byte)Order.PrefabList.IndexOf(info.CurrentOrder.Prefab));
                msg.Write(info.CurrentOrder.TargetEntity == null ? (UInt16)0 :
                          info.CurrentOrder.TargetEntity.ID);
                if (info.CurrentOrder.OrderGiver != null)
                {
                    msg.Write(true);
                    msg.Write(info.CurrentOrder.OrderGiver.ID);
                }
                else
                {
                    msg.Write(false);
                }
                msg.Write((byte)(string.IsNullOrWhiteSpace(info.CurrentOrderOption) ? 0 :
                                 Array.IndexOf(info.CurrentOrder.Prefab.Options, info.CurrentOrderOption)));
            }
            else
            {
                msg.Write(false);
            }

            TryWriteStatus(msg);

            void TryWriteStatus(IWriteMessage msg)
            {
                var tempBuffer = new ReadWriteMessage();

                WriteStatus(tempBuffer);
                if (msg.LengthBytes + tempBuffer.LengthBytes >= 255 && restrictMessageSize)
                {
                    msg.Write(false);
                    DebugConsole.ThrowError($"Error when writing character spawn data: status data caused the length of the message to exceed 255 bytes ({msg.LengthBytes} + {tempBuffer.LengthBytes})");
                }
                else
                {
                    msg.Write(true);
                    WriteStatus(msg);
                }
            }

            DebugConsole.Log("Character spawn message length: " + (msg.LengthBytes - msgLength));
        }