Exemplo n.º 1
0
  public static Object executeScriptFileHeadless(
      ScriptItem scriptItem, boolean forceFromFile, Map<String, Object> additionalBindings) {
    ScriptEngineManager manager = new ScriptEngineManager(ScriptingWindow.class.getClassLoader());
    ScriptEngine scriptEngine =
        manager.getEngineByExtension(getFileExtension(scriptItem.getName()));

    if (scriptEngine == null) {
      scriptEngine = manager.getEngineByName(DEFAULT_SCRIPT);
    }

    SimpleBindings bindings = new SimpleBindings();
    bindings.put(VAR_PROJECT, Core.getProject());
    bindings.put(VAR_EDITOR, Core.getEditor());
    bindings.put(VAR_GLOSSARY, Core.getGlossary());
    bindings.put(VAR_MAINWINDOW, Core.getMainWindow());
    bindings.put(VAR_RESOURCES, scriptItem.getResourceBundle());

    if (additionalBindings != null) {
      bindings.putAll(additionalBindings);
    }

    Object eval = null;
    try {
      eval = scriptEngine.eval(scriptItem.getText(), bindings);
      if (eval != null) {
        Log.logRB("SCW_SCRIPT_RESULT");
        Log.log(eval.toString());
      }
    } catch (Throwable e) {
      Log.logErrorRB(e, "SCW_SCRIPT_ERROR");
    }

    return eval;
  }
  @Override
  public void startQuest() throws Exception {
    final SimpleBindings bindings = new SimpleBindings();
    World w = WorldFactory.getWorld("BlankTest");
    setWorld(w);
    w.waitForWorldLoaded();

    final Farrand farrand = new Farrand();
    farrand.setX(532);
    farrand.setY(532);
    w.loadAndAdd(farrand);
    farrand.select();
    bindings.put("farrand", farrand);

    final Farrand other = new Farrand();
    other.setX(736);
    other.setY(756);
    w.loadAndAdd(other);
    bindings.put("other", other);
    Random random = new Random();
    for (int i = 0; i < 15; i++) {
      Farrand c = new Farrand();
      other.ignoreCollisionWith(c);
      c.setX(random.nextInt(300) + 200);
      c.setY(random.nextInt(300) + 200);
      w.loadAndAdd(c);
    }

    new Thread(
            new Runnable() {
              @Override
              public void run() {
                while (true) {
                  if (InputKeys.isButtonPressed(InputKeys.SELECT)) {
                    if (other.getBehavior() == null) {
                      other.setBehavior(new Idle(other, 100));
                    }
                  }
                }
              }
            })
        .start();
  }
Exemplo n.º 3
0
  public void executeScriptFile(
      ScriptItem scriptItem, boolean forceFromFile, Map<String, Object> additionalBindings) {
    ScriptLogger scriptLogger = new ScriptLogger(m_txtResult);

    ScriptEngine scriptEngine =
        manager.getEngineByExtension(getFileExtension(scriptItem.getName()));

    if (scriptEngine == null) {
      scriptEngine = manager.getEngineByName(DEFAULT_SCRIPT);
    }

    // logResult(StaticUtils.format(OStrings.getString("SCW_SELECTED_LANGUAGE"),
    // scriptEngine.getFactory().getEngineName()));
    SimpleBindings bindings = new SimpleBindings();
    bindings.put(VAR_PROJECT, Core.getProject());
    bindings.put(VAR_EDITOR, Core.getEditor());
    bindings.put(VAR_GLOSSARY, Core.getGlossary());
    bindings.put(VAR_MAINWINDOW, Core.getMainWindow());
    bindings.put(VAR_CONSOLE, scriptLogger);
    bindings.put(VAR_RESOURCES, scriptItem.getResourceBundle());

    if (additionalBindings != null) {
      bindings.putAll(additionalBindings);
    }

    // evaluate JavaScript code from String
    try {
      String scriptString;
      if (forceFromFile) {
        scriptString = scriptItem.getText();
      } else if ("".equals(m_txtScriptEditor.getText().trim())) {
        scriptString = scriptItem.getText();
        m_txtScriptEditor.setText(scriptString);
      } else {
        scriptString = m_txtScriptEditor.getText();
      }

      if (!scriptString.endsWith("\n")) {
        scriptString += "\n";
      }

      Object eval = scriptEngine.eval(scriptString, bindings);
      if (eval != null) {
        logResult(OStrings.getString("SCW_SCRIPT_RESULT"));
        logResult(eval.toString());
      }
    } catch (Throwable e) {
      logResult(OStrings.getString("SCW_SCRIPT_ERROR"));
      logResult(e.getMessage());
      // e.printStackTrace();
    }
  }
 public Bindings getBindings() {
   SimpleBindings bindings = new SimpleBindings();
   bindings.put("console", JSConsole.getInstance(engine));
   return bindings;
 }