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() {
19 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
20 } catch (Throwable t) {
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();
31 File versionJson = new File(versionFolder, version + ".json");
32 Files.copy(Installer.class.getResourceAsStream("/profile.json"), versionJson.toPath(), StandardCopyOption.REPLACE_EXISTING);
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*\\{[^}]*},?", "");
44 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
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" +
55 Files.write(profilesJson.toPath(), contents.getBytes());
57 } catch (Throwable t) {
62 String source = Installer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
63 if (source.startsWith("/") && osName.contains("win")) {
64 source = source.substring(1);
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) {
73 JOptionPane.showMessageDialog(null,
74 "Bounding Box Outline Reloaded " + Versions.build + " for " + Versions.minecraft + " has been successfully installed!\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" +
85 w.toString().replace("\t", " "), "Bounding Box Outline Reloaded Installer", JOptionPane.ERROR_MESSAGE);
89 private static File getMinecraftFolder(String osName) {
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");
96 minecraftFolder = new File(System.getProperty("user.home") + "/.minecraft");
98 return minecraftFolder;
101 private static String getOsName() {
102 return System.getProperty("os.name").toLowerCase(Locale.ROOT);