Exemple #1
0
    private IEnumerator Execute(IEnumerable <string> commands, Object owner)
    {
        foreach (string command in commands)
        {
            if (command == "")
            {
                Debug.LogWarning("empty command");
            }
            else
            {
                string          commandName     = command.Split(' ')[0];
                string          methodName      = commandName.UppercaseFirst();
                MethodInfo      methodToExecute = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(Command) }, null);
                CheatSheet.Rule rule            = cheatSheet.rules.Find(r => r.commandName == commandName);

                // command not found
                if (rule == null)
                {
                    Debug.LogWarning($"command with name '{commandName}' not found: {command}");
                    continue;
                }
                // command not implemented
                if (methodToExecute == null)
                {
                    Debug.LogWarning($"command with name '{commandName}' not implemented: {command}");
                    continue;
                }

                Match match = Regex.Match(command, rule.regexPattern);
                if (match.Success)
                {
                    yield return(StartCoroutine(methodToExecute.Name, new Command(owner, match)));
                }
                else
                {
                    Debug.LogWarning($"command '{command}' doesn't match the syntax rules");
                }
            }
        }
        yield return(null);
    }
 private void EndDrawListElement(int index)
 {
     if (cheatSheet == null)
     {
         cheatSheet = Resources.FindObjectsOfTypeAll <CheatSheet>().FirstOrDefault();
     }
     if (cheatSheet == null)
     {
         SirenixEditorGUI.MessageBox("cheat sheet not found", MessageType.Error);
     }
     else
     {
         if (script[index] != "")
         {
             string commandName = script[index].Split(' ')[0];
             if (commandName != "")
             {
                 CheatSheet.Rule rule = cheatSheet.rules.Find(c => c.commandName == commandName);
                 if (rule == null)
                 {
                     SirenixEditorGUI.MessageBox($"command name '{commandName}' not found", MessageType.Error);
                 }
                 else
                 {
                     if (!Regex.IsMatch(script[index], rule.regexPattern))
                     {
                         string syntax = rule.regexPattern.Replace(@"\d+(,\d+)?", "n,");
                         syntax = syntax.Replace(@"\d+", "n");
                         syntax = syntax.Replace(@"\w+", "string");
                         syntax = syntax.Replace("$", " ");
                         syntax = syntax.Replace(@"\+", "+");
                         syntax = RemoveParentheses(syntax);
                         SirenixEditorGUI.MessageBox($"{syntax}", MessageType.Error);
                     }
                 }
             }
         }
     }
 }