Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.pulchraprospectus.installer;
- import java.util.Map;
- import java.util.zip.ZipFile;
- import java.util.zip.ZipEntry;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.StringWriter;
- import java.io.InputStreamReader;
- import java.net.URLConnection;
- import java.net.URI;
- import org.json.JSONObject;
- import org.json.JSONArray;
- import org.json.JSONException;
- public class Installer {
- public static void main (String[] args) {
- URLConnection con = new URI("https://pulchraprospectus.net/versions/current").toURL().openConnection();
- con.connect();
- File mcWin = new File(System.getProperty("user.home") + "\\AppData\\Roaming\\.minecraft");
- File mcMac = new File(System.getProperty("user.home") + "/Library/Application Support/minecraft");
- File mcLin = new File(System.getProperty("user.home") + ".minecraft");
- File mc = getArchFile(mcWin, mcMac, mcLin);
- File pp = new File(mc, "pp.zip");
- if (!pp.exists()) pp.createNewFile();
- FileOutputStream out = new FileOutputStream(pp);
- con.getInputStream().transferTo(out);
- ZipFile f = new ZipFile(pp);
- ZipEntry entry = null;
- f.stream().forEach((candidate) -> { if (candidate.getName().endsWith("pointers.json") && entry != null) entry = candidate; });
- if (entry == null) throw new NullPointerException("No pointers.json file found");
- StringWriter sw = new StringWriter();
- new InputStreamReader(f.getInputStream(entry)).transferTo(sw);
- copyFiles(mc, f, readPointers(new JSONObject(sw.toString())));
- pp.delete();
- }
- public static File getArchFile (File win, File mac, File linux) {
- switch (System.getProperty("os.arch")) {
- case "windows":
- return win;
- break;
- case "osx":
- return mac;
- break;
- case "linux":
- return linux;
- break;
- default:
- return null;
- }
- }
- public static Map<String, String> readPointers (JSONObject j) {
- try {
- Map<String, String> pointers = Map.<String, String>of();
- JSONArray pointerarray = j.getJSONArray("pointers");
- for (Object o : pointerarray)
- if (o instanceof JSONObject)
- pointers.put(((JSONObject)o).keySet().get(0), ((JSONObject)o).getString(((JSONObject)o).keySet().get(0)));
- return pointers;
- } catch (JSONException je) {
- throw new MalformedPointerFileException("Pointers malformed", je);
- }
- }
- public static void copyFiles(File src, ZipFile z, Map<String, String> pointers) {
- for (String s : pointers.keySet()) {
- File f = new File(src, pointers.get(s));
- if (!f.exists()) f.createNewFile();
- ZipEntry entry = null;
- z.stream().forEach((candidate) -> { if (candidate.getName.endsWith(s) && entry == null) entry = candidate; });
- if (entry == null) throw new MalformedPointerFileException("No associated file for pointer " + s);
- FileOutputStream stream = new FileOutputStream(f);
- z.getInputStream(entry).transferTo(stream);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement