]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/install/Installer.java
327a81334781dd612a05bcf766d682e2367ffdf6
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / install / Installer.java
1 package com.irtimaled.bbor.install;
2
3 import javax.swing.*;
4 import java.io.File;
5 import java.io.PrintWriter;
6 import java.io.StringWriter;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.nio.file.StandardCopyOption;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 import java.util.Locale;
13
14 public class Installer {
15     public static void install(final String version, final String mcVersion) {
16         try {
17             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
18         } catch (Throwable t) {
19             t.printStackTrace();
20         }
21
22         try {
23             String osName = getOsName();
24             File minecraftFolder = getMinecraftFolder(osName);
25             File versionFolder = new File(minecraftFolder, "versions/BBOR-" + version + "/");
26             versionFolder.mkdirs();
27
28             File versionJson = new File(versionFolder, "BBOR-" + version + ".json");
29             Files.copy(Installer.class.getResourceAsStream("/profile.json"), versionJson.toPath(), StandardCopyOption.REPLACE_EXISTING);
30
31             try {
32                 File profilesJson = new File(minecraftFolder, "launcher_profiles.json");
33                 if (profilesJson.exists()) {
34                     String identifier = "\"bbor-" + mcVersion + "\"";
35                     String contents = new String(Files.readAllBytes(profilesJson.toPath()));
36                     if (contents.contains(identifier)) {
37                         contents = contents.replaceAll(",\n?\\s*" + identifier + "\\s*:\\s*\\{[^}]*},", ",");
38                         contents = contents.replaceAll(",?\n?\\s*" + identifier + "\\s*:\\s*\\{[^}]*},?", "");
39                     }
40
41                     String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
42
43                     contents = contents.replaceAll("([}],\n?\\s*\"profiles\"\\s*:\\s*[{]\n?)", "$1" +
44                             "    " + identifier + ": {\n" +
45                             "      \"name\": \"Bounding Box Outline Reloaded\",\n" +
46                             "      \"type\": \"custom\",\n" +
47                             "      \"created\": \"" + date + "T00:00:00.000Z\",\n" +
48                             "      \"lastUsed\": \"2100-01-01T00:00:00.000Z\",\n" +
49                             "      \"lastVersionId\": \"BBOR-" + version + "\"\n" +
50                             "    },\n");
51
52                     Files.write(profilesJson.toPath(), contents.getBytes());
53                 }
54             } catch (Throwable t) {
55                 t.printStackTrace();
56             }
57
58             try {
59                 String source = Installer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
60                 if (source.startsWith("/") && osName.contains("win")) {
61                     source = source.substring(1);
62                 }
63                 File mainJar = new File(minecraftFolder, "libraries/com/irtimaled/bbor/" + version + "/bbor-" + version + ".jar");
64                 mainJar.getParentFile().mkdirs();
65                 Files.copy(Paths.get(source), mainJar.toPath(), StandardCopyOption.REPLACE_EXISTING);
66             } catch (Throwable t) {
67                 t.printStackTrace();
68             }
69
70             JOptionPane.showMessageDialog(null,
71                     "Bounding Box Outline Reloaded " + version + " has been successfully installed!\n" +
72                             "\n" +
73                             "Re-open the Minecraft Launcher to see it in the dropdown.",
74                     "Bounding Box Outline Reloaded Installer", JOptionPane.INFORMATION_MESSAGE);
75         } catch (Throwable t) {
76             StringWriter w = new StringWriter();
77             t.printStackTrace(new PrintWriter(w));
78             JOptionPane.showMessageDialog(null,
79                     "An error occured while installing Bounding Box Outline Reloaded, please report this to the issue\n" +
80                             "tracker (https://github.com/irtimaled/BoundingBoxOutlineReloaded/issues):\n" +
81                             "\n" +
82                             w.toString().replace("\t", "    "), "Bounding Box Outline Reloaded Installer", JOptionPane.ERROR_MESSAGE);
83         }
84     }
85
86     private static File getMinecraftFolder(String osName) {
87         File minecraftFolder;
88         if (osName.contains("win")) {
89             minecraftFolder = new File(System.getenv("APPDATA") + "/.minecraft");
90         } else if (osName.contains("mac")) {
91             minecraftFolder = new File(System.getProperty("user.home") + "/Library/Application Support/minecraft");
92         } else {
93             minecraftFolder = new File(System.getProperty("user.home") + "/.minecraft");
94         }
95         return minecraftFolder;
96     }
97
98     private static String getOsName() {
99         return System.getProperty("os.name").toLowerCase(Locale.ROOT);
100     }
101 }