This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ModUpdater/src/main/java/com/thebrokenrail/modupdater/strategy/MavenStrategy.java

90 lines
3.1 KiB
Java
Raw Normal View History

2020-06-24 02:04:22 +00:00
package com.thebrokenrail.modupdater.strategy;
import com.thebrokenrail.modupdater.ModUpdater;
2020-06-25 20:56:09 +00:00
import com.thebrokenrail.modupdater.api.ConfigObject;
import com.thebrokenrail.modupdater.data.ModUpdate;
import com.thebrokenrail.modupdater.api.UpdateStrategy;
2020-06-24 02:04:22 +00:00
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;
2020-06-25 20:56:09 +00:00
import javax.annotation.Nullable;
2020-06-25 02:05:10 +00:00
import java.io.ByteArrayInputStream;
2020-06-25 01:54:28 +00:00
import java.io.IOException;
2020-06-25 13:16:27 +00:00
import java.io.InputStream;
2020-06-24 02:04:22 +00:00
import java.util.List;
2020-06-25 20:56:09 +00:00
public class MavenStrategy implements UpdateStrategy {
2020-06-24 02:04:22 +00:00
@Override
2020-06-25 20:56:09 +00:00
@Nullable
public ModUpdate run(ConfigObject obj, String oldVersion, String name) {
2020-06-24 02:04:22 +00:00
String repository;
String group;
String artifact;
try {
repository = obj.getString("repository");
group = obj.getString("group");
artifact = obj.getString("artifact");
} catch (ConfigObject.MissingValueException e) {
2020-06-25 20:56:09 +00:00
ModUpdater.log(name, e.getMessage());
2020-06-24 02:04:22 +00:00
return null;
}
2020-06-24 18:21:26 +00:00
String mavenRoot = String.format("%s/%s/%s", repository, group.replaceAll("\\.", "/"), artifact);
2020-06-24 02:04:22 +00:00
2020-06-25 01:54:28 +00:00
String data;
try {
data = Util.urlToString(mavenRoot + "/maven-metadata.xml");
} catch (IOException e) {
2020-06-25 20:56:09 +00:00
ModUpdater.log(name, e.toString());
2020-06-25 01:54:28 +00:00
return null;
}
2020-06-24 02:04:22 +00:00
Document doc;
2020-06-25 13:16:27 +00:00
try (InputStream source = new ByteArrayInputStream(data.getBytes())) {
2020-06-24 02:04:22 +00:00
SAXReader reader = new SAXReader();
2020-06-25 13:16:27 +00:00
doc = reader.read(source);
2020-06-25 20:56:09 +00:00
} catch (DocumentException | IOException e) {
ModUpdater.log(name, e.toString());
2020-06-25 13:16:27 +00:00
return null;
2020-06-24 02:04:22 +00:00
}
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) {
2020-06-25 13:16:27 +00:00
return new ModUpdate(oldVersion, newestVersion, String.format("%s/%s/%s-%s%s", mavenRoot, newestVersion, artifact, newestVersion, Util.JAR_EXTENSION), name);
2020-06-24 02:04:22 +00:00
} else {
return null;
}
} catch (VersionParsingException e) {
return null;
}
} else {
return null;
}
}
}