示例#1
0
 public static void Register(NodeCondition cond)
 {
     registry.Add(cond.Name, cond);
     Log.Debug($"Registered condition token: {cond.Name}", Plugin.Instance.Config.VerboseOutput);
 }
示例#2
0
        public TalkNode(string path)
        {
            Log.Debug($"Parsing node {path}", Plugin.Instance.Config.VerboseOutput);
            NodeFile = path;
            try
            {
                var input        = new StringReader(File.ReadAllText(path));
                var deserializer = new DeserializerBuilder()
                                   .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                   // Workaround to remove YamlAttributesTypeInspector
                                   .WithTypeInspector(inner => inner, s => s.InsteadOf <YamlAttributesTypeInspector>())
                                   .WithTypeInspector(
                    inner => new YamlAttributesTypeInspector(inner),
                    s => s.Before <NamingConventionTypeInspector>()
                    )
                                   .Build();

                TalkNodeSerializationInfo raw_node = deserializer.Deserialize <TalkNodeSerializationInfo>(input);

                Desc  = raw_node.Description;
                Reply = raw_node.Reply;

                //Parse conditions
                //Format:
                //------------
                //conditions:
                // - token: SomeToken
                //   args:
                //    some_arg: some_value
                //    some_arg1: some_value1
                foreach (NpcNodeWithArgsSerializationInfo info in raw_node.Conditions)
                {
                    NodeCondition cond = NodeCondition.GetFromToken(info.Token);
                    if (cond != null)
                    {
                        Log.Debug($"Recognized token: {cond.Name}", Plugin.Instance.Config.VerboseOutput);
                        Conditions.Add(cond, info.Args);
                    }
                    else
                    {
                        Log.Error($"Failed to parse condition: {info.Token} (invalid token)");
                    }
                }

                //Parse actions
                //Format:
                //------------
                //actions:
                // - token: SomeToken
                //   args:
                //    some_arg: some_value
                //    some_arg1: some_value1
                foreach (NpcNodeWithArgsSerializationInfo info in raw_node.Actions)
                {
                    NodeAction cond = NodeAction.GetFromToken(info.Token);
                    if (cond != null)
                    {
                        Log.Debug($"Recognized token: {cond.Name}", Plugin.Instance.Config.VerboseOutput);
                        Actions.Add(cond, info.Args);
                    }
                    else
                    {
                        Log.Error($"Failed to parse action: {info.Token} (invalid token)");
                    }
                }

                //Parse next nodes
                //Format:
                //------------
                //next_nodes:
                // - /relative/path/to/node
                Log.Debug("Parsing next nodes...", Plugin.Instance.Config.VerboseOutput);
                foreach (string item in raw_node.NextNodes)
                {
                    NextNodes.Add(TalkNode.FromFile(Path.Combine(Config.NPCs_nodes_path, item)));
                }
            }
            catch (Exception e)
            {
                Log.Error($"Failed to parse node {path}! {e}");
                this.Desc  = "<ERROR>";
                this.Reply = "<ERROR>";
            }
        }