3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-20 15:00:11 +01:00

Move to consumer function for processing extension folders

Dieser Commit ist enthalten in:
rtm516 2024-08-10 11:05:41 +01:00
Ursprung bbdab1f312
Commit 30288cf98e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 331715B8B007C67A
2 geänderte Dateien mit 115 neuen und 91 gelöschten Zeilen

Datei anzeigen

@ -42,6 +42,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;
@ -55,6 +56,7 @@ 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
@ -163,55 +165,17 @@ 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"); Path updateDirectory = extensionsDirectory.resolve("update");
List<Path> extensionPaths;
if (Files.isDirectory(updateDirectory)) { if (Files.isDirectory(updateDirectory)) {
// Get the current extensions and store them in a map // Get the current extensions and store them in a map
Map<String, Path> extensionFiles = new HashMap<>(); Map<String, Path> extensionFiles = new HashMap<>();
extensionPaths = Files.list(extensionsDirectory).toList(); this.processExtensionsFolder(extensionsDirectory, (path, description) -> {
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);
// Store the file path against ID for later use
extensionFiles.put(description.id(), path); extensionFiles.put(description.id(), path);
} catch (Throwable e) { }, (path, e) -> {
// this file will throw again when we actually try to load extensions, and it will be handled there // this file will throw again when we actually try to load extensions, and it will be handled there
}
}); });
// Perform the updates this.processExtensionsFolder(updateDirectory, (path, description) -> {
List<Path> extensionUpdatePaths = Files.list(updateDirectory).toList();
extensionUpdatePaths.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);
// Remove the old extension with the same ID if it exists // Remove the old extension with the same ID if it exists
Path oldExtensionFile = extensionFiles.get(description.id()); Path oldExtensionFile = extensionFiles.get(description.id());
if (oldExtensionFile != null && Files.exists(oldExtensionFile)) { if (oldExtensionFile != null && Files.exists(oldExtensionFile)) {
@ -220,27 +184,12 @@ public class GeyserExtensionLoader extends ExtensionLoader {
// Overwrite the extension with the new jar // Overwrite the extension with the new jar
Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (Throwable e) { }, (path, e) -> {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e); GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e);
}
}); });
} }
extensionPaths = Files.list(extensionsDirectory).toList(); this.processExtensionsFolder(extensionsDirectory, (path, description) -> {
extensionPaths.forEach(path -> {
if (Files.isDirectory(path)) {
return;
}
for (Pattern filter : extensionFilters) {
if (!filter.matcher(path.getFileName().toString()).matches()) {
return;
}
}
try {
GeyserExtensionDescription description = this.extensionDescription(path);
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) {
@ -269,9 +218,8 @@ 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); GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e);
}
}); });
for (GeyserExtensionContainer container : loadedExtensions.values()) { for (GeyserExtensionContainer container : loadedExtensions.values()) {
@ -283,6 +231,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;

Datei anzeigen

@ -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;
}