Example #1
0
    public void ExternalStringDidChange(string json)
    {
        StringDefinition definition = StringDefinition.OfJson(json);

        ObservableStringOfKey(definition.FullKey).SetValue(definition.Value);
    }
        //-----------------------------------------------------------------------
        private static DataDefinition CreateDefinitionFromElement(XElement el, DataDefinition existing)
        {
            if (!el.HasElements)
            {
                // we are a primitive

                // are we a number?
                float fval;
                bool  isFloat = float.TryParse(el.Value, out fval);

                int  ival;
                bool isInt = int.TryParse(el.Value, out ival);

                bool bval;
                bool isBool = bool.TryParse(el.Value, out bval);

                if (existing != null)
                {
                    if (isFloat || isInt)
                    {
                        if (existing is NumberDefinition)
                        {
                            var def = existing as NumberDefinition;
                            if (!isInt)
                            {
                                def.UseIntegers = false;
                            }

                            if (fval < def.MinValue)
                            {
                                def.MinValue = fval;
                            }
                            if (fval > def.MaxValue)
                            {
                                def.MaxValue = fval;
                            }

                            return(def);
                        }
                        else
                        {
                            // we are actually a string
                            var def = new StringDefinition();
                            def.Name = el.Value.ToString();
                            return(def);
                        }
                    }
                    else if (isBool)
                    {
                        if (existing is BooleanDefinition)
                        {
                            return(existing);
                        }
                        else
                        {
                            // we are actually a string
                            var def = new StringDefinition();
                            def.Name = el.Value.ToString();
                            return(def);
                        }
                    }
                    else
                    {
                        if (existing is EnumDefinition)
                        {
                            if (el.Value.Contains("/") || el.Value.Contains(@"\\"))
                            {
                                var def = new FileDefinition();
                                def.Name = el.Name.ToString();

                                return(def);
                            }
                            else if (el.Value.Contains(" "))
                            {
                                var def = new StringDefinition();
                                def.Name = el.Name.ToString();

                                return(def);
                            }
                            else
                            {
                                var def = existing as EnumDefinition;
                                if (!def.EnumValues.Contains(el.Value))
                                {
                                    def.EnumValues.Add(el.Value);
                                }

                                return(def);
                            }
                        }
                        else
                        {
                            return(existing);
                        }
                    }
                }
                else
                {
                    if (isFloat || isInt)
                    {
                        var def = new NumberDefinition();
                        def.Name        = el.Name.ToString();
                        def.UseIntegers = isInt;
                        def.MinValue    = fval;
                        def.MaxValue    = fval;

                        return(def);
                    }
                    else if (isBool)
                    {
                        var def = new BooleanDefinition();
                        def.Name = el.Name.ToString();

                        return(def);
                    }
                    else
                    {
                        if (el.Value.Contains("/") || el.Value.Contains(@"\\"))
                        {
                            var def = new FileDefinition();
                            def.Name = el.Name.ToString();

                            return(def);
                        }
                        else if (el.Value.Contains(" "))
                        {
                            var def = new StringDefinition();
                            def.Name = el.Name.ToString();

                            return(def);
                        }
                        else
                        {
                            var def = new EnumDefinition();
                            def.Name       = el.Name.ToString();
                            def.EnumValues = new List <string>();
                            def.EnumValues.Add(el.Value);

                            return(def);
                        }
                    }
                }
            }
            else if (el.Elements().Any(e => e.Name.ToString() != el.Elements().First().Name.ToString()))
            {
                // we are a struct

                if (existing != null)
                {
                    var def = existing as StructDefinition;

                    if (def != null)
                    {
                        foreach (var cel in el.Elements())
                        {
                            if (el.Elements(cel.Name).Count() > 1)
                            {
                                // this is actually a collection
                                var existingChild           = def.Children.FirstOrDefault(e => e.Name == cel.Name.ToString());
                                CollectionDefinition coldef = null;

                                if (existingChild == null)
                                {
                                    coldef      = new CollectionDefinition();
                                    coldef.Name = cel.Name.ToString();
                                    coldef.ChildDefinitions.Add(new CollectionChildDefinition());
                                    coldef.ChildDefinitions[0].Name = cel.Name.ToString();

                                    def.Children.Add(coldef);
                                }
                                else if (existingChild is CollectionDefinition)
                                {
                                    coldef = existingChild as CollectionDefinition;
                                }
                                else
                                {
                                    coldef      = new CollectionDefinition();
                                    coldef.Name = cel.Name.ToString();
                                    coldef.ChildDefinitions.Add(new CollectionChildDefinition());
                                    coldef.ChildDefinitions[0].Name = cel.Name.ToString();
                                    coldef.ChildDefinitions[0].WrappedDefinition = existingChild;

                                    var index = def.Children.IndexOf(existingChild);
                                    def.Children[index] = coldef;
                                }

                                coldef.ChildDefinitions[0].WrappedDefinition = CreateDefinitionFromElement(cel, coldef.ChildDefinitions[0].WrappedDefinition);
                            }
                            else
                            {
                                // find existing child
                                var ec = def.Children.FirstOrDefault(e => e.Name == cel.Name.ToString());
                                if (ec != null)
                                {
                                    if (ec is CollectionDefinition)
                                    {
                                        var actualDef = CreateDefinitionFromElement(cel, null);
                                        if (actualDef is CollectionDefinition)
                                        {
                                            var cdef = CreateDefinitionFromElement(cel, ec);
                                            def.Children[def.Children.IndexOf(ec)] = cdef;
                                        }
                                        else
                                        {
                                            var coldef = ec as CollectionDefinition;

                                            coldef.ChildDefinitions[0].WrappedDefinition = CreateDefinitionFromElement(cel, coldef.ChildDefinitions[0].WrappedDefinition);
                                        }
                                    }
                                    else
                                    {
                                        var cdef = CreateDefinitionFromElement(cel, ec);
                                        def.Children[def.Children.IndexOf(ec)] = cdef;
                                    }
                                }
                                else
                                {
                                    var cdef = CreateDefinitionFromElement(cel, null);
                                    def.Children.Add(cdef);
                                }
                            }
                        }
                    }

                    return(existing);
                }
                else
                {
                    var def = new StructDefinition();
                    def.Name = el.Name.ToString();

                    foreach (var cel in el.Elements())
                    {
                        if (el.Elements(cel.Name).Count() > 1)
                        {
                            // this is actually a collection

                            CollectionDefinition coldef = def.Children.FirstOrDefault(e => e.Name == cel.Name.ToString()) as CollectionDefinition;
                            if (coldef == null)
                            {
                                coldef      = new CollectionDefinition();
                                coldef.Name = cel.Name.ToString();
                                coldef.ChildDefinitions.Add(new CollectionChildDefinition());
                                coldef.ChildDefinitions[0].Name = cel.Name.ToString();

                                def.Children.Add(coldef);
                            }

                            coldef.ChildDefinitions[0].WrappedDefinition = CreateDefinitionFromElement(cel, coldef.ChildDefinitions[0].WrappedDefinition);
                        }
                        else
                        {
                            var cdef = CreateDefinitionFromElement(cel, null);
                            def.Children.Add(cdef);
                        }
                    }

                    return(def);
                }
            }
            else
            {
                // we are a collection
                if (existing != null)
                {
                    if (existing is CollectionDefinition)
                    {
                        var def = existing as CollectionDefinition;

                        foreach (var cel in el.Elements())
                        {
                            def.ChildDefinitions[0].WrappedDefinition = CreateDefinitionFromElement(cel, def.ChildDefinitions[0].WrappedDefinition);
                        }

                        return(def);
                    }
                    else
                    {
                        var def = new CollectionDefinition();
                        def.Name = el.Name.ToString();
                        def.ChildDefinitions.Add(new CollectionChildDefinition());
                        def.ChildDefinitions[0].Name = el.Elements().First().Name.ToString();
                        def.ChildDefinitions[0].WrappedDefinition = existing;

                        foreach (var cel in el.Elements())
                        {
                            def.ChildDefinitions[0].WrappedDefinition = CreateDefinitionFromElement(cel, def.ChildDefinitions[0].WrappedDefinition);
                        }

                        return(def);
                    }
                }
                else
                {
                    var def = new CollectionDefinition();
                    def.Name = el.Name.ToString();
                    def.ChildDefinitions.Add(new CollectionChildDefinition());
                    def.ChildDefinitions[0].Name = el.Elements().First().Name.ToString();

                    foreach (var cel in el.Elements())
                    {
                        def.ChildDefinitions[0].WrappedDefinition = CreateDefinitionFromElement(cel, def.ChildDefinitions[0].WrappedDefinition);
                    }

                    return(def);
                }
            }

            throw new Exception("Failed to parse element: " + el.Name);
        }
Example #3
0
 public StringParameter(Int16 id, IParameterManager manager, StringDefinition typeDefinition)
     : base(id, manager, typeDefinition)
 {
 }