Exemplo n.º 1
0
  public static Script GenerateScript(ParseTree tree, String label) {
    Script s = new Script();

    s.hasBeenCompiled = true;
    s.compilerError = false;
    s.cright = new ArrayList<ParseTree>();
    s.cright.add(tree);
    s.label = label;

    return s;
  }
Exemplo n.º 2
0
 @Override
 public Construct execs(Target t, Environment env, Script parent, ParseTree... nodes) {
   switch (nodes.length) {
     case 0:
       return VOID;
     case 1:
       return parent.eval(nodes[0], env);
     default:
       return new __autoconcat__().execs(t, env, parent, nodes);
   }
 }
Exemplo n.º 3
0
  /**
   * This is the workhorse function. It takes a given command, then converts it into the actual
   * command(s). If the command maps to a defined alias, it will run the specified alias. It will
   * search through the global list of aliases, as well as the aliases defined for that specific
   * player. This function doesn't handle the /alias command however.
   *
   * @param command
   * @return
   */
  public boolean alias(String command, final MCCommandSender player, List<Script> playerCommands) {

    GlobalEnv gEnv;
    try {
      gEnv =
          new GlobalEnv(
              parent.executionQueue,
              parent.profiler,
              parent.persistenceNetwork,
              parent.permissionsResolver,
              MethodScriptFileLocations.getDefault().getConfigDirectory(),
              new Profiles(MethodScriptFileLocations.getDefault().getSQLProfilesFile()));
    } catch (IOException ex) {
      Logger.getLogger(AliasCore.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    } catch (Profiles.InvalidProfileException ex) {
      throw ConfigRuntimeException.CreateUncatchableException(ex.getMessage(), Target.UNKNOWN);
    }
    CommandHelperEnvironment cEnv = new CommandHelperEnvironment();
    cEnv.SetCommandSender(player);
    Environment env = Environment.createEnvironment(gEnv, cEnv);

    if (player instanceof MCBlockCommandSender) {
      cEnv.SetBlockCommandSender((MCBlockCommandSender) player);
    }

    if (scripts == null) {
      throw ConfigRuntimeException.CreateUncatchableException(
          "Cannot run alias commands, no config file is loaded", Target.UNKNOWN);
    }

    boolean match = false;
    try { // catch RuntimeException
      // If player is null, we are running the test harness, so don't
      // actually add the player to the array.
      if (player != null
          && player instanceof MCPlayer
          && echoCommand.contains(((MCPlayer) player).getName())) {
        // we are running one of the expanded commands, so exit with false
        return false;
      }

      // Global aliases override personal ones, so check the list first
      for (Script s : scripts) {
        try {
          if (s.match(command)) {
            this.addPlayerReference(player);
            if (Prefs.ConsoleLogCommands() && s.doLog()) {
              StringBuilder b = new StringBuilder("CH: Running original command ");
              if (player instanceof MCPlayer) {
                b.append("on player ").append(((MCPlayer) player).getName());
              } else {
                b.append("from a MCCommandSender");
              }
              b.append(" ----> ").append(command);
              Static.getLogger().log(Level.INFO, b.toString());
            }
            try {
              env.getEnv(CommandHelperEnvironment.class).SetCommand(command);
              ProfilePoint alias =
                  env.getEnv(GlobalEnv.class)
                      .GetProfiler()
                      .start("Global Alias - \"" + command + "\"", LogLevel.ERROR);
              try {
                s.run(
                    s.getVariables(command),
                    env,
                    new MethodScriptComplete() {
                      @Override
                      public void done(String output) {
                        try {
                          if (output != null) {
                            if (!output.trim().isEmpty() && output.trim().startsWith("/")) {
                              if (Prefs.DebugMode()) {
                                if (player instanceof MCPlayer) {
                                  Static.getLogger()
                                      .log(
                                          Level.INFO,
                                          "[CommandHelper]: Executing command on "
                                              + ((MCPlayer) player).getName()
                                              + ": "
                                              + output.trim());
                                } else {
                                  Static.getLogger()
                                      .log(
                                          Level.INFO,
                                          "[CommandHelper]: Executing command from console equivalent: "
                                              + output.trim());
                                }
                              }

                              if (player instanceof MCPlayer) {
                                ((MCPlayer) player).chat(output.trim());
                              } else {
                                Static.getServer()
                                    .dispatchCommand(player, output.trim().substring(1));
                              }
                            }
                          }
                        } catch (Throwable e) {
                          System.err.println(e.getMessage());
                          player.sendMessage(MCChatColor.RED + e.getMessage());
                        } finally {
                          Static.getAliasCore().removePlayerReference(player);
                        }
                      }
                    });
              } finally {
                alias.stop();
              }
            } catch (ConfigRuntimeException ex) {
              ex.setEnv(env);
              ConfigRuntimeException.React(ex, env);
            } catch (Throwable e) {
              // This is not a simple user script error, this is a deeper problem, so we always
              // handle this.
              System.err.println(
                  "An unexpected exception occured: " + e.getClass().getSimpleName());
              player.sendMessage(
                  "An unexpected exception occured: "
                      + MCChatColor.RED
                      + e.getClass().getSimpleName());
              e.printStackTrace();
            } finally {
              Static.getAliasCore().removePlayerReference(player);
            }
            match = true;
            break;
          }
        } catch (Exception e) {
          System.err.println("An unexpected exception occured inside the command " + s.toString());
          e.printStackTrace();
        }
      }

      if (player instanceof MCPlayer) {
        if (match == false && playerCommands != null) {
          // if we are still looking, look in the aliases for this player
          for (Script ac : playerCommands) {
            // RunnableAlias b = ac.getRunnableAliases(command, player);
            try {

              ac.compile();

              if (ac.match(command)) {
                Static.getAliasCore().addPlayerReference(player);
                ProfilePoint alias =
                    env.getEnv(GlobalEnv.class)
                        .GetProfiler()
                        .start(
                            "User Alias (" + player.getName() + ") - \"" + command + "\"",
                            LogLevel.ERROR);
                try {
                  ac.run(
                      ac.getVariables(command),
                      env,
                      new MethodScriptComplete() {
                        @Override
                        public void done(String output) {
                          if (output != null) {
                            if (!output.trim().isEmpty() && output.trim().startsWith("/")) {
                              if (Prefs.DebugMode()) {
                                Static.getLogger()
                                    .log(
                                        Level.INFO,
                                        "[CommandHelper]: Executing command on "
                                            + ((MCPlayer) player).getName()
                                            + ": "
                                            + output.trim());
                              }
                              ((MCPlayer) player).chat(output.trim());
                            }
                          }
                          Static.getAliasCore().removePlayerReference(player);
                        }
                      });
                } finally {
                  alias.stop();
                }
                match = true;
                break;
              }
            } catch (ConfigRuntimeException e) {
              // Unlike system scripts, this should just report the problem to the player
              // env.getEnv(CommandHelperEnvironment.class).SetCommandSender(player);
              Static.getAliasCore().removePlayerReference(player);
              e.setEnv(env);
              ConfigRuntimeException.React(e, env);
              match = true;
            } catch (ConfigCompileException e) {
              // Something strange happened, and a bad alias was added
              // to the database. Our best course of action is to just
              // skip it.
            }
          }
        }
      }
    } catch (Throwable e) {
      // Not only did an error happen, an error happened in our error handler
      throw new InternalException(
          TermColors.RED
              + "An unexpected error occured in the CommandHelper plugin. "
              + "Further, this is likely an error with the error handler, so it may be caused by your script, "
              + "however, there is no more information at this point. Check your script, but also report this "
              + "as a bug in CommandHelper. Also, it's possible that some commands will no longer work. As a temporary "
              + "workaround, restart the server, and avoid doing whatever it is you did to make this happen.\nThe error is as follows: "
              + e.toString()
              + "\n"
              + TermColors.reset()
              + "Stack Trace:\n"
              + StringUtil.joinString(Arrays.asList(e.getStackTrace()), "\n", 0));
    }
    return match;
  }
Exemplo n.º 4
0
    public void compileMSA(List<Script> scripts, MCPlayer player) {

      for (FileInfo fi : msa) {
        List<Script> tempScripts;
        try {
          tempScripts =
              MethodScriptCompiler.preprocess(
                  MethodScriptCompiler.lex(fi.contents, fi.file, false));
          for (Script s : tempScripts) {
            try {
              try {
                s.compile();
                s.checkAmbiguous((ArrayList<Script>) scripts);
                scripts.add(s);
              } catch (ConfigCompileException e) {
                ConfigRuntimeException.React(
                    e,
                    "Compile error in script. Compilation will attempt to continue, however.",
                    player);
              }
            } catch (RuntimeException ee) {
              throw new RuntimeException(
                  "While processing a script, "
                      + "("
                      + fi.file()
                      + ") an unexpected exception occurred. (No further information"
                      + " is available, unfortunately.)",
                  ee);
            }
          }
        } catch (ConfigCompileException e) {
          ConfigRuntimeException.React(
              e, "Could not compile file " + fi.file + " compilation will halt.", player);
          return;
        }
      }
      int errors = 0;
      for (Script s : scripts) {
        if (s.compilerError) {
          errors++;
        }
      }
      if (errors > 0) {
        System.out.println(
            TermColors.YELLOW
                + "[CommandHelper]: "
                + (scripts.size() - errors)
                + " alias(es) defined, "
                + TermColors.RED
                + "with "
                + errors
                + " aliases with compile errors."
                + TermColors.reset());
        if (player != null) {
          player.sendMessage(
              MCChatColor.YELLOW
                  + "[CommandHelper]: "
                  + (scripts.size() - errors)
                  + " alias(es) defined, "
                  + MCChatColor.RED
                  + "with "
                  + errors
                  + " aliases with compile errors.");
        }
      } else {
        System.out.println(
            TermColors.YELLOW
                + "[CommandHelper]: "
                + scripts.size()
                + " alias(es) defined."
                + TermColors.reset());
        if (player != null) {
          player.sendMessage(
              MCChatColor.YELLOW + "[CommandHelper]: " + scripts.size() + " alias(es) defined.");
        }
      }
    }
Exemplo n.º 5
0
    @Override
    public Construct execs(Target t, Environment env, Script parent, ParseTree... nodes) {
      if (nodes.length < 5) {
        throw new ConfigRuntimeException(
            "bind accepts 5 or more parameters", ExceptionType.InsufficientArgumentsException, t);
      }
      Construct name = parent.seval(nodes[0], env);
      Construct options = parent.seval(nodes[1], env);
      Construct prefilter = parent.seval(nodes[2], env);
      Construct event_obj = parent.eval(nodes[3], env);
      IVariableList custom_params = new IVariableList();
      for (int a = 0; a < nodes.length - 5; a++) {
        Construct var = parent.eval(nodes[4 + a], env);
        if (!(var instanceof IVariable)) {
          throw new ConfigRuntimeException(
              "The custom parameters must be ivariables", ExceptionType.CastException, t);
        }
        IVariable cur = (IVariable) var;
        custom_params.set(
            env.getEnv(GlobalEnv.class).GetVarList().get(cur.getName(), cur.getTarget()));
      }
      Environment newEnv = env;
      try {
        newEnv = env.clone();
      } catch (Exception e) {
      }
      newEnv.getEnv(GlobalEnv.class).SetVarList(custom_params);
      ParseTree tree = nodes[nodes.length - 1];

      // Check to see if our arguments are correct
      if (!(options instanceof CNull || options instanceof CArray)) {
        throw new ConfigRuntimeException(
            "The options must be an array or null", ExceptionType.CastException, t);
      }
      if (!(prefilter instanceof CNull || prefilter instanceof CArray)) {
        throw new ConfigRuntimeException(
            "The prefilters must be an array or null", ExceptionType.CastException, t);
      }
      if (!(event_obj instanceof IVariable)) {
        throw new ConfigRuntimeException(
            "The event object must be an IVariable", ExceptionType.CastException, t);
      }
      CString id;
      if (options instanceof CNull) {
        options = null;
      }
      if (prefilter instanceof CNull) {
        prefilter = null;
      }
      Event event;
      try {
        BoundEvent be =
            new BoundEvent(
                name.val(),
                (CArray) options,
                (CArray) prefilter,
                ((IVariable) event_obj).getName(),
                newEnv,
                tree,
                t);
        EventUtils.RegisterEvent(be);
        id = new CString(be.getId(), t);
        event = EventList.getEvent(be.getEventName());
      } catch (EventException ex) {
        throw new ConfigRuntimeException(ex.getMessage(), ExceptionType.BindException, t);
      }

      // Set up our bind counter, but only if the event is supposed to be added to the counter
      if (event.addCounter()) {
        synchronized (bindCounter) {
          if (bindCounter.get() == 0) {
            env.getEnv(GlobalEnv.class).GetDaemonManager().activateThread(null);
            StaticLayer.GetConvertor()
                .addShutdownHook(
                    new Runnable() {

                      @Override
                      public void run() {
                        synchronized (bindCounter) {
                          bindCounter.set(0);
                        }
                      }
                    });
          }
          bindCounter.incrementAndGet();
        }
      }
      return id;
    }