Add extension update folder (#4929)

* Add extension update folder

* Check the update folder exists before trying to use it

* Remove old jars under different names for the same extensions

* Store file path to save on calls

* Fix storing path

* Update languages

* Update core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java

Co-authored-by: chris <github@onechris.mozmail.com>

* Only pull data from the map once

* Update core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java

Co-authored-by: Konicai <71294714+Konicai@users.noreply.github.com>

* Move to consumer function for processing extension folders

* Add back some comments

* Allow cleanup of multiple old extensions

* Address review comments

* Tidy logger calls

---------

Co-authored-by: chris <github@onechris.mozmail.com>
Co-authored-by: Konicai <71294714+Konicai@users.noreply.github.com>
This commit is contained in:
rtm516 2024-12-15 22:40:14 +00:00 committed by GitHub
parent 54bdb639cb
commit a5c77a7072
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 136 additions and 42 deletions

View file

@ -31,6 +31,7 @@ import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.api.util.ApiVersion; import org.geysermc.api.util.ApiVersion;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.GeyserApi;
import org.geysermc.geyser.api.event.ExtensionEventBus; import org.geysermc.geyser.api.event.ExtensionEventBus;
import org.geysermc.geyser.api.extension.Extension; import org.geysermc.geyser.api.extension.Extension;
@ -42,6 +43,7 @@ import org.geysermc.geyser.api.extension.exception.InvalidDescriptionException;
import org.geysermc.geyser.api.extension.exception.InvalidExtensionException; import org.geysermc.geyser.api.extension.exception.InvalidExtensionException;
import org.geysermc.geyser.extension.event.GeyserExtensionEventBus; import org.geysermc.geyser.extension.event.GeyserExtensionEventBus;
import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.util.ThrowingBiConsumer;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -51,10 +53,12 @@ import java.nio.file.Files;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@RequiredArgsConstructor @RequiredArgsConstructor
@ -155,6 +159,7 @@ public class GeyserExtensionLoader extends ExtensionLoader {
@Override @Override
protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) {
GeyserLogger logger = GeyserImpl.getInstance().getLogger();
try { try {
if (Files.notExists(extensionsDirectory)) { if (Files.notExists(extensionsDirectory)) {
Files.createDirectory(extensionsDirectory); Files.createDirectory(extensionsDirectory);
@ -163,26 +168,39 @@ public class GeyserExtensionLoader extends ExtensionLoader {
Map<String, Path> extensions = new LinkedHashMap<>(); Map<String, Path> extensions = new LinkedHashMap<>();
Map<String, GeyserExtensionContainer> loadedExtensions = new LinkedHashMap<>(); Map<String, GeyserExtensionContainer> loadedExtensions = new LinkedHashMap<>();
Pattern[] extensionFilters = this.extensionFilters(); Path updateDirectory = extensionsDirectory.resolve("update");
List<Path> extensionPaths = Files.walk(extensionsDirectory).toList(); if (Files.isDirectory(updateDirectory)) {
extensionPaths.forEach(path -> { // Step 1: Collect the extension files that currently exist so they can be replaced
if (Files.isDirectory(path)) { Map<String, List<Path>> extensionFiles = new HashMap<>();
return; this.processExtensionsFolder(extensionsDirectory, (path, description) -> {
} extensionFiles.computeIfAbsent(description.id(), k -> new ArrayList<>()).add(path);
}, (path, e) -> {
// this file will throw again when we actually try to load extensions, and it will be handled there
});
for (Pattern filter : extensionFilters) { // Step 2: Move the updated/new extensions
if (!filter.matcher(path.getFileName().toString()).matches()) { this.processExtensionsFolder(updateDirectory, (path, description) -> {
return; // Remove the old extension files with the same ID if it exists
List<Path> oldExtensionFiles = extensionFiles.get(description.id());
if (oldExtensionFiles != null) {
for (Path oldExtensionFile : oldExtensionFiles) {
Files.delete(oldExtensionFile);
} }
} }
try { // Overwrite the extension with the new jar
GeyserExtensionDescription description = this.extensionDescription(path); Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING);
}, (path, e) -> {
logger.error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e);
});
}
// Step 3: Load the extensions
this.processExtensionsFolder(extensionsDirectory, (path, description) -> {
String name = description.name(); String name = description.name();
String id = description.id(); String id = description.id();
if (extensions.containsKey(id) || extensionManager.extension(id) != null) { if (extensions.containsKey(id) || extensionManager.extension(id) != null) {
GeyserImpl.getInstance().getLogger().warning(GeyserLocale.getLocaleStringLog("geyser.extensions.load.duplicate", name, path.toString())); logger.warning(GeyserLocale.getLocaleStringLog("geyser.extensions.load.duplicate", name, path.toString()));
return; return;
} }
@ -196,10 +214,10 @@ public class GeyserExtensionLoader extends ExtensionLoader {
if (compatibility != ApiVersion.Compatibility.COMPATIBLE) { if (compatibility != ApiVersion.Compatibility.COMPATIBLE) {
// Workaround for the switch to the Geyser API version instead of the Base API version in extensions // Workaround for the switch to the Geyser API version instead of the Base API version in extensions
if (compatibility == ApiVersion.Compatibility.HUMAN_DIFFER && description.humanApiVersion() == 1) { if (compatibility == ApiVersion.Compatibility.HUMAN_DIFFER && description.humanApiVersion() == 1) {
GeyserImpl.getInstance().getLogger().warning("The extension %s requested the Base API version %s, which is deprecated in favor of specifying the Geyser API version. Please update the extension, or contact its developer." logger.warning("The extension %s requested the Base API version %s, which is deprecated in favor of specifying the Geyser API version. Please update the extension, or contact its developer."
.formatted(name, description.apiVersion())); .formatted(name, description.apiVersion()));
} else { } else {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_api_version", name, description.apiVersion())); logger.error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_api_version", name, description.apiVersion()));
return; return;
} }
} }
@ -207,11 +225,11 @@ public class GeyserExtensionLoader extends ExtensionLoader {
GeyserExtensionContainer container = this.loadExtension(path, description); GeyserExtensionContainer container = this.loadExtension(path, description);
extensions.put(id, path); extensions.put(id, path);
loadedExtensions.put(id, container); loadedExtensions.put(id, container);
} catch (Throwable e) { }, (path, e) -> {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e); logger.error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e);
}
}); });
// Step 4: Register the extensions
for (GeyserExtensionContainer container : loadedExtensions.values()) { for (GeyserExtensionContainer container : loadedExtensions.values()) {
this.extensionContainers.put(container.extension(), container); this.extensionContainers.put(container.extension(), container);
this.register(container.extension(), extensionManager); this.register(container.extension(), extensionManager);
@ -221,6 +239,40 @@ public class GeyserExtensionLoader extends ExtensionLoader {
} }
} }
/**
* Process extension jars in a folder and call the accept or reject consumer based on the result
*
* @param directory the directory to process
* @param accept the consumer to call when an extension is accepted
* @param reject the consumer to call when an extension is rejected
* @throws IOException if an I/O error occurs
*/
private void processExtensionsFolder(Path directory, ThrowingBiConsumer<Path, GeyserExtensionDescription> accept, BiConsumer<Path, Throwable> reject) throws IOException {
List<Path> extensionPaths = Files.list(directory).toList();
Pattern[] extensionFilters = this.extensionFilters();
extensionPaths.forEach(path -> {
if (Files.isDirectory(path)) {
return;
}
// Only look at files that meet the extension filter
for (Pattern filter : extensionFilters) {
if (!filter.matcher(path.getFileName().toString()).matches()) {
return;
}
}
try {
// Try load the description, so we know it's a valid extension
GeyserExtensionDescription description = this.extensionDescription(path);
accept.acceptThrows(path, description);
} catch (Throwable e) {
reject.accept(path, e);
}
});
}
@Override @Override
protected boolean isEnabled(@NonNull Extension extension) { protected boolean isEnabled(@NonNull Extension extension) {
return this.extensionContainers.get(extension).enabled; return this.extensionContainers.get(extension).enabled;

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.util;
import java.util.function.BiConsumer;
@FunctionalInterface
public interface ThrowingBiConsumer<T, U> extends BiConsumer<T, U> {
@Override
default void accept(T t, U u) {
try {
acceptThrows(t, u);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
void acceptThrows(T t, U u) throws Throwable;
}