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