public class SpoutcraftYML {
  private static volatile boolean updated = false;
  private static File spoutcraftYML =
      new File(Utils.getWorkingDirectory(), "spoutcraft" + File.separator + "spoutcraft.yml");
  private static Object key = new Object();

  public static YAMLProcessor getSpoutcraftYML() {
    updateSpoutcraftYMLCache();
    YAMLProcessor config = new YAMLProcessor(spoutcraftYML, false);
    try {
      config.load();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return config;
  }

  public static void updateSpoutcraftYMLCache() {
    if (!updated) {
      synchronized (key) {
        String urlName =
            MirrorUtils.getMirrorUrl("spoutcraft.yml", "http://get.spout.org/spoutcraft.yml", null);
        if (urlName != null) {
          try {
            int selected = -1;
            if (spoutcraftYML.exists()) {
              try {
                YAMLProcessor config = new YAMLProcessor(spoutcraftYML, false);
                config.load();
                selected = config.getInt("current", -1);
              } catch (Exception ex) {
                ex.printStackTrace();
              }
            }

            URL url = new URL(urlName);
            HttpURLConnection con = (HttpURLConnection) (url.openConnection());
            System.setProperty("http.agent", "");
            con.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30");
            Utils.copy(con.getInputStream(), new FileOutputStream(spoutcraftYML));

            YAMLProcessor config = new YAMLProcessor(spoutcraftYML, false);
            config.load();
            config.setProperty("current", selected);
            config.setProperty("launcher", Main.getBuild("launcher-version"));
            config.save();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        updated = true;
      }
    }
  }
}
 public static Download downloadFile(
     String url, String output, String cacheName, String md5, DownloadListener listener)
     throws IOException {
   int tries = Settings.getLoginTries();
   File outputFile = null;
   Download download = null;
   while (tries > 0) {
     System.out.println("Starting download of " + url + ", with " + tries + " tries remaining");
     tries--;
     download = new Download(url, output);
     download.setListener(listener);
     download.run();
     if (!download.isSuccess()) {
       if (download.getOutFile() != null) {
         download.getOutFile().delete();
       }
       System.err.println("Download of " + url + " Failed!");
       if (listener != null)
         listener.stateChanged("Download Failed, retries remaining: " + tries, 0F);
     } else {
       if (md5 != null) {
         String resultMD5 = MD5Utils.getMD5(download.getOutFile());
         System.out.println("Expected MD5: " + md5 + " Calculated MD5: " + resultMD5);
         if (resultMD5.equals(md5)) {
           outputFile = download.getOutFile();
           break;
         }
       } else {
         outputFile = download.getOutFile();
         break;
       }
     }
   }
   if (outputFile == null) {
     throw new IOException("Failed to download " + url);
   }
   if (cacheName != null) {
     Utils.copy(outputFile, new File(Launcher.getGameUpdater().getBinCacheDir(), cacheName));
   }
   return download;
 }
  @Override
  protected Object doInBackground() throws Exception {
    loginFrame.getProgressBar().setVisible(true);
    loginFrame.getProgressBar().setString("Connecting to www.minecraft.net...");
    try {
      values = Utils.doLogin(user, pass, loginFrame.getProgressBar());
      Launcher.getGameUpdater().setMinecraftUser(values[2].trim());
      Launcher.getGameUpdater().setMinecraftSession(values[3].trim());
      Launcher.getGameUpdater().setDownloadTicket(values[1].trim());
      Launcher.getGameUpdater().setMinecraftPass(pass);

      loginFrame.onRawEvent(Event.SUCESSFUL_LOGIN);
      return true;
    } catch (BadLoginException e) {
      loginFrame.onRawEvent(Event.BAD_LOGIN);
    } catch (MinecraftUserNotPremiumException e) {
      loginFrame.onRawEvent(Event.USER_NOT_PREMIUM);
      loginFrame.getProgressBar().setVisible(false);
    } catch (MCNetworkException e) {
      UserPasswordInformation info = null;

      for (String username : loginFrame.usernames.keySet()) {
        if (username.equalsIgnoreCase(user)) {
          info = loginFrame.usernames.get(username);
          break;
        }
      }

      boolean authFailed = (info == null);

      if (!authFailed) {
        if (info.isHash) {
          try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(pass.getBytes());
            for (int i = 0; i < hash.length; i++) {
              if (hash[i] != info.passwordHash[i]) {
                authFailed = true;
                break;
              }
            }
          } catch (NoSuchAlgorithmException ex) {
            authFailed = true;
          }
        } else {
          authFailed = !(pass.equals(info.password));
        }
      }

      if (authFailed) {
        loginFrame.offline = false;
        loginFrame.onRawEvent(Event.MINECRAFT_NETWORK_DOWN);
      } else {
        loginFrame.offline = true;
        loginFrame.onRawEvent(Event.MINECRAFT_NETWORK_DOWN);
      }
      this.cancel(true);
      loginFrame.getProgressBar().setVisible(false);
    } catch (OutdatedMCLauncherException e) {
      JOptionPane.showMessageDialog(
          loginFrame.getParent(),
          "Incompatible Login Version. Contact Spout about updating the Launcher!");
      loginFrame.getProgressBar().setVisible(false);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      this.cancel(true);
      loginFrame.getProgressBar().setVisible(false);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  @Override
  public void runGame(String user, String session, String downloadTicket) {
    Dimension size;
    if (OperatingSystem.getOS() == OperatingSystem.WINDOWS_8) {
      size = new Dimension(900, 540);
    } else {
      size = new Dimension(880, 520);
    }

    Dimension currentSize = Launcher.getSkin().getLoginFrame().getSize();
    Point location = Launcher.getSkin().getLoginFrame().getLocation();
    Point centeredLoc =
        new Point(
            location.x + (currentSize.width - size.width) / 2,
            location.y + (currentSize.height - size.height) / 2);

    this.setLocation(centeredLoc);
    this.setSize(size);

    ((SimpleGameUpdater) Launcher.getGameUpdater()).setWaiting(true);
    while (!((SimpleGameUpdater) Launcher.getGameUpdater()).isFinished()) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException ignore) {
      }
    }

    Applet applet = null;
    try {
      applet = MinecraftLauncher.getMinecraftApplet();
    } catch (CorruptedMinecraftJarException corruption) {
      corruption.printStackTrace();
    } catch (MinecraftVerifyException verify) {
      Launcher.clearCache();
      JOptionPane.showMessageDialog(
          getParent(),
          "Your Minecraft installation is corrupt, but has been cleaned. \nTry to login again. If that fails, close and \nrestart the appplication.");
      this.setVisible(false);
      this.dispose();
      return;
    }
    if (applet == null) {
      String message = "Failed to launch Spoutcraft!";
      this.setVisible(false);
      JOptionPane.showMessageDialog(getParent(), message);
      this.dispose();
      return;
    }

    StartupParameters params = Utils.getStartupParameters();

    minecraft = new MinecraftAppletEnglober(applet);
    minecraft.addParameter("username", user);
    minecraft.addParameter("sessionid", session);
    minecraft.addParameter("downloadticket", downloadTicket);
    minecraft.addParameter("spoutcraftlauncher", "true");
    minecraft.addParameter("portable", params.isPortable() + "");
    if (params.getServer() != null) {
      minecraft.addParameter("server", params.getServer());
      if (params.getPort() != null) {
        minecraft.addParameter("port", params.getPort());
      } else {
        minecraft.addParameter("port", "25565");
      }
    }
    if (params.getProxyHost() != null) {
      minecraft.addParameter("proxy_host", params.getProxyHost());
    }
    if (params.getProxyPort() != null) {
      minecraft.addParameter("proxy_port", params.getProxyPort());
    }
    if (params.getProxyUser() != null) {
      minecraft.addParameter("proxy_user", params.getProxyUser());
    }
    if (params.getProxyPassword() != null) {
      minecraft.addParameter("proxy_pass", params.getProxyPassword());
    }

    applet.setStub(minecraft);
    this.add(minecraft);

    validate();
    this.setVisible(true);
    minecraft.init();
    minecraft.setSize(getWidth(), getHeight());
    minecraft.start();
    Launcher.getSkin().getLoginFrame().onRawEvent(Event.GAME_LAUNCH);
    return;
  }