Esempio n. 1
0
 public override void Visit(BooleanValueSyntax boolValue)
 {
     if (_currentValue != null)
     {
         throw new InvalidOperationException("A Boolean value must be at a leaf.");
     }
     _currentValue = new Value(boolValue.Value);
 }
Esempio n. 2
0
        private static void SaveClass(Type type, DocumentSyntax document, KeySyntax key = null)
        {
            key ??= new KeySyntax(type.Name);

            var fields   = type.GetFields(StaticFlags);
            var children = type.GetNestedTypes(StaticFlags);

            var table      = new TableSyntax(key);
            var tableItems = table.Items;

            foreach (var field in fields)
            {
                if (field.IsPrivate || field.IsInitOnly || !field.IsStatic || field.IsLiteral)
                {
                    continue;
                }

                if (field.GetCustomAttribute <Config.ConfigIgnoreAttribute>() != null)
                {
                    continue;
                }

                ValueSyntax value      = null;
                object      fieldValue = field.GetValue(null);

                switch (fieldValue)
                {
                case string v:
                    value = new StringValueSyntax(v);
                    break;

                case sbyte v:
                    value = new IntegerValueSyntax(v);
                    break;

                case byte v:
                    value = new IntegerValueSyntax(v);
                    break;

                case short v:
                    value = new IntegerValueSyntax(v);
                    break;

                case ushort v:
                    value = new IntegerValueSyntax(v);
                    break;

                case int v:
                    value = new IntegerValueSyntax(v);
                    break;

                case uint v:
                    value = new IntegerValueSyntax(v);
                    break;

                case ulong v:
                    value = new IntegerValueSyntax(unchecked ((long)v));
                    break;

                case float v:
                    value = new FloatValueSyntax(v);
                    break;

                case double v:
                    value = new FloatValueSyntax(v);
                    break;

                case bool v:
                    value = new BooleanValueSyntax(v);
                    break;

                default:
                    if (fieldValue is List <string> slist)
                    {
                        value = new ArraySyntax(slist.ToArray());
                    }
                    else if (fieldValue is List <int> ilist)
                    {
                        value = new ArraySyntax(ilist.ToArray());
                    }
                    else if (fieldValue.GetType().IsEnum)
                    {
                        value = new StringValueSyntax(fieldValue.GetType().GetEnumName(fieldValue));
                    }
                    break;
                }

                if (value == null)
                {
                    continue;
                }

                var keyValue = new KeyValueSyntax(
                    field.Name,
                    value
                    );

                //if (field.GetAttribute<Config.CommentAttribute>(out var attribute)) {
                //keyValue.GetChildren(Math.Max(0, keyValue.ChildrenCount - 2)).AddComment(attribute.Message);
                //}

                tableItems.Add(keyValue);
            }

            if (table.Items.ChildrenCount != 0)
            {
                document.Tables.Add(table);
            }

            foreach (var child in children)
            {
                if (child.IsNestedPrivate)
                {
                    continue;
                }
                if (child.GetCustomAttribute <Config.ConfigIgnoreAttribute>() != null)
                {
                    continue;
                }
                var childKey  = new KeySyntax(typeof(Config).Name);
                var parentKey = key.ToString().Split('.');
                if (parentKey.Length != 0)
                {
                    parentKey[0] = null;
                }
                foreach (var subKey in parentKey)
                {
                    if (subKey == null)
                    {
                        continue;
                    }
                    childKey.DotKeys.Add(new DottedKeyItemSyntax(subKey));
                }
                childKey.DotKeys.Add(new DottedKeyItemSyntax(child.Name));
                SaveClass(child, document, childKey);
            }
        }
 public override void Visit(BooleanValueSyntax boolValue)
 {
     _currentValue = boolValue.Value;
 }