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