Example #1
0
        public ScriptLineView CreateLineView(string scriptLineText, int lineIndex, bool @default = false)
        {
            var lineType = Script.ResolveLineType(scriptLineText);
            var lineView = default(ScriptLineView);

            switch (lineType.Name)
            {
            case nameof(CommentScriptLine):
                var commentScriptLine = new CommentScriptLine(null, lineIndex, scriptLineText, null, true);
                lineView = new CommentLineView(commentScriptLine, linesContainer);
                break;

            case nameof(LabelScriptLine):
                var labelScriptLine = new LabelScriptLine(null, lineIndex, scriptLineText, null, true);
                lineView = new LabelLineView(labelScriptLine, linesContainer);
                break;

            case nameof(DefineScriptLine):
                var defineScriptLine = new DefineScriptLine(null, lineIndex, scriptLineText, null, true);
                lineView = new DefineLineView(defineScriptLine, linesContainer);
                break;

            case nameof(CommandScriptLine):
                var commandScriptLine = new CommandScriptLine(null, lineIndex, scriptLineText, null, true);
                lineView = CommandLineView.CreateOrError(commandScriptLine, linesContainer, config.HideUnusedParameters, @default);
                break;

            case nameof(GenericTextScriptLine):
                var genericTextScriptLine = new GenericTextScriptLine(null, lineIndex, scriptLineText, null, true);
                lineView = new GenericTextLineView(genericTextScriptLine, linesContainer);
                break;
            }
            return(lineView);
        }
Example #2
0
        private static string ProcessActionInput(string input)
        {
            if (input is null || !input.StartsWithFast(CommandScriptLine.IdentifierLiteral))
            {
                return(input);
            }

            var scriptLine = new CommandScriptLine(string.Empty, 0, input, null, false);

            if (scriptLine is null)
            {
                return(null);
            }

            var command = Commands.Command.FromScriptLine(scriptLine);

            if (command is null)
            {
                return(null);
            }

            if (command.ShouldExecute)
            {
                command.ExecuteAsync();
            }
            return(null);
        }
Example #3
0
 public ErrorLineView(CommandScriptLine scriptLine, VisualElement container, string error = default)
     : base(scriptLine, container)
 {
     CommandId  = scriptLine.CommandName.ToLowerInvariant();
     valueField = new LineTextField(value: scriptLine.Text);
     Content.Add(valueField);
     if (error != default)
     {
         tooltip = "Error: " + error;
     }
 }
Example #4
0
        public static ScriptLineView CreateOrError(CommandScriptLine scriptLine, VisualElement container, bool hideParameters, bool @default = false)
        {
            ErrorLineView Error(string error) => new ErrorLineView(scriptLine, container, error);

            if (!scriptLine.Valid)
            {
                return(Error("Incorrect syntax."));
            }

            var commandType = Command.FindCommandType(scriptLine.CommandName);

            if (commandType is null)
            {
                return(Error($"Unknown command `{scriptLine.CommandName}`."));
            }

            var commandLineView = new CommandLineView(scriptLine, container);

            commandLineView.hideParameters = hideParameters;
            commandLineView.CommandId      = scriptLine.CommandName;
            var nameLabel = new Label(scriptLine.CommandName);

            nameLabel.name = "InputLabel";
            nameLabel.AddToClassList("Inlined");
            commandLineView.Content.Add(nameLabel);

            var paramaterFieldInfos = commandType.GetProperties()
                                      .Where(property => property.IsDefined(typeof(Command.CommandParameterAttribute), false)).ToList();
            var parameterAttributes = paramaterFieldInfos
                                      .Select(f => f.GetCustomAttributes(typeof(Command.CommandParameterAttribute), false).First() as Command.CommandParameterAttribute).ToList();

            Debug.Assert(paramaterFieldInfos.Count == parameterAttributes.Count);

            for (int i = 0; i < paramaterFieldInfos.Count; i++)
            {
                var paramFieldInfo = paramaterFieldInfos[i];
                var paramAttribute = parameterAttributes[i];

                var paramName = paramAttribute.Alias != null && scriptLine.CommandParameters.ContainsKey(paramAttribute.Alias) ? paramAttribute.Alias : paramFieldInfo.Name;
                if (!scriptLine.CommandParameters.ContainsKey(paramName) && !paramAttribute.Optional && !@default)
                {
                    return(Error($"Missing `{paramName}` parameter."));
                }

                scriptLine.CommandParameters.TryGetValue(paramName, out var paramValue);
                var textField = new LineTextField(paramAttribute.Alias ?? char.ToLowerInvariant(paramName[0]) + paramName.Substring(1), paramValue);
                // Show parameter ID of the nameless parameters via tooltip.
                if (string.IsNullOrEmpty(textField.label))
                {
                    textField.tooltip = paramFieldInfo.Name;
                }
                else
                {
                    textField.AddToClassList("NamedParameterLabel");
                }
                commandLineView.parameterFields.Add(textField);
                // Show the un-assigned named parameters only when hovered or focused.
                if (string.IsNullOrEmpty(textField.label) || !hideParameters || !string.IsNullOrEmpty(textField.value))
                {
                    commandLineView.Content.Add(textField);
                }
            }

            foreach (var paramId in scriptLine.CommandParameters.Keys)
            {
                if (parameterAttributes.Exists(a => a.Alias?.EqualsFastIgnoreCase(paramId) ?? false))
                {
                    continue;
                }
                if (paramaterFieldInfos.Exists(f => f.Name.EqualsFastIgnoreCase(paramId)))
                {
                    continue;
                }
                return(Error($"Unsupported `{paramId}` parameter."));
            }

            return(commandLineView);
        }
