From e9d4abd84b418aa378c39c8d9fa382fa1609686c Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 9 Jul 2023 11:41:27 -0700 Subject: [PATCH] Fix reloads by recreating the channel registry Currently, this persists API-registered entries. Not sure if that's what we want to do, or if we should have an event for registering channels instead... We only support registering commands for channels added at startup anyways. --- .../channels/CarbonChannelRegistry.java | 69 +++++++------------ 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java index 83216514..906df55e 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java @@ -37,6 +37,7 @@ import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.channels.ChannelRegistry; @@ -53,6 +54,7 @@ import net.draycia.carbon.common.event.events.CarbonReloadEvent; import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl; import net.draycia.carbon.common.listeners.ChatListenerInternal; import net.draycia.carbon.common.messages.CarbonMessages; +import net.draycia.carbon.common.util.Exceptions; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; @@ -92,7 +94,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann private final CarbonEventHandler eventHandler; private final Provider carbonChatProvider; - private final Registry channelRegistry = Registry.create(); + private volatile Registry channelRegistry = Registry.create(); + private final Set configChannels = ConcurrentHashMap.newKeySet(); // // private final BiMap channelMap = Maps.synchronizedBiMap(HashBiMap.create()); @@ -115,7 +118,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann this.eventHandler = events; this.carbonChatProvider = carbonChatProvider; - events.subscribe(CarbonReloadEvent.class, event -> this.reloadRegisteredConfigChannels()); + events.subscribe(CarbonReloadEvent.class, event -> this.reloadConfigChannels()); } public static ConfigurationTransformation.Versioned versioned() { @@ -162,64 +165,45 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann return node; } - public void reloadRegisteredConfigChannels() { - try (final Stream paths = Files.walk(this.configChannelDir)) { - paths.forEach(path -> { - final String fileName = path.getFileName().toString(); - - if (!fileName.endsWith(".conf")) { - return; - } - - final @Nullable ChatChannel chatChannel = this.registerChannelFromPath(path); - - if (chatChannel == null) { - return; - } - - this.injector.injectMembers(chatChannel); - - final @Nullable ChatChannel existingChannel = this.channel(chatChannel.key()); - - if (existingChannel == null) { - return; - } - - this.register(chatChannel); - }); - } catch (final IOException exception) { - exception.printStackTrace(); + public void reloadConfigChannels() { + final Registry newRegistry = Registry.create(); + // Copy API registrations over + for (final Key registered : this.channelRegistry.keys()) { + if (!this.configChannels.contains(registered)) { + newRegistry.register(registered, this.channelRegistry.getHolder(registered).valueOrThrow()); + } } + this.configChannels.clear(); + this.channelRegistry = newRegistry; + this.loadConfigChannels(this.carbonMessages); } - public void loadConfigChannels(final CarbonMessages carbonMessages) { + public void loadConfigChannels(final CarbonMessages messages) { this.defaultKey = this.configFactory.primaryConfig().defaultChannel(); - if (!Files.exists(this.configChannelDir)) { + if (!Files.exists(this.configChannelDir) || this.isPathEmpty(this.configChannelDir)) { // no channels to register, register default channel this.registerDefaultChannel(); return; - } else if (this.isPathEmpty(this.configChannelDir)) { - this.register(this.injector.getInstance(BasicChatChannel.class)); } // otherwise, register all channels found try (final Stream paths = Files.walk(this.configChannelDir)) { paths.forEach(path -> { final String fileName = path.getFileName().toString(); - if (!fileName.endsWith(".conf")) { return; } - final @Nullable ChatChannel chatChannel = this.registerChannelFromPath(path); - + final @Nullable ChatChannel chatChannel = this.channelFromPath(path); if (chatChannel == null) { this.logger.warn("Failed to load channel from file [" + fileName + "]"); return; } this.injector.injectMembers(chatChannel); + this.configChannels.add(chatChannel.key()); + this.register(chatChannel); if (chatChannel.shouldRegisterCommands()) { this.registerChannelCommands(chatChannel); @@ -240,7 +224,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann this.logger.info("Registered channels: [" + channels + "]"); } catch (final IOException exception) { - exception.printStackTrace(); + throw Exceptions.rethrow(exception); } this.eventHandler.emit(new ChannelRegisterEventImpl(this.keys(), this)); @@ -275,12 +259,13 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann loader.save(node); this.register(configChannel); + this.configChannels.add(configChannel.key()); } catch (final IOException exception) { - exception.printStackTrace(); + throw Exceptions.rethrow(exception); } } - private @Nullable ChatChannel registerChannelFromPath(final Path channelPath) { + private @Nullable ChatChannel channelFromPath(final Path channelPath) { final @Nullable ChatChannel channel = this.loadChannel(channelPath); if (channel == null) { @@ -293,7 +278,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann this.logger.info("Default channel is [" + channelKey + "]"); } - this.register(channel); + //this.register(channel); return channel; } @@ -324,10 +309,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann try (DirectoryStream directory = Files.newDirectoryStream(path)) { return !directory.iterator().hasNext(); } catch (final IOException exception) { - exception.printStackTrace(); + throw Exceptions.rethrow(exception); } - - return false; } @Override