Esempio n. 1
0
 protected static String[] getWindowsCommand(Integer roundsNumber, String initialPosition) {
   String[] cmd = new String[12];
   cmd[0] = "cmd.exe";
   cmd[1] = "/C";
   cmd[2] = "java";
   cmd[3] = "-cp";
   try {
     cmd[4] =
         Settings.getPathTo("lib")
             + File.separator
             + "rita.jar;"
             + Settings.getPathTo("lib")
             + File.separator
             + "robocode.jar;"
             + Settings.getPathTo("lib")
             + File.separator
             + "robocode.ui-1.7.3.6.jar";
   } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   cmd[5] = "-Xmx512M";
   cmd[6] = "rita.battle.Batalla";
   cmd[7] = Settings.getInstallPath(); // 1er argumento
   cmd[8] =
       HelperEditor.currentRobotPackage + "." + HelperEditor.currentRobotName; // 2do argumento
   cmd[9] = BatallaConfig.chooseEnemy(Settings.getProperty("level.default")); // 3er argumento
   cmd[10] =
       roundsNumber != null
           ? roundsNumber.toString()
           : Integer.toString(Batalla.NUMBER_OF_ROUNDS); // 4to argumento Número de rondas
   cmd[11] = initialPosition;
   return cmd;
 }
Esempio n. 2
0
  /**
   * @return El path del archivo con el codigo fuente java del robot
   * @throws FileNotFoundException
   * @throws IOException
   */
  private File saveSourceCode() throws FileNotFoundException, IOException {
    final String fullPath =
        Settings.getRobotsPath()
            + File.separator
            + HelperEditor.currentRobotPackage.replace(".", File.separator);

    String fileSource = HelperEditor.currentRobotName + ".java";
    File javaSourceFile = new File(fullPath, fileSource);
    writeSourceFile(javaSourceFile, this.getText());
    return javaSourceFile;
  }
Esempio n. 3
0
 protected static String[] getUnixCommand(Integer roundsNumber, String initialPosition) {
   String[] cmd = new String[10];
   cmd[0] = "java";
   cmd[1] = "-cp";
   cmd[2] =
       Settings.getInstallPath()
           + "lib/rita.jar:"
           + Settings.getInstallPath()
           + "lib/robocode.jar:"
           + Settings.getInstallPath()
           + "lib/robocode.ui-1.7.3.5.jar";
   cmd[3] = "-Xmx512M";
   cmd[4] = "rita.battle.Batalla";
   cmd[5] = Settings.getInstallPath(); // 1er argumento
   cmd[6] =
       HelperEditor.currentRobotPackage + "." + HelperEditor.currentRobotName; // 2do argumento
   cmd[7] = BatallaConfig.chooseEnemy(Settings.getProperty("level.default")); // 3er argumento
   cmd[8] =
       roundsNumber != null
           ? roundsNumber.toString()
           : Integer.toString(Batalla.NUMBER_OF_ROUNDS); // 4to argumento Número de rondas
   cmd[9] = initialPosition;
   return cmd;
 }
