Esempio n. 1
0
        static int Main(string[] args)
        {
            ProtocolManager protocol;
            List<string> unused = new List<string>();
            TextWriter writer;

            try
            {
                if (args.Length != 4)
                {
                    Console.WriteLine("Usage: [message_template.msg] [template.cs] [unusedpackets.txt] [_Packets_.cs]");
                    return -1;
                }

                writer = new StreamWriter(args[3]);
                protocol = new ProtocolManager(args[0]);

                // Build a list of unused packets
                using (StreamReader unusedReader = new StreamReader(args[2]))
                {
                    while (unusedReader.Peek() >= 0)
                    {
                        unused.Add(unusedReader.ReadLine().Trim());
                    }
                }

                // Read in the template.cs file and write it to our output
                TextReader reader = new StreamReader(args[1]);
                writer.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return -2;
            }

            // Prune all of the unused packets out of the protocol
            int i = 0;
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.LowMaps[i] = null;
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.MediumMaps[i] = null;
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.HighMaps[i] = null;
                i++;
            }

            // Write the PacketType enum
            writer.WriteLine("    public enum PacketType" + Environment.NewLine + "    {" + Environment.NewLine +
                "        /// <summary>A generic value, not an actual packet type</summary>" + Environment.NewLine +
                "        Default,");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x10000 | packet.ID) + ",");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x20000 | packet.ID) + ",");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x30000 | packet.ID) + ",");
            writer.WriteLine("    }" + Environment.NewLine);

            // Write the base Packet class
            writer.WriteLine(
                "    public abstract partial class Packet" + Environment.NewLine + "    {" + Environment.NewLine +
                "        public const int MTU = 1200;" + Environment.NewLine +
                Environment.NewLine +
                "        public Header Header;" + Environment.NewLine +
                "        public bool HasVariableBlocks;" + Environment.NewLine +
                "        public PacketType Type;" + Environment.NewLine +
                "        public abstract int Length { get; }" + Environment.NewLine +
                "        public abstract void FromBytes(byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine +
                "        public abstract void FromBytes(Header header, byte[] bytes, ref int i, ref int packetEnd);" + Environment.NewLine +
                "        public abstract byte[] ToBytes();" + Environment.NewLine +
                "        public abstract byte[][] ToBytesMultiple();"
            );
            writer.WriteLine();

            // Write the Packet.GetType() function
            writer.WriteLine(
                "        public static PacketType GetType(ushort id, PacketFrequency frequency)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            switch (frequency)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                case PacketFrequency.Low:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine +
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine +
                "                case PacketFrequency.Medium:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine +
                "                case PacketFrequency.High:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                "            return PacketType.Default;" + Environment.NewLine + "        }" + Environment.NewLine);

            // Write the Packet.BuildPacket(PacketType) function
            writer.WriteLine("        public static Packet BuildPacket(PacketType type)");
            writer.WriteLine("        {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("            if(type == PacketType." + packet.Name + ") return new " + packet.Name + "Packet();");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("            if(type == PacketType." + packet.Name + ") return new " + packet.Name + "Packet();");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("            if(type == PacketType." + packet.Name + ") return new " + packet.Name + "Packet();");
            writer.WriteLine("            return null;" + Environment.NewLine);
            writer.WriteLine("        }");

            // Write the Packet.BuildPacket() function
            writer.WriteLine(@"
            public static Packet BuildPacket(byte[] packetBuffer, ref int packetEnd, byte[] zeroBuffer)
            {
            byte[] bytes;
            int i = 0;
            Header header = Header.BuildHeader(packetBuffer, ref i, ref packetEnd);
            if (header.Zerocoded)
            {
                packetEnd = Helpers.ZeroDecode(packetBuffer, packetEnd + 1, zeroBuffer) - 1;
                bytes = zeroBuffer;
            }
            else
            {
                bytes = packetBuffer;
            }
            Array.Clear(bytes, packetEnd + 1, bytes.Length - packetEnd - 1);

            switch (header.Frequency)
            {
                case PacketFrequency.Low:
                    switch (header.ID)
                    {");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine(@"
                    }
                    break;
                case PacketFrequency.Medium:
                    switch (header.ID)
                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine(@"
                    }
                    break;
                case PacketFrequency.High:
                    switch (header.ID)
                    {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine(@"
                    }
                    break;
            }

            throw new MalformedDataException(""Unknown packet ID "" + header.Frequency + "" "" + header.ID);
            }
            }");

            // Write the packet classes
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null) { WritePacketClass(writer, packet); }
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null) { WritePacketClass(writer, packet); }
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null) { WritePacketClass(writer, packet); }

            // Finish up
            writer.WriteLine("}");
            writer.Close();
            return 0;
        }
