Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension update folder #4929

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.geysermc.geyser.api.extension.exception.InvalidExtensionException;
import org.geysermc.geyser.extension.event.GeyserExtensionEventBus;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.util.ThrowingBiConsumer;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -51,10 +52,12 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;

@RequiredArgsConstructor
Expand Down Expand Up @@ -163,53 +166,62 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) {
Map<String, Path> extensions = new LinkedHashMap<>();
Map<String, GeyserExtensionContainer> loadedExtensions = new LinkedHashMap<>();

Pattern[] extensionFilters = this.extensionFilters();
List<Path> extensionPaths = Files.walk(extensionsDirectory).toList();
extensionPaths.forEach(path -> {
if (Files.isDirectory(path)) {
Path updateDirectory = extensionsDirectory.resolve("update");
if (Files.isDirectory(updateDirectory)) {
// Get the current extensions and store them in a map
rtm516 marked this conversation as resolved.
Show resolved Hide resolved
Map<String, List<Path>> extensionFiles = new HashMap<>();
this.processExtensionsFolder(extensionsDirectory, (path, description) -> extensionFiles.computeIfAbsent(description.id(), k -> new ArrayList<>()).add(path), (path, e) -> {
rtm516 marked this conversation as resolved.
Show resolved Hide resolved
// this file will throw again when we actually try to load extensions, and it will be handled there
});

// Perform the updates
rtm516 marked this conversation as resolved.
Show resolved Hide resolved
this.processExtensionsFolder(updateDirectory, (path, description) -> {
// 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);
}
}

// Overwrite the extension with the new jar
Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING);
}, (path, e) -> {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e);
rtm516 marked this conversation as resolved.
Show resolved Hide resolved
});
}

this.processExtensionsFolder(extensionsDirectory, (path, description) -> {
rtm516 marked this conversation as resolved.
Show resolved Hide resolved
String name = description.name();
String id = description.id();
if (extensions.containsKey(id) || extensionManager.extension(id) != null) {
GeyserImpl.getInstance().getLogger().warning(GeyserLocale.getLocaleStringLog("geyser.extensions.load.duplicate", name, path.toString()));
return;
}

for (Pattern filter : extensionFilters) {
if (!filter.matcher(path.getFileName().toString()).matches()) {
// Check whether an extensions' requested api version is compatible
ApiVersion.Compatibility compatibility = GeyserApi.api().geyserApiVersion().supportsRequestedVersion(
description.humanApiVersion(),
description.majorApiVersion(),
description.minorApiVersion()
);

if (compatibility != ApiVersion.Compatibility.COMPATIBLE) {
// 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) {
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."
.formatted(name, description.apiVersion()));
} else {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_api_version", name, description.apiVersion()));
return;
}
}

try {
GeyserExtensionDescription description = this.extensionDescription(path);

String name = description.name();
String id = description.id();
if (extensions.containsKey(id) || extensionManager.extension(id) != null) {
GeyserImpl.getInstance().getLogger().warning(GeyserLocale.getLocaleStringLog("geyser.extensions.load.duplicate", name, path.toString()));
return;
}

// Check whether an extensions' requested api version is compatible
ApiVersion.Compatibility compatibility = GeyserApi.api().geyserApiVersion().supportsRequestedVersion(
description.humanApiVersion(),
description.majorApiVersion(),
description.minorApiVersion()
);

if (compatibility != ApiVersion.Compatibility.COMPATIBLE) {
// 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) {
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."
.formatted(name, description.apiVersion()));
} else {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_api_version", name, description.apiVersion()));
return;
}
}

GeyserExtensionContainer container = this.loadExtension(path, description);
extensions.put(id, path);
loadedExtensions.put(id, container);
} catch (Throwable e) {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e);
}
GeyserExtensionContainer container = this.loadExtension(path, description);
extensions.put(id, path);
loadedExtensions.put(id, container);
}, (path, e) -> {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e);
});

for (GeyserExtensionContainer container : loadedExtensions.values()) {
Expand All @@ -221,6 +233,40 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) {
}
}

/**
* 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
protected boolean isEnabled(@NonNull Extension extension) {
return this.extensionContainers.get(extension).enabled;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}