private void ApplyOrderToCategories(SettingCategoryMetaData group, List <string> categories)
        {
            int order = categories.Count + 1;

            foreach (string category in categories)
            {
                foreach (ISettingMetaData var in group.items)
                {
                    if (var.Category != category)
                    {
                        continue;
                    }

                    if (var is SettingValue)
                    {
                        (var as SettingValue).CategoryPrefix = order;
                    }
                    else if (var is SettingCategoryMetaData)
                    {
                        (var as SettingCategoryMetaData).CategoryPrefix = order;
                    }
                }

                order--;
            }
        }
 private static void GenerateCategoryMember(StringBuilder Members, SettingCategoryMetaData subCategory)
 {
     Members.AppendLine("/// <summary>");
     Members.AppendLine("/// Gets " + subCategory.UIText + " settings category.");
     Members.AppendLine("/// </summary>");
     Members.AppendLine(string.Format("public SettingsCategoryTypes.{0} {0} {{ get; private set; }}", subCategory.MemberName));
     Members.AppendLine();
 }
        private static void GenerateCategoryType(string parentClassName, SettingCategoryMetaData category, ref List <string> groupTypesDefinitions)
        {
            Dictionary <string, string> replacements = new Dictionary <string, string>();

            replacements.Add("[$ParentCategroryClassName]", parentClassName);

            GenerateMembers(category, false, groupTypesDefinitions, replacements, category.MemberName);

            groupTypesDefinitions.Add(DoReplacements(replacements, CategoryTemplate));
        }
        private void Load(XmlDocument doc)
        {
            try
            {
                XmlNode node = doc.DocumentElement;

                if (node == null)
                {
                    throw new Exception("Missing root node.");
                }

                NoItemSelectedText = node.SelectSingleNode("NoItemText").InnerText.Trim();

                List <string> categories = new List <string>();

                foreach (XmlNode categoryNode in node.SelectNodes("Category"))
                {
                    string subCategoryMemberName = Helper.GetAttributeValue(categoryNode, "MemberName", "Uncategorised");
                    string subCategoryText       = Helper.GetAttributeValue(categoryNode, "UIText", "Uncategorised");
                    bool   isHidden = Helper.GetAttributeValue(categoryNode, "Hidden", false);

                    SettingCategoryMetaData settingGroup = new SettingCategoryMetaData()
                    {
                        UIText                 = subCategoryText,
                        MemberName             = subCategoryMemberName,
                        Category               = subCategoryText, // string.Empty
                        CategoryFullMemberName = subCategoryMemberName,
                        IsHidden               = isHidden,
                    };

                    if (categories.Contains(subCategoryText) == false)
                    {
                        categories.Add(subCategoryText);
                    }

                    List <string> categoriesList = new List <string>();

                    LoadCategory(settingGroup, categoriesList, categoryNode);

                    ApplyOrderToCategories(settingGroup, categoriesList);

                    this.items.Add(settingGroup);
                }

                ApplyOrderToCategories(this, categories);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not load IMU settings from XML.", ex);
            }
        }
 private static void GenerateCategoryContruction(StringBuilder Members_Initiation, SettingCategoryMetaData subCategory)
 {
     Members_Initiation.AppendLine(string.Format("{0} = new SettingsCategoryTypes.{0}(this);", subCategory.MemberName));
 }
 private static void GenerateCategoryCopy(StringBuilder Members_Copy, SettingCategoryMetaData subCategory)
 {
     Members_Copy.AppendLine(string.Format("{0}.CopyTo(other.{0});", subCategory.MemberName));
 }
        private static void GenerateMembers(SettingCategoryMetaData category, bool isRoot, List <string> groupTypesDefinitions, Dictionary <string, string> replacements, string className)
        {
            RC.WriteLine(ConsoleThemeColor.TitleText, " - " + category.UIText);

            StringBuilder Members             = new StringBuilder();
            StringBuilder Members_Initiation  = new StringBuilder();
            StringBuilder Settings_Initiation = new StringBuilder();
            StringBuilder Members_Attach      = new StringBuilder();
            StringBuilder Members_Copy        = new StringBuilder();

            List <ISettingMetaData> items = new List <ISettingMetaData>();

            items.AddRange(category.Items);

            // if we are in the root then all the settings
            if (isRoot == true)
            {
                foreach (SettingValue value in (category as SettingsMetaData).AllVariables)
                {
                    items.Add(value);
                }
            }

            foreach (ISettingMetaData item in items)
            {
                if (item is SettingValue)
                {
                    SettingValue value = item as SettingValue;

                    GenerateSettingMember(Members, value);

                    if (isRoot == true)
                    {
                        GenerateSettingMemberConstruction(Settings_Initiation, value);
                    }
                    else
                    {
                        GenerateSettingMemberAttach(Members_Attach, value);
                        GenerateSettingMemberCopy(Members_Copy, value);
                    }
                }
                else if (item is SettingCategoryMetaData)
                {
                    SettingCategoryMetaData subCategory = item as SettingCategoryMetaData;

                    GenerateCategoryType(className, subCategory, ref groupTypesDefinitions);

                    GenerateCategoryMember(Members, subCategory);

                    GenerateCategoryContruction(Members_Initiation, subCategory);

                    GenerateCategoryCopy(Members_Copy, subCategory);
                }
            }

            if (Settings_Initiation.Length > 0)
            {
                Members_Initiation.AppendLine();
                Members_Initiation.Append(Settings_Initiation.ToString());
            }

            foreach (ISettingMetaData item in category.Items)
            {
                if (item is SettingCategoryMetaData)
                {
                    Members_Attach.AppendLine(string.Format("{0}.AttachSettings(settings);", item.MemberName));
                }
            }

            Members_Attach.AppendLine();

            foreach (ISettingMetaData item in category.Items)
            {
                if (item is SettingCategoryMetaData)
                {
                    //if ((isRoot == false) || (item as ImuSettingCategoryMetaData).IsHidden == false))
                    {
                        Members_Attach.AppendLine(string.Format("Add({0});", item.MemberName));
                    }
                }
                else if (isRoot == false)
                {
                    Members_Attach.AppendLine(string.Format("Add(settings.{0});", item.MemberName));
                }
                else
                {
                    // do nothing?
                }
            }

            replacements.Add("[$CategroryClassName]", className);
            replacements.Add("[$CategroryText]", category.UIText);
            replacements.Add("[$CategroryIsHidden]", category.IsHidden.ToString().ToLowerInvariant());
            replacements.Add("[$CategoryPrefix]", category.CategoryPrefix.ToString());
            replacements.Add("[$Members]", PrefixLines(new string[] { Members.ToString() }, '\t', 1));
            replacements.Add("[$Members_Initiation]", PrefixLines(new string[] { Members_Initiation.ToString() }, '\t', 2));
            replacements.Add("[$Members_Attach]", PrefixLines(new string[] { Members_Attach.ToString() }, '\t', 2));
            replacements.Add("[$Members_Copy]", PrefixLines(new string[] { Members_Copy.ToString() }, '\t', 2));
        }
        private void LoadCategory(SettingCategoryMetaData category, List <string> categories, XmlNode node)
        {
            string categoryText = Helper.GetAttributeValue(node, "UIText", "Uncategorised");

            if (categories.Contains(categoryText) == false)
            {
                categories.Add(categoryText);
            }

            foreach (XmlNode subNode in node.ChildNodes)
            {
                if (subNode.Name == "Setting")
                {
                    XmlNode settingNode = subNode;

                    SettingValueType type = Helper.GetAttributeValue(settingNode, "Type", (SettingValueType)(-1));

                    string memberName         = Helper.GetAttributeValue(settingNode, "MemberName", null);
                    string uiText             = Helper.GetAttributeValue(settingNode, "UIText", null);
                    string documentationTitle = Helper.GetAttributeValue(settingNode, "DocumentationTitle", uiText);

                    string oscAddress = Helper.GetAttributeValue(settingNode, "OscAddress", null);
                    bool   isReadonly = Helper.GetAttributeValue(settingNode, "Readonly", false);
                    bool   isHidden   = Helper.GetAttributeValue(settingNode, "Hidden", false);

                    string documentationBody = settingNode.InnerText.Trim();

                    if (Helper.IsNullOrEmpty(memberName) == true)
                    {
                        throw new Exception("One or more setting does not have a \"MemberName\" attribute.");
                    }

                    if (Helper.IsNullOrEmpty(uiText) == true)
                    {
                        throw new Exception("One or more setting does not have a \"Text\" attribute.");
                    }

                    if (Helper.IsNullOrEmpty(oscAddress) == true)
                    {
                        throw new Exception("One or more setting does not have a \"OscAddress\" attribute.");
                    }

                    SettingValue var = new SettingValue()
                    {
                        Category               = categoryText,
                        MemberName             = memberName,
                        UIText                 = uiText,
                        DocumentationTitle     = documentationTitle,
                        DocumentationBody      = documentationBody,
                        OscAddress             = oscAddress,
                        IsReadOnly             = isReadonly,
                        IsHidden               = isHidden,
                        SettingValueType       = type,
                        CategoryFullMemberName = category.CategoryFullMemberName,
                    };

                    allVariables.Add(var);

                    variableLookup.Add(var.OscAddress, var);

                    category.variables.Add(var);
                    category.items.Add(var);
                }
                else if (subNode.Name == "Category")
                {
                    XmlNode categoryNode = subNode;

                    string subCategoryMemberName = Helper.GetAttributeValue(categoryNode, "MemberName", "Uncategorised");
                    string subCategoryText       = Helper.GetAttributeValue(categoryNode, "UIText", "Uncategorised");
                    bool   isHidden = Helper.GetAttributeValue(categoryNode, "Hidden", false);

                    SettingCategoryMetaData settingCategory = new SettingCategoryMetaData()
                    {
                        UIText                 = subCategoryText,
                        MemberName             = subCategoryMemberName,
                        Category               = categoryText,
                        CategoryFullMemberName = category.CategoryFullMemberName + "." + subCategoryMemberName,
                        IsHidden               = isHidden,
                    };

                    List <string> categoriesList = new List <string>();

                    LoadCategory(settingCategory, categoriesList, categoryNode);

                    ApplyOrderToCategories(settingCategory, categoriesList);

                    category.items.Add(settingCategory);
                }
            }
        }