Search in sources :

Example 6 with CommentedConfigurationNode

use of org.spongepowered.configurate.CommentedConfigurationNode in project SpongeCommon by SpongePowered.

the class SpongeConfigs method splitFiles.

private static void splitFiles() {
    final Path commonFile = SpongeConfigs.getDirectory().resolve(CommonConfig.FILE_NAME);
    final Path metricsFile = SpongeConfigs.getDirectory().resolve(SpongeConfigs.METRICS_NAME);
    final Path oldGlobalFile = SpongeConfigs.getDirectory().resolve(SpongeConfigs.GLOBAL_NAME);
    // Is this migration unnecessary?
    if (!Files.exists(oldGlobalFile) || Files.exists(commonFile) || Files.exists(metricsFile)) {
        return;
    }
    try {
        final ConfigurationTransformation xform = ConfigurationTransformation.chain(new FileMovingConfigurationTransformation(SpongeConfigs.MIGRATE_SPONGE_PATHS, SpongeConfigs.createLoader(commonFile), true), new FileMovingConfigurationTransformation(SpongeConfigs.MIGRATE_METRICS_PATHS, SpongeConfigs.createLoader(metricsFile), true));
        final ConfigurationLoader<CommentedConfigurationNode> globalLoader = SpongeConfigs.createLoader(oldGlobalFile);
        Files.copy(oldGlobalFile, oldGlobalFile.resolveSibling(SpongeConfigs.GLOBAL_NAME + ".old-backup"));
        final CommentedConfigurationNode source = globalLoader.load();
        xform.apply(source);
        globalLoader.save(source);
        SpongeConfigs.LOGGER.info("Migrated Sponge configuration to 1.15+ split-file layout");
    } catch (final IOException ex) {
        SpongeConfigs.LOGGER.error("An error occurred while trying to migrate to a split-file configuration layout", ex);
    }
}
Also used : NodePath(org.spongepowered.configurate.NodePath) Path(java.nio.file.Path) ConfigurationTransformation(org.spongepowered.configurate.transformation.ConfigurationTransformation) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) IOException(java.io.IOException)

Example 7 with CommentedConfigurationNode

use of org.spongepowered.configurate.CommentedConfigurationNode in project SpongeCommon by SpongePowered.

the class ConfigHandle method doSave.

protected void doSave() throws ConfigurateException {
    if (this.loader == null) {
        return;
    }
    if (this.node == null) {
        this.node = this.loader.createNode();
    }
    final T instance = this.instance;
    final CommentedConfigurationNode node = this.node;
    if (instance != null && node != null) {
        node.set(this.instanceType, instance);
    }
    this.loader.save(node);
}
Also used : CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode)

Example 8 with CommentedConfigurationNode

use of org.spongepowered.configurate.CommentedConfigurationNode in project SpongeCommon by SpongePowered.

the class ConfigHandle method doVersionUpdate.

protected final void doVersionUpdate(final CommentedConfigurationNode node) throws ConfigurateException {
    if (this.transformer != null) {
        final boolean wasEmpty = node.empty();
        final CommentedConfigurationNode versionNode = node.node(ConfigHandle.VERSION_PATH);
        final int existingVersion = versionNode.getInt(-1);
        this.transformer.get().apply(node);
        final int newVersion = versionNode.getInt(-1);
        if (!wasEmpty && newVersion > existingVersion) {
            ConfigHandle.LOGGER.info("Updated {} from version {} to {}", this.instance, existingVersion, newVersion);
        }
        versionNode.commentIfAbsent(ConfigHandle.VERSION_COMMENT);
        node.node(ConfigHandle.VERSION_PATH).set(versionNode);
    }
}
Also used : CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode)

Aggregations

CommentedConfigurationNode (org.spongepowered.configurate.CommentedConfigurationNode)8 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 NodePath (org.spongepowered.configurate.NodePath)1 ConfigurationTransformation (org.spongepowered.configurate.transformation.ConfigurationTransformation)1