Refactor ConfigurationProvider

This commit is contained in:
Nassim Jahnke 2023-10-08 14:23:55 +10:00
parent 0c3d709456
commit d8f98945e2
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
25 changed files with 170 additions and 93 deletions

View File

@ -63,7 +63,7 @@ public interface ViaAPI<T> {
* @return API version incremented with meaningful API changes
*/
default int apiVersion() {
return 20;
return 21;
}
/**

View File

@ -23,6 +23,7 @@
package com.viaversion.viaversion.api;
import com.viaversion.viaversion.api.command.ViaVersionCommand;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.connection.ConnectionManager;
import com.viaversion.viaversion.api.debug.DebugHandler;
import com.viaversion.viaversion.api.platform.ViaInjector;
@ -91,6 +92,13 @@ public interface ViaManager {
*/
Scheduler getScheduler();
/**
* Returns the configuration provider.
*
* @return the configuration provider
*/
ConfigurationProvider getConfigurationProvider();
/**
* If debug is enabled, packets and other otherwise suppressed warnings will be logged.
*
@ -138,4 +146,5 @@ public interface ViaManager {
* @return whether the manager has been initialized
*/
boolean isInitialized();
}

View File

@ -0,0 +1,53 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2023 ViaVersion and contributors
*
* 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.
*/
package com.viaversion.viaversion.api.configuration;
import java.util.Map;
public interface Config {
/**
* Reloads the config
*/
void reload();
/**
* Saves the config
*/
void save();
/**
* Sets the specified path to the given value.
*
* @param path Path of the object to set.
* @param value New value to set the path to
*/
void set(String path, Object value);
/**
* Get all the configuration values
*
* @return Map with key-values
*/
Map<String, Object> getValues();
}

View File

@ -22,32 +22,26 @@
*/
package com.viaversion.viaversion.api.configuration;
import java.util.Map;
import java.util.Collection;
public interface ConfigurationProvider {
/**
* Sets the specified path to the given value.
* Registers a config to be saved or reloaded when {@link #reloadConfigs()} is called.
*
* @param path Path of the object to set.
* @param value New value to set the path to
* @param config the config to register
*/
void set(String path, Object value);
void register(Config config);
/**
* Saves the config
*/
void saveConfig();
/**
* Reloads the config
*/
void reloadConfig();
/**
* Get all the configuration values
* Returns all registered configs.
*
* @return Map with key-values
* @return unmodifiable collection of all registered configs
*/
Map<String, Object> getValues();
Collection<Config> configs();
/**
* Reloads the configuration files.
*/
void reloadConfigs();
}

View File

