2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2024-01-01 12:39:45 +01:00
|
|
|
* Copyright (C) 2016-2024 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.util;
|
2016-09-30 01:34:16 +02:00
|
|
|
|
2021-06-21 14:55:49 +02:00
|
|
|
import com.google.gson.JsonElement;
|
2023-02-27 14:16:40 +01:00
|
|
|
import com.viaversion.viaversion.compatibility.YamlCompat;
|
|
|
|
import com.viaversion.viaversion.compatibility.unsafe.Yaml1Compat;
|
|
|
|
import com.viaversion.viaversion.compatibility.unsafe.Yaml2Compat;
|
2023-02-12 11:44:25 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2016-09-30 01:34:16 +02:00
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2016-10-02 21:50:34 +02:00
|
|
|
import java.util.concurrent.ConcurrentSkipListMap;
|
2023-10-11 05:23:40 +02:00
|
|
|
import java.util.logging.Logger;
|
2023-02-12 11:44:25 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
|
|
import org.yaml.snakeyaml.DumperOptions;
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
2016-09-30 01:34:16 +02:00
|
|
|
|
2023-03-16 20:41:03 +01:00
|
|
|
@SuppressWarnings("VulnerableCodeUsages")
|
2023-10-08 06:23:55 +02:00
|
|
|
public abstract class Config {
|
2023-10-11 05:23:40 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger("ViaVersion Config");
|
2023-03-16 20:41:03 +01:00
|
|
|
private static final YamlCompat YAMP_COMPAT = YamlCompat.isVersion1() ? new Yaml1Compat() : new Yaml2Compat();
|
2019-08-23 22:13:37 +02:00
|
|
|
private static final ThreadLocal<Yaml> YAML = ThreadLocal.withInitial(() -> {
|
|
|
|
DumperOptions options = new DumperOptions();
|
|
|
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
|
|
options.setPrettyFlow(false);
|
|
|
|
options.setIndent(2);
|
2023-03-16 20:41:03 +01:00
|
|
|
return new Yaml(YAMP_COMPAT.createSafeConstructor(), YAMP_COMPAT.createRepresenter(options), options);
|
2019-08-23 22:13:37 +02:00
|
|
|
});
|
2017-07-31 16:24:25 +02:00
|
|
|
|
2019-08-23 22:13:37 +02:00
|
|
|
private final CommentStore commentStore = new CommentStore('.', 2);
|
2016-09-30 01:34:16 +02:00
|
|
|
private final File configFile;
|
2019-11-22 22:00:41 +01:00
|
|
|
private Map<String, Object> config;
|
2016-09-30 01:34:16 +02:00
|
|
|
|
2017-09-11 14:10:54 +02:00
|
|
|
/**
|
|
|
|
* Create a new Config instance, this will *not* load the config by default.
|
2023-10-08 06:23:55 +02:00
|
|
|
* To load config see {@link #reload()}
|
2017-09-11 14:10:54 +02:00
|
|
|
*
|
|
|
|
* @param configFile The location of where the config is loaded/saved.
|
|
|
|
*/
|
2023-02-27 14:16:40 +01:00
|
|
|
protected Config(File configFile) {
|
2016-09-30 01:34:16 +02:00
|
|
|
this.configFile = configFile;
|
|
|
|
}
|
|
|
|
|
2022-01-09 18:30:51 +01:00
|
|
|
public URL getDefaultConfigURL() {
|
|
|
|
return getClass().getClassLoader().getResource("assets/viaversion/config.yml");
|
|
|
|
}
|
2017-09-11 14:10:54 +02:00
|
|
|
|
2022-08-01 11:34:21 +02:00
|
|
|
public Map<String, Object> loadConfig(File location) {
|
|
|
|
return loadConfig(location, getDefaultConfigURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized Map<String, Object> loadConfig(File location, URL jarConfigFile) {
|
2016-09-30 01:34:16 +02:00
|
|
|
List<String> unsupported = getUnsupportedOptions();
|
|
|
|
try {
|
|
|
|
commentStore.storeComments(jarConfigFile.openStream());
|
|
|
|
for (String option : unsupported) {
|
|
|
|
List<String> comments = commentStore.header(option);
|
|
|
|
if (comments != null) {
|
|
|
|
comments.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
Map<String, Object> config = null;
|
|
|
|
if (location.exists()) {
|
|
|
|
try (FileInputStream input = new FileInputStream(location)) {
|
2018-09-30 20:48:23 +02:00
|
|
|
config = (Map<String, Object>) YAML.get().load(input);
|
2016-09-30 01:34:16 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config == null) {
|
|
|
|
config = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, Object> defaults = config;
|
|
|
|
try (InputStream stream = jarConfigFile.openStream()) {
|
2018-09-30 20:48:23 +02:00
|
|
|
defaults = (Map<String, Object>) YAML.get().load(stream);
|
2016-09-30 01:34:16 +02:00
|
|
|
for (String option : unsupported) {
|
|
|
|
defaults.remove(option);
|
|
|
|
}
|
|
|
|
// Merge with defaultLoader
|
2019-05-27 18:50:08 +02:00
|
|
|
for (Map.Entry<String, Object> entry : config.entrySet()) {
|
2016-09-30 01:34:16 +02:00
|
|
|
// Set option in new conf if exists
|
2019-05-27 18:50:08 +02:00
|
|
|
if (defaults.containsKey(entry.getKey()) && !unsupported.contains(entry.getKey())) {
|
|
|
|
defaults.put(entry.getKey(), entry.getValue());
|
2016-09-30 01:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-10-02 20:40:38 +02:00
|
|
|
// Call Handler
|
|
|
|
handleConfig(defaults);
|
2016-09-30 01:34:16 +02:00
|
|
|
// Save
|
2023-10-08 06:23:55 +02:00
|
|
|
save(location, defaults);
|
2016-09-30 01:34:16 +02:00
|
|
|
|
|
|
|
return defaults;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:40:38 +02:00
|
|
|
protected abstract void handleConfig(Map<String, Object> config);
|
|
|
|
|
2023-10-08 06:23:55 +02:00
|
|
|
public synchronized void save(File location, Map<String, Object> config) {
|
2016-09-30 01:34:16 +02:00
|
|
|
try {
|
2018-09-30 20:48:23 +02:00
|
|
|
commentStore.writeComments(YAML.get().dump(config), location);
|
2016-09-30 01:34:16 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract List<String> getUnsupportedOptions();
|
|
|
|
|
|
|
|
public void set(String path, Object value) {
|
2016-10-02 12:03:56 +02:00
|
|
|
config.put(path, value);
|
2016-09-30 01:34:16 +02:00
|
|
|
}
|
|
|
|
|
2023-10-08 06:23:55 +02:00
|
|
|
public void save() {
|
2016-09-30 01:34:16 +02:00
|
|
|
this.configFile.getParentFile().mkdirs();
|
2023-10-08 06:23:55 +02:00
|
|
|
save(this.configFile, this.config);
|
2016-09-30 01:34:16 +02:00
|
|
|
}
|
|
|
|
|
2023-10-08 06:23:55 +02:00
|
|
|
public void save(final File file) {
|
|
|
|
save(file, this.config);
|
2022-08-01 11:34:21 +02:00
|
|
|
}
|
|
|
|
|
2023-10-08 06:23:55 +02:00
|
|
|
public void reload() {
|
2016-09-30 01:34:16 +02:00
|
|
|
this.configFile.getParentFile().mkdirs();
|
2016-10-02 21:50:34 +02:00
|
|
|
this.config = new ConcurrentSkipListMap<>(loadConfig(this.configFile));
|
2016-09-30 01:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getValues() {
|
|
|
|
return this.config;
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:15:30 +02:00
|
|
|
public @Nullable <T> T get(String key, Class<T> clazz, T def) {
|
2018-09-30 20:48:23 +02:00
|
|
|
Object o = this.config.get(key);
|
|
|
|
if (o != null) {
|
|
|
|
return (T) o;
|
2016-10-02 20:40:38 +02:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 01:34:16 +02:00
|
|
|
public boolean getBoolean(String key, boolean def) {
|
2018-09-30 20:48:23 +02:00
|
|
|
Object o = this.config.get(key);
|
|
|
|
if (o != null) {
|
|
|
|
return (boolean) o;
|
2016-09-30 01:34:16 +02:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:15:30 +02:00
|
|
|
public @Nullable String getString(String key, @Nullable String def) {
|
2018-09-30 20:48:23 +02:00
|
|
|
final Object o = this.config.get(key);
|
|
|
|
if (o != null) {
|
|
|
|
return (String) o;
|
2016-09-30 01:34:16 +02:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInt(String key, int def) {
|
2018-09-30 20:48:23 +02:00
|
|
|
Object o = this.config.get(key);
|
|
|
|
if (o != null) {
|
|
|
|
if (o instanceof Number) {
|
|
|
|
return ((Number) o).intValue();
|
2016-11-14 19:29:45 +01:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
2016-09-30 01:34:16 +02:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getDouble(String key, double def) {
|
2018-09-30 20:48:23 +02:00
|
|
|
Object o = this.config.get(key);
|
|
|
|
if (o != null) {
|
|
|
|
if (o instanceof Number) {
|
|
|
|
return ((Number) o).doubleValue();
|
2016-11-14 19:29:45 +01:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
2016-09-30 01:34:16 +02:00
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Integer> getIntegerList(String key) {
|
2018-09-30 20:48:23 +02:00
|
|
|
Object o = this.config.get(key);
|
2021-09-20 15:15:25 +02:00
|
|
|
return o != null ? (List<Integer>) o : new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getStringList(String key) {
|
|
|
|
Object o = this.config.get(key);
|
|
|
|
return o != null ? (List<String>) o : new ArrayList<>();
|
2016-09-30 01:34:16 +02:00
|
|
|
}
|
2021-06-21 14:55:49 +02:00
|
|
|
|
2022-06-28 11:22:42 +02:00
|
|
|
public <T> List<T> getListSafe(String key, Class<T> type, String invalidValueMessage) {
|
|
|
|
Object o = this.config.get(key);
|
|
|
|
if (o instanceof List) {
|
|
|
|
List<?> list = (List<?>) o;
|
|
|
|
List<T> filteredValues = new ArrayList<>();
|
|
|
|
for (Object o1 : list) {
|
|
|
|
if (type.isInstance(o1)) {
|
|
|
|
filteredValues.add(type.cast(o1));
|
|
|
|
} else if (invalidValueMessage != null) {
|
2023-10-11 05:23:40 +02:00
|
|
|
LOGGER.warning(String.format(invalidValueMessage, o1));
|
2022-06-28 11:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredValues;
|
|
|
|
}
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:55:49 +02:00
|
|
|
public @Nullable JsonElement getSerializedComponent(String key) {
|
|
|
|
final Object o = this.config.get(key);
|
|
|
|
if (o != null && !((String) o).isEmpty()) {
|
2023-12-26 23:38:02 +01:00
|
|
|
return ComponentUtil.legacyToJson((String) o);
|
2021-06-21 14:55:49 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 01:34:16 +02:00
|
|
|
}
|