Esempio n. 2
0
        static int Main(string[] args)
        {
            ProtocolManager protocol;
            List<string> unused = new List<string>();
            TextWriter writer;

            try
            {
                if (args.Length != 4)
                {
                    Console.WriteLine("Usage: [message_template.msg] [template.cs] [unusedpackets.txt] [_Packets_.cs]");
                    return -1;
                }

                writer = new StreamWriter(args[3]);
                protocol = new ProtocolManager(args[0]);

                // Build a list of unused packets
                using (StreamReader unusedReader = new StreamReader(args[2]))
                {
                    while (unusedReader.Peek() >= 0)
                    {
                        unused.Add(unusedReader.ReadLine().Trim());
                    }
                }

                // Read in the template.cs file and write it to our output
                TextReader reader = new StreamReader(args[1]);
                writer.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return -2;
            }

            // Prune all of the unused packets out of the protocol
            int i = 0;
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.LowMaps[i] = null;
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.MediumMaps[i] = null;
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.HighMaps[i] = null;
                i++;
            }

            // Write the PacketType enum
            writer.WriteLine("    public enum PacketType" + Environment.NewLine + "    {" + Environment.NewLine +
                "        /// <summary>A generic value, not an actual packet type</summary>" + Environment.NewLine +
                "        Default,");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x10000 | packet.ID)  + ",");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x20000 | packet.ID) + ",");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x30000 | packet.ID) + ",");
            writer.WriteLine("    }" + Environment.NewLine);

            // Write the base Packet class
            writer.WriteLine(
                "    public abstract partial class Packet" + Environment.NewLine + "    {" + Environment.NewLine +
                "        public abstract Header Header { get; set; }" + Environment.NewLine +
                "        public abstract PacketType Type { get; }" + Environment.NewLine +
                "        public abstract void FromBytes(byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine +
                "        public abstract void FromBytes(Header header, byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine +
                "        public abstract byte[] ToBytes();"
            );

            // Write the Packet.GetType() function
            writer.WriteLine(
                "        public static PacketType GetType(ushort id, PacketFrequency frequency)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            switch (frequency)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                case PacketFrequency.Low:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine +
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine +
                "                case PacketFrequency.Medium:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine +
                "                case PacketFrequency.High:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                "            return PacketType.Default;" + Environment.NewLine + "        }" + Environment.NewLine);

            // Write the Packet.BuildPacket() function
            writer.WriteLine(
                "        public static Packet BuildPacket(byte[] packetBuffer, ref int packetEnd, byte[] zeroBuffer)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            byte[] bytes; ushort id; PacketFrequency freq;" + Environment.NewLine +
                "            int i = 0;" + Environment.NewLine +
                "            Header header = Header.BuildHeader(packetBuffer, ref i, ref packetEnd);" + Environment.NewLine +
                "            if (header.Zerocoded)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                packetEnd = Helpers.ZeroDecode(packetBuffer, packetEnd + 1, zeroBuffer) - 1;" + Environment.NewLine +
                "                bytes = zeroBuffer;" + Environment.NewLine +
                "            }" + Environment.NewLine +
                "            else" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                bytes = packetBuffer;" + Environment.NewLine +
                "            }" + Environment.NewLine + Environment.NewLine +
                "            if (bytes[6] == 0xFF)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                if (bytes[7] == 0xFF)" + Environment.NewLine +
                "                {" + Environment.NewLine +
                "                    id = (ushort)((bytes[8] << 8) + bytes[9]); freq = PacketFrequency.Low;" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine +
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine("                    }" + Environment.NewLine + "                }" + Environment.NewLine +
                "                else" + Environment.NewLine +
                "                {" + Environment.NewLine + "                    id = (ushort)bytes[7];  freq = PacketFrequency.Medium;" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine("                    }" + Environment.NewLine + "                }" + Environment.NewLine + "            }" + Environment.NewLine +
                "            else" + Environment.NewLine + "            {" + Environment.NewLine +
                "                id = (ushort)bytes[6];  freq = PacketFrequency.High;" + Environment.NewLine +
                "                switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                "            throw new MalformedDataException(\"Unknown packet ID \"+freq+\" \"+id);" + Environment.NewLine +
                "        }" + Environment.NewLine + "    }" + Environment.NewLine);

            // Write the packet classes
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null) { WritePacketClass(writer, packet); }
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null) { WritePacketClass(writer, packet); }
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null) { WritePacketClass(writer, packet); }

            // Finish up
            writer.WriteLine("}");
            writer.Close();
            return 0;
        }
