seppuku/src/main/java/me/rigamortis/seppuku/impl/management/ConfigManager.java

185 lines
5.8 KiB
Java
Raw Normal View History

2019-10-27 15:45:44 +00:00
package me.rigamortis.seppuku.impl.management;
2019-12-02 01:58:25 +00:00
import me.rigamortis.seppuku.Seppuku;
2019-10-27 15:45:44 +00:00
import me.rigamortis.seppuku.api.config.Configurable;
import me.rigamortis.seppuku.api.event.client.EventLoadConfig;
import me.rigamortis.seppuku.api.event.client.EventSaveConfig;
2019-10-27 15:45:44 +00:00
import me.rigamortis.seppuku.impl.config.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
2019-10-27 15:45:44 +00:00
import java.util.ArrayList;
import java.util.List;
/**
2019-12-02 01:58:25 +00:00
* @author noil
2019-10-27 15:45:44 +00:00
*/
public final class ConfigManager {
public static final String CONFIG_PATH = "Seppuku/Config/%s/";
public String activeConfig;
2019-12-02 01:58:25 +00:00
private File configDir;
private File moduleConfigDir;
private File hudComponentConfigDir;
private boolean firstLaunch = false;
private boolean customMainMenuHidden = false;
2019-12-02 01:58:25 +00:00
private List<Configurable> configurableList = new ArrayList<>();
public ConfigManager() {
this.activeConfig = readActiveConfig();
2019-12-02 01:58:25 +00:00
this.generateDirectories();
}
2019-10-27 15:45:44 +00:00
public void switchToConfig(final String config) {
this.saveAll();
this.activeConfig = config;
this.writeActiveConfig(config);
Seppuku.INSTANCE.unloadSimple();
Seppuku.INSTANCE.init();
}
public String readActiveConfig() {
try {
final byte[] bytes = Files.readAllBytes(Paths.get("Seppuku/Config/active.txt"));
return new String(bytes, StandardCharsets.UTF_8);
} catch (IOException e) {
return "Default";
}
}
public void writeActiveConfig(final String config) {
try {
final FileOutputStream fos = new FileOutputStream("Seppuku/Config/active.txt");
fos.write(config.getBytes());
fos.close();
} catch (IOException e) {
System.err.println("Could not create file active.txt in config directory.");
}
}
2019-12-02 01:58:25 +00:00
private void generateDirectories() {
this.configDir = new File(String.format(CONFIG_PATH, activeConfig));
2019-12-02 01:58:25 +00:00
if (!this.configDir.exists()) {
this.setFirstLaunch(true);
2019-12-02 01:58:25 +00:00
this.configDir.mkdirs();
2019-10-27 15:45:44 +00:00
}
this.moduleConfigDir = new File(String.format(CONFIG_PATH, activeConfig) + "Modules" + "/");
2019-12-02 01:58:25 +00:00
if (!this.moduleConfigDir.exists()) {
this.moduleConfigDir.mkdirs();
}
this.hudComponentConfigDir = new File(String.format(CONFIG_PATH, activeConfig) + "HudComponents" + "/");
2019-12-02 01:58:25 +00:00
if (!this.hudComponentConfigDir.exists()) {
this.hudComponentConfigDir.mkdirs();
}
}
public void init() {
Seppuku.INSTANCE.getModuleManager().getModuleList().forEach(module -> {
this.configurableList.add(new ModuleConfig(this.moduleConfigDir, module));
});
Seppuku.INSTANCE.getHudManager().getComponentList().forEach(hudComponent -> {
this.configurableList.add(new HudConfig(this.hudComponentConfigDir, hudComponent));
2019-12-02 01:58:25 +00:00
});
this.configurableList.add(new ClientConfig(configDir));
2019-12-02 01:58:25 +00:00
this.configurableList.add(new FriendConfig(configDir));
2021-02-08 20:57:33 +00:00
this.configurableList.add(new XrayConfig(configDir));
this.configurableList.add(new SearchConfig(configDir));
2019-12-02 01:58:25 +00:00
this.configurableList.add(new MacroConfig(configDir));
this.configurableList.add(new WaypointsConfig(configDir));
this.configurableList.add(new WorldConfig(configDir));
this.configurableList.add(new IgnoreConfig(configDir));
this.configurableList.add(new AutoIgnoreConfig(configDir));
2021-01-16 07:38:53 +00:00
this.configurableList.add(new AltConfig(configDir));
this.configurableList.add(new NukerFilterConfig(configDir));
2019-10-27 15:45:44 +00:00
if (this.firstLaunch) {
this.saveAll();
} else {
this.loadAll();
}
}
public void save(Class configurableClassType) {
for (Configurable cfg : configurableList) {
if (cfg.getClass().isAssignableFrom(configurableClassType)) {
cfg.onSave();
}
}
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventSaveConfig());
}
2019-10-27 15:45:44 +00:00
public void saveAll() {
for (Configurable cfg : configurableList) {
cfg.onSave();
}
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventSaveConfig());
2019-10-27 15:45:44 +00:00
}
public void load(Class configurableClassType) {
for (Configurable cfg : configurableList) {
if (cfg.getClass().isAssignableFrom(configurableClassType)) {
2023-05-16 05:50:39 +00:00
cfg.onLoad(null);
}
}
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventLoadConfig());
}
2019-10-27 15:45:44 +00:00
public void loadAll() {
for (Configurable cfg : configurableList) {
2023-05-16 05:50:39 +00:00
cfg.onLoad(null);
}
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventLoadConfig());
2019-10-27 15:45:44 +00:00
}
2019-12-02 01:58:25 +00:00
public File getConfigDir() {
return configDir;
}
public File getModuleConfigDir() {
return moduleConfigDir;
}
public File getHudComponentConfigDir() {
return hudComponentConfigDir;
}
2019-10-27 15:45:44 +00:00
public boolean isFirstLaunch() {
return firstLaunch;
}
public void setFirstLaunch(boolean firstLaunch) {
this.firstLaunch = firstLaunch;
}
public boolean isCustomMainMenuHidden() {
return this.customMainMenuHidden;
}
public void setCustomMainMenuHidden(boolean hidden) {
this.customMainMenuHidden = hidden;
}
2019-12-02 01:58:25 +00:00
public void addConfigurable(Configurable configurable) {
this.configurableList.add(configurable);
}
2019-10-27 15:45:44 +00:00
public List<Configurable> getConfigurableList() {
return configurableList;
}
public void setConfigurableList(List<Configurable> configurableList) {
this.configurableList = configurableList;
}
}