Example #5
0
 private CommandLineView(CommandScriptLine scriptLine, VisualElement container)
     : base(scriptLine, container)
 {
 }
        private List <CommandScriptLine> ExtractInlinedCommandLines(string lineText, LiteralMap <string> scriptDefines = null)
        {
            // When actor name is present at the start of the line: extract it and cut from the line.
            var actorId = lineText.GetBefore(ActorIdLiteral);

            if (!string.IsNullOrEmpty(actorId) && !actorId.Any(char.IsWhiteSpace) && !actorId.StartsWithFast("\""))
            {
                lineText = lineText.GetAfterFirst(ActorIdLiteral);
            }
            else
            {
                actorId = null;
            }

            // Collect all inlined command strings (text inside square brackets).
            var inlinedCommandMatches = Regex.Matches(lineText, "\\[.*?\\]").Cast <Match>().ToList();

            // In case no inlined commands found, just add a single @print command line.
            if (inlinedCommandMatches.Count == 0)
            {
                var printLineText = TransformGenericToPrintText(lineText, actorId);
                var printLine     = new CommandScriptLine(ScriptName, LineIndex, printLineText, scriptDefines, ignoreErrors: IgnoreParseErrors);
                return(new List <CommandScriptLine> {
                    printLine
                });
            }

            var result            = new List <CommandScriptLine>();
            var printedTextBefore = false;

            for (int i = 0; i < inlinedCommandMatches.Count; i++)
            {
                // Check if we need to print any text before the current inlined command.
                var precedingGenericText = StringUtils.TrySubset(lineText,
                                                                 i > 0 ? inlinedCommandMatches[i - 1].GetEndIndex() + 1 : 0,
                                                                 inlinedCommandMatches[i].Index - 1);
                if (!string.IsNullOrEmpty(precedingGenericText))
                {
                    var printLineText = TransformGenericToPrintText(precedingGenericText, actorId, printedTextBefore ? (bool?)false : null, false);
                    var printLine     = new CommandScriptLine(ScriptName, LineIndex, printLineText, scriptDefines, result.Count, IgnoreParseErrors);
                    result.Add(printLine);
                    printedTextBefore = true;
                }

                // Extract inlined command script line.
                var commandLineText = CommandScriptLine.IdentifierLiteral + inlinedCommandMatches[i].ToString().GetBetween("[", "]").TrimFull();
                var commandLine     = new CommandScriptLine(ScriptName, LineIndex, commandLineText, scriptDefines, result.Count, IgnoreParseErrors);
                result.Add(commandLine);
            }

            // Check if we need to print any text after the last inlined command.
            var lastGenericText = StringUtils.TrySubset(lineText,
                                                        inlinedCommandMatches.Last().GetEndIndex() + 1,
                                                        lineText.Length - 1);

            if (!string.IsNullOrEmpty(lastGenericText))
            {
                var printLineText = TransformGenericToPrintText(lastGenericText, actorId, printedTextBefore ? (bool?)false : null, false);
                var printLine     = new CommandScriptLine(ScriptName, LineIndex, printLineText, scriptDefines, result.Count, IgnoreParseErrors);
                result.Add(printLine);
            }

            // Add wait input command at the end; except when generic text contains a [skipInput] command.
            if (lineText?.IndexOf(nameof(SkipInput), StringComparison.OrdinalIgnoreCase) == -1)
            {
                var waitCommandLineText = CommandScriptLine.IdentifierLiteral + typeof(WaitForInput).Name;
                var waitCommandLine     = new CommandScriptLine(ScriptName, LineIndex, waitCommandLineText, scriptDefines, result.Count, IgnoreParseErrors);
                result.Add(waitCommandLine);
            }

            return(result);
        }