[FEATURE] Новое API для удобной работы с конфигурациями

This commit is contained in:
Gravit 2019-09-20 04:55:57 +07:00
parent 05b214c11a
commit d5692bd575
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 143 additions and 65 deletions

View file

@ -102,9 +102,4 @@ public void loadHDirStore() throws IOException {
public void saveHDirStore() throws IOException { public void saveHDirStore() throws IOException {
saveHDirStore(DirBridge.dirProjectStore); saveHDirStore(DirBridge.dirProjectStore);
} }
@Override
public void setType(Type type) {
super.setType(type);
}
} }

View file

@ -11,19 +11,9 @@
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
public abstract class JsonConfigurable<T> { public abstract class JsonConfigurable<T> implements JsonConfigurableInterface<T> {
private Type type; private transient final Type type;
protected Path configPath; protected transient final Path configPath;
@LauncherAPI
public void saveConfig() throws IOException {
saveConfig(configPath);
}
@LauncherAPI
public void loadConfig() throws IOException {
loadConfig(configPath);
}
@LauncherAPI @LauncherAPI
public JsonConfigurable(Type type, Path configPath) { public JsonConfigurable(Type type, Path configPath) {
@ -31,55 +21,14 @@ public JsonConfigurable(Type type, Path configPath) {
this.configPath = configPath; this.configPath = configPath;
} }
@LauncherAPI @Override
public void saveConfig(Path configPath) throws IOException { public Path getPath() {
try (BufferedWriter writer = IOHelper.newWriter(configPath)) { return configPath;
Launcher.gsonManager.configGson.toJson(getConfig(), type, writer);
}
} }
@LauncherAPI @Override
public void loadConfig(Path configPath) throws IOException { public Type getType() {
if (generateConfigIfNotExists(configPath)) return; return type;
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;
} }
@LauncherAPI @LauncherAPI

View file

@ -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<T> {
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();
}

View file

@ -0,0 +1,32 @@
package pro.gravit.launcher.config;
import java.nio.file.Path;
public class SimpleConfigurable<T> extends JsonConfigurable<T> {
public T config;
private final Class<T> tClass;
public SimpleConfigurable(Class<T> 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;
}
}

View file

@ -4,6 +4,7 @@
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import pro.gravit.launcher.config.SimpleConfigurable;
import pro.gravit.launcher.modules.ModulesConfigManager; import pro.gravit.launcher.modules.ModulesConfigManager;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
@ -34,4 +35,9 @@ public Path getModuleConfigDir(String moduleName) {
} }
return configDir.resolve(moduleName); return configDir.resolve(moduleName);
} }
@Override
public <T> SimpleConfigurable<T> getConfigurable(Class<T> tClass, Path configPath) {
return new SimpleConfigurable<>(tClass, configPath);
}
} }

View file

@ -1,5 +1,7 @@
package pro.gravit.launcher.modules; package pro.gravit.launcher.modules;
import pro.gravit.launcher.config.SimpleConfigurable;
import java.nio.file.Path; import java.nio.file.Path;
public interface ModulesConfigManager { public interface ModulesConfigManager {
@ -8,4 +10,16 @@ public interface ModulesConfigManager {
Path getModuleConfig(String moduleName, String configName); Path getModuleConfig(String moduleName, String configName);
Path getModuleConfigDir(String moduleName); Path getModuleConfigDir(String moduleName);
<T> SimpleConfigurable<T> getConfigurable(Class<T> tClass, Path configPath);
default <T> SimpleConfigurable<T> getConfigurable(Class<T> tClass, String moduleName)
{
return getConfigurable(tClass, getModuleConfig(moduleName));
}
default <T> SimpleConfigurable<T> getConfigurable(Class<T> tClass, String moduleName, String configName)
{
return getConfigurable(tClass, getModuleConfig(moduleName, configName));
}
} }