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

111 lines
3.9 KiB
Java
Raw Normal View History

2020-06-24 02:04:22 +00:00
package com.thebrokenrail.modupdater.strategy;
import com.mojang.bridge.game.GameVersion;
import com.squareup.moshi.JsonAdapter;
2020-06-24 18:21:26 +00:00
import com.squareup.moshi.JsonDataException;
2020-06-24 02:04:22 +00:00
import com.squareup.moshi.Moshi;
import com.thebrokenrail.modupdater.ModUpdater;
2020-06-25 20:56:09 +00:00
import com.thebrokenrail.modupdater.api.ConfigObject;
import com.thebrokenrail.modupdater.api.UpdateStrategy;
2020-06-28 17:43:17 +00:00
import com.thebrokenrail.modupdater.data.ModUpdate;
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;
2020-06-25 20:56:09 +00:00
import javax.annotation.Nullable;
2020-06-24 02:04:22 +00:00
import java.io.IOException;
import java.util.Arrays;
2020-06-25 20:56:09 +00:00
public class CurseForgeStrategy implements UpdateStrategy {
2020-06-24 02:04:22 +00:00
@SuppressWarnings({"unused", "MismatchedReadAndWriteOfArray"})
private static class CurseForgeFile {
private String fileName;
private String downloadUrl;
private String[] gameVersion;
}
2020-06-27 17:56:22 +00:00
private final JsonAdapter<CurseForgeFile[]> jsonAdapter;
public CurseForgeStrategy() {
Moshi moshi = new Moshi.Builder().build();
jsonAdapter = moshi.adapter(CurseForgeFile[].class);
}
2020-06-24 02:04:22 +00:00
@Override
2020-06-25 20:56:09 +00:00
@Nullable
2020-08-05 17:06:45 +00:00
public ModUpdate run(ConfigObject obj, String oldVersion, String name, String id) {
2020-06-24 02:04:22 +00:00
int projectID;
try {
projectID = obj.getInt("projectID");
} catch (ConfigObject.MissingValueException e) {
2020-06-27 17:56:22 +00:00
ModUpdater.logWarn(name, e.getMessage());
2020-06-24 02:04:22 +00:00
return null;
}
2020-06-25 01:54:28 +00:00
String data;
2020-06-24 02:04:22 +00:00
try {
2020-06-25 01:54:28 +00:00
data = Util.urlToString("https://addons-ecs.forgesvc.net/api/v2/addon/" + projectID + "/files");
2020-06-24 02:04:22 +00:00
} catch (IOException e) {
2020-06-27 17:56:22 +00:00
ModUpdater.logWarn(name, e.toString());
2020-06-24 02:04:22 +00:00
return null;
2020-06-25 01:54:28 +00:00
}
CurseForgeFile[] files;
try {
files = jsonAdapter.fromJson(data);
} catch (JsonDataException | IOException e) {
2020-06-27 17:56:22 +00:00
ModUpdater.logWarn(name, e.toString());
2020-06-24 18:21:26 +00:00
return null;
2020-06-24 02:04:22 +00:00
}
if (files == null) {
return null;
}
String versionStr;
GameVersion version = Util.getMinecraftVersion();
if (version.isStable()) {
versionStr = version.getName();
} else {
versionStr = version.getReleaseTarget() + "-Snapshot";
}
2020-07-25 18:10:06 +00:00
boolean strict = isStrict(obj);
2020-06-24 02:04:22 +00:00
CurseForgeFile newestFile = null;
for (CurseForgeFile file : files) {
2020-06-24 18:21:26 +00:00
if (Util.isFileCompatible(file.fileName)) {
String fileVersion = Util.getVersionFromFileName(file.fileName);
2020-08-05 17:06:45 +00:00
if ((Arrays.asList(file.gameVersion).contains(versionStr) && !strict) || Util.isVersionCompatible(id, fileVersion, strict)) {
2020-06-24 18:21:26 +00:00
if (newestFile != null) {
String newestFileVersion = Util.getVersionFromFileName(newestFile.fileName);
try {
if (SemanticVersion.parse(fileVersion).compareTo(SemanticVersion.parse(newestFileVersion)) > 0) {
newestFile = file;
}
} catch (VersionParsingException ignored) {
2020-06-24 02:04:22 +00:00
}
2020-06-24 18:21:26 +00:00
} else {
newestFile = file;
2020-06-24 02:04:22 +00:00
}
}
}
}
if (newestFile != null) {
String newestFileVersion = Util.getVersionFromFileName(newestFile.fileName);
try {
if (SemanticVersion.parse(newestFileVersion).compareTo(SemanticVersion.parse(oldVersion)) > 0) {
return new ModUpdate(oldVersion, newestFileVersion, newestFile.downloadUrl, name);
} else {
return null;
}
} catch (VersionParsingException e) {
return null;
}
} else {
return null;
}
}
}