Launcher/LauncherAPI/src/main/java/pro/gravit/launcher/config/JsonConfigurable.java

94 lines
2.4 KiB
Java
Raw Normal View History

package pro.gravit.launcher.config;
2019-04-03 13:09:53 +03:00
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Path;
2019-06-03 10:58:10 +03:00
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.LauncherAPI;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
2019-06-03 10:58:10 +03:00
2019-04-03 13:09:53 +03:00
public abstract class JsonConfigurable<T> {
private Type type;
protected Path configPath;
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public void saveConfig() throws IOException {
2019-04-03 13:09:53 +03:00
saveConfig(configPath);
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public void loadConfig() throws IOException {
2019-04-03 13:09:53 +03:00
loadConfig(configPath);
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 13:09:53 +03:00
public JsonConfigurable(Type type, Path configPath) {
this.type = type;
this.configPath = configPath;
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public void saveConfig(Path configPath) throws IOException {
try (BufferedWriter writer = IOHelper.newWriter(configPath)) {
Launcher.gsonManager.configGson.toJson(getConfig(), type, writer);
2019-04-03 13:09:53 +03:00
}
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public void loadConfig(Path configPath) throws IOException {
if (generateConfigIfNotExists(configPath)) return;
2019-04-03 13:09:53 +03:00
try (BufferedReader reader = IOHelper.newReader(configPath)) {
setConfig(Launcher.gsonManager.configGson.fromJson(reader, type));
} catch (Exception e)
{
LogHelper.error(e);
resetConfig(configPath);
2019-04-03 13:09:53 +03:00
}
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public void resetConfig() throws IOException {
2019-04-03 13:09:53 +03:00
setConfig(getDefaultConfig());
saveConfig();
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public void resetConfig(Path newPath) throws IOException {
2019-04-03 13:09:53 +03:00
setConfig(getDefaultConfig());
saveConfig(newPath);
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public boolean generateConfigIfNotExists(Path path) throws IOException {
if (IOHelper.isFile(path))
return false;
resetConfig(path);
return true;
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 16:27:40 +03:00
public boolean generateConfigIfNotExists() throws IOException {
if (IOHelper.isFile(configPath))
return false;
resetConfig();
return true;
}
2019-05-15 14:11:22 +03:00
protected void setType(Type type) {
this.type = type;
}
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 13:09:53 +03:00
public abstract T getConfig();
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 13:09:53 +03:00
public abstract T getDefaultConfig();
2019-05-15 14:11:22 +03:00
@LauncherAPI
2019-04-03 13:09:53 +03:00
public abstract void setConfig(T config);
}