From 23e6dbc8999de33ada413ace8a9c6d182cc58e8f Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 9 Aug 2024 12:02:08 +0100 Subject: [PATCH] Remove old jars under different names for the same extensions --- .../extension/GeyserExtensionLoader.java | 40 +++++++++++++++++-- 1 file changed, 37 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 d3f40e6d2..0b80a9448 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,17 @@ public class GeyserExtensionLoader extends ExtensionLoader { Pattern[] extensionFilters = this.extensionFilters(); Path updateDirectory = extensionsDirectory.resolve("update"); + List extensionPaths; if (Files.isDirectory(updateDirectory)) { - List extensionUpdatePaths = Files.list(updateDirectory).toList(); - extensionUpdatePaths.forEach(path -> { + // 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; @@ -183,6 +187,36 @@ public class GeyserExtensionLoader extends ExtensionLoader { // 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; + } + } + + 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 + 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 @@ public class GeyserExtensionLoader extends ExtensionLoader { }); } - List extensionPaths = Files.list(extensionsDirectory).toList(); + extensionPaths = Files.list(extensionsDirectory).toList(); extensionPaths.forEach(path -> { if (Files.isDirectory(path)) { return;