Skip to content

Commit

Permalink
Always override existing config comments (closes #311)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Oct 23, 2023
1 parent 5483bfd commit 1376964
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,13 @@
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;

@Singleton
@DefaultQualifier(NonNull.class)
public class CarbonChannelRegistry extends ChatListenerInternal implements ChannelRegistry {

private static final String PARTYCHAT_CONF = "partychat.conf";
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
private static @MonotonicNonNull ObjectMapper<PartyChatChannel> PARTY_MAPPER;

static {
try {
MAPPER = ObjectMapper.factory().get(ConfigChatChannel.class);
PARTY_MAPPER = ObjectMapper.factory().get(PartyChatChannel.class);
} catch (final SerializationException e) {
e.printStackTrace();
}
}

private final Path configChannelDir;
private final Injector injector;
Expand Down Expand Up @@ -297,9 +284,21 @@ private void saveDefaultPartyConfig() {
final ConfigurationLoader<?> loader = this.config.configurationLoader(channelFile);

try {
final Class<? extends ConfigChatChannel> type = this.config.primaryConfig().partyChat().enabled && channelFile.getFileName().toString().equals(PARTYCHAT_CONF)
? PartyChatChannel.class
: ConfigChatChannel.class;

final ConfigurationNode loaded = updateNode(loader.load());
loader.save(loaded);
return (this.config.primaryConfig().partyChat().enabled && channelFile.getFileName().toString().equals(PARTYCHAT_CONF) ? PARTY_MAPPER : MAPPER).load(loaded);
final @Nullable ConfigChatChannel channel = loaded.get(type);
if (channel == null) {
throw new ConfigurateException("Config deserialized to null.");
}

final ConfigurationNode newNode = loader.createNode();
newNode.set(type, channel);
loader.save(newNode);

return channel;
} catch (final ConfigurateException exception) {
this.logger.warn("Failed to load channel from file '{}'", channelFile, exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.spongepowered.configurate.CommentedConfigurationNodeIntermediary;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.objectmapping.meta.Processor;

@DefaultQualifier(NonNull.class)
@Singleton
Expand Down Expand Up @@ -112,15 +116,26 @@ public ConfigurationLoader<?> configurationLoader(final Path file) {
final ConfigurateComponentSerializer serializer =
ConfigurateComponentSerializer.configurate();

return opts.shouldCopyDefaults(true).serializers(serializerBuilder ->
serializerBuilder.registerAll(serializer.serializers())
.register(Locale.class, this.locale)
);
return opts.shouldCopyDefaults(true)
.serializers(serializerBuilder ->
serializerBuilder.registerAll(serializer.serializers())
.register(Locale.class, this.locale)
.registerAnnotatedObjects(ObjectMapper.factoryBuilder()
.addProcessor(Comment.class, overrideComments())
.build()));
})
.path(file)
.build();
}

private static Processor.Factory<Comment, Object> overrideComments() {
return (data, fieldType) -> (value, destination) -> {
if (destination instanceof final CommentedConfigurationNodeIntermediary<?> commented) {
commented.comment(data.value());
}
};
}

public <T> @Nullable T load(final Class<T> clazz, final String fileName) {
final Path file = this.dataDirectory.resolve(fileName);
try {
Expand Down

0 comments on commit 1376964

Please sign in to comment.