Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package fridgegameinstaller; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import javax.swing.JLabel; import javax.swing.JProgressBar; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.json.JSONException; import org.json.JSONObject; import java.io.FileReader; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.io.BufferedOutputStream; import java.net.URI; import java.net.URISyntaxException; import java.awt.Desktop; import java.util.Enumeration; import java.util.zip.ZipFile; import java.io.BufferedInputStream; import java.nio.charset.Charset; /** * * @author Laurin */ public class installation { private static String mcloc = null; private JLabel progmsg; private JProgressBar prgbar; private MainFrame mainFrame; public installation(MainFrame mainFrame) { this.mainFrame = mainFrame; this.progmsg = mainFrame.getOutputLabel(); this.prgbar = mainFrame.getProgressbar(); } //will be executed by GUI public void preinstall(int ram, boolean shader) { System.out.println("Installing..."); //check Modpack Update progmsg.setText("Checking for Modpack Update..."); if (true) { progmsg.setText("Locating .minecraft..."); //set .minecraft location System.out.println(DEF.sysos); if (DEF.sysos.indexOf("win") >= 0) { mcloc = System.getenv("appdata") + "/.minecraft"; } if (DEF.sysos.indexOf("mac") >= 0) { mcloc = System.getProperty("user.home") + "/Library/Application Support/minecraft"; } if (DEF.sysos.indexOf("nix") >= 0 || DEF.sysos.indexOf("nux") >= 0 || DEF.sysos.indexOf("aix") > 0) { mcloc = System.getProperty("user.home") + "/.minecraft"; } if (mcloc == null) { mainFrame.errorMsg("No support for this OS.Try to install manually.", "Error"); } //old Modpack version existing? if (checkModpackUpdate()) { System.out.println("installing.."); System.out.println(System.getenv("appdata")); uninstall(); install(ram); if (shader) { installShader(); } prgbar.setValue(100); progmsg.setText("100%"); mainFrame.errorMsg("Installation finished!", "100%"); mainFrame.setFormToPostInstallation(); try { String url = "http://www.fridgegame.com/credits"; if (Desktop.isDesktopSupported()) { Desktop.getDesktop().browse(new URI(url)); } else { Runtime.getRuntime().exec("xdg-open " + url); } } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } else { } } else { mainFrame.errorMsg( "The modpack is up to date.If you want to reinstall it to fix errors delete the fridgegame_version.fg file in your .minecraft", "Error"); mainFrame.setFormToPostInstallation(); } } //it's obvious public void install(int mb) { //check .minecraft exists File mclocf = new File(mcloc); if (!mclocf.isDirectory()) { System.out.println("wrong mc loc"); mainFrame.errorMsg("Could not find minecraft directory.", "Error"); mainFrame.setFormToPostInstallation(); } else { progmsg.setText("Checking release 1.6.4..."); //Check 1.6.4 Version exists File v164 = new File(mcloc + "/versions/1.6.4"); if (!v164.exists()) { mainFrame.errorMsg( "Could not find release 1.6.4.Start Minecraft with version 1.6.4 before installing Fridgegame.", "Error"); mainFrame.setFormToPostInstallation(); } else { progmsg.setText("Setting up Fridgegame environment..."); prgbar.setValue(2); //creating Fridgegame version folder File vfg = new File(mcloc + "/versions/Fridgegame"); vfg.mkdir(); //copy 1.6.4.jar File v164jar = new File(mcloc + "/versions/1.6.4/1.6.4.jar"); File vfgjar = new File(mcloc + "/versions/Fridgegame/Fridgegame.jar"); try { copyFolder(v164jar, vfgjar); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while dublicating 1.6.4.jar.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } progmsg.setText("Setting up natives..."); prgbar.setValue(5); File v164n = new File(mcloc + "/versions/1.6.4/1.6.4-natives"); File vfgn = new File(mcloc + "/versions/Fridgegame/Fridgegame-natives"); try { copyFolder(v164n, vfgn); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while dublicating natives.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } //init JSON progmsg.setText("Setting up basic json..."); prgbar.setValue(7); File vfgjson = new File(mcloc + "/versions/Fridgegame/Fridgegame.json"); //config json try { MCJsonConf.getJson(mcloc + "/versions/Fridgegame/Fridgegame.json", mb); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while getting json information.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } //get json obj prgbar.setValue(10); progmsg.setText("Fetshing information..."); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://api.fridgegame.com/version/"); JSONObject json; String path = ""; String scala_library_version = ""; String scala_library_dl_url = ""; String scala_compiler_version = ""; String scala_compiler_dl_url = ""; String forge_version = ""; String forge_dl_url = ""; try { CloseableHttpResponse response1 = httpclient.execute(httpGet); System.out.println(httpGet.toString()); System.out.println(response1.getStatusLine()); BufferedReader br = new BufferedReader( new InputStreamReader(response1.getEntity().getContent())); String a; String output = ""; while ((a = br.readLine()) != null) { output += a + "\n"; } System.out.println(output); try { json = new JSONObject(output); path = json.getString("path"); scala_library_version = json.getString("scala_library_version"); scala_library_dl_url = json.getString("scala_library_dl_url"); scala_compiler_version = json.getString("scala_compiler_version"); scala_compiler_dl_url = json.getString("scala_compiler_dl_url"); forge_version = json.getString("forge_version"); forge_dl_url = json.getString("forge_dl_url"); } catch (JSONException e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while fetshing information from server.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } catch (Exception e) { e.printStackTrace(); mainFrame.errorMsg("An error occured.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } //scala setup progmsg.setText("Checking for latest scala lib..."); progmsg.setText("Downloading and installing scala lib..."); prgbar.setValue(15); mainFrame.repaint(); File scalalang = new File(mcloc + "/libraries/org/scala-lang"); scalalang.mkdir(); File scalalibdir = new File(mcloc + "/libraries/org/scala-lang/scala-library"); scalalibdir.mkdir(); File scalalibverdir = new File( mcloc + "/libraries/org/scala-lang/scala-library/" + scala_library_version); scalalibverdir.mkdir(); File scalalib = new File(mcloc + "/libraries/org/scala-lang/scala-library/" + scala_library_version + "/scala-library-" + scala_library_version + ".jar"); //################## try { URL scalalibdlurl = new URL(scala_library_dl_url); org.apache.commons.io.FileUtils.copyURLToFile(scalalibdlurl, scalalib); } catch (Exception e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while downloading scala library.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } progmsg.setText("Checking for latest scala compiler..."); prgbar.setValue(30); progmsg.setText("Downloading and installing scala compiler..."); File scalacomdir = new File(mcloc + "/libraries/org/scala-lang/scala-compiler"); scalacomdir.mkdir(); File scalacomverdir = new File( mcloc + "/libraries/org/scala-lang/scala-compiler/" + scala_compiler_version); scalacomverdir.mkdir(); File scalacom = new File(mcloc + "/libraries/org/scala-lang/scala-compiler/" + scala_compiler_version + "/scala-compiler-" + scala_compiler_version + ".jar"); try { URL scalacomdlurl = new URL(scala_compiler_dl_url); org.apache.commons.io.FileUtils.copyURLToFile(scalacomdlurl, scalacom); } catch (Exception e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while downloading scala compiler.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } prgbar.setValue(50); progmsg.setText("Checking for latest Minecraft Forge..."); progmsg.setText("Downloading and installing Minecraft Forge..."); File mcfidir1 = new File(mcloc + "/libraries/net/minecraftforge"); File mcfidir2 = new File(mcloc + "/libraries/net/minecraftforge/minecraftforge"); File mcfidir3 = new File(mcloc + "/libraries/net/minecraftforge/minecraftforge/"); File mcfidir4 = new File(mcloc + "/libraries/net/minecraftforge/minecraftforge/" + forge_version); mcfidir1.mkdir(); mcfidir2.mkdir(); mcfidir3.mkdir(); mcfidir4.mkdir(); File mcfidir = new File(mcloc + "/libraries/net/minecraftforge/minecraftforge/" + forge_version + "/minecraftforge-" + forge_version + ".jar"); try { URL forgedlurl = new URL(forge_dl_url); org.apache.commons.io.FileUtils.copyURLToFile(forgedlurl, mcfidir); } catch (Exception e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while downloading Minecraft Forge.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } File fgtemp = new File(mcloc + "/fgtemp"); fgtemp.mkdir(); prgbar.setValue(65); progmsg.setText("Downloading mods(may take a while)..."); File fgzipfile = new File(mcloc + "/fgtemp/fg.zip"); try { URL fgzipdlurl = new URL(path); org.apache.commons.io.FileUtils.copyURLToFile(fgzipdlurl, fgzipfile); } catch (Exception e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while downloading mods.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } progmsg.setText("Installing mods..."); File mclocfile = new File(mcloc); prgbar.setValue(85); File mclocfilea = new File(mcloc); try { Charset CP866 = Charset.forName("CP866"); ZipFile fgzipa = new ZipFile(mcloc + "/fgtemp/fg.zip", CP866); unzip(fgzipa, mclocfilea); } catch (IOException e) { e.printStackTrace(); } if (fgtemp.exists()) { try { deletedir(fgtemp); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while deleting temps.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } } } } public boolean checkModpackUpdate() { boolean o = true; if (getLocalModpackVersion().equals(getLatestModpackVersion())) { mainFrame.errorMsg("Your Modpack is up to date.", "100%"); mainFrame.setFormToPostInstallation(); o = false; } return o; } public String getLocalModpackVersion() { System.out.println(mcloc + "/fridgegame_version.fg"); File fgver = new File(mcloc + "/fridgegame_version.fg"); String ver = null; if (fgver.exists()) { try { FileReader reader = new FileReader(fgver); char[] chars = new char[(int) fgver.length()]; reader.read(chars); ver = new String(chars); reader.close(); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while reading Modpack version.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } else { ver = "0"; } System.out.println(ver); return ver; } public String getLatestModpackVersion() { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://api.fridgegame.com/version/"); JSONObject json; String ver = ""; try { CloseableHttpResponse response1 = httpclient.execute(httpGet); System.out.println(httpGet.toString()); System.out.println(response1.getStatusLine()); BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); String a; String output = ""; while ((a = br.readLine()) != null) { output += a + "\n"; } System.out.println(output); try { json = new JSONObject(output); ver = json.getString("version"); } catch (JSONException e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while reading latest Modpack version.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } System.out.println(ver); return ver; } public void installShader() { progmsg.setText("Installing shader..."); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://api.fridgegame.com/version/"); JSONObject json; String shader_dl_url = ""; try { CloseableHttpResponse response1 = httpclient.execute(httpGet); System.out.println(httpGet.toString()); System.out.println(response1.getStatusLine()); BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); String a; String output = ""; while ((a = br.readLine()) != null) { output += a + "\n"; } System.out.println(output); try { json = new JSONObject(output); shader_dl_url = json.getString("shader_dl_url"); } catch (JSONException e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while reading latest Modpack version.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } File shaderfile = new File(mcloc + "/mods/shader.jar"); try { URL shaderdlurl = new URL(shader_dl_url); org.apache.commons.io.FileUtils.copyURLToFile(shaderdlurl, shaderfile); } catch (Exception e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while downloading mods.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } public boolean checkOldModpack() { return true; } public void uninstall() { File mcmods = new File(mcloc + "/mods"); if (mcmods.exists()) { try { deletedir(mcmods); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while cleaning mods.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } File mcconf = new File(mcloc + "/config"); if (mcconf.exists()) { try { deletedir(mcconf); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while cleaning config.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } File mcforge = new File(mcloc + "/libraries/net/minecraftforge"); if (mcforge.exists()) { try { deletedir(mcforge); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while cleaning Forge.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } File sclang = new File(mcloc + "/libraries/org/scala-lang"); if (sclang.exists()) { try { deletedir(sclang); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while cleaning scala lang.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } File fgvers = new File(mcloc + "/versions/Fridgegame"); if (fgvers.exists()) { try { deletedir(fgvers); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while cleaning release Fridgegame.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } File fgversloc = new File(mcloc + "/fridgegame_version.fg"); if (fgversloc.exists()) { try { deletedir(fgversloc); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg( "An error occured while cleaning Fridgegame_Version_File.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } File flanfile = new File(mcloc + "/Flan"); if (flanfile.exists()) { try { deletedir(flanfile); } catch (IOException e) { e.printStackTrace(); mainFrame.errorMsg("An error occured while cleaning Flan.More information in the log", "Error"); mainFrame.setFormToPostInstallation(); } } } public void deletedir(File file) throws IOException { if (file.isDirectory()) { //directory is empty, then delete it if (file.list().length == 0) { file.delete(); System.out.println("Directory is deleted : " + file.getAbsolutePath()); } else { //list all the directory contents String files[] = file.list(); for (String temp : files) { //construct the file structure File fileDelete = new File(file, temp); //recursive delete deletedir(fileDelete); } //check the directory again, if empty then delete it if (file.list().length == 0) { file.delete(); System.out.println("Directory is deleted : " + file.getAbsolutePath()); } } } else { //if file, then delete it file.delete(); System.out.println("File is deleted : " + file.getAbsolutePath()); } } public void copyFolder(File src, File dest) throws IOException { if (src.isDirectory()) { //if directory not exists, create it if (!dest.exists()) { dest.mkdir(); System.out.println("Directory copied from " + src + " to " + dest); } //list all the directory contents String files[] = src.list(); for (String file : files) { //construct the src and dest file structure File srcFile = new File(src, file); File destFile = new File(dest, file); //recursive copy copyFolder(srcFile, destFile); } } else { //if file, then copy it //Use bytes stream to support all file types InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; //copy the file content in bytes while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); System.out.println("File copied from " + src + " to " + dest); } } private static final int BUFFER_SIZE = 4096; private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } public static void unzip(ZipFile zipFile, File jiniHomeParentDir) { Enumeration files = zipFile.entries(); File f = null; FileOutputStream fos = null; while (files.hasMoreElements()) { try { ZipEntry entry = (ZipEntry) files.nextElement(); InputStream eis = zipFile.getInputStream(entry); byte[] buffer = new byte[1024]; int bytesRead = 0; f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName()); if (entry.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); f.createNewFile(); } fos = new FileOutputStream(f); while ((bytesRead = eis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); continue; } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { // ignore } } } } } }