diff --git a/api/src/main/java/com/bivashy/auth/api/config/PluginConfig.java b/api/src/main/java/com/bivashy/auth/api/config/PluginConfig.java index 4f3b282a..3932bfcd 100644 --- a/api/src/main/java/com/bivashy/auth/api/config/PluginConfig.java +++ b/api/src/main/java/com/bivashy/auth/api/config/PluginConfig.java @@ -25,6 +25,8 @@ public interface PluginConfig { boolean isNameCaseCheckEnabled(); + boolean isAutoMigrateConfigEnabled(); + CryptoProvider getActiveHashType(); DatabaseConnectionProvider getStorageType(); diff --git a/core/pom.xml b/core/pom.xml index 8443ae0b..07b142fa 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -23,6 +23,7 @@ 4.13.0 0.4.2 1.7.0 + 1.4.2 @@ -136,5 +137,12 @@ password4j ${password4j.version} + + + + ru.vyarus + yaml-config-updater + ${yaml-config-updater.version} + \ No newline at end of file diff --git a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java index e4afe2d2..1e34abf5 100644 --- a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java +++ b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java @@ -1,6 +1,8 @@ package me.mastercapexd.auth; import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; @@ -8,6 +10,9 @@ import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.AuthPluginProvider; import com.bivashy.auth.api.account.AccountFactory; +import com.bivashy.auth.api.asset.resource.Resource; +import com.bivashy.auth.api.asset.resource.impl.FolderResource; +import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader; import com.bivashy.auth.api.bucket.AuthenticatingAccountBucket; import com.bivashy.auth.api.bucket.AuthenticationStepContextFactoryBucket; import com.bivashy.auth.api.bucket.AuthenticationStepFactoryBucket; @@ -75,6 +80,7 @@ import me.mastercapexd.auth.util.HashUtils; import me.mastercapexd.auth.util.TimeUtils; import net.kyori.adventure.platform.AudienceProvider; +import ru.vyarus.yaml.updater.YamlUpdater; public class BaseAuthPlugin implements AuthPlugin { private final ConfigurationProcessor configurationProcessor = new SpongeConfigurateProcessor(); @@ -118,6 +124,13 @@ private void initializeBasic() { this.registerCryptoProviders(); this.registerConfigurationProcessor(); this.config = new BasePluginConfig(this); + if (config.isAutoMigrateConfigEnabled()) { + try { + this.migrateConfig(); + } catch(IOException | URISyntaxException e) { + e.printStackTrace(); + } + } this.authenticationStepContextFactoryBucket = new BaseAuthenticationStepContextFactoryBucket(config.getAuthenticationSteps()); DatabaseHelper databaseHelper = new DatabaseHelper(this); @@ -167,6 +180,17 @@ private void initializeTelegram() { new TelegramCommandRegistry(); } + private void migrateConfig() throws IOException, URISyntaxException { + FolderResource folderResource = new FolderResourceReader(getClass().getClassLoader(), "configurations").read(); + for (Resource resource : folderResource.getResources()) { + String realConfigurationName = resource.getName().substring(folderResource.getName().length() + 1); + File resourceConfiguration = new File(getFolder(), realConfigurationName); + if (!resourceConfiguration.exists()) + continue; + YamlUpdater.create(resourceConfiguration, resource.getStream()).backup(true).update(); + } + } + private void registerConfigurationProcessor() { configurationProcessor.registerFieldResolver(ConfigurationServer.class, (context) -> new BaseConfigurationServer(context.getString())) .registerFieldResolver(ConfigurationDuration.class, (context) -> new ConfigurationDuration(TimeUtils.parseDuration(context.getString("1s")))) diff --git a/core/src/main/java/me/mastercapexd/auth/config/PluginConfigTemplate.java b/core/src/main/java/me/mastercapexd/auth/config/PluginConfigTemplate.java index 627a7b1e..7698073a 100644 --- a/core/src/main/java/me/mastercapexd/auth/config/PluginConfigTemplate.java +++ b/core/src/main/java/me/mastercapexd/auth/config/PluginConfigTemplate.java @@ -38,6 +38,8 @@ public abstract class PluginConfigTemplate implements PluginConfig { protected final AuthPlugin plugin; private final List allowedPatternCommands; protected ConfigurationSectionHolder configurationRoot; + @ConfigField("auto-migrate-config") + private boolean autoMigrateConfig; @ConfigField("id-type") private IdentifierType activeIdentifierType = IdentifierType.NAME; @ConfigField("check-name-case") @@ -144,6 +146,11 @@ public boolean isNameCaseCheckEnabled() { return nameCaseCheckEnabled; } + @Override + public boolean isAutoMigrateConfigEnabled() { + return autoMigrateConfig; + } + @Override public boolean isPasswordConfirmationEnabled() { return passwordConfirmationEnabled; diff --git a/core/src/main/java/me/mastercapexd/auth/server/commands/AuthCommand.java b/core/src/main/java/me/mastercapexd/auth/server/commands/AuthCommand.java index 8a6e44cb..e3a1fc92 100644 --- a/core/src/main/java/me/mastercapexd/auth/server/commands/AuthCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/server/commands/AuthCommand.java @@ -1,6 +1,13 @@ package me.mastercapexd.auth.server.commands; +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; + import com.bivashy.auth.api.AuthPlugin; +import com.bivashy.auth.api.asset.resource.Resource; +import com.bivashy.auth.api.asset.resource.impl.FolderResource; +import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader; import com.bivashy.auth.api.config.PluginConfig; import com.bivashy.auth.api.config.message.MessageContext; import com.bivashy.auth.api.crypto.HashInput; @@ -17,6 +24,7 @@ import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.annotation.Subcommand; +import ru.vyarus.yaml.updater.YamlUpdater; @Command({"authadmin", "adminauth", "auth"}) @Permission("auth.admin") @@ -86,4 +94,17 @@ public void reload(ServerCommandActor actor) { plugin.getConfig().reload(); actor.reply(config.getServerMessages().getMessage("auth-reloaded")); } + + @Subcommand("migrateconfig") + public void migrateConfig(ServerCommandActor actor) throws IOException, URISyntaxException { + FolderResource folderResource = new FolderResourceReader(plugin.getClass().getClassLoader(), "configurations").read(); + for (Resource resource : folderResource.getResources()) { + String realConfigurationName = resource.getName().substring(folderResource.getName().length() + 1); + File resourceConfiguration = new File(plugin.getFolder(), realConfigurationName); + if (!resourceConfiguration.exists()) + continue; + YamlUpdater.create(resourceConfiguration, resource.getStream()).backup(true).update(); + } + actor.reply(config.getServerMessages().getMessage("config-migrated")); + } } \ No newline at end of file diff --git a/core/src/main/resources/configurations/config.yml b/core/src/main/resources/configurations/config.yml index eadde399..8e0e606f 100644 --- a/core/src/main/resources/configurations/config.yml +++ b/core/src/main/resources/configurations/config.yml @@ -1,3 +1,6 @@ +# Автоматическая миграция конфигурации. Плагин мигрриует все конфигурации при включении. +# При миграции плагин создает бекап. +auto-migrate-config: false # Как должен хранить данные плагин # Возможные варианты: MYSQL, SQLITE, POSTGRESQL, MARIADB storage-type: SQLITE @@ -343,6 +346,7 @@ messages: auth-change-success: '&aУспешно изменен пароль игрока' auth-delete-success: '&aАккаунт успешно &cудалён' auth-reloaded: '&aПлагин успешно перезагрузил конфигурацию' + config-migrated: '&aКонфигурация успешно мигрирована' # Ошибки в командах player-offline: '&cИгрок с ником %account_name% оффлайн!'