Пример #1
0
        /// <summary>
        /// Casts a scalar node to a C# object following the rules:
        /// Single-Length, single quoted strings become char
        /// Else, quoted (single or double), literal or folded values become strings
        /// Else, plain values equal to the string "null" become null
        /// Else, plain values that start with "~" become scene resources
        /// Else, plain values that can be cast to int become int
        /// Else, plain values that can be cast to float become float
        /// Else, plain values are cast to a object using the parameterless constructor
        /// (For parameterized constructors, see casting a mapping node)
        /// </summary>
        /// <param name="node"></param>
        private object parseArgument(YamlScalarNode node)
        {
            switch (node.Style)
            {
            case ScalarStyle.SingleQuoted:
                if (node.String().Length == 1)
                {
                    return(node.String()[0]);
                }
                else
                {
                    return(node.String());
                }

            case ScalarStyle.DoubleQuoted:
            case ScalarStyle.Literal:
            case ScalarStyle.Folded:
                return(node.String());

            case ScalarStyle.Plain:
                if (node.String() == "null")
                {
                    return(null);
                }

                if (node.String().StartsWith("~"))
                {
                    return(new ObjectModel
                    {
                        Type = typeof(Resource),
                        CtorArgs = new Object[] { node.String().Substring(1) }
                    });
                }

                int   i;
                float f;
                if (node.TryInt(out i))
                {
                    return(i);
                }
                if (node.TryFloat(out f))
                {
                    return(f);
                }

                return(new ObjectModel
                {
                    Type = RegisteredTypes.GetType(node.String()),
                    CtorArgs = new object[] { }
                });

            default:
                throw new Exception("Trying to parse arguments from invalid node!");
            }
        }
 public static bool TryFloat(this YamlScalarNode self, out float val)
 {
     return(float.TryParse(self.String(), NumberStyles.Float, CultureInfo.InvariantCulture, out val));
 }
 public static bool TryInt(this YamlScalarNode self, out int val)
 {
     return(int.TryParse(self.String(), NumberStyles.Number, CultureInfo.InvariantCulture, out val));
 }