public void SetDialog(string name, UiPolicy uiPolicy)
    {
        if (!initialized)
        {
            Init();
        }
        Dialog dialog = dialogs.Find(d => d.name == name);

        if (dialog == null)
        {
            tokenizer.AddError("missing dialog \"" + name + "\"");
        }
        SetDialog(dialog, uiPolicy);
    }
Exemplo n.º 2
0
        public void Parse(Tokenizer tokenizer, object scriptVariables = null)
        {
            //Show.Log(tokenizer);
            //tokenizer = command.Tokenize(text);
            List <Token> tokens = tokenizer.tokens;

            orderedValues.Add(tokenizer.GetStr(0));
            Argument[] args = command.arguments;
            // add arguments to the dictionary
            for (int i = 1; i < tokens.Count; ++i)
            {
                Token tArg = tokens[i];
                //Show.Log(tArg+" "+tArg.IsSimpleString+" "+tArg.IsDelim);
                int argIndex = tArg.IsSimpleString || tArg.IsDelim ? GetArgumentIndex(command, tArg.ToString()) : -1;
                if (argIndex >= 0)
                {
                    Argument arg = args[argIndex];
                    bool     argumentHasValidValue = AddArg(arg, ref i, tokens, tokenizer, scriptVariables);
                    if (!argumentHasValidValue)
                    {
                        tokenizer.AddError(tArg.index, "expected <" + arg.valueType + "> for argument \"" + tArg.ToString() + "\"");
                    }
                }
                else
                {
                    orderedValues.Add(tokenizer.GetResolvedToken(i, scriptVariables));
                }
            }
            // put ordered arguments into the dictionary, include default values if needed, recognize required flag
            for (int i = 0; i < args.Length; ++i)
            {
                Argument arg    = args[i];
                bool     hasArg = namedValues.ContainsKey(arg.id);
                if (!hasArg && arg.order > 0 && arg.order < orderedValues.Count)
                {
                    namedValues[arg.id] = orderedValues[arg.order];
                    hasArg = true;
                }
                if (!hasArg && arg.defaultValue != null)
                {
                    namedValues[arg.id] = arg.defaultValue;
                    hasArg = true;
                }
                if (!hasArg && arg.required)
                {
                    tokenizer.AddError("missing required argument \"" + arg.id + "\" (" + arg.Name + ")");
                }
            }
        }
Exemplo n.º 3
0
        public void SetDialog(object src, Tokenizer tok, string name, UiPolicy uiPolicy)
        {
            if (!initialized)
            {
                Init();
            }
            Dialog dialog = DialogManager.Instance.dialogs.Find(d => d.name == name);

            if (dialog == null)
            {
                tok.AddError("missing dialog \"" + name + "\"");
            }
            SetDialog(src, tok, dialog, uiPolicy);
        }
Exemplo n.º 4
0
        public static bool TryParseTokens(Type type, List <Token> tokens, ref object data, object scope, Tokenizer tokenizer)
        {
            bool   result = false;
            Parser p      = new Parser();

            p.Init(type, tokens, data, tokenizer, scope);
            try {
                result = p.TryParse(type);
                data   = p.result;
            } catch (Exception e) {
                tokenizer.AddError("TryParseTokens:" + e + "\n" + p.GetCurrentTokenIndex().JoinToString(", ") + "\n" + tokenizer.DebugPrint());
            }
            return(result);
        }
Exemplo n.º 5
0
        public void SetDialog(object src, Tokenizer tok, Dialog dialog, UiPolicy uiPolicy)
        {
            if (!initialized)
            {
                Init();
            }
            bool isScrolledAllTheWayDown = !scrollRect.verticalScrollbar.gameObject.activeInHierarchy ||
                                           scrollRect.verticalNormalizedPosition < 1f / 1024; // keep scrolling down if really close to bottom

            switch (uiPolicy)
            {
            case UiPolicy.StartOver: RemoveDialogElements(); break;

            case UiPolicy.DisablePrev: DeactivateDialogChoices(); break;

            case UiPolicy.Continue: break;
            }
            if (dialog == null)
            {
                tok.AddError("missing dialog"); return;
            }
            if (dialog.options != null)
            {
                for (int i = 0; i < dialog.options.Length; ++i)
                {
                    Dialog.DialogOption opt = dialog.options[i];
                    //NonStandard.Show.Log("checking opt " + NonStandard.Show.Stringify(opt, false));
                    ScriptedDictionaryManager m = Global.Get <ScriptedDictionaryManager>();
                    if (opt.Available(tok, m.Main))
                    {
                        AddDialogOption(opt, isScrolledAllTheWayDown);
                        //NonStandard.Show.Log("added" + NonStandard.Show.Stringify(opt, false));
                    }
                    else
                    {
                        //NonStandard.Show.Log("ignored" + NonStandard.Show.Stringify(opt, false));
                    }
                }
            }
            ShowErrors(tok.errors);
            onDialog?.Invoke();
        }
Exemplo n.º 6
0
        bool AddArg(Argument arg, ref int i, List <Token> tokens, Tokenizer tokenizer, object scriptVariables)
        {
            if (arg.flag)
            {
                namedValues[arg.id] = true;
                return(true);
            }
            if (++i >= tokens.Count)
            {
                return(false);
            }
            Token tValue = tokens[i];

            CodeConvert.TryParse(tokens[i], out object result, scriptVariables, tokenizer);
            Type type = arg.valueType;
            //Show.Log(resultType + " : " + result.StringifySmall());
            bool goodArgument = false;

            if (result != null)
            {
                if (!(goodArgument = result.GetType() == type))
                {
                    goodArgument = CodeConvert.TryConvert(ref result, type);
                }
                if (goodArgument)
                {
                    namedValues[arg.id] = result;
                }
            }
            if (!goodArgument)
            {
                tokenizer.AddError(tValue.index, "Could not cast (" + type.Name + ") from (" +
                                   (result?.GetType().ToString() ?? "null") + ")");
            }
            return(goodArgument);
        }