Esempio n. 3
0
        static int Main(string[] args)
        {
            ProtocolManager protocol;
            List<string> unused = new List<string>();
            TextWriter writer;

            try
            {
                if (args.Length != 4)
                {
                    Console.WriteLine("Usage: [message_template.msg] [template.cs] [unusedpackets.txt] [_Packets_.cs]");
                    return -1;
                }

                writer = new StreamWriter(args[3]);
                protocol = new ProtocolManager(args[0]);

                // Build a list of unused packets
                using (StreamReader unusedReader = new StreamReader(args[2])) 
                {
                    while (unusedReader.Peek() >= 0) 
                    {
                        unused.Add(unusedReader.ReadLine().Trim());
                    }
                }

                // Read in the template.cs file and write it to our output
                TextReader reader = new StreamReader(args[1]);
                writer.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return -2;
            }


            // Prune all of the unused packets out of the protocol
            int i = 0;
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.LowMaps[i] = null;
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.MediumMaps[i] = null;
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                    protocol.HighMaps[i] = null;
                i++;
            }


            // Write the PacketType enum
            writer.WriteLine("    public enum PacketType" + Environment.NewLine + "    {" + Environment.NewLine +
                "        /// <summary>A generic value, not an actual packet type</summary>" + Environment.NewLine +
                "        Default,");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x10000 | packet.ID)  + ",");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x20000 | packet.ID) + ",");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("        " + packet.Name + " = " + (0x30000 | packet.ID) + ",");
            writer.WriteLine("    }" + Environment.NewLine);


            // Write all of the XmlInclude statements for the Packet class to allow packet serialization
            //writer.WriteLine("#if PACKETSERIALIZE");
            //foreach (MapPacket packet in protocol.LowMaps)
            //    if (packet != null)
            //        writer.WriteLine("    [XmlInclude(typeof(" + packet.Name + "Packet))]");
            //foreach (MapPacket packet in protocol.MediumMaps)
            //    if (packet != null)
            //        writer.WriteLine("    [XmlInclude(typeof(" + packet.Name + "Packet))]");
            //foreach (MapPacket packet in protocol.HighMaps)
            //    if (packet != null)
            //        writer.WriteLine("    [XmlInclude(typeof(" + packet.Name + "Packet))]");
            //writer.WriteLine("#endif");

            // Write the base Packet class
            writer.WriteLine(
                "    public abstract partial class Packet" + Environment.NewLine + "    {" + Environment.NewLine + 
                "        public abstract Header Header { get; set; }" + Environment.NewLine +
                "        public abstract PacketType Type { get; }" + Environment.NewLine +
                "        public abstract void FromBytes(byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine + 
                "        public abstract void FromBytes(Header header, byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine +
                "        public int ResendCount;" + Environment.NewLine +
                "        public int TickCount;" + Environment.NewLine + Environment.NewLine +
                "        public abstract byte[] ToBytes();" //+ Environment.NewLine + Environment.NewLine +
                //"        public void ToXml(XmlWriter xmlWriter)" + Environment.NewLine +
                //"        {" + Environment.NewLine +
                //"            XmlSerializer serializer = new XmlSerializer(typeof(Packet));" + Environment.NewLine +
                //"            serializer.Serialize(xmlWriter, this);" + Environment.NewLine +
                //"        }");
            );


            // Write the Packet.GetType() function
            writer.WriteLine(
                "        public static PacketType GetType(ushort id, PacketFrequency frequency)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            switch (frequency)" + Environment.NewLine + 
                "            {" + Environment.NewLine + 
                "                case PacketFrequency.Low:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + 
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine + 
                "                    break;" + Environment.NewLine +
                "                case PacketFrequency.Medium:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine + 
                "                    break;" + Environment.NewLine +
                "                case PacketFrequency.High:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
            writer.WriteLine("                    }" + Environment.NewLine +
                "                    break;" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                "            return PacketType.Default;" + Environment.NewLine + "        }" + Environment.NewLine);
            
            // TODO: Not sure if this function is useful to anyone, but if it is it needs to be
            // rewritten to not overwrite passed in pointers and decode the entire packet just
            // to read the header

            // Write the Packet.GetType() function
            //writer.WriteLine(
            //    "        public static PacketType GetType(byte[] bytes, int packetEnd, byte[] zeroBuffer)" + Environment.NewLine +
            //    "        {" + Environment.NewLine + "            ushort id; PacketFrequency freq;" + Environment.NewLine + 
            //    "            int i = 0, end = packetEnd;" + Environment.NewLine +
            //    "            Header header = Header.BuildHeader(bytes, ref i, ref end);" + Environment.NewLine +
            //    "            if (header.Zerocoded)" + Environment.NewLine + "            {" + Environment.NewLine +
            //    "                end = Helpers.ZeroDecode(bytes, end + 1, zeroBuffer) - 1;" + Environment.NewLine +
            //    "                bytes = zeroBuffer;" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
            //    "            if (bytes[6] == 0xFF)" + Environment.NewLine + "            {" + Environment.NewLine +
            //    "                if (bytes[7] == 0xFF)" + Environment.NewLine + "                {" + Environment.NewLine +
            //    "                    id = (ushort)((bytes[8] << 8) + bytes[9]); freq = PacketFrequency.Low;" + Environment.NewLine +
            //    "                }" + Environment.NewLine + 
            //    "                else" + Environment.NewLine +
            //    "                {" + Environment.NewLine + "                    id = (ushort)bytes[7];  freq = PacketFrequency.Medium;" +
            //    Environment.NewLine + "                }" + Environment.NewLine + "            }" + Environment.NewLine +
            //    "            else" + Environment.NewLine + "            {" + Environment.NewLine +
            //    "                id = (ushort)bytes[6];  freq = PacketFrequency.High;" + Environment.NewLine +
            //    "            }" + Environment.NewLine +
            //    "            return GetType(id, freq);" + Environment.NewLine +
            //    "        }" + Environment.NewLine);

            // Write the Packet.BuildPacket(PacketType) function
            writer.WriteLine("        public static Packet BuildPacket(PacketType type)");
            writer.WriteLine("        {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("            if(type == PacketType." + packet.Name  + ") return new " + packet.Name +"Packet();");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("            if(type == PacketType." + packet.Name  + ") return new " + packet.Name +"Packet();");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("            if(type == PacketType." + packet.Name  + ") return new " + packet.Name +"Packet();");
            writer.WriteLine("            return null;" + Environment.NewLine);
            writer.WriteLine("        }" + Environment.NewLine);

            // Write the Packet.BuildPacket() function
            writer.WriteLine(
                "        public static Packet BuildPacket(byte[] packetBuffer, ref int packetEnd, byte[] zeroBuffer)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            byte[] bytes; ushort id; PacketFrequency freq;" + Environment.NewLine + 
                "            int i = 0;" + Environment.NewLine +
                "            Header header = Header.BuildHeader(packetBuffer, ref i, ref packetEnd);" + Environment.NewLine +
                "            if (header.Zerocoded)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                packetEnd = Helpers.ZeroDecode(packetBuffer, packetEnd + 1, zeroBuffer) - 1;" + Environment.NewLine +
                "                bytes = zeroBuffer;" + Environment.NewLine +
                "            }" + Environment.NewLine +
                "            else" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                bytes = packetBuffer;" + Environment.NewLine +
                "            }" + Environment.NewLine + Environment.NewLine +
                "            if (bytes[6] == 0xFF)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                if (bytes[7] == 0xFF)" + Environment.NewLine +
                "                {" + Environment.NewLine +
                "                    id = (ushort)((bytes[8] << 8) + bytes[9]); freq = PacketFrequency.Low;" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine +
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine("                    }" + Environment.NewLine + "                }" + Environment.NewLine + 
                "                else" + Environment.NewLine +
                "                {" + Environment.NewLine + "                    id = (ushort)bytes[7];  freq = PacketFrequency.Medium;" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine("                    }" + Environment.NewLine + "                }" + Environment.NewLine + "            }" + Environment.NewLine +
                "            else" + Environment.NewLine + "            {" + Environment.NewLine +
                "                id = (ushort)bytes[6];  freq = PacketFrequency.High;" + Environment.NewLine +
                "                switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null)
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                "            throw new MalformedDataException(\"Unknown packet ID \"+freq+\" \"+id);" + Environment.NewLine +
                "        }" + Environment.NewLine + "    }" + Environment.NewLine);


            // Write the packet classes
            foreach (MapPacket packet in protocol.LowMaps)
                if (packet != null) { WritePacketClass(writer, packet); }
            foreach (MapPacket packet in protocol.MediumMaps)
                if (packet != null) { WritePacketClass(writer, packet); }
            foreach (MapPacket packet in protocol.HighMaps)
                if (packet != null) { WritePacketClass(writer, packet); }


            // Finish up
            writer.WriteLine("}");
            writer.Close();
            return 0;
        }
Esempio n. 4
0
        static int Main(string[] args)
        {
            ProtocolManager protocol;
            List <string>   unused = new List <string>();
            TextWriter      writer;

            try
            {
                if (args.Length != 4)
                {
                    Console.WriteLine("Usage: [message_template.msg] [template.cs] [unusedpackets.txt] [_Packets_.cs]");
                    return(-1);
                }

                writer   = new StreamWriter(args[3]);
                protocol = new ProtocolManager(args[0]);

                // Build a list of unused packets
                using (StreamReader unusedReader = new StreamReader(args[2]))
                {
                    while (unusedReader.Peek() >= 0)
                    {
                        unused.Add(unusedReader.ReadLine().Trim());
                    }
                }

                // Read in the template.cs file and write it to our output
                TextReader reader = new StreamReader(args[1]);
                writer.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(-2);
            }


            // Prune all of the unused packets out of the protocol
            int i = 0;

            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                {
                    protocol.LowMaps[i] = null;
                }
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                {
                    protocol.MediumMaps[i] = null;
                }
                i++;
            }
            i = 0;
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null && unused.Contains(packet.Name))
                {
                    protocol.HighMaps[i] = null;
                }
                i++;
            }


            // Write the PacketType enum
            writer.WriteLine("    public enum PacketType" + Environment.NewLine + "    {" + Environment.NewLine +
                             "        /// <summary>A generic value, not an actual packet type</summary>" + Environment.NewLine +
                             "        Default,");
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("        " + packet.Name + " = " + (0x10000 | packet.ID) + ",");
                }
            }
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("        " + packet.Name + " = " + (0x20000 | packet.ID) + ",");
                }
            }
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("        " + packet.Name + " = " + (0x30000 | packet.ID) + ",");
                }
            }
            writer.WriteLine("    }" + Environment.NewLine);

            // Write the base Packet class
            writer.WriteLine(
                "    public abstract partial class Packet" + Environment.NewLine + "    {" + Environment.NewLine +
                "        public abstract Header Header { get; set; }" + Environment.NewLine +
                "        public abstract PacketType Type { get; }" + Environment.NewLine +
                "        public abstract void FromBytes(byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine +
                "        public abstract void FromBytes(Header header, byte[] bytes, ref int i, ref int packetEnd, byte[] zeroBuffer);" + Environment.NewLine +
                "        public abstract byte[] ToBytes();"
                );


            // Write the Packet.GetType() function
            writer.WriteLine(
                "        public static PacketType GetType(ushort id, PacketFrequency frequency)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            switch (frequency)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                case PacketFrequency.Low:" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine +
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
                }
            }
            writer.WriteLine("                    }" + Environment.NewLine +
                             "                    break;" + Environment.NewLine +
                             "                case PacketFrequency.Medium:" + Environment.NewLine +
                             "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
                }
            }
            writer.WriteLine("                    }" + Environment.NewLine +
                             "                    break;" + Environment.NewLine +
                             "                case PacketFrequency.High:" + Environment.NewLine +
                             "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID + ": return PacketType." + packet.Name + ";");
                }
            }
            writer.WriteLine("                    }" + Environment.NewLine +
                             "                    break;" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                             "            return PacketType.Default;" + Environment.NewLine + "        }" + Environment.NewLine);

            // Write the Packet.BuildPacket() function
            writer.WriteLine(
                "        public static Packet BuildPacket(byte[] packetBuffer, ref int packetEnd, byte[] zeroBuffer)" + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            byte[] bytes; ushort id; PacketFrequency freq;" + Environment.NewLine +
                "            int i = 0;" + Environment.NewLine +
                "            Header header = Header.BuildHeader(packetBuffer, ref i, ref packetEnd);" + Environment.NewLine +
                "            if (header.Zerocoded)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                packetEnd = Helpers.ZeroDecode(packetBuffer, packetEnd + 1, zeroBuffer) - 1;" + Environment.NewLine +
                "                bytes = zeroBuffer;" + Environment.NewLine +
                "            }" + Environment.NewLine +
                "            else" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                bytes = packetBuffer;" + Environment.NewLine +
                "            }" + Environment.NewLine + Environment.NewLine +
                "            if (bytes[6] == 0xFF)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                if (bytes[7] == 0xFF)" + Environment.NewLine +
                "                {" + Environment.NewLine +
                "                    id = (ushort)((bytes[8] << 8) + bytes[9]); freq = PacketFrequency.Low;" + Environment.NewLine +
                "                    switch (id)" + Environment.NewLine +
                "                    {");
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
                }
            }
            writer.WriteLine("                    }" + Environment.NewLine + "                }" + Environment.NewLine +
                             "                else" + Environment.NewLine +
                             "                {" + Environment.NewLine + "                    id = (ushort)bytes[7];  freq = PacketFrequency.Medium;" + Environment.NewLine +
                             "                    switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
                }
            }
            writer.WriteLine("                    }" + Environment.NewLine + "                }" + Environment.NewLine + "            }" + Environment.NewLine +
                             "            else" + Environment.NewLine + "            {" + Environment.NewLine +
                             "                id = (ushort)bytes[6];  freq = PacketFrequency.High;" + Environment.NewLine +
                             "                switch (id)" + Environment.NewLine + "                    {");
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID + ": return new " + packet.Name + "Packet(header, bytes, ref i);");
                }
            }
            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine + Environment.NewLine +
                             "            throw new MalformedDataException(\"Unknown packet ID \"+freq+\" \"+id);" + Environment.NewLine +
                             "        }" + Environment.NewLine + "    }" + Environment.NewLine);


            // Write the packet classes
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    WritePacketClass(writer, packet);
                }
            }
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    WritePacketClass(writer, packet);
                }
            }
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    WritePacketClass(writer, packet);
                }
            }


            // Finish up
            writer.WriteLine("}");
            writer.Close();
            return(0);
        }
