Beispiel #1
0
        public Widget LoadWidget(WidgetArgs args, Widget parent, MiniYamlNode node)
        {
            if (!args.ContainsKey("modRules"))
            {
                args = new WidgetArgs(args)
                {
                    { "modRules", modData.DefaultRules }
                }
            }
            ;

            var widget = NewWidget(node.Key, args);

            if (parent != null)
            {
                parent.AddChild(widget);
            }

            if (node.Key.Contains("@"))
            {
                FieldLoader.LoadField(widget, "Id", node.Key.Split('@')[1]);
            }

            foreach (var child in node.Value.Nodes)
            {
                if (child.Key != "Children")
                {
                    FieldLoader.LoadField(widget, child.Key, child.Value.Value);
                }
            }

            widget.Initialize(args);

            foreach (var child in node.Value.Nodes)
            {
                if (child.Key == "Children")
                {
                    foreach (var c in child.Value.Nodes)
                    {
                        LoadWidget(args, widget, c);
                    }
                }
            }

            widget.PostInit(args);
            return(widget);
        }
Beispiel #2
0
        public void Deserialize(Map map, List <MiniYamlNode> nodes)
        {
            var node = nodes.FirstOrDefault(n => n.Key == key);

            if (node == null)
            {
                if (required)
                {
                    throw new YamlException("Required field `{0}` not found in map.yaml".F(key));
                }
                return;
            }

            if (field != null)
            {
                if (type == Type.NodeList)
                {
                    field.SetValue(map, node.Value.Nodes);
                }
                else if (type == Type.MiniYaml)
                {
                    field.SetValue(map, node.Value);
                }
                else
                {
                    FieldLoader.LoadField(map, fieldName, node.Value.Value);
                }
            }

            if (property != null)
            {
                if (type == Type.NodeList)
                {
                    property.SetValue(map, node.Value.Nodes, null);
                }
                else if (type == Type.MiniYaml)
                {
                    property.SetValue(map, node.Value, null);
                }
                else
                {
                    FieldLoader.LoadField(map, fieldName, node.Value.Value);
                }
            }
        }
Beispiel #3
0
        public Settings(string file, Arguments args)
        {
            settingsFile = file;
            Sections     = new Dictionary <string, object>()
            {
                { "Player", Player },
                { "Game", Game },
                { "Sound", Sound },
                { "Graphics", Graphics },
                { "Server", Server },
                { "Debug", Debug },
            };

            // Override fieldloader to ignore invalid entries
            var err1 = FieldLoader.UnknownFieldAction;
            var err2 = FieldLoader.InvalidValueAction;

            try
            {
                FieldLoader.UnknownFieldAction = (s, f) => Console.WriteLine("Ignoring unknown field `{0}` on `{1}`".F(s, f.Name));

                if (File.Exists(settingsFile))
                {
                    yamlCache = MiniYaml.FromFile(settingsFile, false);
                    foreach (var yamlSection in yamlCache)
                    {
                        object settingsSection;
                        if (yamlSection.Key != null && Sections.TryGetValue(yamlSection.Key, out settingsSection))
                        {
                            LoadSectionYaml(yamlSection.Value, settingsSection);
                        }
                    }

                    var keysNode = yamlCache.FirstOrDefault(n => n.Key == "Keys");
                    if (keysNode != null)
                    {
                        foreach (var node in keysNode.Value.Nodes)
                        {
                            if (node.Key != null)
                            {
                                Keys[node.Key] = FieldLoader.GetValue <Hotkey>(node.Key, node.Value.Value);
                            }
                        }
                    }
                }

                // Override with commandline args
                foreach (var kv in Sections)
                {
                    foreach (var f in kv.Value.GetType().GetFields())
                    {
                        if (args.Contains(kv.Key + "." + f.Name))
                        {
                            FieldLoader.LoadField(kv.Value, f.Name, args.GetValue(kv.Key + "." + f.Name, ""));
                        }
                    }
                }
            }
            finally
            {
                FieldLoader.UnknownFieldAction = err1;
                FieldLoader.InvalidValueAction = err2;
            }
        }