Example #1
0
        public static void WriteEnum <T>(
            string enumName,
            string enumSuffix,
            string group,
            IEnumerable <T> values,
            bool hexadecimal,
            Validator <T> validator,
            GetField <T> getNameDelegate,
            GetField <T> getCommentDelegate,
            GetField <T, string> getDuplNameDelegate,
            GetField <T> getIdDelegate)
        {
            Init();
            var dir = Path.Combine(Dir, group);

            Directory.CreateDirectory(dir);

            var file = Path.Combine(dir, enumName + ".cs");

            Console.Write("Writing enum {0} to {1}...", enumName, new DirectoryInfo(file).FullName);

            var first = true;

            using (var writer = new CodeFileWriter(file,
                                                   "WCell.Constants." + group, enumName, "enum", enumSuffix))
            {
                try
                {
                    var names = new Dictionary <string, int>(values.Count());

                    foreach (T item in values)
                    {
                        if (item == null || item.Equals(default(T)) || !validator(item))
                        {
                            continue;
                        }

                        var name = getNameDelegate(item);

                        if (name == null)
                        {
                            throw new Exception(string.Format("Name for Item {0} in {1}/{2} was null.", item, group, enumName));
                        }

                        name = BeautifyName(name);

                        int count;
                        if (!names.TryGetValue(name, out count))
                        {
                            names.Add(name, 1);
                        }
                        else
                        {
                            names.Remove(name);
                            names.Add(name, ++count);

                            string duplName = null;
                            if (getDuplNameDelegate != null)
                            {
                                duplName = getDuplNameDelegate(item, name);
                            }

                            if (duplName != null)
                            {
                                name = duplName;
                            }
                            else
                            {
                                name = name + "_" + count;
                            }
                        }

                        var val = getIdDelegate(item);
                        if (hexadecimal)
                        {
                            int ival;
                            if (int.TryParse(val, out ival))
                            {
                                val = string.Format("0x{0:x}", ival);
                            }
                        }

                        if (first)
                        {
                            first = false;
                            long id;
                            if (!long.TryParse(val, out id))
                            {
                                throw new InvalidDataException("Invalid ID was not numeric: " + val);
                            }

                            if (id > 0)
                            {
                                writer.WriteLine("None = 0,");
                            }
                        }

                        string comment;
                        if (getCommentDelegate != null)
                        {
                            comment = getCommentDelegate(item);
                            if (comment != null)
                            {
                                var commentLines = comment.Split(new[] { "\n", "\r\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
                                if (commentLines.Length > 0)
                                {
                                    writer.StartSummary();
                                    foreach (string line in commentLines)
                                    {
                                        writer.WriteXmlCommentLine(line);
                                    }
                                    writer.EndSummary();
                                }
                            }
                        }
                        writer.WriteLine(string.Format("{0} = {1},", name, val));
                    }

                    writer.WriteLine("End");

                    writer.Finish();

                    Console.WriteLine(" Done.");
                }
                catch (Exception ex)
                {
                    writer.OnException(ex);
                }
            }
        }