Esempio n. 5
0
        static int Main(string[] args)
        {
            ProtocolManager protocol;
            TextWriter      writer;

            try
            {
                if (args.Length < 3)
                {
                    Console.WriteLine("Invalid arguments, need [message_template.msg] [template.cs] [_Packets_.cs]");
                    return(-1);
                }

                writer   = new StreamWriter(args[2]);
                protocol = new ProtocolManager(args[0]);

                TextReader reader = new StreamReader(args[1]);
                writer.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(-2);
            }

            // Write the PacketType enum
            writer.WriteLine("    /// <summary>Used to identify the type of a packet</summary>");
            writer.WriteLine("    public enum PacketType\n    {\n" +
                             "        /// <summary>A generic value, not an actual packet type</summary>\n" +
                             "        Default,");
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("        /// <summary>" + packet.Name + "</summary>");
                    writer.WriteLine("        " + packet.Name + ",");
                }
            }
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("        /// <summary>" + packet.Name + "</summary>");
                    writer.WriteLine("        " + packet.Name + ",");
                }
            }
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("        /// <summary>" + packet.Name + "</summary>");
                    writer.WriteLine("        " + packet.Name + ",");
                }
            }
            writer.WriteLine("    }\n");

            // Write the base Packet class
            writer.WriteLine("    /// <summary>Base class for all packet classes</summary>\n" +
                             "    public abstract class Packet\n    {\n" +
                             "        /// <summary>Either a LowHeader, MediumHeader, or HighHeader representing the first bytes of the packet</summary>\n" +
                             "        public abstract Header Header { get; set; }\n" +
                             "        /// <summary>The type of this packet, identified by it's frequency and ID</summary>\n" +
                             "        public abstract PacketType Type { get; }\n" +
                             "        /// <summary>Used internally to track timeouts, do not use</summary>\n" +
                             "        public int TickCount;\n\n" +
                             "        /// <summary>Serializes the packet in to a byte array</summary>\n" +
                             "        /// <returns>A byte array containing the serialized packet payload, ready to be sent across the wire</returns>\n" +
                             "        public abstract byte[] ToBytes();\n\n" +
                             "        /// <summary>Get the PacketType for a given packet id and packet frequency</summary>\n" +
                             "        /// <param name=\"id\">The packet ID from the header</param>\n" +
                             "        /// <param name=\"frequency\">Frequency of this packet</param>\n" +
                             "        /// <returns>The packet type, or PacketType.Default</returns>\n" +
                             "        public static PacketType GetType(ushort id, PacketFrequency frequency)\n        {\n" +
                             "            switch (frequency)\n            {\n                case PacketFrequency.Low:\n" +
                             "                    switch (id)\n                    {");

            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID +
                                     ": return PacketType." + packet.Name + ";");
                }
            }

            writer.WriteLine("                    }\n                    break;\n" +
                             "                case PacketFrequency.Medium:\n                    switch (id)\n                    {");

            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID +
                                     ": return PacketType." + packet.Name + ";");
                }
            }

            writer.WriteLine("                    }\n                    break;\n" +
                             "                case PacketFrequency.High:\n                    switch (id)\n                    {");

            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID +
                                     ": return PacketType." + packet.Name + ";");
                }
            }

            writer.WriteLine("                    }\n                    break;\n            }\n\n" +
                             "            return PacketType.Default;\n        }\n");

            writer.WriteLine("        /// <summary>Construct a packet in it's native class from a byte array</summary>\n" +
                             "        /// <param name=\"bytes\">Byte array containing the packet, starting at position 0</param>\n" +
                             "        /// <param name=\"packetEnd\">The last byte of the packet. If the packet was 76 bytes long, packetEnd would be 75</param>\n" +
                             "        /// <returns>The native packet class for this type of packet, typecasted to the generic Packet</returns>\n" +
                             "        public static Packet BuildPacket(byte[] bytes, ref int packetEnd)\n" +
                             "        {\n            ushort id;\n            int i = 0;\n" +
                             "            Header header = Header.BuildHeader(bytes, ref i, ref packetEnd);\n" +
                             "            if (header.Zerocoded)\n            {\n" +
                             "                byte[] zeroBuffer = new byte[8192];\n" +
                             "                packetEnd = Helpers.ZeroDecode(bytes, packetEnd + 1, zeroBuffer) - 1;\n" +
                             "                bytes = zeroBuffer;\n            }\n\n" +
                             "            if (bytes[4] == 0xFF)\n            {\n" +
                             "                if (bytes[5] == 0xFF)\n                {\n" +
                             "                    id = (ushort)((bytes[6] << 8) + bytes[7]);\n" +
                             "                    switch (id)\n                    {");
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID +
                                     ": return new " + packet.Name + "Packet(header, bytes, ref i);");
                }
            }
            writer.WriteLine("                    }\n                }\n                else\n" +
                             "                {\n                    id = (ushort)bytes[5];\n" +
                             "                    switch (id)\n                    {");
            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID +
                                     ": return new " + packet.Name + "Packet(header, bytes, ref i);");
                }
            }
            writer.WriteLine("                    }\n                }\n            }\n" +
                             "            else\n            {\n" +
                             "                id = (ushort)bytes[4];\n" +
                             "                switch (id)\n                    {");
            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    writer.WriteLine("                        case " + packet.ID +
                                     ": return new " + packet.Name + "Packet(header, bytes, ref i);");
                }
            }
            writer.WriteLine("                }\n            }\n\n" +
                             "            throw new MalformedDataException(\"Unknown packet ID\");\n" +
                             "        }\n    }\n");

            // Write the packet classes
            foreach (MapPacket packet in protocol.LowMaps)
            {
                if (packet != null)
                {
                    WritePacketClass(writer, packet);
                }
            }

            foreach (MapPacket packet in protocol.MediumMaps)
            {
                if (packet != null)
                {
                    WritePacketClass(writer, packet);
                }
            }

            foreach (MapPacket packet in protocol.HighMaps)
            {
                if (packet != null)
                {
                    WritePacketClass(writer, packet);
                }
            }

            writer.WriteLine("}");

            writer.Close();

            return(0);
        }