2013-02-24 06:18:01 +01:00
|
|
|
package com.gmail.nossr50.config;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
|
|
|
import java.io.*;
|
2013-02-24 06:18:01 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
2019-01-15 07:11:58 +01:00
|
|
|
import java.util.LinkedHashMap;
|
2013-02-24 06:18:01 +01:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
|
|
|
public AutoUpdateConfigLoader(String relativePath, String fileName) {
|
|
|
|
super(relativePath, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AutoUpdateConfigLoader(String fileName) {
|
|
|
|
super(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void loadFile() {
|
|
|
|
super.loadFile();
|
2017-05-14 17:36:31 +02:00
|
|
|
FileConfiguration internalConfig = YamlConfiguration.loadConfiguration(plugin.getResourceAsReader(fileName));
|
2013-02-24 06:18:01 +01:00
|
|
|
|
|
|
|
Set<String> configKeys = config.getKeys(true);
|
|
|
|
Set<String> internalConfigKeys = internalConfig.getKeys(true);
|
|
|
|
|
|
|
|
boolean needSave = false;
|
|
|
|
|
|
|
|
Set<String> oldKeys = new HashSet<String>(configKeys);
|
|
|
|
oldKeys.removeAll(internalConfigKeys);
|
|
|
|
|
|
|
|
Set<String> newKeys = new HashSet<String>(internalConfigKeys);
|
|
|
|
newKeys.removeAll(configKeys);
|
|
|
|
|
|
|
|
// Don't need a re-save if we have old keys sticking around?
|
|
|
|
// Would be less saving, but less... correct?
|
|
|
|
if (!newKeys.isEmpty() || !oldKeys.isEmpty()) {
|
|
|
|
needSave = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String key : oldKeys) {
|
2017-04-15 06:24:39 +02:00
|
|
|
plugin.debug("Detected potentially unused key: " + key);
|
|
|
|
//config.set(key, null);
|
2013-02-24 06:18:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (String key : newKeys) {
|
|
|
|
plugin.debug("Adding new key: " + key + " = " + internalConfig.get(key));
|
|
|
|
config.set(key, internalConfig.get(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needSave) {
|
|
|
|
// Get Bukkit's version of an acceptable config with new keys, and no old keys
|
|
|
|
String output = config.saveToString();
|
|
|
|
|
|
|
|
// Convert to the superior 4 space indentation
|
|
|
|
output = output.replace(" ", " ");
|
|
|
|
|
|
|
|
// Rip out Bukkit's attempt to save comments at the top of the file
|
2013-10-02 10:42:06 +02:00
|
|
|
while (output.replaceAll("[//s]", "").startsWith("#")) {
|
2013-03-01 06:52:01 +01:00
|
|
|
output = output.substring(output.indexOf('\n', output.indexOf('#')) + 1);
|
2013-02-24 06:18:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the internal config to get comments, then put them in the new one
|
|
|
|
try {
|
|
|
|
// Read internal
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName)));
|
2016-03-12 04:10:41 +01:00
|
|
|
LinkedHashMap<String, String> comments = new LinkedHashMap<String, String>();
|
2013-02-24 06:18:01 +01:00
|
|
|
String temp = "";
|
|
|
|
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
2013-09-30 22:17:44 +02:00
|
|
|
if (line.contains("#")) {
|
2013-02-24 06:18:01 +01:00
|
|
|
temp += line + "\n";
|
|
|
|
}
|
|
|
|
else if (line.contains(":")) {
|
|
|
|
line = line.substring(0, line.indexOf(":") + 1);
|
2013-03-01 06:52:01 +01:00
|
|
|
if (!temp.isEmpty()) {
|
2016-03-12 04:10:41 +01:00
|
|
|
if(comments.containsKey(line)) {
|
|
|
|
int index = 0;
|
|
|
|
while(comments.containsKey(line + index)) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = line + index;
|
|
|
|
}
|
|
|
|
|
2013-02-24 06:18:01 +01:00
|
|
|
comments.put(line, temp);
|
|
|
|
temp = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump to the new one
|
2016-03-12 04:10:41 +01:00
|
|
|
HashMap<String, Integer> indexed = new HashMap<String, Integer>();
|
|
|
|
for (String key : comments.keySet()) {
|
|
|
|
String actualkey = key.substring(0, key.indexOf(":") + 1);
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
if(indexed.containsKey(actualkey)) {
|
|
|
|
index = indexed.get(actualkey);
|
|
|
|
}
|
|
|
|
boolean isAtTop = !output.contains("\n" + actualkey);
|
|
|
|
index = output.indexOf((isAtTop ? "" : "\n") + actualkey, index);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
output = output.substring(0, index) + "\n" + comments.get(key) + output.substring(isAtTop ? index : index + 1);
|
|
|
|
indexed.put(actualkey, index + comments.get(key).length() + actualkey.length() + 1);
|
2013-02-24 06:18:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it
|
|
|
|
try {
|
|
|
|
String saveName = fileName;
|
|
|
|
// At this stage we cannot guarantee that Config has been loaded, so we do the check directly here
|
|
|
|
if (!plugin.getConfig().getBoolean("General.Config_Update_Overwrite", true)) {
|
|
|
|
saveName += ".new";
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), saveName)));
|
|
|
|
writer.write(output);
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|