Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-09-17 01:23:43 +02:00
Ensure order is kept with maps & yaml (as well as concurrency with maps)
Dieser Commit ist enthalten in:
Ursprung
7d23b12f85
Commit
64cde13ea7
@ -63,8 +63,12 @@ public class CommentStore {
|
|||||||
|
|
||||||
public void storeComments(InputStream inputStream) throws IOException {
|
public void storeComments(InputStream inputStream) throws IOException {
|
||||||
InputStreamReader reader = new InputStreamReader(inputStream);
|
InputStreamReader reader = new InputStreamReader(inputStream);
|
||||||
String contents = CharStreams.toString(reader);
|
String contents;
|
||||||
|
try {
|
||||||
|
contents = CharStreams.toString(reader);
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
StringBuilder memoryData = new StringBuilder();
|
StringBuilder memoryData = new StringBuilder();
|
||||||
// Parse headers
|
// Parse headers
|
||||||
final String pathSeparator = Character.toString(this.pathSeperator);
|
final String pathSeparator = Character.toString(this.pathSeperator);
|
||||||
|
@ -2,6 +2,7 @@ package us.myles.ViaVersion.util;
|
|||||||
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
import org.yaml.snakeyaml.representer.Representer;
|
||||||
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -10,7 +11,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentSkipListMap;
|
||||||
|
|
||||||
public abstract class Config implements ConfigurationProvider {
|
public abstract class Config implements ConfigurationProvider {
|
||||||
private static ThreadLocal<Yaml> yaml = new ThreadLocal<Yaml>() {
|
private static ThreadLocal<Yaml> yaml = new ThreadLocal<Yaml>() {
|
||||||
@ -18,12 +19,14 @@ public abstract class Config implements ConfigurationProvider {
|
|||||||
protected Yaml initialValue() {
|
protected Yaml initialValue() {
|
||||||
DumperOptions options = new DumperOptions();
|
DumperOptions options = new DumperOptions();
|
||||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
return new Yaml(options);
|
options.setPrettyFlow(false);
|
||||||
|
options.setIndent(2);
|
||||||
|
return new Yaml(new YamlConstructor(), new Representer(), options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private CommentStore commentStore = new CommentStore('.', 2);
|
private CommentStore commentStore = new CommentStore('.', 2);
|
||||||
private final File configFile;
|
private final File configFile;
|
||||||
private ConcurrentHashMap<String, Object> config;
|
private ConcurrentSkipListMap<String, Object> config;
|
||||||
|
|
||||||
public Config(File configFile) {
|
public Config(File configFile) {
|
||||||
this.configFile = configFile;
|
this.configFile = configFile;
|
||||||
@ -108,7 +111,7 @@ public abstract class Config implements ConfigurationProvider {
|
|||||||
@Override
|
@Override
|
||||||
public void reloadConfig() {
|
public void reloadConfig() {
|
||||||
this.configFile.getParentFile().mkdirs();
|
this.configFile.getParentFile().mkdirs();
|
||||||
this.config = new ConcurrentHashMap<>(loadConfig(this.configFile));
|
this.config = new ConcurrentSkipListMap<>(loadConfig(this.configFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
38
common/src/main/java/us/myles/ViaVersion/util/YamlConstructor.java
Normale Datei
38
common/src/main/java/us/myles/ViaVersion/util/YamlConstructor.java
Normale Datei
@ -0,0 +1,38 @@
|
|||||||
|
package us.myles.ViaVersion.util;
|
||||||
|
|
||||||
|
import org.yaml.snakeyaml.constructor.Constructor;
|
||||||
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||||
|
import org.yaml.snakeyaml.nodes.Node;
|
||||||
|
import org.yaml.snakeyaml.nodes.NodeId;
|
||||||
|
import org.yaml.snakeyaml.nodes.Tag;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentSkipListMap;
|
||||||
|
|
||||||
|
public class YamlConstructor extends SafeConstructor {
|
||||||
|
public YamlConstructor() {
|
||||||
|
super();
|
||||||
|
yamlClassConstructors.put(NodeId.mapping, new YamlConstructor.ConstructYamlMap());
|
||||||
|
yamlConstructors.put(Tag.OMAP, new YamlConstructor.ConstructYamlOmap());
|
||||||
|
}
|
||||||
|
|
||||||
|
class Map extends Constructor.ConstructYamlMap {
|
||||||
|
@Override
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object o = super.construct(node);
|
||||||
|
if (o instanceof Map && !(o instanceof ConcurrentSkipListMap)) {
|
||||||
|
return new ConcurrentSkipListMap<>((java.util.Map<?, ?>) o);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConstructYamlOmap extends Constructor.ConstructYamlOmap {
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object o = super.construct(node);
|
||||||
|
if (o instanceof Map && !(o instanceof ConcurrentSkipListMap)) {
|
||||||
|
return new ConcurrentSkipListMap<>((java.util.Map<?, ?>) o);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren