Exemple #1
0
        /// <summary>
        /// Recursive method to save the Option's value and its variants'
        /// and children's values to the node.
        /// </summary>
        private void SaveNode(ValueStore.Node node, Option option, bool isDefaultNode = false)
        {
            if (!isDefaultNode && option.IsDefaultVariant)
            {
                var defaultVariant = node.GetOrCreateVariant(option.VariantDefaultParameter);
                SaveNode(defaultVariant, option, isDefaultNode: true);

                for (int i = node.variants.Count - 1; i >= 0; i--)
                {
                    if (option.GetVariant(node.variants[i].name, create:false) == null)
                    {
                        node.RemoveVariant(i);
                    }
                }

                foreach (var variantOption in option.Variants)
                {
                    var variantNode = node.GetOrCreateVariant(variantOption.VariantParameter);
                    SaveNode(variantNode, variantOption);
                }
            }
            else
            {
                node.Value = option.Save();

                foreach (var childOption in option.Children)
                {
                    var childNode = node.GetOrCreateChild(childOption.Name);
                    SaveNode(childNode, childOption);
                }
            }
        }
Exemple #2
0
        static ValueStore.Node GetNodeRecursive(ValueStore.Node current, Match match, int index)
        {
            Capture next;
            var     groupIndex = FindNextCapture(match, index, out next);

            if (groupIndex < 0)
            {
                Debug.LogWarning("Unexpected end of captures in match: " + match);
                return(current);
            }
            else if (groupIndex == 2)
            {
                var child = current.GetOrCreateChild(next.Value);
                return(GetNodeRecursive(child, match, next.Index + next.Length));
            }
            else if (groupIndex == 3 || groupIndex == 4)
            {
                var isQuoted = (groupIndex == 3);
                var variant  = current.GetOrCreateVariant(ProcessQuotedString(next.Value, isQuoted));
                return(GetNodeRecursive(variant, match, next.Index + next.Length));
            }
            else
            {
                return(current);
            }
        }
Exemple #3
0
        /// <summary>
        /// Recursive method to apply a node with all its variants and
        /// children to an Option.
        /// </summary>
        private void LoadNode(Option option, ValueStore.Node node, bool isDefaultNode = false)
        {
            if (!isDefaultNode && option.IsDefaultVariant)
            {
                // Load default variant sub-node into the main variant option
                // (The container node's value is ignored)
                var defaultNode = node.GetVariant(option.VariantDefaultParameter);
                if (defaultNode != null)
                {
                    LoadNode(option, defaultNode, isDefaultNode: true);
                }

                if (node.VariantCount > 0)
                {
                    foreach (var variantNode in node.Variants)
                    {
                        if (variantNode.Name.EqualsIgnoringCase(option.VariantDefaultParameter))
                        {
                            continue;
                        }
                        loaded.Add(variantNode.Name);
                        var variantOption = option.GetVariant(variantNode.Name);
                        LoadNode(variantOption, variantNode);
                    }

                    // Remove variants that don't exist in the node
                    variantsCopy.AddRange(option.Variants);
                    foreach (var variantOption in variantsCopy)
                    {
                        if (!loaded.Contains(variantOption.VariantParameter))
                        {
                            option.RemoveVariant(variantOption);
                        }
                    }
                    variantsCopy.Clear();
                    loaded.Clear();
                }
                else
                {
                    option.ClearVariants();
                }
            }
            else
            {
                option.Load(node.Value ?? string.Empty);

                if (node.ChildCount > 0)
                {
                    foreach (var childNode in node.Children)
                    {
                        var childOption = option.GetChild(childNode.Name);
                        if (childOption != null)
                        {
                            LoadNode(childOption, childNode);
                        }
                    }
                }
            }
        }
Exemple #4
0
        protected void CleanStoreRecursive(ValueStore.Node node, Option option, bool isDefaultNode = false)
        {
            if (!isDefaultNode && option.IsDefaultVariant)
            {
                // Value and children are stored in the default parameter sub-node
                node.Value = null;
                node.ClearChildren();

                if (node.VariantCount > 0)
                {
                    foreach (var variant in node.Variants.ToArray())
                    {
                        if (variant.Name.EqualsIgnoringCase(option.VariantDefaultParameter))
                        {
                            CleanStoreRecursive(variant, option, isDefaultNode: true);
                            continue;
                        }
                        var variantOption = option.GetVariant(variant.Name, create: false);
                        if (variantOption == null)
                        {
                            node.RemoveVariant(variant.Name);
                        }
                        else
                        {
                            CleanStoreRecursive(variant, variantOption);
                        }
                    }
                }
            }
            else
            {
                node.ClearVariants();
            }

            if (node.ChildCount > 0)
            {
                foreach (var child in node.Children.ToArray())
                {
                    var childOption = option.GetChild(child.Name);
                    if (childOption == null)
                    {
                        node.RemoveChild(child.Name);
                    }
                    else
                    {
                        CleanStoreRecursive(child, childOption);
                    }
                }
            }
        }
Exemple #5
0
        static void SaveIniRecursive(StringBuilder output, string path, ValueStore.Node node, bool isVariantChild)
        {
            if (!isVariantChild)
            {
                path += node.name;
            }
            else
            {
                path += "[" + QuoteParameterIfNeeded(node.name) + "]";
            }

            if (!string.IsNullOrEmpty(node.value))
            {
                output.Append(path);
                output.Append(" = ");
                output.Append(QuoteValueIfNeeded(node.value));
                output.Append("\n");
            }

            if (node.VariantCount > 0)
            {
                foreach (var variant in node.variants)
                {
                    SaveIniRecursive(output, path, variant, true);
                }
            }

            if (node.ChildCount > 0)
            {
                var childPath = path + ".";
                foreach (var child in node.children)
                {
                    SaveIniRecursive(output, childPath, child, false);
                }
            }
        }