testcraftupdate/src/main/java/ley/modding/tcu/Util.java

105 lines
3.5 KiB
Java
Raw Normal View History

2021-05-27 21:48:38 +02:00
package ley.modding.tcu;
2021-06-01 15:33:08 +02:00
import ley.anvil.addonscript.v1.AddonscriptJSON;
import ley.modding.tcu.model.Config;
import ley.modding.tcu.model.RelationFile;
import ley.modding.tcu.model.Release;
import ley.modding.tcu.model.Version;
2021-05-27 21:48:38 +02:00
2021-06-01 15:33:08 +02:00
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
2021-05-27 21:48:38 +02:00
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
2021-06-01 15:33:08 +02:00
import java.util.HashMap;
2021-05-27 21:48:38 +02:00
import java.util.List;
2021-06-01 15:33:08 +02:00
import java.util.Map;
2021-05-27 21:48:38 +02:00
public class Util {
2021-06-01 15:33:08 +02:00
public static Config getConfig(File local) throws IOException {
2021-05-27 21:48:38 +02:00
Reader r = new FileReader(local);
2021-06-01 15:33:08 +02:00
Config cfg = Config.fromJSON(r);
2021-05-27 21:48:38 +02:00
r.close();
2021-06-01 15:33:08 +02:00
return cfg;
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
public static Version checkVersion(Config conf) {
GiteaAPI api = conf.getAPI();
List<Release> releases = api.getReleases();
return new Version(releases.get(0).tag, conf.version, releases.get(0).zipUrl);
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
public static String resolve(String url) {
try {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
con.setInstanceFollowRedirects(false);
int status = con.getResponseCode();
if (status == 301 || status == 302) {
String loc = con.getHeaderField("location");
return loc;
} else {
return url;
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
} catch (IOException e) {
return url;
2021-05-27 21:48:38 +02:00
}
}
2021-06-01 15:33:08 +02:00
public static List<RelationFile> getRelations(AddonscriptJSON as) {
List<RelationFile> relations = new ArrayList<>();
Map<String, String> repos = new HashMap<>();
2021-05-27 21:48:38 +02:00
2021-06-01 15:33:08 +02:00
for (AddonscriptJSON.Repository r : as.repositories) {
String url = r.url;
if (!url.endsWith("/")) {
url += "/";
}
repos.put(r.id, url);
}
for (AddonscriptJSON.Relation r : as.versions.get(0).relations) {
if (r.type.equals("modloader"))
continue;
RelationFile rel = new RelationFile();
rel.id = r.id;
rel.dir = r.file.installer.split(":")[1];
if (r.file.link != null && !r.file.link.isEmpty()) {
rel.url = r.file.link;
} else {
String[] parts = r.file.artifact.split(":");
if (parts[0].equals("curse.maven")) {
parts[1] = parts[1] + "-" + parts[1];
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
parts[0] = parts[0].replace('.', '/');
rel.url = repos.get(r.file.repository) + parts[0] + "/" + parts[1] + "/" + parts[2] + "/" + parts[1] + "-" + parts[2];
if (parts.length == 4) {
rel.url += "-";
rel.url += parts[3];
}
rel.url += ".jar";
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
relations.add(rel);
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
return relations;
}
2021-05-27 21:48:38 +02:00
2021-06-01 15:33:08 +02:00
public static List<RelationFile> getToRemove(List<RelationFile> old, List<RelationFile> newrel) {
List<RelationFile> oldRel = new ArrayList<>(old);
oldRel.removeAll(newrel);
return oldRel;
2021-05-27 21:48:38 +02:00
}
2021-06-01 15:33:08 +02:00
public static List<RelationFile> getToAdd(List<RelationFile> old, List<RelationFile> newrel) {
List<RelationFile> newRel = new ArrayList<>(newrel);
newRel.removeAll(old);
return newRel;
2021-05-27 21:48:38 +02:00
}
}