/* * This file is part of FTB Launcher. * * Copyright © 2012-2016, FTB Launcher Contributors <https://github.com/Slowpoke101/FTBLaunch/> * FTB Launcher is licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.ftb.workers; import static net.ftb.download.Locations.MAPS; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.security.NoSuchAlgorithmException; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; import net.ftb.data.Map; import net.ftb.data.Settings; import net.ftb.gui.dialogs.MapOverwriteDialog; import net.ftb.log.Logger; import net.ftb.util.DownloadUtils; import net.ftb.util.FTBFileUtils; import net.ftb.util.OSUtils; import net.ftb.util.TrackerUtils; public class MapManagerWorker extends SwingWorker<Boolean, Void> { private static boolean overwrite; private double downloadedPerc; public MapManagerWorker (Boolean overwrite) { MapManagerWorker.overwrite = overwrite; } @Override protected Boolean doInBackground () throws Exception { String installPath = Settings.getSettings().getInstallPath(); Map map = Map.getSelectedMap(); if (new File(installPath, map.getSelectedCompatible() + "/minecraft/saves/" + map.getMapName()).exists()) { MapOverwriteDialog dialog = new MapOverwriteDialog(); dialog.setVisible(true); if (overwrite) { FTBFileUtils.delete(new File(installPath, map.getSelectedCompatible() + "/minecraft/saves/" + map.getMapName())); } else { Logger.logInfo("Canceled map installation."); return false; } } downloadMap(map.getUrl(), map.getMapName()); return false; } public void downloadUrl (String filename, String urlString) throws IOException, NoSuchAlgorithmException { BufferedInputStream in = null; FileOutputStream fout = null; try { URL url_ = new URL(urlString); in = new BufferedInputStream(url_.openStream()); fout = new FileOutputStream(filename); byte data[] = new byte[1024]; int count, amount = 0, steps = 0, mapSize = url_.openConnection().getContentLength(); SwingUtilities.invokeLater(new Runnable() { public void run () { setProgressBarMaximum(10000); } }); while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); downloadedPerc += (count * 1.0 / mapSize) * 100; amount += count; steps++; if (steps > 100) { steps = 0; final String txt = (amount / 1024) + "Kb / " + (mapSize / 1024) + "Kb"; final int perc = (int)downloadedPerc * 100; SwingUtilities.invokeLater(new Runnable() { public void run () { setProgressBarValue(perc); setLabelText(txt); } }); } } } catch (Exception e) { Logger.logError(e.getMessage(), e); } finally { in.close(); fout.flush(); fout.close(); } } protected void downloadMap (String mapName, String dir) throws IOException, NoSuchAlgorithmException { Logger.logInfo("Downloading Map"); String installPath = OSUtils.getCacheStorageLocation(); Map map = Map.getSelectedMap(); new File(installPath + "/" + MAPS + dir + "/").mkdirs(); new File(installPath + "/" + MAPS + dir + "/" + mapName).createNewFile(); downloadUrl(installPath + "/" + MAPS + dir + "/" + mapName, DownloadUtils.getCreeperhostLink(MAPS + dir + "/" + map.getVersion().replace(".", "_") + "/" + mapName)); FTBFileUtils.extractZipTo(installPath + "/" + MAPS + dir + "/" + mapName, installPath + "/" + MAPS + dir); installMap(mapName, dir); } protected void installMap (String mapName, String dir) throws IOException { Logger.logInfo("Installing Map"); String installPath = Settings.getSettings().getInstallPath(); String tempPath = OSUtils.getCacheStorageLocation(); Map map = Map.getSelectedMap(); new File(installPath, map.getSelectedCompatible() + "/minecraft/saves/" + dir).mkdirs(); FTBFileUtils.copyFolder(new File(tempPath, MAPS + dir + "/" + dir), new File(installPath, map.getSelectedCompatible() + "/minecraft/saves/" + dir)); FTBFileUtils.copyFile(new File(tempPath, MAPS + dir + "/" + "version"), new File(installPath, map.getSelectedCompatible() + "/minecraft/saves/" + dir + "/version")); TrackerUtils.sendPageView(map.getName() + " Install", "Maps / " + map.getName()); } public void setLabelText (String s) { } public void setProgressBarMaximum (int i) { } public void setProgressBarValue (int i) { } }