2022-11-22 00:28:33 +01:00
|
|
|
package wtf.beatrice.hidekobot.datasource;
|
2022-11-21 20:19:35 +01:00
|
|
|
|
2022-11-21 23:28:33 +01:00
|
|
|
import org.yaml.snakeyaml.DumperOptions;
|
2022-11-21 20:19:35 +01:00
|
|
|
import org.yaml.snakeyaml.Yaml;
|
2022-11-21 23:28:33 +01:00
|
|
|
import wtf.beatrice.hidekobot.HidekoBot;
|
2022-11-22 00:31:52 +01:00
|
|
|
import wtf.beatrice.hidekobot.util.Logger;
|
2022-11-21 20:19:35 +01:00
|
|
|
|
2022-11-21 23:28:33 +01:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.LinkedHashMap;
|
2022-11-21 20:19:35 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
public class ConfigurationSource
|
2022-11-21 20:19:35 +01:00
|
|
|
{
|
2022-11-21 23:28:33 +01:00
|
|
|
|
2022-11-21 23:36:42 +01:00
|
|
|
|
|
|
|
private final LinkedHashMap<String, Object> configurationEntries = new LinkedHashMap<>();
|
2022-11-21 23:28:33 +01:00
|
|
|
private final Logger logger;
|
2022-11-21 20:19:35 +01:00
|
|
|
private final String configFilePath;
|
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
public ConfigurationSource(String configFilePath)
|
2022-11-21 20:19:35 +01:00
|
|
|
{
|
|
|
|
this.configFilePath = configFilePath;
|
2022-11-21 23:28:33 +01:00
|
|
|
logger = new Logger(getClass());
|
2022-11-21 20:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void initConfig()
|
|
|
|
{
|
2022-11-21 23:28:33 +01:00
|
|
|
// load the YAML file from the archive's resources folder
|
2022-11-22 23:40:44 +01:00
|
|
|
/*
|
|
|
|
* note: this is no longer technically a YAML file, but we are using a very similar structure
|
|
|
|
* to what SnakeYaml does, so that it can map all entries directly to a YAML file itself.
|
|
|
|
* we used to have a config.yml file in the "resources" folder, but that is no longer necessary.
|
|
|
|
*/
|
|
|
|
LinkedHashMap<String, Object> internalConfigContents = new LinkedHashMap<>(); // map holding all file entries
|
|
|
|
for(ConfigurationEntry entry : ConfigurationEntry.values())
|
|
|
|
{
|
|
|
|
internalConfigContents.put(entry.getPath(), entry.getDefaultValue());
|
2022-11-22 23:28:59 +01:00
|
|
|
}
|
2022-11-21 23:28:33 +01:00
|
|
|
|
2022-11-22 23:40:44 +01:00
|
|
|
if(internalConfigContents.isEmpty())
|
2022-11-21 20:19:35 +01:00
|
|
|
{
|
2022-11-21 23:28:33 +01:00
|
|
|
logger.log("Error reading internal configuration!");
|
|
|
|
HidekoBot.shutdown();
|
|
|
|
return;
|
2022-11-21 20:19:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:28:33 +01:00
|
|
|
// check if config files exists in filesystem
|
|
|
|
File fsConfigFile = new File(configFilePath);
|
|
|
|
if(!fsConfigFile.exists())
|
2022-11-21 20:19:35 +01:00
|
|
|
{
|
2022-11-21 23:28:33 +01:00
|
|
|
// try to create config file
|
|
|
|
try { fsConfigFile.createNewFile(); }
|
|
|
|
catch (IOException e) {
|
|
|
|
logger.log("Error creating configuration file!");
|
|
|
|
logger.log(e.getMessage());
|
|
|
|
HidekoBot.shutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// load the YAML file from the filesystem
|
|
|
|
Yaml fsConfigYaml = new Yaml();
|
|
|
|
LinkedHashMap<String, Object> fsConfigContents = null; // map holding all file entries
|
|
|
|
try (InputStream fsConfigStream = new FileInputStream(fsConfigFile))
|
|
|
|
{ fsConfigContents = fsConfigYaml.load(fsConfigStream); }
|
|
|
|
catch (IOException e) { logger.log(e.getMessage()); }
|
2022-11-21 20:19:35 +01:00
|
|
|
|
2022-11-21 23:28:33 +01:00
|
|
|
|
|
|
|
if(fsConfigContents == null) // if file contents are empty or corrupted...
|
|
|
|
{
|
|
|
|
// "clean" them (this effectively forces a config file reset)
|
|
|
|
fsConfigContents = new LinkedHashMap<>();
|
2022-11-21 20:19:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:28:33 +01:00
|
|
|
// check for missing keys
|
|
|
|
boolean missingKeys = false;
|
|
|
|
for(String key : internalConfigContents.keySet())
|
|
|
|
{
|
|
|
|
// if key is missing
|
|
|
|
if(!fsConfigContents.containsKey(key))
|
|
|
|
{
|
|
|
|
// quit and flag it, as we need to complete the file with the missing ones
|
|
|
|
missingKeys = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if keys are missing
|
|
|
|
if(missingKeys)
|
|
|
|
{
|
|
|
|
// create a new mixed map that will take existing values from the non-missing keys
|
|
|
|
// and fill everything else with the default values
|
|
|
|
LinkedHashMap<String, Object> filledEntries = new LinkedHashMap<>();
|
|
|
|
for(String key : internalConfigContents.keySet())
|
|
|
|
{
|
|
|
|
if(fsConfigContents.containsKey(key))
|
|
|
|
{
|
|
|
|
// if the key already exists, copy the original value
|
|
|
|
filledEntries.put(key, fsConfigContents.get(key));
|
|
|
|
} else {
|
|
|
|
// else, copy the value from the example config file
|
|
|
|
filledEntries.put(key, internalConfigContents.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// new writer to actually write the contents to the file
|
|
|
|
PrintWriter missingKeysWriter = new PrintWriter(fsConfigFile);
|
|
|
|
|
|
|
|
// set yaml options to make the output prettier
|
|
|
|
DumperOptions dumperOptions = new DumperOptions();
|
|
|
|
dumperOptions.setIndent(2);
|
|
|
|
dumperOptions.setPrettyFlow(true);
|
|
|
|
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
|
|
|
|
|
|
// create the yaml object and dump the values to filesystem
|
|
|
|
Yaml yaml = new Yaml(dumperOptions);
|
|
|
|
yaml.dump(filledEntries, missingKeysWriter);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
logger.log(e.getMessage());
|
|
|
|
HidekoBot.shutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 23:36:42 +01:00
|
|
|
// finally, dump all entries to cache.
|
|
|
|
loadConfig(filledEntries);
|
|
|
|
} else {
|
|
|
|
// if no key is missing, just cache all entries and values from filesystem.
|
|
|
|
loadConfig(fsConfigContents);
|
2022-11-21 23:28:33 +01:00
|
|
|
}
|
2022-11-21 23:36:42 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 20:19:35 +01:00
|
|
|
|
2022-11-21 23:36:42 +01:00
|
|
|
private void loadConfig(LinkedHashMap<String, Object> configurationEntries)
|
|
|
|
{
|
2022-11-22 00:04:34 +01:00
|
|
|
this.configurationEntries.putAll(configurationEntries);
|
2022-11-21 23:36:42 +01:00
|
|
|
}
|
2022-11-22 00:04:34 +01:00
|
|
|
public Object getConfigValue(String key)
|
2022-11-21 23:36:42 +01:00
|
|
|
{
|
|
|
|
return configurationEntries.get(key);
|
2022-11-21 20:19:35 +01:00
|
|
|
}
|
|
|
|
}
|