public void compileMS(MCPlayer player, Environment env) {
   for (FileInfo fi : ms) {
     boolean exception = false;
     try {
       env.getEnv(CommandHelperEnvironment.class)
           .SetCommandSender(Static.getServer().getConsole());
       MethodScriptCompiler.registerAutoIncludes(env, null);
       MethodScriptCompiler.execute(
           MethodScriptCompiler.compile(MethodScriptCompiler.lex(fi.contents, fi.file, true)),
           env,
           null,
           null);
     } catch (ConfigCompileException e) {
       exception = true;
       ConfigRuntimeException.React(
           e,
           fi.file.getAbsolutePath() + " could not be compiled, due to a compile error.",
           player);
     } catch (ConfigRuntimeException e) {
       exception = true;
       ConfigRuntimeException.React(e, env);
     } catch (CancelCommandException e) {
       if (e.getMessage() != null && !"".equals(e.getMessage().trim())) {
         logger.log(Level.INFO, e.getMessage());
       }
     } catch (ProgramFlowManipulationException e) {
       exception = true;
       ConfigRuntimeException.React(
           ConfigRuntimeException.CreateUncatchableException(
               "Cannot break program flow in main files.", e.getTarget()),
           env);
     } finally {
       env.getEnv(CommandHelperEnvironment.class).SetCommandSender(null);
     }
     if (exception) {
       if (Prefs.HaltOnFailure()) {
         logger.log(
             Level.SEVERE,
             TermColors.RED
                 + "[CommandHelper]: Compilation halted due to unrecoverable failure."
                 + TermColors.reset());
         return;
       }
     }
   }
   logger.log(
       Level.INFO,
       TermColors.YELLOW + "[CommandHelper]: MethodScript files processed" + TermColors.reset());
   if (player != null) {
     player.sendMessage(MCChatColor.YELLOW + "[CommandHelper]: MethodScript files processed");
   }
 }
示例#2
0
 public void compileRight() throws ConfigCompileException {
   List<Token> temp = new ArrayList<Token>();
   right = new ArrayList<List<Token>>();
   for (Token t : fullRight) {
     if (t.type == TType.SEPERATOR) {
       right.add(temp);
       temp = new ArrayList<Token>();
     } else {
       if (t.type == TType.WHITESPACE) {
         continue; // Whitespace is ignored on the right side
       }
       temp.add(t);
     }
   }
   right.add(temp);
   cright = new ArrayList<ParseTree>();
   for (List<Token> l : right) {
     cright.add(MethodScriptCompiler.compile(l));
   }
 }
示例#3
0
  public void run(final List<Variable> vars, Environment myEnv, final MethodScriptComplete done) {
    // Some things, such as the label are determined at compile time
    this.CurrentEnv = myEnv;
    this.CurrentEnv.getEnv(GlobalEnv.class).SetLabel(this.label);
    MCCommandSender p = myEnv.getEnv(CommandHelperEnvironment.class).GetCommandSender();
    if (!hasBeenCompiled || compilerError) {
      Target target = Target.UNKNOWN;
      if (left.size() >= 1) {
        try {
          target = new Target(left.get(0).line_num, left.get(0).file, left.get(0).column);
        } catch (NullPointerException e) {
          // Oh well, we tried to get more information
        }
      }
      throw new ConfigRuntimeException(
          "Unable to run command, script not yet compiled, or a compiler error occured for that command."
              + " To see the compile error, run /reloadaliases",
          null,
          target);
    }
    if (p instanceof MCPlayer) {
      if (CurrentEnv.getEnv(GlobalEnv.class).GetLabel() != null) {
        PermissionsResolver perms = CurrentEnv.getEnv(GlobalEnv.class).GetPermissionsResolver();
        String[] groups = CurrentEnv.getEnv(GlobalEnv.class).GetLabel().split("/");
        for (String group : groups) {
          if (group.startsWith("-")
              && perms.inGroup(((MCPlayer) p).getName(), group.substring(1))) {
            // negative permission
            throw new ConfigRuntimeException(
                "You do not have permission to use that command",
                ExceptionType.InsufficientPermissionException,
                Target.UNKNOWN);
          } else if (perms.inGroup(((MCPlayer) p).getName(), group)) {
            // They do have permission.
            break;
          }
        }
      }
    }

    try {
      for (ParseTree rootNode : cright) {
        for (Construct tempNode : rootNode.getAllData()) {
          if (tempNode instanceof Variable) {
            if (left_vars == null) {
              throw new ConfigRuntimeException(
                  "$variables may not be used in this context. Only @variables may be.",
                  null,
                  tempNode.getTarget());
            }
            ((Variable) tempNode)
                .setVal(
                    new CString(
                        Static.resolveDollarVar(
                                left_vars.get(((Variable) tempNode).getName()), vars)
                            .toString(),
                        tempNode.getTarget()));
          }
        }

        MethodScriptCompiler.registerAutoIncludes(CurrentEnv, this);
        MethodScriptCompiler.execute(rootNode, CurrentEnv, done, this);
      }
    } catch (ConfigRuntimeException ex) {
      // We don't know how to handle this really, so let's pass it up the chain.
      throw ex;
    } catch (CancelCommandException e) {
      // p.sendMessage(e.getMessage());
      // The message in the exception is actually empty
    } catch (LoopBreakException e) {
      if (p != null) {
        p.sendMessage("The break() function must be used inside a for() or foreach() loop");
      }
      System.out.println("The break() function must be used inside a for() or foreach() loop");
    } catch (LoopContinueException e) {
      if (p != null) {
        p.sendMessage("The continue() function must be used inside a for() or foreach() loop");
      }
      System.out.println("The continue() function must be used inside a for() or foreach() loop");
    } catch (FunctionReturnException e) {
      if (myEnv.getEnv(CommandHelperEnvironment.class).GetEvent() != null) {
        // Oh, we're running in an event handler. Those know how to catch it too.
        throw e;
      }
      if (p != null) {
        p.sendMessage("The return() function must be used inside a procedure.");
      }
      System.out.println("The return() function must be used inside a procedure.");
    } catch (Throwable t) {
      System.out.println("An unexpected exception occured during the execution of a script.");
      t.printStackTrace();
      if (p != null) {
        p.sendMessage(
            "An unexpected exception occured during the execution of your script. Please check the console for more information.");
      }
    }
    if (done != null) {
      done.done(null);
    }
  }
    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.");
        }
      }
    }