public static Subpacket CreateSubpacket(SubpacketHeader header, byte[] body, bool isSent, bool isChat) { Subpacket sp; Dictionary <int, Type> subpacketDefinitions; if (isChat) { subpacketDefinitions = isSent ? SubpacketChatSentDefinitions : SubpacketChatRecievedDefinitions; } else { subpacketDefinitions = isSent ? SubpacketSentDefinitions : SubpacketRecievedDefinitions; } Dictionary <int, Dictionary <int, Type> > subpacketExpandedDefinitions = isSent ? SubpacketSentExpandedDefinitions : SubpacketReceivedExpandedDefinitions; if (!subpacketDefinitions.ContainsKey(header.Id)) { sp = new UnknownSubpacket(); //sp.PacketData = body; UnknownSubpacket.UnknownBodyData unkData = new UnknownSubpacket.UnknownBodyData(); unkData.BodySize = body.Count(); unkData.IsSent = isSent; unkData.Id = header.Id; sp.PacketData = unkData; sp.SubpacketHeader = header; } else { Type subpacketDefinition = subpacketDefinitions[header.Id]; sp = (Subpacket)Activator.CreateInstance(subpacketDefinition); sp.InitializeSubpacket(header, body); if (subpacketExpandedDefinitions.ContainsKey(header.Id)) { object data = sp.PacketData; if (data.GetType().GetProperty("ExpandedId") != null && subpacketExpandedDefinitions[header.Id].ContainsKey((int)(Convert.ToInt32(data.GetType().GetProperty("ExpandedId").GetValue(data))))) { sp = (Subpacket)Activator.CreateInstance(SubpacketSentExpandedDefinitions[header.Id][(int)((Convert.ToInt32(data.GetType().GetProperty("ExpandedId").GetValue(data))))]); sp.InitializeSubpacket(header, body); } } } sp.RawPacketData = body; return(sp); }
internal void InitializeSubpacket(SubpacketHeader header, byte[] body) { this.SubpacketHeader = header; if (body.Length == this.GetStructureSize()) { this.PacketData = ReadStruct(body); } else { int sizeCount = 0; List <Object> subpackets = new List <Object>(); while (sizeCount < body.Length) { subpackets.Add(ReadStruct(body.Skip(sizeCount).ToArray())); sizeCount += GetStructureSize(); } PacketData = subpackets; } }