Allow custom update URLs for modules

This commit is contained in:
2026-05-19 21:29:55 -04:00
parent 2543c7f19e
commit 42f7a02a7a
9 changed files with 224 additions and 42 deletions
@@ -124,7 +124,7 @@ public class PlexCMD extends ServerCommand
}
for (PlexModule module : plugin.getModuleManager().getModules())
{
plugin.getUpdateChecker().updateJar(sender, module.getPlexModuleFile().getName(), true);
plugin.getUpdateChecker().updateModuleJar(sender, module);
}
plugin.getModuleManager().reloadModules();
return context.mmString("<green>All modules updated and reloaded!");
@@ -14,6 +14,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -95,10 +96,22 @@ public class ModuleManager
.map(id -> internalModuleConfig.getConfigurationSection("repositories").getString(id, ""))
.filter(repository -> !repository.isBlank())
.toList();
boolean updaterEnabled = internalModuleConfig.getBoolean("updater.enabled", true);
List<String> updateUrls = new ArrayList<>();
String updateUrl = internalModuleConfig.getString("updater.url", "");
if (!updateUrl.isBlank())
{
updateUrls.add(updateUrl);
}
updateUrls.addAll(internalModuleConfig.getStringList("updater.urls").stream()
.filter(url -> !url.isBlank())
.toList());
PlexModuleFile plexModuleFile = new PlexModuleFile(name, main, description, version, apiCompatibility);
plexModuleFile.setLibraries(libraries);
plexModuleFile.setRepositories(repositories);
plexModuleFile.setUpdaterEnabled(updaterEnabled);
plexModuleFile.setUpdateUrls(updateUrls);
Class<? extends PlexModule> module = (Class<? extends PlexModule>) Class.forName(main, true, loader);
PlexModule plexModule = module.getConstructor().newInstance();
@@ -15,7 +15,7 @@ import java.util.Optional;
public final class UpdateMetadataClient
{
private static final List<String> BASE_URLS = List.of("https://updater.plex.us.org", "https://plex-updater.com");
private static final List<String> DEFAULT_BASE_URLS = List.of("https://updater.plex.us.org", "https://plex-updater.com");
private final Gson gson = new Gson();
private final UpdateChannel channel;
@@ -38,9 +38,14 @@ public final class UpdateMetadataClient
}
public ArtifactMetadata fetchModuleLatest(String moduleName, int apiCompatibility) throws MetadataException
{
return fetchModuleLatest(moduleName, apiCompatibility, List.of());
}
public ArtifactMetadata fetchModuleLatest(String moduleName, int apiCompatibility, List<String> baseUrls) throws MetadataException
{
String path = "/api/v1/projects/" + encodePathSegment(moduleName) + "/channels/" + channel.id() + "/latest/api/" + apiCompatibility + ".json";
ArtifactMetadata metadata = fetch(path);
ArtifactMetadata metadata = fetch(path, baseUrls);
Optional<String> validationError = metadata.validateModule(channel, moduleName, apiCompatibility);
if (validationError.isPresent())
{
@@ -50,10 +55,15 @@ public final class UpdateMetadataClient
}
private ArtifactMetadata fetch(String path) throws MetadataException
{
return fetch(path, DEFAULT_BASE_URLS);
}
private ArtifactMetadata fetch(String path, List<String> baseUrls) throws MetadataException
{
MetadataException notFound = null;
MetadataException failure = null;
for (String baseUrl : BASE_URLS)
for (String baseUrl : normalizeBaseUrls(baseUrls))
{
try
{
@@ -133,6 +143,17 @@ public final class UpdateMetadataClient
return URLEncoder.encode(value, StandardCharsets.UTF_8).replace("+", "%20");
}
private static List<String> normalizeBaseUrls(List<String> baseUrls)
{
List<String> urls = baseUrls == null || baseUrls.isEmpty() ? DEFAULT_BASE_URLS : baseUrls;
return urls.stream()
.map(String::trim)
.filter(url -> !url.isBlank())
.map(url -> url.endsWith("/") ? url.substring(0, url.length() - 1) : url)
.distinct()
.toList();
}
public static final class MetadataException extends Exception
{
private final boolean notFound;
@@ -1,6 +1,8 @@
package dev.plex.util;
import dev.plex.Plex;
import dev.plex.module.PlexModule;
import dev.plex.module.PlexModuleFile;
import dev.plex.updater.ArtifactMetadata;
import dev.plex.updater.UpdateChannel;
import dev.plex.updater.UpdateMetadataClient;
@@ -17,6 +19,7 @@ import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.List;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
@@ -84,11 +87,27 @@ public class UpdateChecker
}
public void updateJar(CommandSender sender, String name, boolean module)
{
updateJar(sender, name, module, List.of());
}
public void updateModuleJar(CommandSender sender, PlexModule module)
{
PlexModuleFile moduleFile = module.getPlexModuleFile();
if (!moduleFile.isUpdaterEnabled())
{
sendMessage(sender, PlexUtils.messageComponent("moduleUpdateDisabled", moduleFile.getName()));
return;
}
updateJar(sender, moduleFile.getName(), true, moduleFile.getUpdateUrls());
}
private void updateJar(CommandSender sender, String name, boolean module, List<String> moduleUpdateUrls)
{
try
{
ArtifactMetadata metadata = module
? metadataClient.fetchModuleLatest(name, plugin.getApi().compatibility().version())
? metadataClient.fetchModuleLatest(name, plugin.getApi().compatibility().version(), moduleUpdateUrls)
: fetchLatestPlexMetadata(false);
if (!module && metadata.matchesCurrentBuild(plugin.getPluginMeta().getVersion(), BuildInfo.getNumber(), BuildInfo.getCommit()))
+1
View File
@@ -356,3 +356,4 @@ updateDownloadFailed: "<red>Something went wrong while downloading {0}. Please c
updateMetadataNotFound: "<red>No compatible update is available on the {0} channel."
# 0 - The error message
updateMetadataError: "<red>There was an error checking update metadata: {0}"
moduleUpdateDisabled: "<yellow>Skipping {0}; module updates are disabled."