Example #1
0
        public static SpawnedEntity FromStream(IStarboundStream stream)
        {
            SpawnedEntity se;
            EntityType    et = (EntityType)stream.ReadUInt8();

            byte[] storeData = stream.ReadUInt8Array((int)(stream.Length - stream.Position));


            using (StarboundStream ss = new StarboundStream(storeData))
            {
                if (et == EntityType.Projectile)
                {
                    var sp = new SpawnedProjectile();
                    sp.ProjectileKey = ss.ReadString();
                    sp.Parameters    = ss.ReadVariant();

                    se = sp;
                }
                else
                {
                    se = new SpawnedEntity();
                }
            }

            se.EntityType = et;
            se.StoreData  = storeData;

            return(se);
        }
Example #2
0
        public static WorldCoordinate GetGlobalCoords(byte[] data)
        {
            for (int i = 0; i < data.Length - 26; i++)
            {
                foreach (var sector in StarboundSector.Sectors.Select(p => p.Data))
                {
                    byte[] buffer = new byte[sector.Length];

                    Buffer.BlockCopy(data, i, buffer, 0, sector.Length);

                    if (sector.SequenceEqual(buffer))
                    {
                        byte[] sectorData = new byte[sector.Length + 21];

                        Buffer.BlockCopy(data, i - 1, sectorData, 0, sector.Length + 21);

                        using (StarboundStream s = new StarboundStream(sectorData))
                        {
                            WorldCoordinate coords = FromStream(s);

                            /*
                             * if (string.IsNullOrEmpty(coords.Sector))
                             *  return null;
                             */

                            return(coords);
                        }
                    }
                }
            }

            return(null);
        }
Example #3
0
        public static CelestialLog FromStream(IStarboundStream stream)
        {
            CelestialLog log = new CelestialLog();

            byte[] logDat = stream.ReadUInt8Array();

            using (StarboundStream s = new StarboundStream(logDat))
            {
                uint visited = s.ReadUInt32();

                for (int i = 0; i < visited; i++)
                {
                    log.Visited.Add(s.ReadSystemCoordinate());
                }

                uint sectors = s.ReadUInt32();

                for (int i = 0; i < sectors; i++)
                {
                    log.Sectors.Add(new LogSector {
                        SectorName = s.ReadString(), Unknown = s.ReadBoolean()
                    });
                }

                s.ReadUInt8(); //unknown

                log.CurrentSystem   = s.ReadSystemCoordinate();
                log.CurrentLocation = s.ReadWorldCoordinate();
                log.HomeCoordinate  = s.ReadWorldCoordinate();
            }

            return(log);
        }
        public void Read(StarboundStream stream)
        {
            int discarded;

            Success         = stream.ReadBoolean();
            ClientId        = stream.ReadVLQ(out discarded);
            RejectionReason = stream.ReadString();
        }
 public void Write(StarboundStream stream)
 {
     stream.WriteString(AssetDigest);
     stream.WriteVariant(Claim);
     stream.WriteBoolean(UUID != null);
     if (UUID != null)
     {
         stream.WriteUInt8Array(UUID, false);
     }
     stream.WriteString(PlayerName);
     stream.WriteString(Species);
     stream.WriteUInt8Array(Shipworld);
     stream.WriteString(Account);
 }
        public void Read(StarboundStream stream)
        {
            AssetDigest = stream.ReadString();
            Claim       = stream.ReadVariant();
            bool uuid = stream.ReadBoolean();

            if (uuid)
            {
                UUID = stream.ReadUInt8Array(16);
            }
            PlayerName = stream.ReadString();
            Species    = stream.ReadString();
            Shipworld  = stream.ReadUInt8Array();
            Account    = stream.ReadString();
        }
Example #7
0
        public override void WriteTo(IStarboundStream stream)
        {
            stream.WriteUInt8((byte)EntityType);

            using (StarboundStream s = new StarboundStream())
            {
                s.WriteString(Object);
                s.WriteVariant(Information);
                s.WriteUInt8Array(Unknown, false);

                stream.WriteUInt8Array(s.ToArray());
            }

            stream.WriteSignedVLQ(EntityId);
        }
Example #8
0
        public static List <Document> ListFromStream(byte[] data)
        {
            int discarded;

            var documents = new List <Document>();

            using (StarboundStream ss = new StarboundStream(data))
            {
                int len = (int)VLQ.ReadVLQ(ss, out discarded);

                for (int i = 0; i < len; i++)
                {
                    documents.Add(FromStream(ss));
                }
            }

            return(documents);
        }
