2019-06-02 05:03:08 +03:00
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
2019-04-13 18:31: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
|
|
|
|
2019-04-13 18:31: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
|
|
|
|
2019-04-13 18:31: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
|
|
|
|
2019-04-13 18:31: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)) {
|
2019-05-31 01:42:51 +03:00
|
|
|
Launcher.gsonManager.configGson.toJson(getConfig(), type, writer);
|
2019-04-03 13:09:53 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-04-13 18:31: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)) {
|
2019-05-31 01:42:51 +03:00
|
|
|
setConfig(Launcher.gsonManager.configGson.fromJson(reader, type));
|
2019-04-03 13:09:53 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-04-13 18:31: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
|
|
|
|
2019-04-13 18:31: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
|
|
|
|
2019-04-13 18:31:22 +03:00
|
|
|
@LauncherAPI
|
2019-04-03 16:27:40 +03:00
|
|
|
public boolean generateConfigIfNotExists(Path path) throws IOException {
|
|
|
|
if (IOHelper.isFile(path))
|
2019-04-03 13:30:58 +03:00
|
|
|
return false;
|
|
|
|
resetConfig(path);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-04-13 18:31:22 +03:00
|
|
|
@LauncherAPI
|
2019-04-03 16:27:40 +03:00
|
|
|
public boolean generateConfigIfNotExists() throws IOException {
|
|
|
|
if (IOHelper.isFile(configPath))
|
2019-04-03 13:30:58 +03:00
|
|
|
return false;
|
|
|
|
resetConfig();
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
|
|
|
protected void setType(Type type) {
|
2019-04-13 18:31:22 +03:00
|
|
|
this.type = type;
|
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-04-13 18:31:22 +03:00
|
|
|
@LauncherAPI
|
2019-04-03 13:09:53 +03:00
|
|
|
public abstract T getConfig();
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-04-13 18:31:22 +03:00
|
|
|
@LauncherAPI
|
2019-04-03 13:09:53 +03:00
|
|
|
public abstract T getDefaultConfig();
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-04-13 18:31:22 +03:00
|
|
|
@LauncherAPI
|
2019-04-03 13:09:53 +03:00
|
|
|
public abstract void setConfig(T config);
|
|
|
|
}
|