Add update checker

This commit is contained in:
Jason Penilla committed 2023-08-19 13:07:43 -07:00
1 parent 02774c763f
commit b9f9322122
3 files changed
+144

No files matched your search

@@ -24,6 +24,7 @@ import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import net.draycia.carbon.api.CarbonChat;
@@ -32,6 +33,7 @@ import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.listeners.Listener;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messaging.MessagingManager;
@@ -42,6 +44,7 @@ import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.PlayerUtils;
import net.draycia.carbon.common.util.UpdateChecker;
import ninja.egg82.messenger.services.PacketService;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -123,6 +126,13 @@ public abstract class CarbonChatInternal implements CarbonChat {
// Load channels
this.channelRegistry().loadConfigChannels(this.carbonMessages);
CompletableFuture.runAsync(() -> {
if (!this.injector.getInstance(ConfigFactory.class).primaryConfig().updateChecker()) {
return;
}
new UpdateChecker(this.logger).checkVersion();
});
}
protected void shutdown() {
@@ -93,6 +93,9 @@ public class PrimaryConfig {
@Comment("Whether Carbon's nickname management should be used. Disable this if you wish to have another plugin manage nicknames.")
private boolean useCarbonNicknames = true;
@Comment("Whether Carbon should check for updates using the GitHub API on startup.")
private boolean updateChecker = true;
public Locale defaultLocale() {
return this.defaultLocale;
}
@@ -173,6 +176,10 @@ public class PrimaryConfig {
return this.useCarbonNicknames;
}
public boolean updateChecker() {
return this.updateChecker;
}
public enum StorageType {
JSON,
MYSQL,
@@ -0,0 +1,127 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Manifest;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public record UpdateChecker(Logger logger) {
private static final String GITHUB_REPO = "Hexaoxide/Carbon";
private static final String UPDATE_CHECKER_FETCHING_VERSION_INFORMATION = "Fetching version information...";
private static final String DEV_BUILD_NOTICE = "This is a development version of CarbonChat (<version>)!";
private static final String UPDATE_CHECKER_BEHIND_RELEASES = "CarbonChat is <behind> version(s) out of date (<version>).";
private static final String UPDATE_CHECKER_DOWNLOAD_RELEASE = "Download the latest release (<latest>) from <link>";
private static final String RELEASE_DOWNLOADS_URL = "https://modrinth.com/plugin/carbon/versions";
private static final Gson GSON = new GsonBuilder().create();
public void checkVersion() {
this.logger.info(UPDATE_CHECKER_FETCHING_VERSION_INFORMATION);
final @Nullable Manifest manifest = manifest(UpdateChecker.class); // we expect to be shaded into platform jars
if (manifest == null) {
this.logger.warn("Failed to locate manifest, cannot check for updates.");
return;
}
final String currentVersion = manifest.getMainAttributes().getValue("carbon-version");
final Releases releases;
try {
releases = this.fetchReleases();
} catch (final IOException e) {
this.logger.warn("Failed to list releases, cannot check for updates.", e);
return;
}
final String ver = "v" + currentVersion;
if (releases.releaseList().get(0).equals(ver)) {
return;
}
if (currentVersion.contains("-SNAPSHOT")) {
this.logger.info(DEV_BUILD_NOTICE.replace("<version>", ver));
} else {
final int versionsBehind = releases.releaseList().indexOf(ver);
this.logger.info(
UPDATE_CHECKER_BEHIND_RELEASES
.replace("<behind>", String.valueOf(versionsBehind == -1 ? "?" : versionsBehind))
.replace("<version>", ver)
);
}
this.logger.info(
UPDATE_CHECKER_DOWNLOAD_RELEASE
.replace("<latest>", releases.releaseList().get(0))
.replace("<link>", RELEASE_DOWNLOADS_URL) // , releases.releaseUrls().get(releases.releaseList().get(0)))
);
}
private Releases fetchReleases() throws IOException {
final JsonArray result;
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("https://api.github.com/repos/%s/releases".formatted(GITHUB_REPO)).openStream(), StandardCharsets.UTF_8))) {
result = GSON.fromJson(reader, JsonArray.class);
}
final Map<String, String> versionMap = new LinkedHashMap<>();
for (final JsonElement element : result) {
versionMap.put(
element.getAsJsonObject().get("tag_name").getAsString(),
element.getAsJsonObject().get("html_url").getAsString()
);
}
return new Releases(new ArrayList<>(versionMap.keySet()), versionMap);
}
private record Releases(List<String> releaseList, Map<String, String> releaseUrls) {
}
public static @Nullable Manifest manifest(final Class<?> clazz) {
final String classLocation = "/" + clazz.getName().replace(".", "/") + ".class";
final @Nullable URL resource = clazz.getResource(classLocation);
if (resource == null) {
return null;
}
final String classFilePath = resource.toString().replace("\\", "/");
final String archivePath = classFilePath.substring(0, classFilePath.length() - classLocation.length());
try (final InputStream stream = new URL(archivePath + "/META-INF/MANIFEST.MF").openStream()) {
return new Manifest(stream);
} catch (final IOException ex) {
return null;
}
}
}