From d3c59615471a09cbbe14faa36248256b21c3ee3f Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 2 Aug 2024 16:00:23 +0100 Subject: [PATCH 01/14] Add extension update folder --- .../extension/GeyserExtensionLoader.java | 25 ++++++++++++++++++- core/src/main/resources/languages | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index a56e00671a7..ecb75601722 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -164,7 +164,30 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { Map loadedExtensions = new LinkedHashMap<>(); Pattern[] extensionFilters = this.extensionFilters(); - List extensionPaths = Files.walk(extensionsDirectory).toList(); + + List extensionUpdatePaths = Files.list(extensionsDirectory.resolve("update")).toList(); + extensionUpdatePaths.forEach(path -> { + if (Files.isDirectory(path)) { + return; + } + + 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); + + Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); + } catch (Throwable e) { + GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e); + } + }); + + List extensionPaths = Files.list(extensionsDirectory).toList(); extensionPaths.forEach(path -> { if (Files.isDirectory(path)) { return; diff --git a/core/src/main/resources/languages b/core/src/main/resources/languages index 60b20023a92..8c4b92445bf 160000 --- a/core/src/main/resources/languages +++ b/core/src/main/resources/languages @@ -1 +1 @@ -Subproject commit 60b20023a92f084aba895ab0336e70fa7fb311fb +Subproject commit 8c4b92445bf48f7104f9ab1708efdd3b6acf1f2c From 4106d24f2af7a3f0b768a4c103291bd7bdef5896 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 2 Aug 2024 16:07:04 +0100 Subject: [PATCH 02/14] Check the update folder exists before trying to use it --- .../extension/GeyserExtensionLoader.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index ecb75601722..d3f40e6d261 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -165,27 +165,31 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { Pattern[] extensionFilters = this.extensionFilters(); - List extensionUpdatePaths = Files.list(extensionsDirectory.resolve("update")).toList(); - extensionUpdatePaths.forEach(path -> { - if (Files.isDirectory(path)) { - return; - } - - for (Pattern filter : extensionFilters) { - if (!filter.matcher(path.getFileName().toString()).matches()) { + Path updateDirectory = extensionsDirectory.resolve("update"); + if (Files.isDirectory(updateDirectory)) { + List extensionUpdatePaths = Files.list(updateDirectory).toList(); + extensionUpdatePaths.forEach(path -> { + if (Files.isDirectory(path)) { return; } - } - try { - // Try load the description, so we know it's a valid extension - GeyserExtensionDescription description = this.extensionDescription(path); + for (Pattern filter : extensionFilters) { + if (!filter.matcher(path.getFileName().toString()).matches()) { + return; + } + } - Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); - } catch (Throwable e) { - GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e); - } - }); + try { + // Try load the description, so we know it's a valid extension + GeyserExtensionDescription description = this.extensionDescription(path); + + // Overwrite the extension with the new jar + Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); + } catch (Throwable e) { + GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e); + } + }); + } List extensionPaths = Files.list(extensionsDirectory).toList(); extensionPaths.forEach(path -> { From 23e6dbc8999de33ada413ace8a9c6d182cc58e8f Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 12:02:08 +0100 Subject: [PATCH 03/14] Remove old jars under different names for the same extensions --- .../extension/GeyserExtensionLoader.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index d3f40e6d261..0b80a944872 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -166,13 +166,42 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { Pattern[] extensionFilters = this.extensionFilters(); Path updateDirectory = extensionsDirectory.resolve("update"); + List extensionPaths; if (Files.isDirectory(updateDirectory)) { + // Get the current extensions and store them in a map + Map extensionFiles = new HashMap<>(); + extensionPaths = Files.list(extensionsDirectory).toList(); + 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 name against ID for later use + extensionFiles.put(description.id(), path.getFileName().toString()); + } catch (Throwable e) { + // no-op + } + }); + + // Perform the updates List 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; @@ -183,6 +212,11 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { // 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 + if (extensionFiles.containsKey(description.id())) { + Files.delete(extensionsDirectory.resolve(extensionFiles.get(description.id()))); + } + // Overwrite the extension with the new jar Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); } catch (Throwable e) { @@ -191,7 +225,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { }); } - List extensionPaths = Files.list(extensionsDirectory).toList(); + extensionPaths = Files.list(extensionsDirectory).toList(); extensionPaths.forEach(path -> { if (Files.isDirectory(path)) { return; From 571e97332d0c3400f1d7d6dcd68412833d45396a Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 13:35:19 +0100 Subject: [PATCH 04/14] Store file path to save on calls --- .../geysermc/geyser/extension/GeyserExtensionLoader.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index 0b80a944872..2dabe8a4ce8 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -169,7 +169,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { List extensionPaths; if (Files.isDirectory(updateDirectory)) { // Get the current extensions and store them in a map - Map extensionFiles = new HashMap<>(); + Map extensionFiles = new HashMap<>(); extensionPaths = Files.list(extensionsDirectory).toList(); extensionPaths.forEach(path -> { if (Files.isDirectory(path)) { @@ -188,7 +188,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { GeyserExtensionDescription description = this.extensionDescription(path); // Store the file name against ID for later use - extensionFiles.put(description.id(), path.getFileName().toString()); + extensionFiles.put(description.id(), path.getFileName()); } catch (Throwable e) { // no-op } @@ -214,7 +214,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { // Remove the old extension with the same ID if it exists if (extensionFiles.containsKey(description.id())) { - Files.delete(extensionsDirectory.resolve(extensionFiles.get(description.id()))); + Files.delete(extensionFiles.get(description.id())); } // Overwrite the extension with the new jar From 82b0555e5ce061cd0c2f9b9536683ddcbe403ec7 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 13:41:53 +0100 Subject: [PATCH 05/14] Fix storing path --- .../org/geysermc/geyser/extension/GeyserExtensionLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index 2dabe8a4ce8..a648d2ddaf6 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -188,7 +188,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { GeyserExtensionDescription description = this.extensionDescription(path); // Store the file name against ID for later use - extensionFiles.put(description.id(), path.getFileName()); + extensionFiles.put(description.id(), path); } catch (Throwable e) { // no-op } From 7621a0ccedef8f71afcb29d36c732c6980be76ed Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 13:46:02 +0100 Subject: [PATCH 06/14] Update languages --- core/src/main/resources/languages | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/resources/languages b/core/src/main/resources/languages index 8c4b92445bf..7499daf712a 160000 --- a/core/src/main/resources/languages +++ b/core/src/main/resources/languages @@ -1 +1 @@ -Subproject commit 8c4b92445bf48f7104f9ab1708efdd3b6acf1f2c +Subproject commit 7499daf712ad6de70a07fba471b51b4ad92315c5 From 06664032431ef8cf6fa2d637c2dbe7df0aed9e84 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 13:48:59 +0100 Subject: [PATCH 07/14] Update core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java Co-authored-by: chris --- .../org/geysermc/geyser/extension/GeyserExtensionLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index a648d2ddaf6..bfa4bba8d56 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -187,7 +187,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { // Try load the description, so we know it's a valid extension GeyserExtensionDescription description = this.extensionDescription(path); - // Store the file name against ID for later use + // Store the file path against ID for later use extensionFiles.put(description.id(), path); } catch (Throwable e) { // no-op From 64974d63401b29ba107e8df33bd7e65d4cfdd195 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 13:52:25 +0100 Subject: [PATCH 08/14] Only pull data from the map once --- .../org/geysermc/geyser/extension/GeyserExtensionLoader.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index bfa4bba8d56..84fc8249218 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -213,7 +213,8 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { GeyserExtensionDescription description = this.extensionDescription(path); // Remove the old extension with the same ID if it exists - if (extensionFiles.containsKey(description.id())) { + Path oldExtensionFile = extensionFiles.get(description.id()); + if (oldExtensionFile != null && Files.exists(oldExtensionFile)) { Files.delete(extensionFiles.get(description.id())); } From bbdab1f312b30924a71cb880c2a7016fda5d15f6 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sat, 10 Aug 2024 10:33:12 +0100 Subject: [PATCH 09/14] Update core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java Co-authored-by: Konicai <71294714+Konicai@users.noreply.github.com> --- .../org/geysermc/geyser/extension/GeyserExtensionLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index 84fc8249218..3b3ca30281f 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -190,7 +190,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { // Store the file path against ID for later use extensionFiles.put(description.id(), path); } catch (Throwable e) { - // no-op + // this file will throw again when we actually try to load extensions, and it will be handled there } }); From 30288cf98e08ffb863d940120c2aed311dfc70ec Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sat, 10 Aug 2024 11:05:41 +0100 Subject: [PATCH 10/14] Move to consumer function for processing extension folders --- .../extension/GeyserExtensionLoader.java | 164 ++++++++---------- .../geyser/util/ThrowingBiConsumer.java | 42 +++++ 2 files changed, 115 insertions(+), 91 deletions(-) create mode 100644 core/src/main/java/org/geysermc/geyser/util/ThrowingBiConsumer.java diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index 3b3ca30281f..ed92d848a6d 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -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; @@ -55,6 +56,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; import java.util.regex.Pattern; @RequiredArgsConstructor @@ -163,115 +165,61 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { Map extensions = new LinkedHashMap<>(); Map loadedExtensions = new LinkedHashMap<>(); - Pattern[] extensionFilters = this.extensionFilters(); - Path updateDirectory = extensionsDirectory.resolve("update"); - List extensionPaths; if (Files.isDirectory(updateDirectory)) { // Get the current extensions and store them in a map Map extensionFiles = new HashMap<>(); - extensionPaths = Files.list(extensionsDirectory).toList(); - 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); - } catch (Throwable e) { - // this file will throw again when we actually try to load extensions, and it will be handled there - } + this.processExtensionsFolder(extensionsDirectory, (path, description) -> { + extensionFiles.put(description.id(), path); + }, (path, e) -> { + // this file will throw again when we actually try to load extensions, and it will be handled there }); - // Perform the updates - List 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; - } + this.processExtensionsFolder(updateDirectory, (path, description) -> { + // Remove the old extension with the same ID if it exists + Path oldExtensionFile = extensionFiles.get(description.id()); + if (oldExtensionFile != null && Files.exists(oldExtensionFile)) { + Files.delete(extensionFiles.get(description.id())); } - 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 - Path oldExtensionFile = extensionFiles.get(description.id()); - if (oldExtensionFile != null && Files.exists(oldExtensionFile)) { - Files.delete(extensionFiles.get(description.id())); - } - - // Overwrite the extension with the new jar - Files.move(path, extensionsDirectory.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); - } catch (Throwable e) { - GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e); - } + // 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); }); } - extensionPaths = Files.list(extensionsDirectory).toList(); - extensionPaths.forEach(path -> { - if (Files.isDirectory(path)) { + this.processExtensionsFolder(extensionsDirectory, (path, description) -> { + 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()) { @@ -283,6 +231,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 accept, BiConsumer reject) throws IOException { + List 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; diff --git a/core/src/main/java/org/geysermc/geyser/util/ThrowingBiConsumer.java b/core/src/main/java/org/geysermc/geyser/util/ThrowingBiConsumer.java new file mode 100644 index 00000000000..96adbb5c920 --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/util/ThrowingBiConsumer.java @@ -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 extends BiConsumer { + @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; +} From 37a608444a9274b41952c02af24e5de6bf3caa09 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sat, 10 Aug 2024 11:08:45 +0100 Subject: [PATCH 11/14] Add back some comments --- .../org/geysermc/geyser/extension/GeyserExtensionLoader.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index ed92d848a6d..801055c5153 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -169,12 +169,11 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { if (Files.isDirectory(updateDirectory)) { // Get the current extensions and store them in a map Map extensionFiles = new HashMap<>(); - this.processExtensionsFolder(extensionsDirectory, (path, description) -> { - extensionFiles.put(description.id(), path); - }, (path, e) -> { + this.processExtensionsFolder(extensionsDirectory, (path, description) -> extensionFiles.put(description.id(), path), (path, e) -> { // 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) -> { // Remove the old extension with the same ID if it exists Path oldExtensionFile = extensionFiles.get(description.id()); From e0df59d31aa00dfe8fb915fc103fe251726bb9fe Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sat, 10 Aug 2024 11:12:07 +0100 Subject: [PATCH 12/14] Allow cleanup of multiple old extensions --- .../geyser/extension/GeyserExtensionLoader.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index 801055c5153..a4e9fcc89e3 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -52,6 +52,7 @@ 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; @@ -168,17 +169,19 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { Path updateDirectory = extensionsDirectory.resolve("update"); if (Files.isDirectory(updateDirectory)) { // Get the current extensions and store them in a map - Map extensionFiles = new HashMap<>(); - this.processExtensionsFolder(extensionsDirectory, (path, description) -> extensionFiles.put(description.id(), path), (path, e) -> { + Map> extensionFiles = new HashMap<>(); + 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 }); // Perform the updates this.processExtensionsFolder(updateDirectory, (path, description) -> { - // Remove the old extension with the same ID if it exists - Path oldExtensionFile = extensionFiles.get(description.id()); - if (oldExtensionFile != null && Files.exists(oldExtensionFile)) { - Files.delete(extensionFiles.get(description.id())); + // Remove the old extension files with the same ID if it exists + List oldExtensionFiles = extensionFiles.get(description.id()); + if (oldExtensionFiles != null) { + for (Path oldExtensionFile : oldExtensionFiles) { + Files.delete(oldExtensionFile); + } } // Overwrite the extension with the new jar From 3ada95144e3fb3854915e0b1e58fbcc29271f5e1 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Tue, 27 Aug 2024 17:59:09 +0100 Subject: [PATCH 13/14] Address review comments --- .../geyser/extension/GeyserExtensionLoader.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index a4e9fcc89e3..c35f6342dd3 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -168,13 +168,15 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { Path updateDirectory = extensionsDirectory.resolve("update"); if (Files.isDirectory(updateDirectory)) { - // Get the current extensions and store them in a map + // Step 1: Collect the extension files that currently exist so they can be replaced Map> extensionFiles = new HashMap<>(); - this.processExtensionsFolder(extensionsDirectory, (path, description) -> extensionFiles.computeIfAbsent(description.id(), k -> new ArrayList<>()).add(path), (path, e) -> { + 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 }); - // Perform the updates + // Step 2: Move the updated/new extensions this.processExtensionsFolder(updateDirectory, (path, description) -> { // Remove the old extension files with the same ID if it exists List oldExtensionFiles = extensionFiles.get(description.id()); @@ -191,6 +193,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { }); } + // Step 3: Load the extensions this.processExtensionsFolder(extensionsDirectory, (path, description) -> { String name = description.name(); String id = description.id(); @@ -224,6 +227,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e); }); + // Step 4: Register the extensions for (GeyserExtensionContainer container : loadedExtensions.values()) { this.extensionContainers.put(container.extension(), container); this.register(container.extension(), extensionManager); From 360678ca13491406d7ba32ecb9833dedb6b3eddd Mon Sep 17 00:00:00 2001 From: rtm516 Date: Tue, 27 Aug 2024 18:02:57 +0100 Subject: [PATCH 14/14] Tidy logger calls --- .../geyser/extension/GeyserExtensionLoader.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index c35f6342dd3..10cbcf55686 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -31,6 +31,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.api.util.ApiVersion; import org.geysermc.geyser.GeyserImpl; +import org.geysermc.geyser.GeyserLogger; import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.event.ExtensionEventBus; import org.geysermc.geyser.api.extension.Extension; @@ -158,6 +159,7 @@ void setClass(String name, final Class clazz) { @Override protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { + GeyserLogger logger = GeyserImpl.getInstance().getLogger(); try { if (Files.notExists(extensionsDirectory)) { Files.createDirectory(extensionsDirectory); @@ -189,7 +191,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { // 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); + logger.error(GeyserLocale.getLocaleStringLog("geyser.extensions.update.failed", path.getFileName()), e); }); } @@ -198,7 +200,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { 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())); + logger.warning(GeyserLocale.getLocaleStringLog("geyser.extensions.load.duplicate", name, path.toString())); return; } @@ -212,10 +214,10 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { 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." + 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())); } 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; } } @@ -224,7 +226,7 @@ protected void loadAllExtensions(@NonNull ExtensionManager extensionManager) { 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); + logger.error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e); }); // Step 4: Register the extensions