Example #1
0
        public static void Main(string[] args)
        {
#if SAVE_ALL_TYPES
            List<string> fieldTypes = new List<string>();
#endif
            var generatorList = new List<ProtobufPacketGenerator>();


            // Those ID's are not supported and should be parsed manually.
            var bannedIDs = new int[] { 0x20, 0x22, 0x26, 0x34, 0x37, 0x38, 0x44, 0x45 };

            var protocols = GetProtocolTables();
            foreach (var protocol in protocols)
            {
                var className = $"{protocol.Name.Replace("-", "").Replace("_", "")}Packet";
                var id = new VarInt(int.Parse(protocol.GetAt(0, 0).Remove(0, 2), NumberStyles.AllowHexSpecifier));
                if (bannedIDs.Contains(id))
                    continue;

                var state = (State) Enum.Parse(typeof(State), protocol.GetAt(0, 1).ExceptBlanks());
                var boundTo = (BoundTo) Enum.Parse(typeof(BoundTo), protocol.GetAt(0, 2).ExceptBlanks());

                var builder = new ProtobufPacketGenerator(className, id, boundTo, state);

                for (int ix = 0; ix < protocol.Height; ix++)
                {
                    var fieldName = protocol.GetAt(ix, 3).Replace("-", "").ExceptBlanks();
                    if (fieldName == "nofields")
                        continue;

                    var fieldType = protocol.GetAt(ix, 4).Replace("-", "").ExceptBlanks();
                    var note = protocol.GetAt(ix, 5);

                    builder.AddField(fieldName, ReplaceTypes(fieldType));

#if SAVE_ALL_TYPES
                    if (!fieldTypes.Contains(fieldType))
                        fieldTypes.Add(fieldType);
#endif
                }

                generatorList.Add(builder);
            }

#if SAVE_ALL_TYPES
            var fieldTypesBuilder = new StringBuilder();
            foreach (string str in fieldTypes)
                fieldTypesBuilder.AppendLine(str);

            var fileTypeFile = FileSystemWrapper.OutputFolder.CreateFileAsync("FileTypes.txt", CreationCollisionOption.ReplaceExisting).Result;
            using (StreamWriter stream = new StreamWriter(fileTypeFile.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).Result))
                stream.Write(fieldTypesBuilder.ToString());
#endif

            foreach (var generator in generatorList)
            {
                var folder = FileSystemWrapper.OutputFolder.CreateFolderAsync("Generated", CreationCollisionOption.OpenIfExists).Result;

                if (generator.BoundTo != BoundTo.NONE)
                    folder = folder.CreateFolderAsync(generator.BoundTo.ToString(), CreationCollisionOption.OpenIfExists).Result;
                if (generator.State != State.NONE)
                    folder = folder.CreateFolderAsync(generator.State.ToString(), CreationCollisionOption.OpenIfExists).Result;

                var classFile = folder.CreateFileAsync($"{generator.ClassName}.cs", CreationCollisionOption.ReplaceExisting).Result;
                using (StreamWriter stream = new StreamWriter(classFile.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).Result))
                    stream.Write(generator.GenerateClass());
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
#if SAVE_ALL_TYPES
            List <string> fieldTypes = new List <string>();
#endif
            var generatorList = new List <ProtobufPacketGenerator>();


            // Those ID's are not supported and should be parsed manually.
            var bannedIDs = new int[] { 0x20, 0x22, 0x26, 0x34, 0x37, 0x38, 0x44, 0x45 };

            var protocols = GetProtocolTables();
            foreach (var protocol in protocols)
            {
                var className = $"{protocol.Name.Replace("-", "").Replace("_", "")}Packet";
                var id        = new VarInt(int.Parse(protocol.GetAt(0, 0).Remove(0, 2), NumberStyles.AllowHexSpecifier));
                if (bannedIDs.Contains(id))
                {
                    continue;
                }

                var state   = (State)Enum.Parse(typeof(State), protocol.GetAt(0, 1).ExceptBlanks());
                var boundTo = (BoundTo)Enum.Parse(typeof(BoundTo), protocol.GetAt(0, 2).ExceptBlanks());

                var builder = new ProtobufPacketGenerator(className, id, boundTo, state);

                for (int ix = 0; ix < protocol.Height; ix++)
                {
                    var fieldName = protocol.GetAt(ix, 3).Replace("-", "").ExceptBlanks();
                    if (fieldName == "nofields")
                    {
                        continue;
                    }

                    var fieldType = protocol.GetAt(ix, 4).Replace("-", "").ExceptBlanks();
                    var note      = protocol.GetAt(ix, 5);

                    builder.AddField(fieldName, ReplaceTypes(fieldType));

#if SAVE_ALL_TYPES
                    if (!fieldTypes.Contains(fieldType))
                    {
                        fieldTypes.Add(fieldType);
                    }
#endif
                }

                generatorList.Add(builder);
            }

#if SAVE_ALL_TYPES
            var fieldTypesBuilder = new StringBuilder();
            foreach (string str in fieldTypes)
            {
                fieldTypesBuilder.AppendLine(str);
            }

            var fileTypeFile = FileSystemWrapper.OutputFolder.CreateFileAsync("FileTypes.txt", CreationCollisionOption.ReplaceExisting).Result;
            using (StreamWriter stream = new StreamWriter(fileTypeFile.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).Result))
                stream.Write(fieldTypesBuilder.ToString());
#endif

            foreach (var generator in generatorList)
            {
                var folder = FileSystemWrapper.OutputFolder.CreateFolderAsync("Generated", CreationCollisionOption.OpenIfExists).Result;

                if (generator.BoundTo != BoundTo.NONE)
                {
                    folder = folder.CreateFolderAsync(generator.BoundTo.ToString(), CreationCollisionOption.OpenIfExists).Result;
                }
                if (generator.State != State.NONE)
                {
                    folder = folder.CreateFolderAsync(generator.State.ToString(), CreationCollisionOption.OpenIfExists).Result;
                }

                var classFile = folder.CreateFileAsync($"{generator.ClassName}.cs", CreationCollisionOption.ReplaceExisting).Result;
                using (StreamWriter stream = new StreamWriter(classFile.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).Result))
                    stream.Write(generator.GenerateClass());
            }
        }