ModUpdater/src/main/java/com/thebrokenrail/modupdater/strategy/MavenStrategy.java

79 lines
2.8 KiB
Java

package com.thebrokenrail.modupdater.strategy;
import com.thebrokenrail.modupdater.ModUpdater;
import com.thebrokenrail.modupdater.util.ConfigObject;
import com.thebrokenrail.modupdater.util.ModUpdate;
import com.thebrokenrail.modupdater.util.ModUpdateStrategy;
import com.thebrokenrail.modupdater.util.Util;
import net.fabricmc.loader.api.SemanticVersion;
import net.fabricmc.loader.util.version.VersionParsingException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class MavenStrategy implements ModUpdateStrategy {
@Override
public ModUpdate checkForUpdate(ConfigObject obj, String oldVersion, String name) {
String repository;
String group;
String artifact;
try {
repository = obj.getString("repository");
group = obj.getString("group");
artifact = obj.getString("artifact");
} catch (ConfigObject.MissingValueException e) {
ModUpdater.invalidModUpdaterConfig(name);
return null;
}
String mavenRoot = repository + '/' + group.replaceAll("\\.", "/") + '/' + artifact;
Document doc;
try {
SAXReader reader = new SAXReader();
doc = reader.read(new URL(mavenRoot + "/maven-metadata.xml"));
} catch (MalformedURLException | DocumentException e) {
ModUpdater.invalidModUpdaterConfig(name);
return null;
}
List<Node> versions = doc.selectNodes("/metadata/versioning/versions/*");
String newestVersion = null;
for (Node node : versions) {
String version = node.getText();
if (Util.isVersionCompatible(version)) {
if (newestVersion != null) {
try {
if (SemanticVersion.parse(version).compareTo(SemanticVersion.parse(newestVersion)) > 0) {
newestVersion = version;
}
} catch (VersionParsingException ignored) {
}
} else {
newestVersion = version;
}
}
}
if (newestVersion != null) {
try {
if (SemanticVersion.parse(newestVersion).compareTo(SemanticVersion.parse(oldVersion)) > 0) {
return new ModUpdate(oldVersion, newestVersion, mavenRoot + '/' + newestVersion + '/' + artifact + '-' + newestVersion + Util.JAR_EXTENSION, name);
} else {
return null;
}
} catch (VersionParsingException e) {
return null;
}
} else {
return null;
}
}
}