1 package com.irtimaled.bbor.install;
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;
14 public class Installer {
15 public static void install(final String version, final String mcVersion) {
17 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
18 } catch (Throwable t) {
23 String osName = getOsName();
24 File minecraftFolder = getMinecraftFolder(osName);
25 File versionFolder = new File(minecraftFolder, "versions/BBOR-" + version + "/");
26 versionFolder.mkdirs();
28 File versionJson = new File(versionFolder, "BBOR-" + version + ".json");
29 Files.copy(Installer.class.getResourceAsStream("/profile.json"), versionJson.toPath(), StandardCopyOption.REPLACE_EXISTING);
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*\\{[^}]*},?", "");
41 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
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" +
52 Files.write(profilesJson.toPath(), contents.getBytes());
54 } catch (Throwable t) {
59 String source = Installer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
60 if (source.startsWith("/") && osName.contains("win")) {
61 source = source.substring(1);
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) {
70 JOptionPane.showMessageDialog(null,
71 "Bounding Box Outline Reloaded " + version + " has been successfully installed!\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" +
82 w.toString().replace("\t", " "), "Bounding Box Outline Reloaded Installer", JOptionPane.ERROR_MESSAGE);
86 private static File getMinecraftFolder(String osName) {
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");
93 minecraftFolder = new File(System.getProperty("user.home") + "/.minecraft");
95 return minecraftFolder;
98 private static String getOsName() {
99 return System.getProperty("os.name").toLowerCase(Locale.ROOT);