Example #9
0
 public void FlushPackets()
 {
     while (PacketQueue.Count > 0)
     {
         IStarboundPacket next;
         while (!PacketQueue.TryDequeue(out next))
         {
             ;
         }
         var memoryStream = new MemoryStream();
         var stream       = new StarboundStream(memoryStream);
         next.Write(stream);
         byte[] buffer = new byte[stream.Position];
         Array.Copy(memoryStream.GetBuffer(), buffer, buffer.Length);
         int length     = buffer.Length;
         var compressed = ZlibStream.CompressBuffer(buffer);
         if (compressed.Length < buffer.Length)
         {
             buffer = compressed;
             length = -buffer.Length;
         }
         byte[] header = new byte[StarboundStream.GetSignedVLQLength(length) + 1];
         header[0] = next.PacketId;
         int discarded;
         StarboundStream.WriteSignedVLQ(header, 1, length, out discarded);
         int payloadStart = header.Length;
         Array.Resize(ref header, header.Length + buffer.Length);
         Array.Copy(buffer, 0, header, payloadStart, buffer.Length);
         lock (PacketsWaitingLock)
             PacketsWaiting++;
         EmptyQueueReset.Reset();
         Socket.BeginSend(header, 0, header.Length, SocketFlags.None, r =>
         {
             lock (PacketsWaitingLock)
             {
                 PacketsWaiting--;
                 if (PacketsWaiting == 0)
                 {
                     EmptyQueueReset.Set();
                 }
             }
         }, null);
     }
 }
Example #10
0
        public IPacket Decode(byte packetId, byte[] payload)
        {
            StarboundStream stream = new StarboundStream(payload);

            IPacket packet;

            /*
             * string val = null;
             * foreach (var s in payload)
             * {
             *  val += s;
             * }
             * SharpStarLogger.DefaultLogger.Info(string.Format("{0}{1}","Payload : ",val));
             */

            if (RegisteredPacketTypes.ContainsKey(packetId))
            {
                packet = RegisteredPacketTypes[packetId]();
            }
            else
            {
                packet = new UnknownPacket(Compressed, payload.Length, packetId);
            }

            if (packet != null)
            {
                packet.IsReceive = true;

                try
                {
                    packet.Read(stream);
                }
                catch (Exception e)
                {
                    SharpStarLogger.DefaultLogger.Error("Packet {0} caused an error!", packet.GetType().Name);

                    e.LogError();
                }
            }

            stream.Dispose();

            return(packet);
        }
Example #11
0
        public IStarboundPacket[] Decode(byte packetId, byte[] payload)
        {
            var memoryStream = new MemoryStream(payload);
            var stream       = new StarboundStream(memoryStream);
            List <IStarboundPacket> packets = new List <IStarboundPacket>();

            while (stream.Position < stream.Length)
            {
                IStarboundPacket packet;
                if (PacketFactories.ContainsKey(packetId))
                {
                    packet = PacketFactories[packetId]();
                }
                else
                {
                    packet = new UnhandledPacket(payload, packetId);
                }
                packet.Read(stream);
                packets.Add(packet);
            }
            return(packets.ToArray());
        }
Example #12
0
        public Metadata GetMetadata()
        {
            if (Metadata != null)
            {
                return(Metadata);
            }

            using (StarboundStream ss = new StarboundStream(GetRaw(new byte[] { 0, 0, 0 })))
            {
                var unpacked = DataConverter.Unpack("^ii", ss.ReadUInt8Array(8), 0); //unknown

                Document doc = Document.FromStream(ss);

                if (doc.Name != "WorldMetadata")
                {
                    throw new Exception("Invalid world data!");
                }

                Metadata = new Metadata(doc.Data, doc.Version);

                return(Metadata);
            }
        }