Esempio n. 4
0
  private void createCompileButton() {
    ImageIcon imgIcon = new ImageIcon(getClass().getResource("/images/sourcecode/bytecode.png"));
    this.compileButton = new JButton(imgIcon);
    this.compileButton.setToolTipText(Language.get("compileButton.tooltip"));
    final File basePathRobots = new File(Settings.getRobotsPath());
    compileButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              // guardar el codigo fuente
              File sourcePath = saveSourceCode();
              // COMPILA EN EL DIRECTORIO POR DEFAULT + LA RUTA DEL PACKAGE
              Collection<File> inFiles = createClassFiles(sourcePath);
              if (inFiles != null) {
                /* transformar el codigo fuente, que no tiene errores, para que los println aparezcan en una ventana.
                 * La transformaci�n no deberia generar errores.
                 */
                writeSourceFile(
                    sourcePath,
                    AgregadorDeConsola.getInstance().transformar(readSourceFile(sourcePath)));
                // volver a compilar, ahora con el codigo transformado

                inFiles = createClassFiles(sourcePath);
                if (inFiles != null) {
                  createJarFile(inFiles);

                  System.out.println("INSTALLPATH=" + Settings.getInstallPath());
                  System.out.println(
                      "SE ENVIA ROBOT:"
                          + HelperEditor.currentRobotPackage
                          + "."
                          + HelperEditor.currentRobotName);

                  // si quiere seleccionar enemigos
                  if (Settings.getProperty("level.default").equals(Language.get("level.four"))) {
                    try {
                      DialogSelectEnemies.getInstance();
                    } catch (NoEnemiesException e2) {
                      new MessageDialog(Language.get("robot.noEnemies"), MessageType.ERROR);
                    }
                    return;
                  } else {
                    callBatalla(null, null);
                  }
                } else {
                  System.out.println("Error en codigo transformado por AgregadorDeConsola");
                }
              }
            } catch (Exception e1) {
              e1.printStackTrace();
            }
          }

          /**
           * Recibe un archivo conteniendo codigo fuente java, y crea el .class correspondiente
           *
           * @param sourcePath El archivo .java
           * @return Un archivo conteniendo el path al .class generado, o null si no fue posible
           *     compilar porque hubo errores en el codigo fuente.
           */
          private Collection<File> createClassFiles(File sourcePath) throws Exception, IOException {
            Collection<File> f = CompileString.compile(sourcePath, basePathRobots);
            if (CompileString.hasError()) {
              int cantErrores = 0;
              for (Diagnostic<?> diag : CompileString.diagnostics) {
                if (!diag.getKind().equals(Kind.WARNING)) {
                  int line = (int) diag.getLineNumber();
                  int col = (int) diag.getColumnNumber();
                  if (line > 0 && col > 0) {
                    highlightCode(line, col);
                    cantErrores++;
                  }
                }
              }
              if (cantErrores > 0) {
                new MessageDialog(Language.get("compile.error"), MessageType.ERROR);
              }
              return null;
            } else {
              return f;
            }
          }

          /* crea un jar con todas las clases del robot. el nombre del jar es el nombre del robot */
          private void createJarFile(Collection<File> inFiles)
              throws FileNotFoundException, IOException {
            File jarFile = new File(basePathRobots, HelperEditor.currentRobotName + ".jar");
            if (jarFile.exists()) {
              jarFile.delete();
            }
            System.out.println("Path del JAR ==" + jarFile);
            jarFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(jarFile);
            BufferedOutputStream bo = new BufferedOutputStream(fos);

            Manifest manifest = new Manifest();
            manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
            JarOutputStream jarOutput = new JarOutputStream(fos, manifest);
            int basePathLength =
                basePathRobots.getAbsolutePath().length() + 1; // +1 para incluir al "/" final
            byte[] buf = new byte[1024];
            int anz;
            try {
              // para todas las clases...
              for (File inFile : inFiles) {
                BufferedInputStream bi = new BufferedInputStream(new FileInputStream(inFile));
                try {
                  String relative = inFile.getAbsolutePath().substring(basePathLength);
                  // copia y agrega el archivo .class al jar
                  JarEntry je2 = new JarEntry(relative);
                  jarOutput.putNextEntry(je2);
                  while ((anz = bi.read(buf)) != -1) {
                    jarOutput.write(buf, 0, anz);
                  }
                  jarOutput.closeEntry();
                } finally {
                  try {
                    bi.close();
                  } catch (IOException ignored) {
                  }
                }
              }
            } finally {
              try {
                jarOutput.close();
              } catch (IOException ignored) {
              }
              try {
                fos.close();
              } catch (IOException ignored) {
              }
              try {
                bo.close();
              } catch (IOException ignored) {
              }
            }
          }
        });
    compileButton.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseEntered(MouseEvent e) {
            e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
          }

          @Override
          public void mouseExited(MouseEvent e) {
            e.getComponent().setCursor(Cursor.getDefaultCursor());
          }
        });

    compileButton.setBounds(MIN_WIDTH, 0, MAX_WIDTH - MIN_WIDTH, BUTTON_HEIGHT);
    compileButton.setFont(smallButtonFont);
    compileButton.setAlignmentX(LEFT_ALIGNMENT);
    compileButton.setText(Language.get("compileButton.title"));
  }