Inceptum/wrapper/src/main/java/io/gitlab/jfronny/inceptum/util/MetaHolder.java

84 lines
3.5 KiB
Java
Raw Normal View History

2021-11-02 13:43:18 +01:00
package io.gitlab.jfronny.inceptum.util;
2022-04-28 23:13:37 +02:00
import io.gitlab.jfronny.commons.ComparableVersion;
import io.gitlab.jfronny.commons.OSUtils;
import io.gitlab.jfronny.commons.serialize.gson.ComparableVersionAdapter;
import io.gitlab.jfronny.gson.GsonBuilder;
2021-11-02 13:43:18 +01:00
import io.gitlab.jfronny.inceptum.model.inceptum.InceptumVersion;
2021-11-11 18:14:51 +01:00
import java.io.File;
2021-11-02 13:43:18 +01:00
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
2021-11-11 18:14:51 +01:00
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
2021-11-02 13:43:18 +01:00
import java.nio.file.Path;
2022-01-27 17:05:32 +01:00
import java.nio.file.Paths;
2021-11-02 13:43:18 +01:00
public class MetaHolder {
public static final InceptumVersion VERSION;
public static final Path BASE_PATH;
static {
try (InputStream is = MetaHolder.class.getClassLoader().getResourceAsStream("version.json");
InputStreamReader isr = new InputStreamReader(is)) {
VERSION = new GsonBuilder()
.registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter())
.create()
.fromJson(isr, InceptumVersion.class);
if (System.getProperty("inceptum.base") == null) {
Path runDir = getRunDir();
BASE_PATH = VERSION.isRelease && !Files.exists(runDir)
? getConfigPath().resolve("Inceptum")
: runDir;
}
else {
2022-01-27 17:05:32 +01:00
BASE_PATH = Paths.get(System.getProperty("inceptum.base"));
}
2021-11-02 13:43:18 +01:00
} catch (IOException e) {
throw new RuntimeException("Could not get version info", e);
}
}
public static final Path ACCOUNTS_PATH = BASE_PATH.resolve("accounts.json");
public static final Path CONFIG_PATH = BASE_PATH.resolve("inceptum.json");
public static final Path NATIVES_DIR = BASE_PATH.resolve("natives");
public static final Path FORCE_LOAD_PATH = NATIVES_DIR.resolve("forceload");
public static final Path LIBRARIES_DIR = BASE_PATH.resolve("libraries");
public static final Path ASSETS_DIR = BASE_PATH.resolve("assets");
public static final Path INSTANCE_DIR = BASE_PATH.resolve("instances");
public static final Path CACHE_DIR = BASE_PATH.resolve("cache");
2021-11-02 13:43:18 +01:00
private static Path getConfigPath() {
2022-04-28 23:13:37 +02:00
return switch (OSUtils.TYPE) {
2022-01-27 17:05:32 +01:00
case WINDOWS -> Paths.get(System.getenv("APPDATA"));
case MAC_OS -> Paths.get(System.getProperty("user.home")).resolve("Library").resolve("Application Support");
2021-11-02 13:43:18 +01:00
case LINUX -> {
String s = System.getenv().get("XDG_CONFIG_HOME");
if (s == null)
2022-01-27 17:05:32 +01:00
yield Paths.get(System.getProperty("user.home")).resolve(".config");
2021-11-02 13:43:18 +01:00
else
2022-01-27 17:05:32 +01:00
yield Paths.get(s);
2021-11-02 13:43:18 +01:00
}
};
}
2021-11-11 20:11:20 +01:00
private static Path getRunDir() {
Path simpleRunDir = Path.of(".").resolve("run");
if (Files.exists(simpleRunDir)) return simpleRunDir;
2021-11-11 18:14:51 +01:00
URL url = MetaHolder.class.getProtectionDomain().getCodeSource().getLocation();
String p = url.getPath();
2021-11-02 13:43:18 +01:00
if (p.endsWith(".jar") && !p.endsWith("/build/libs/wrapper-" + VERSION.version + ".jar")) {
2021-11-11 18:14:51 +01:00
try {
2021-11-11 20:11:20 +01:00
return new File(url.toURI()).toPath().getParent().resolve("run");
2021-11-11 18:14:51 +01:00
} catch (URISyntaxException e) {
Utils.LOGGER.error("Could not resolve dev env run dir");
2021-11-11 20:11:20 +01:00
return simpleRunDir;
2021-11-11 18:14:51 +01:00
}
2021-11-02 13:43:18 +01:00
} else {
System.out.println("Not running in a jar, using ./run");
2021-11-11 20:11:20 +01:00
return simpleRunDir;
2021-11-02 13:43:18 +01:00
}
}
}