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;
|
|
|
|
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 java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
class CurseForgeStrategy implements ModUpdateStrategy {
|
|
|
|
@SuppressWarnings({"unused", "MismatchedReadAndWriteOfArray"})
|
|
|
|
private static class CurseForgeFile {
|
|
|
|
private String fileName;
|
|
|
|
private String downloadUrl;
|
|
|
|
private String[] gameVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ModUpdate checkForUpdate(ConfigObject obj, String oldVersion, String name) {
|
|
|
|
int projectID;
|
|
|
|
try {
|
|
|
|
projectID = obj.getInt("projectID");
|
|
|
|
} catch (ConfigObject.MissingValueException e) {
|
|
|
|
ModUpdater.invalidModUpdaterConfig(name);
|
|
|
|
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-24 14:02:31 +00:00
|
|
|
ModUpdater.getLogger().warn("Unable To Access CurseForge: " + name);
|
2020-06-24 02:04:22 +00:00
|
|
|
return null;
|
2020-06-25 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CurseForgeFile[] files;
|
|
|
|
try {
|
|
|
|
Moshi moshi = new Moshi.Builder().build();
|
|
|
|
JsonAdapter<CurseForgeFile[]> jsonAdapter = moshi.adapter(CurseForgeFile[].class);
|
|
|
|
|
|
|
|
files = jsonAdapter.fromJson(data);
|
|
|
|
} catch (JsonDataException | IOException e) {
|
2020-06-24 18:21:26 +00:00
|
|
|
ModUpdater.getLogger().warn("CurseForge Sent Invalid Data: ", e);
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (Arrays.asList(file.gameVersion).contains(versionStr) || Util.isVersionCompatible(fileVersion)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|