2021-11-02 13:43:18 +01:00
|
|
|
package io.gitlab.jfronny.inceptum.util;
|
|
|
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import io.gitlab.jfronny.inceptum.gson.ComparableVersionAdapter;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.ComparableVersion;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.InceptumVersion;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2021-11-11 17:59:35 +01:00
|
|
|
import java.nio.file.Files;
|
2021-11-02 13:43:18 +01:00
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
|
|
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);
|
2021-11-11 17:59:35 +01:00
|
|
|
Path runDir = getDir().resolve("run");
|
|
|
|
BASE_PATH = VERSION.isPublic && !Files.exists(runDir)
|
2021-11-02 13:43:18 +01:00
|
|
|
? getConfigPath().resolve("Inceptum")
|
2021-11-11 17:59:35 +01:00
|
|
|
: runDir;
|
2021-11-02 13:43:18 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Could not get version info", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Path getConfigPath() {
|
|
|
|
return switch (OSCheck.OS) {
|
|
|
|
case WINDOWS -> Path.of(System.getenv("APPDATA"));
|
|
|
|
case MAC_OS -> Path.of(System.getProperty("user.home")).resolve("Library").resolve("Application Support");
|
|
|
|
case LINUX -> {
|
|
|
|
String s = System.getenv().get("XDG_CONFIG_HOME");
|
|
|
|
if (s == null)
|
|
|
|
yield Path.of(System.getProperty("user.home")).resolve(".config");
|
|
|
|
else
|
|
|
|
yield Path.of(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Path getDir() {
|
|
|
|
String p = MetaHolder.class.getProtectionDomain().getCodeSource().getLocation().getPath();
|
|
|
|
if (p.endsWith(".jar") && !p.endsWith("/build/libs/wrapper-" + VERSION.version + ".jar")) {
|
|
|
|
return Path.of(p).getParent();
|
|
|
|
} else {
|
|
|
|
System.out.println("Not running in a jar, using ./run");
|
|
|
|
return Path.of(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|