diff --git a/Launcher/src/main/java/pro/gravit/launcher/managers/SettingsManager.java b/Launcher/src/main/java/pro/gravit/launcher/managers/SettingsManager.java index 6df99215..5aaa9a55 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/managers/SettingsManager.java +++ b/Launcher/src/main/java/pro/gravit/launcher/managers/SettingsManager.java @@ -102,9 +102,4 @@ public void loadHDirStore() throws IOException { public void saveHDirStore() throws IOException { saveHDirStore(DirBridge.dirProjectStore); } - - @Override - public void setType(Type type) { - super.setType(type); - } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurable.java b/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurable.java index 6972e023..63258b6c 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurable.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurable.java @@ -11,19 +11,9 @@ import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; -public abstract class JsonConfigurable { - private Type type; - protected Path configPath; - - @LauncherAPI - public void saveConfig() throws IOException { - saveConfig(configPath); - } - - @LauncherAPI - public void loadConfig() throws IOException { - loadConfig(configPath); - } +public abstract class JsonConfigurable implements JsonConfigurableInterface { + private transient final Type type; + protected transient final Path configPath; @LauncherAPI public JsonConfigurable(Type type, Path configPath) { @@ -31,55 +21,14 @@ public JsonConfigurable(Type type, Path configPath) { this.configPath = configPath; } - @LauncherAPI - public void saveConfig(Path configPath) throws IOException { - try (BufferedWriter writer = IOHelper.newWriter(configPath)) { - Launcher.gsonManager.configGson.toJson(getConfig(), type, writer); - } + @Override + public Path getPath() { + return configPath; } - @LauncherAPI - public void loadConfig(Path configPath) throws IOException { - if (generateConfigIfNotExists(configPath)) return; - try (BufferedReader reader = IOHelper.newReader(configPath)) { - setConfig(Launcher.gsonManager.configGson.fromJson(reader, type)); - } catch (Exception e) - { - LogHelper.error(e); - resetConfig(configPath); - } - } - - @LauncherAPI - public void resetConfig() throws IOException { - setConfig(getDefaultConfig()); - saveConfig(); - } - - @LauncherAPI - public void resetConfig(Path newPath) throws IOException { - setConfig(getDefaultConfig()); - saveConfig(newPath); - } - - @LauncherAPI - public boolean generateConfigIfNotExists(Path path) throws IOException { - if (IOHelper.isFile(path)) - return false; - resetConfig(path); - return true; - } - - @LauncherAPI - public boolean generateConfigIfNotExists() throws IOException { - if (IOHelper.isFile(configPath)) - return false; - resetConfig(); - return true; - } - - protected void setType(Type type) { - this.type = type; + @Override + public Type getType() { + return type; } @LauncherAPI diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurableInterface.java b/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurableInterface.java new file mode 100644 index 00000000..dc02c2cc --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurableInterface.java @@ -0,0 +1,82 @@ +package pro.gravit.launcher.config; + +import com.google.gson.Gson; +import pro.gravit.launcher.Launcher; +import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.LogHelper; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.lang.reflect.Type; +import java.nio.file.Path; + +public interface JsonConfigurableInterface { + default void saveConfig() throws IOException { + saveConfig(getPath()); + } + + default void loadConfig() throws IOException { + loadConfig(getPath()); + } + + default void saveConfig(Gson gson, Path configPath) throws IOException { + try (BufferedWriter writer = IOHelper.newWriter(configPath)) { + gson.toJson(getConfig(), getType(), writer); + } + } + + default void loadConfig(Gson gson, Path configPath) throws IOException { + if (generateConfigIfNotExists(configPath)) return; + try (BufferedReader reader = IOHelper.newReader(configPath)) { + setConfig(gson.fromJson(reader, getType())); + } catch (Exception e) + { + LogHelper.error(e); + resetConfig(configPath); + } + } + + default void saveConfig(Path configPath) throws IOException { + saveConfig(Launcher.gsonManager.configGson, configPath); + } + + default void loadConfig(Path configPath) throws IOException { + loadConfig(Launcher.gsonManager.configGson, configPath); + } + + default void resetConfig() throws IOException { + setConfig(getDefaultConfig()); + saveConfig(); + } + + default void resetConfig(Path newPath) throws IOException { + setConfig(getDefaultConfig()); + saveConfig(newPath); + } + + default boolean generateConfigIfNotExists(Path path) throws IOException { + if (IOHelper.isFile(path)) + return false; + resetConfig(path); + return true; + } + + default boolean generateConfigIfNotExists() throws IOException { + if (IOHelper.isFile(getPath())) + return false; + resetConfig(); + return true; + } + + T getConfig(); + + T getDefaultConfig(); + + void setConfig(T config); + + Path getPath(); + + Type getType(); + +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/config/SimpleConfigurable.java b/LauncherAPI/src/main/java/pro/gravit/launcher/config/SimpleConfigurable.java new file mode 100644 index 00000000..5027e9eb --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/config/SimpleConfigurable.java @@ -0,0 +1,32 @@ +package pro.gravit.launcher.config; + +import java.nio.file.Path; + +public class SimpleConfigurable extends JsonConfigurable { + public T config; + private final Class tClass; + + public SimpleConfigurable(Class type, Path configPath) { + super(type, configPath); + tClass = type; + } + + @Override + public T getConfig() { + return config; + } + + @Override + public T getDefaultConfig() { + try { + return tClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + return null; + } + } + + @Override + public void setConfig(T config) { + this.config = config; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java index 54b1754c..a5379675 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModulesConfigManager.java @@ -4,6 +4,7 @@ import java.nio.file.Files; import java.nio.file.Path; +import pro.gravit.launcher.config.SimpleConfigurable; import pro.gravit.launcher.modules.ModulesConfigManager; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; @@ -34,4 +35,9 @@ public Path getModuleConfigDir(String moduleName) { } return configDir.resolve(moduleName); } + + @Override + public SimpleConfigurable getConfigurable(Class tClass, Path configPath) { + return new SimpleConfigurable<>(tClass, configPath); + } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java index 3d5522e5..1016ac14 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesConfigManager.java @@ -1,5 +1,7 @@ package pro.gravit.launcher.modules; +import pro.gravit.launcher.config.SimpleConfigurable; + import java.nio.file.Path; public interface ModulesConfigManager { @@ -8,4 +10,16 @@ public interface ModulesConfigManager { Path getModuleConfig(String moduleName, String configName); Path getModuleConfigDir(String moduleName); + + SimpleConfigurable getConfigurable(Class tClass, Path configPath); + + default SimpleConfigurable getConfigurable(Class tClass, String moduleName) + { + return getConfigurable(tClass, getModuleConfig(moduleName)); + } + + default SimpleConfigurable getConfigurable(Class tClass, String moduleName, String configName) + { + return getConfigurable(tClass, getModuleConfig(moduleName, configName)); + } }