1 package com.irtimaled.bbor.install;
3 import com.irtimaled.bbor.Versions;
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;
16 public class Installer {
17 public static void install() {
18 String version = Versions.build;
19 String mcVersion = Versions.minecraft;
21 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
22 } catch (Throwable t) {
27 String osName = getOsName();
28 File minecraftFolder = getMinecraftFolder(osName);
29 File versionFolder = new File(minecraftFolder, "versions/BBOR-" + version + "/");
30 versionFolder.mkdirs();
32 File versionJson = new File(versionFolder, "BBOR-" + version + ".json");
33 Files.copy(Installer.class.getResourceAsStream("/profile.json"), versionJson.toPath(), StandardCopyOption.REPLACE_EXISTING);
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*\\{[^}]*},?", "");
45 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
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" +
56 Files.write(profilesJson.toPath(), contents.getBytes());
58 } catch (Throwable t) {
63 String source = Installer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
64 if (source.startsWith("/") && osName.contains("win")) {
65 source = source.substring(1);
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) {
74 JOptionPane.showMessageDialog(null,
75 "Bounding Box Outline Reloaded " + version + " has been successfully installed!\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" +
86 w.toString().replace("\t", " "), "Bounding Box Outline Reloaded Installer", JOptionPane.ERROR_MESSAGE);
90 private static File getMinecraftFolder(String osName) {
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");
97 minecraftFolder = new File(System.getProperty("user.home") + "/.minecraft");
99 return minecraftFolder;
102 private static String getOsName() {
103 return System.getProperty("os.name").toLowerCase(Locale.ROOT);