-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4954 from Konicai/feature/configurate
Improve node ordering when updating configs
- Loading branch information
Showing
4 changed files
with
196 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/src/main/java/org/geysermc/geyser/configuration/ConfigurationCommentMover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.configuration; | ||
|
||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.spongepowered.configurate.CommentedConfigurationNode; | ||
import org.spongepowered.configurate.ConfigurationNode; | ||
import org.spongepowered.configurate.ConfigurationVisitor; | ||
|
||
/** | ||
* Moves comments from a different node and puts them on this node | ||
*/ | ||
public final class ConfigurationCommentMover implements ConfigurationVisitor.Stateless<RuntimeException> { | ||
|
||
private final CommentedConfigurationNode otherRoot; | ||
|
||
private ConfigurationCommentMover(@NonNull CommentedConfigurationNode otherNode) { | ||
this.otherRoot = otherNode; | ||
} | ||
|
||
@Override | ||
public void enterNode(final ConfigurationNode node) { | ||
if (!(node instanceof CommentedConfigurationNode destination)) { | ||
// Should not occur because all nodes in a tree are the same type, | ||
// and our static method below ensures this visitor is only used on CommentedConfigurationNodes | ||
throw new IllegalStateException(node.path() + " is not a CommentedConfigurationNode"); | ||
} | ||
// Node with the same path | ||
CommentedConfigurationNode source = otherRoot.node(node.path()); | ||
|
||
moveSingle(source, destination); | ||
} | ||
|
||
private static void moveSingle(@NonNull CommentedConfigurationNode source, @NonNull CommentedConfigurationNode destination) { | ||
// Only transfer the comment, overriding if necessary | ||
String comment = source.comment(); | ||
if (comment != null) { | ||
destination.comment(comment); | ||
} | ||
} | ||
|
||
/** | ||
* Moves comments from a source node and its children to a destination node and its children (of a different tree), overriding if necessary. | ||
* Comments are only moved to the destination node and its children which exist. | ||
* Comments are only moved to and from nodes with the exact same path. | ||
* | ||
* @param source the source of the comments, which must be the topmost parent of a tree | ||
* @param destination the destination of the comments, any node in a different tree | ||
*/ | ||
public static void moveComments(@NonNull CommentedConfigurationNode source, @NonNull CommentedConfigurationNode destination) { | ||
if (source.parent() != null) { | ||
throw new IllegalArgumentException("source is not the base of the tree it is within: " + source.path()); | ||
} | ||
|
||
if (source.isNull()) { | ||
// It has no value(s), but may still have a comment on it. Don't both traversing the whole destination tree. | ||
moveSingle(source, destination); | ||
} else { | ||
destination.visit(new ConfigurationCommentMover(source)); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.configuration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.spongepowered.configurate.CommentedConfigurationNode; | ||
import org.spongepowered.configurate.util.CheckedConsumer; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class ConfigLoaderTest { | ||
|
||
@TempDir | ||
Path tempDirectory; | ||
|
||
// Hack until improving ConfigLoader | ||
CommentedConfigurationNode config1; | ||
CommentedConfigurationNode config2; | ||
|
||
@Test | ||
void testCreateNewConfig() throws Exception { | ||
// Test that the result of generating a config, and the result of reading it back after writing it, is the same | ||
|
||
File file = tempDirectory.resolve("config.yml").toFile(); | ||
|
||
forAllConfigs(type -> { | ||
ConfigLoader.load(file, type, n -> this.config1 = n.copy()); | ||
long initialModification = file.lastModified(); | ||
assertTrue(file.exists()); // should have been created | ||
List<String> firstContents = Files.readAllLines(file.toPath()); | ||
|
||
ConfigLoader.load(file, type, n -> this.config2 = n.copy()); | ||
List<String> secondContents = Files.readAllLines(file.toPath()); | ||
|
||
assertEquals(initialModification, file.lastModified()); // should not have been touched | ||
assertEquals(firstContents, secondContents); | ||
|
||
// Must ignore this, as when the config is read back, the header is interpreted as a comment on the first node in the map | ||
config1.node("java").comment(null); | ||
config2.node("java").comment(null); | ||
assertEquals(config1, config2); | ||
}); | ||
} | ||
|
||
void forAllConfigs(CheckedConsumer<Class<? extends GeyserConfig>, Exception> consumer) throws Exception { | ||
consumer.accept(GeyserRemoteConfig.class); | ||
consumer.accept(GeyserPluginConfig.class); | ||
} | ||
} |