@ -30,7 +30,7 @@ import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.checkerframework.checker.nullness.qual.Nullable;
public interface ViaVersionConfig {
public interface ViaVersionConfig extends Config {
/**
* Get if the plugin should check for updates

View File

@ -23,6 +23,7 @@
package com.viaversion.viaversion.api.platform;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.ViaAPI;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
@ -180,13 +181,10 @@ public interface ViaPlatform<T> {
*/
ViaVersionConfig getConf();
/**
* Get the backend configuration provider for this platform.
* (On some platforms this returns the same as getConf())
*
* @return The configuration provider
*/
ConfigurationProvider getConfigurationProvider();
@Deprecated/*(forRemoval = true)*/
default ConfigurationProvider getConfigurationProvider() {
return Via.getManager().getConfigurationProvider();
}
/**
* Get ViaVersions's data folder.

View File

@ -221,11 +221,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player>
return Bukkit.getPluginManager().getPlugin("ViaVersion").isEnabled();
}
@Override
public ConfigurationProvider getConfigurationProvider() {
return conf;
}
@Override
public void onReload() {
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {

View File

@ -36,7 +36,7 @@ public class BukkitViaConfig extends AbstractViaConfig {
public BukkitViaConfig() {
super(new File(((Plugin) Via.getPlatform()).getDataFolder(), "config.yml"));
reloadConfig();
reload();
}
@Override

View File

@ -22,7 +22,6 @@ import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.ViaAPI;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.platform.PlatformTask;
import com.viaversion.viaversion.api.platform.UnsupportedSoftware;
import com.viaversion.viaversion.api.platform.ViaServerProxyPlatform;
@ -176,11 +175,6 @@ public class BungeePlugin extends Plugin implements ViaServerProxyPlatform<Proxi
return config;
}
@Override
public ConfigurationProvider getConfigurationProvider() {
return config;
}
@Override
public void onReload() {
// Injector prints a message <3

View File

@ -35,7 +35,7 @@ public class BungeeViaConfig extends AbstractViaConfig {
public BungeeViaConfig(File configFile) {
super(new File(configFile, "config.yml"));
reloadConfig();
reload();
}
@Override

View File

@ -46,8 +46,9 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi
setProtocolVersion(serverName, serverPing.getVersion().getProtocol());
if (((BungeeViaConfig) Via.getConfig()).isBungeePingSave()) {
final Map<String, Integer> servers = ((BungeeViaConfig) Via.getConfig()).getBungeeServerProtocols();
final BungeeViaConfig config = (BungeeViaConfig) Via.getConfig();
if (config.isBungeePingSave()) {
final Map<String, Integer> servers = config.getBungeeServerProtocols();
final Integer protocol = servers.get(serverName);
if (protocol != null && protocol == serverPing.getVersion().getProtocol()) {
return;
@ -57,7 +58,7 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi
synchronized (Via.getPlatform().getConfigurationProvider()) {
servers.put(serverName, serverPing.getVersion().getProtocol());
}
Via.getPlatform().getConfigurationProvider().saveConfig();
config.save();
}
});
}

View File

@ -19,6 +19,7 @@ package com.viaversion.viaversion;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.ViaManager;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.connection.ConnectionManager;
import com.viaversion.viaversion.api.debug.DebugHandler;
import com.viaversion.viaversion.api.platform.PlatformTask;
@ -32,6 +33,7 @@ import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion;
import com.viaversion.viaversion.api.scheduler.Scheduler;
import com.viaversion.viaversion.commands.ViaCommandHandler;
import com.viaversion.viaversion.configuration.ConfigurationProviderImpl;
import com.viaversion.viaversion.connection.ConnectionManagerImpl;
import com.viaversion.viaversion.debug.DebugHandlerImpl;
import com.viaversion.viaversion.protocol.ProtocolManagerImpl;
@ -53,6 +55,7 @@ import java.util.regex.Pattern;
public class ViaManagerImpl implements ViaManager {
private final ProtocolManagerImpl protocolManager = new ProtocolManagerImpl();
private final ConnectionManager connectionManager = new ConnectionManagerImpl();
private final ConfigurationProvider configurationProvider = new ConfigurationProviderImpl();
private final DebugHandler debugHandler = new DebugHandlerImpl();
private final ViaProviders providers = new ViaProviders();
private final Scheduler scheduler = new TaskScheduler();
@ -306,6 +309,11 @@ public class ViaManagerImpl implements ViaManager {
return scheduler;
}
@Override
public ConfigurationProvider getConfigurationProvider() {
return configurationProvider;
}
/**
* Returns a mutable set of self-added subplatform version strings.
* This set is expanded by the subplatform itself (e.g. ViaBackwards), and may not contain all running ones.

View File

@ -20,7 +20,7 @@ package com.viaversion.viaversion.commands.defaultsubs;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.command.ViaSubCommand;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.util.Config;
public class AutoTeamSubCmd extends ViaSubCommand {
@Override
@ -35,11 +35,11 @@ public class AutoTeamSubCmd extends ViaSubCommand {
@Override
public boolean execute(ViaCommandSender sender, String[] args) {
ConfigurationProvider provider = Via.getPlatform().getConfigurationProvider();
boolean newValue = !Via.getConfig().isAutoTeam();
provider.set("auto-team", newValue);
provider.saveConfig();
final Config config = (Config) Via.getConfig();
config.set("auto-team", newValue);
config.save();
sendMessage(sender, "&6We will %s", (newValue ? "&aautomatically team players" : "&cno longer auto team players"));
sendMessage(sender, "&6All players will need to re-login for the change to take place.");

View File

@ -20,7 +20,7 @@ package com.viaversion.viaversion.commands.defaultsubs;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.command.ViaSubCommand;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.configuration.ViaVersionConfig;
public class DontBugMeSubCmd extends ViaSubCommand {
@ -36,11 +36,11 @@ public class DontBugMeSubCmd extends ViaSubCommand {
@Override
public boolean execute(ViaCommandSender sender, String[] args) {
ConfigurationProvider provider = Via.getPlatform().getConfigurationProvider();
boolean newValue = !Via.getConfig().isCheckForUpdates();
final ViaVersionConfig config = Via.getConfig();
boolean newValue = !config.isCheckForUpdates();
Via.getConfig().setCheckForUpdates(newValue);
provider.saveConfig();
config.setCheckForUpdates(newValue);
config.save();
sendMessage(sender, "&6We will %snotify you about updates.", (newValue ? "&a" : "&cnot "));
return true;

View File

@ -34,7 +34,7 @@ public class ReloadSubCmd extends ViaSubCommand {
@Override
public boolean execute(ViaCommandSender sender, String[] args) {
Via.getPlatform().getConfigurationProvider().reloadConfig();
Via.getPlatform().getConfigurationProvider().reloadConfigs();
sendMessage(sender, "&6Configuration successfully reloaded! Some features may need a restart.");
return true;
}

View File

@ -91,13 +91,14 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
private boolean cache1_17Light;
private Map<String, String> chatTypeFormats;
protected AbstractViaConfig(File configFile) {
protected AbstractViaConfig(final File configFile) {
super(configFile);
Via.getManager().getConfigurationProvider().register(this);
}
@Override
public void reloadConfig() {
super.reloadConfig();
public void reload() {
super.reload();
loadFields();
}

View File

@ -0,0 +1,46 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 ViaVersion and contributors
*
* 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/>.
*/
package com.viaversion.viaversion.configuration;
import com.viaversion.viaversion.api.configuration.Config;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class ConfigurationProviderImpl implements ConfigurationProvider {
private final List<Config> configs = new ArrayList<>();
@Override
public void register(final Config config) {
configs.add(config);
}
@Override
public Collection<Config> configs() {
return Collections.unmodifiableCollection(configs);
}
@Override
public void reloadConfigs() {
for (final Config config : configs) {
config.reload();
}
}
}

View File

@ -19,7 +19,6 @@ package com.viaversion.viaversion.util;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.compatibility.YamlCompat;
import com.viaversion.viaversion.compatibility.unsafe.Yaml1Compat;
import com.viaversion.viaversion.compatibility.unsafe.Yaml2Compat;
@ -40,7 +39,7 @@ import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
@SuppressWarnings("VulnerableCodeUsages")
public abstract class Config implements ConfigurationProvider {
public abstract class Config {
private static final YamlCompat YAMP_COMPAT = YamlCompat.isVersion1() ? new Yaml1Compat() : new Yaml2Compat();
private static final ThreadLocal<Yaml> YAML = ThreadLocal.withInitial(() -> {
DumperOptions options = new DumperOptions();
@ -56,7 +55,7 @@ public abstract class Config implements ConfigurationProvider {
/**
* Create a new Config instance, this will *not* load the config by default.
* To load config see {@link #reloadConfig()}
* To load config see {@link #reload()}
*
* @param configFile The location of where the config is loaded/saved.
*/
@ -116,14 +115,14 @@ public abstract class Config implements ConfigurationProvider {
// Call Handler
handleConfig(defaults);
// Save
saveConfig(location, defaults);
save(location, defaults);
return defaults;
}
protected abstract void handleConfig(Map<String, Object> config);
public synchronized void saveConfig(File location, Map<String, Object> config) {
public synchronized void save(File location, Map<String, Object> config) {
try {
commentStore.writeComments(YAML.get().dump(config), location);
} catch (IOException e) {
@ -133,28 +132,24 @@ public abstract class Config implements ConfigurationProvider {
public abstract List<String> getUnsupportedOptions();
@Override
public void set(String path, Object value) {
config.put(path, value);
}
@Override
public void saveConfig() {
public void save() {
this.configFile.getParentFile().mkdirs();
saveConfig(this.configFile, this.config);
save(this.configFile, this.config);
}
public void saveConfig(final File file) {
saveConfig(file, this.config);
public void save(final File file) {
save(file, this.config);
}
@Override
public void reloadConfig() {
public void reload() {
this.configFile.getParentFile().mkdirs();
this.config = new ConcurrentSkipListMap<>(loadConfig(this.configFile));
}
@Override
public Map<String, Object> getValues() {
return this.config;
}

View File

@ -65,7 +65,7 @@ public final class DumpUtil {
com.viaversion.viaversion.util.VersionInfo.getImplementationVersion(),
Via.getManager().getSubPlatforms()
);
final Map<String, Object> configuration = Via.getPlatform().getConfigurationProvider().getValues();
final Map<String, Object> configuration = ((Config) Via.getConfig()).getValues();
final DumpTemplate template = new DumpTemplate(version, configuration, Via.getPlatform().getDump(), Via.getManager().getInjector().getDump(), getPlayerSample(playerToSample));
final CompletableFuture<String> result = new CompletableFuture<>();
Via.getPlatform().runAsync(() -> {

View File

@ -21,7 +21,6 @@ import com.google.gson.JsonObject;
import com.viaversion.viaversion.ViaAPIBase;
import com.viaversion.viaversion.api.ViaAPI;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.configuration.ViaVersionConfig;
import com.viaversion.viaversion.api.platform.PlatformTask;
import com.viaversion.viaversion.api.platform.ViaPlatform;
@ -118,11 +117,6 @@ public final class TestPlatform implements ViaPlatform {
return testConfig;
}
@Override
public ConfigurationProvider getConfigurationProvider() {
return null;
}
@Override
public File getDataFolder() {
return null;

View File

@ -21,7 +21,6 @@ import com.google.gson.JsonObject;
import com.google.inject.Inject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.platform.PlatformTask;
import com.viaversion.viaversion.api.platform.ViaPlatform;
import com.viaversion.viaversion.dump.PluginInfo;
@ -193,11 +192,6 @@ public class SpongePlugin implements ViaPlatform<Player> {
return true;
}
@Override
public ConfigurationProvider getConfigurationProvider() {
return conf;
}
@Override
public File getDataFolder() {
return configDir.toFile();

View File

@ -30,7 +30,7 @@ public class SpongeViaConfig extends AbstractViaConfig {
public SpongeViaConfig(File configFile) {
super(new File(configFile, "config.yml"));
reloadConfig();
reload();
}
@Override

View File

@ -29,7 +29,6 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.api.platform.PlatformTask;
import com.viaversion.viaversion.api.platform.ViaServerProxyPlatform;
import com.viaversion.viaversion.dump.PluginInfo;
@ -193,11 +192,6 @@ public class VelocityPlugin implements ViaServerProxyPlatform<Player> {
return true;
}
@Override
public ConfigurationProvider getConfigurationProvider() {
return conf;
}
@Override
public File getDataFolder() {
return configDir.toFile();

View File

@ -34,7 +34,7 @@ public class VelocityViaConfig extends AbstractViaConfig {
public VelocityViaConfig(File configFile) {
super(new File(configFile, "config.yml"));
reloadConfig();
reload();
}
@Override

View File

@ -63,7 +63,8 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi
setProtocolVersion(serverName, serverPing.getVersion().getProtocol());
if (((VelocityViaConfig) Via.getConfig()).isVelocityPingSave()) {
final VelocityViaConfig config = (VelocityViaConfig) Via.getConfig();
if (config.isVelocityPingSave()) {
final Map<String, Integer> servers = configuredServers();
final Integer protocol = servers.get(serverName);
if (protocol != null && protocol == serverPing.getVersion().getProtocol()) {
@ -74,7 +75,7 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi
synchronized (Via.getPlatform().getConfigurationProvider()) {
servers.put(serverName, serverPing.getVersion().getProtocol());
}
Via.getPlatform().getConfigurationProvider().saveConfig();
config.save();
}
});
}