Example #13
0
        public IStarboundPacket[] UpdateBuffer(int length)
        {
            if (length == 0)
            {
                throw new IOException("Client sent no data, connection is likely terminated.");
            }
            int index = PacketBuffer.Length;

            if (WorkingLength == long.MaxValue)
            {
                // We don't know the length of the packet yet, so keep going
                if (PacketBuffer.Length < index + length)
                {
                    Array.Resize(ref PacketBuffer, index + length);
                }
                Array.Copy(NetworkBuffer, 0, PacketBuffer, index, length);
                if (PacketBuffer.Length > 1)
                {
                    // Check to see if we have the entire length yet
                    int i;
                    for (i = 1; i < 5 && i < PacketBuffer.Length; i++)
                    {
                        if ((PacketBuffer[i] & 0x80) == 0)
                        {
                            WorkingLength = StarboundStream.ReadSignedVLQ(PacketBuffer, 1, out DataIndex);
                            DataIndex++;
                            Compressed = WorkingLength < 0;
                            if (Compressed)
                            {
                                WorkingLength = -WorkingLength;
                            }
                            if (WorkingLength > MaxPacketLength)
                            {
                                throw new IOException("Packet exceeded maximum permissible length.");
                            }
                            break;
                        }
                    }
                    if (i == 5)
                    {
                        throw new IOException("Packet exceeded maximum permissible length.");
                    }
                }
            }
            if (WorkingLength != long.MaxValue) // This could be an else, but the earlier block can change the state and this needs to happen if it does
            {
                if (PacketBuffer.Length < index + length)
                {
                    Array.Resize(ref PacketBuffer, index + length);
                }
                Array.Copy(NetworkBuffer, 0, PacketBuffer, index, length);
                if (PacketBuffer.Length >= WorkingLength + DataIndex)
                {
                    // Ready to decode packet
                    var data = new byte[WorkingLength];
                    Array.Copy(PacketBuffer, DataIndex, data, 0, WorkingLength);
                    if (Compressed)
                    {
                        data = ZlibStream.UncompressBuffer(data); // TODO: Prevent compressed packets from exceeding MaxInflatedPacketLength
                    }
                    var packets = Decode(PacketBuffer[0], data);
                    Array.Copy(PacketBuffer, DataIndex + WorkingLength, PacketBuffer, 0, PacketBuffer.Length - (DataIndex + WorkingLength));
                    Array.Resize(ref PacketBuffer, (int)(PacketBuffer.Length - (DataIndex + WorkingLength)));
                    WorkingLength = long.MaxValue;
                    return(packets);
                }
            }
            return(null);
        }
 public void Read(StarboundStream stream)
 {
     ClaimMessage = stream.ReadString();
     PasswordHash = stream.ReadString();
 }
 public void Write(StarboundStream stream)
 {
     stream.WriteBoolean(Success);
     stream.WriteVLQ(ClientId);
     stream.WriteString(RejectionReason);
 }
 public void Read(StarboundStream stream)
 {
     ProtocolVersion = stream.ReadUInt32();
 }
Example #17
0
 public void Write(StarboundStream stream)
 {
     stream.Write(Data, 0, Data.Length);
 }
Example #18
0
 public void Read(StarboundStream stream)
 {
     stream.Read(Data, 0, Data.Length);
 }
Example #19
0
        public static Entity FromStream(IStarboundStream stream)
        {
            EntityType et = (EntityType)stream.ReadUInt8();

            byte[] storeData = stream.ReadUInt8Array();

            Entity ent;

            if (et == EntityType.Projectile)
            {
                ProjectileEntity pent = new ProjectileEntity();

                using (StarboundStream s = new StarboundStream(storeData))
                {
                    pent.Projectile      = s.ReadString();
                    pent.Information     = s.ReadVariant().Value as VariantDict;
                    pent.Unknown1        = s.ReadUInt8Array(17);
                    pent.ThrowerEntityId = s.ReadSignedVLQ();
                    pent.Unknown2        = s.ReadUInt8Array((int)(s.Length - s.Position));
                }

                ent = pent;
            }
            else if (et == EntityType.Player)
            {
                PlayerEntity pent = new PlayerEntity();

                using (StarboundStream s = new StarboundStream(storeData))
                {
                    /*
                     * bool uuid = s.ReadBoolean();
                     *
                     * if (uuid)
                     * {
                     */
                    byte[] uuidDat = s.ReadUInt8Array(16);

                    pent.UUID = BitConverter.ToString(uuidDat, 0).Replace("-", "").ToLower();

                    //}
                }

                ent = pent;
            }
            else if (et == EntityType.Object)
            {
                ObjectEntity oent = new ObjectEntity();

                using (StarboundStream s = new StarboundStream(storeData))
                {
                    oent.Object      = s.ReadString();
                    oent.Information = s.ReadVariant();
                    oent.Unknown     = s.ReadUInt8Array((int)(s.Length - s.Position));
                }

                ent = oent;
            }
            else
            {
                ent = new Entity();
            }

            ent.EntityType = et;
            ent.StoreData  = storeData;
            ent.EntityId   = stream.ReadSignedVLQ();

            return(ent);
        }
Example #20
0
 public void Read(StarboundStream stream)
 {
     int discarded;
     Value = stream.ReadVLQ(out discarded);
 }
Example #21
0
        public IEnumerable <IPacket> UpdateBuffer(bool shouldCopy)
        {
            if (shouldCopy)
            {
                PacketBuffer.AddRange(NetworkBuffer);
            }

            Queue <byte[]> toProcess = new Queue <byte[]>();

            toProcess.Enqueue(PacketBuffer.ToArray());

            while (toProcess.Count > 0)
            {
                byte[] arr = toProcess.Dequeue();

                using (StarboundStream s = new StarboundStream(arr))
                {
                    if (WorkingLength == long.MaxValue && s.Length > 1)
                    {
                        _packetId = s.ReadUInt8();

                        try
                        {
                            WorkingLength = s.ReadSignedVLQ();
                        }
                        catch
                        {
                            WorkingLength = long.MaxValue;

                            yield break;
                        }

                        DataIndex = (int)s.Position;

                        Compressed = WorkingLength < 0;

                        if (Compressed)
                        {
                            WorkingLength = -WorkingLength;
                        }
                    }

                    if (WorkingLength != long.MaxValue)
                    {
                        if (s.Length >= WorkingLength + DataIndex)
                        {
                            if (s.Position != DataIndex)
                            {
                                s.Seek(DataIndex, SeekOrigin.Begin);
                            }

                            byte[] data = s.ReadUInt8Array((int)WorkingLength);

                            if (Compressed)
                            {
                                data = ZlibStream.UncompressBuffer(data);
                            }

                            //todo Omit this after testing
                            //SharpStarLogger.DefaultLogger.Info(string.Format("{0}{1}","ID : ",_packetId));

                            IPacket packet = Decode(_packetId, data);

                            WorkingLength = long.MaxValue;

                            byte[] rest = s.ReadToEnd();
                            PacketBuffer = rest.ToList();

                            if (rest.Length > 0)
                            {
                                toProcess.Enqueue(rest);
                            }

                            yield return(packet);
                        }
                    }
                }
            }
        }
Example #22
0
        public async Task FlushPackets()
        {
            while (PacketQueue.Count > 0)
            {
                try
                {
                    IPacket next;

                    if (!PacketQueue.TryDequeue(out next))
                    {
                        continue;
                    }

                    if (!next.IsReceive)
                    {
                        foreach (var handler in Server.PacketHandlers)
                        {
                            if (next.PacketId == handler.PacketId)
                            {
                                await handler.Handle(next, OtherClient);
                            }
                        }
                    }

                    if (next.Ignore)
                    {
                        continue;
                    }

                    var stream = new StarboundStream();
                    next.Write(stream);

                    byte[] buffer     = stream.ToArray();
                    bool   compressed = stream.Length >= 512;

                    if (compressed)
                    {
                        buffer = ZlibStream.CompressBuffer(buffer);
                    }

                    stream.Dispose();

                    int length = compressed ? -buffer.Length : buffer.Length;

                    var finalStream = new StarboundStream();

                    finalStream.WriteUInt8(next.PacketId);
                    finalStream.WriteSignedVLQ(length);
                    finalStream.Write(buffer, 0, buffer.Length);

                    byte[] toSend = finalStream.ToArray();

                    finalStream.Dispose();

                    if (Socket == null)
                    {
                        return;
                    }

                    var token = new AsyncUserToken();
                    token.Socket = Socket;

                    SocketAsyncEventArgs writeArgs = new SocketAsyncEventArgs();
                    writeArgs.RemoteEndPoint = Socket.RemoteEndPoint;
                    writeArgs.UserToken      = token;
                    writeArgs.SetBuffer(toSend, 0, toSend.Length);
                    writeArgs.Completed += IO_Completed;

                    Socket.SendAsync(writeArgs);
                }
                catch
                {
                    CloseClientSocket(readEventArgs);
                }
            }
        }
 public void Write(StarboundStream stream)
 {
     stream.WriteString(ClaimMessage);
     stream.WriteString(PasswordHash);
 }
 public void Read(StarboundStream stream)
 {
     ClaimMessage = stream.ReadString();
     Salt         = stream.ReadString();
     Rounds       = stream.ReadInt32();
 }
 public void Write(StarboundStream stream)
 {
     stream.WriteUInt32(ProtocolVersion);
 }
Example #26
0
 public void Write(StarboundStream stream)
 {
     stream.WriteVLQ(Value);
 }
 public void Write(StarboundStream stream)
 {
     stream.WriteString(ClaimMessage);
     stream.WriteString(Salt);
     stream.WriteInt32(Rounds);
 }