2020-06-24 02:04:22 +00:00
|
|
|
package com.thebrokenrail.modupdater.util;
|
|
|
|
|
|
|
|
import com.mojang.bridge.game.GameVersion;
|
2020-06-25 20:56:09 +00:00
|
|
|
import com.thebrokenrail.modupdater.api.ConfigObject;
|
|
|
|
import com.thebrokenrail.modupdater.api.impl.ConfigObjectHardcoded;
|
2020-06-24 02:04:22 +00:00
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
import net.fabricmc.loader.api.ModContainer;
|
|
|
|
import net.minecraft.MinecraftVersion;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.URL;
|
2020-06-25 20:56:09 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-06-24 02:04:22 +00:00
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
public class Util {
|
2020-06-25 01:54:28 +00:00
|
|
|
public static String urlToString(String urlStr) throws IOException {
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
URL url = new URL(urlStr);
|
2020-06-24 02:04:22 +00:00
|
|
|
|
2020-06-25 01:54:28 +00:00
|
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()))) {
|
|
|
|
String line;
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
stringBuilder.append(line);
|
|
|
|
stringBuilder.append('\n');
|
2020-06-24 02:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 01:54:28 +00:00
|
|
|
|
|
|
|
return stringBuilder.toString();
|
2020-06-24 02:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final String JAR_EXTENSION = ".jar";
|
|
|
|
|
|
|
|
public static String getVersionFromFileName(String fileName) {
|
2020-06-24 18:03:02 +00:00
|
|
|
while (!Character.isDigit(fileName.charAt(0))) {
|
|
|
|
int index = fileName.indexOf("-");
|
|
|
|
fileName = fileName.substring(index != -1 ? index + 1 : 0);
|
|
|
|
}
|
2020-06-24 02:04:22 +00:00
|
|
|
if (fileName.endsWith(JAR_EXTENSION)) {
|
|
|
|
fileName = fileName.substring(0, fileName.length() - JAR_EXTENSION.length());
|
|
|
|
}
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getMinecraftSemanticVersion() {
|
|
|
|
Optional<ModContainer> mod = FabricLoader.getInstance().getModContainer("minecraft");
|
|
|
|
if (mod.isPresent()) {
|
|
|
|
return mod.get().getMetadata().getVersion().getFriendlyString();
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String minecraftVersionSemantic = null;
|
|
|
|
private static GameVersion minecraftVersion = null;
|
|
|
|
|
|
|
|
private static void updateMinecraftVersion() {
|
|
|
|
if (minecraftVersionSemantic == null) {
|
|
|
|
minecraftVersionSemantic = getMinecraftSemanticVersion();
|
|
|
|
}
|
|
|
|
if (minecraftVersion == null) {
|
|
|
|
minecraftVersion = MinecraftVersion.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:50:48 +00:00
|
|
|
private static String getMajorVersion() {
|
|
|
|
updateMinecraftVersion();
|
|
|
|
String[] parts = minecraftVersion.getReleaseTarget().split("\\.");
|
|
|
|
if (parts.length > 1) {
|
|
|
|
return String.format("%s.%s", parts[0], parts[1]);
|
|
|
|
} else {
|
|
|
|
return minecraftVersion.getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 02:04:22 +00:00
|
|
|
private static boolean isVersionCompatible(String versionStr, char prefix) {
|
|
|
|
updateMinecraftVersion();
|
2020-06-25 12:50:48 +00:00
|
|
|
return versionStr.endsWith(prefix + minecraftVersionSemantic) || versionStr.endsWith(prefix + minecraftVersion.getReleaseTarget()) || versionStr.endsWith(prefix + getMajorVersion());
|
2020-06-24 02:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isVersionCompatible(String versionStr) {
|
|
|
|
return isVersionCompatible(versionStr,'+') || isVersionCompatible(versionStr, '-');
|
|
|
|
}
|
|
|
|
|
2020-06-24 18:21:26 +00:00
|
|
|
public static boolean isFileCompatible(String fileName) {
|
2020-06-24 20:22:00 +00:00
|
|
|
return !fileName.endsWith("-dev" + JAR_EXTENSION) && !fileName.endsWith("-sources" + JAR_EXTENSION) && !fileName.endsWith("-sources-dev" + JAR_EXTENSION) && fileName.endsWith(JAR_EXTENSION);
|
2020-06-24 18:21:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 02:04:22 +00:00
|
|
|
public static GameVersion getMinecraftVersion() {
|
|
|
|
updateMinecraftVersion();
|
|
|
|
return minecraftVersion;
|
|
|
|
}
|
2020-06-25 20:56:09 +00:00
|
|
|
|
|
|
|
public static ConfigObject getHardcodedConfig(String modID) {
|
|
|
|
switch (modID) {
|
|
|
|
case "fabric": {
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
2020-06-25 22:24:27 +00:00
|
|
|
map.put("strategy", "curseforge");
|
|
|
|
map.put("projectID", 306612);
|
2020-06-25 20:56:09 +00:00
|
|
|
return new ConfigObjectHardcoded(map);
|
|
|
|
}
|
|
|
|
case "modmenu": {
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
map.put("strategy", "curseforge");
|
|
|
|
map.put("projectID", 308702);
|
|
|
|
return new ConfigObjectHardcoded(map);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 02:04:22 +00:00
|
|
|
}
|