Advertisement
SforzandoCF

suc

Aug 29th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package net.pulchraprospectus.installer;
  2.  
  3. import java.util.Map;
  4. import java.util.zip.ZipFile;
  5. import java.util.zip.ZipEntry;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.StringWriter;
  9. import java.io.InputStreamReader;
  10. import java.net.URLConnection;
  11. import java.net.URI;
  12. import org.json.JSONObject;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15.  
  16. public class Installer {
  17.     public static void main (String[] args) {
  18.         URLConnection con = new URI("https://pulchraprospectus.net/versions/current").toURL().openConnection();
  19.         con.connect();
  20.         File mcWin = new File(System.getProperty("user.home") + "\\AppData\\Roaming\\.minecraft");
  21.         File mcMac = new File(System.getProperty("user.home") + "/Library/Application Support/minecraft");
  22.         File mcLin = new File(System.getProperty("user.home") + ".minecraft");
  23.         File mc = getArchFile(mcWin, mcMac, mcLin);
  24.         File pp = new File(mc, "pp.zip");
  25.         if (!pp.exists()) pp.createNewFile();
  26.         FileOutputStream out = new FileOutputStream(pp);
  27.         con.getInputStream().transferTo(out);
  28.         ZipFile f = new ZipFile(pp);
  29.         ZipEntry entry = null;
  30.         f.stream().forEach((candidate) -> { if (candidate.getName().endsWith("pointers.json") && entry != null) entry = candidate; });
  31.         if (entry == null) throw new NullPointerException("No pointers.json file found");
  32.         StringWriter sw = new StringWriter();
  33.         new InputStreamReader(f.getInputStream(entry)).transferTo(sw);
  34.         copyFiles(mc, f, readPointers(new JSONObject(sw.toString())));
  35.         pp.delete();
  36.     }
  37.    
  38.     public static File getArchFile (File win, File mac, File linux) {
  39.         switch (System.getProperty("os.arch")) {
  40.             case "windows":
  41.                 return win;
  42.                 break;
  43.             case "osx":
  44.                 return mac;
  45.                 break;
  46.             case "linux":
  47.                 return linux;
  48.                 break;
  49.             default:
  50.                 return null;
  51.         }
  52.     }
  53.    
  54.     public static Map<String, String> readPointers (JSONObject j) {
  55.         try {
  56.             Map<String, String> pointers = Map.<String, String>of();
  57.             JSONArray pointerarray = j.getJSONArray("pointers");
  58.             for (Object o : pointerarray)
  59.                 if (o instanceof JSONObject)
  60.                     pointers.put(((JSONObject)o).keySet().get(0), ((JSONObject)o).getString(((JSONObject)o).keySet().get(0)));
  61.             return pointers;
  62.         } catch (JSONException je) {
  63.             throw new MalformedPointerFileException("Pointers malformed", je);
  64.         }
  65.     }
  66.    
  67.     public static void copyFiles(File src, ZipFile z, Map<String, String> pointers) {
  68.         for (String s : pointers.keySet()) {
  69.             File f = new File(src, pointers.get(s));
  70.             if (!f.exists()) f.createNewFile();
  71.             ZipEntry entry = null;
  72.             z.stream().forEach((candidate) -> { if (candidate.getName.endsWith(s) && entry == null) entry = candidate; });
  73.             if (entry == null) throw new MalformedPointerFileException("No associated file for pointer " + s);
  74.             FileOutputStream stream = new FileOutputStream(f);
  75.             z.getInputStream(entry).transferTo(stream);
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement