public static List <Document> ListFromBuffer(byte[] data) { var documents = new List <Document>(); using (StarReader reader = new StarReader(data)) { int pos; bool success; int len = (int)VLQ.FromBuffer(data, 0, data.Length, out pos, out success); if (!success) { return(null); } reader.BaseStream.Seek(pos, SeekOrigin.Begin); for (int i = 0; i < len; i++) { documents.Add(ReadDocument(reader)); } } return(documents); }
/// <summary> /// Gets or builds a deserialization expression tree to convert the specified <see cref="StarReader" /> to an instance of /// the specified type. /// </summary> public static object Deserialize(StarReader reader, Type type) { object result = DeserializeInternal(reader, type); //if (reader.DataLeft != 0) // StarLog.DefaultLogger.Warn("Packet {0} is incomplete ({1} bytes left)!", type.FullName, reader.DataLeft); return(result); }
/// <summary> /// Decodes (i.e. reads) a packet /// </summary> /// <param name="packetId">The id of the packet</param> /// <param name="data">The data that will be fed into the packet to be read</param> /// <returns>The decoded packet</returns> protected virtual Packet Decode(byte packetId, byte[] data) { Packet packet; try { if (_packets.ContainsKey(packetId)) { using (StarReader reader = new StarReader(data)) { packet = PacketSerializer.Deserialize(reader, _packets[packetId]) as Packet; if (reader.DataLeft != 0) { if (packet != null) { StarLog.DefaultLogger.Warn("Packet {0} is incomplete ({1} bytes left)!", packet.GetType().FullName, reader.DataLeft); } else { StarLog.DefaultLogger.Warn("Packet {0} is incomplete ({1} bytes left)!", packetId, reader.DataLeft); } } if (packet == null) { StarLog.DefaultLogger.Warn("Error deserializing packet {0} ({1})", (PacketType)packetId, packetId); packet = new GenericPacket(packetId) { Data = data }; } } } else { packet = new GenericPacket(packetId) { Data = data }; } packet.IsReceive = true; } catch (Exception ex) { StarLog.DefaultLogger.Error("Packet {0} caused an error!", _packets[packetId].FullName); ex.LogError(); return(null); } return(packet); }
public static T Deserialize <T>(StarReader reader) where T : Packet { T result = Deserialize(reader, typeof(T)) as T; if (reader.DataLeft != 0) { StarLog.DefaultLogger.Warn("Packet {0} is incomplete!", typeof(T).FullName); } return(result); }
public static Document ReadDocument(StarReader reader) { Document doc = new Document(); doc.Name = reader.ReadString(); reader.ReadByte(); doc.Version = DataConverter.BigEndian.GetInt32(reader.ReadBytes(4), 0); doc.Data = (StarVariant)PacketSerializer.Deserialize(reader, typeof(StarVariant)); return(doc); }
private static object DeserializeInternal(StarReader reader, Type type) { try { BuildAndStore(type); var lambda = PacketDeserializers[type]; return(lambda(reader)); } catch (Exception e) { throw new ApplicationException(string.Format("Serialization error. Type: {0}", type), e); } }
public Metadata GetMetadata() { if (Metadata != null) { return(Metadata); } using (StarReader reader = new StarReader(GetRaw(new byte[] { 0, 0, 0 }))) { var unpacked = DataConverter.Unpack("^ii", reader.ReadBytes(8), 0); //unknown Document doc = Document.ReadDocument(reader); if (doc.Name != "WorldMetadata") { throw new Exception("Invalid world data!"); } Metadata = new Metadata(doc.Data, doc.Version); return(Metadata); } }
public abstract Task Read(StarReader reader);
void Start() { var s = new StarReader(Application.streamingAssetsPath); System.Threading.ThreadPool.QueueUserWorkItem(s.read); }
private static StarCollection GetStarCollection() { return(StarReader.Parse(TxtData.Lines())); }