AutoUpdateConfigLoader.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.gmail.nossr50.config;
  2. import org.bukkit.configuration.file.FileConfiguration;
  3. import org.bukkit.configuration.file.YamlConfiguration;
  4. import org.jetbrains.annotations.NotNull;
  5. import java.io.*;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.LinkedHashMap;
  9. import java.util.Set;
  10. public abstract class AutoUpdateConfigLoader extends ConfigLoader {
  11. public AutoUpdateConfigLoader(String relativePath, String fileName) {
  12. super(relativePath, fileName);
  13. }
  14. public AutoUpdateConfigLoader(String fileName) {
  15. super(fileName);
  16. }
  17. protected void saveConfig() {
  18. try {
  19. plugin.getLogger().info("Saving changes to config file - "+fileName);
  20. config.save(configFile);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. protected @NotNull FileConfiguration getInternalConfig() {
  26. return YamlConfiguration.loadConfiguration(plugin.getResourceAsReader(fileName));
  27. }
  28. @Override
  29. protected void loadFile() {
  30. super.loadFile();
  31. FileConfiguration internalConfig = YamlConfiguration.loadConfiguration(plugin.getResourceAsReader(fileName));
  32. Set<String> configKeys = config.getKeys(true);
  33. Set<String> internalConfigKeys = internalConfig.getKeys(true);
  34. boolean needSave = false;
  35. Set<String> oldKeys = new HashSet<>(configKeys);
  36. oldKeys.removeAll(internalConfigKeys);
  37. Set<String> newKeys = new HashSet<>(internalConfigKeys);
  38. newKeys.removeAll(configKeys);
  39. // Don't need a re-save if we have old keys sticking around?
  40. // Would be less saving, but less... correct?
  41. if (!newKeys.isEmpty() || !oldKeys.isEmpty()) {
  42. needSave = true;
  43. }
  44. for (String key : oldKeys) {
  45. plugin.debug("Detected potentially unused key: " + key);
  46. //config.set(key, null);
  47. }
  48. for (String key : newKeys) {
  49. plugin.debug("Adding new key: " + key + " = " + internalConfig.get(key));
  50. config.set(key, internalConfig.get(key));
  51. }
  52. if (needSave) {
  53. // Get Bukkit's version of an acceptable config with new keys, and no old keys
  54. String output = config.saveToString();
  55. // Convert to the superior 4 space indentation
  56. output = output.replace(" ", " ");
  57. // Rip out Bukkit's attempt to save comments at the top of the file
  58. while (output.replaceAll("[//s]", "").startsWith("#")) {
  59. output = output.substring(output.indexOf('\n', output.indexOf('#')) + 1);
  60. }
  61. // Read the internal config to get comments, then put them in the new one
  62. try {
  63. // Read internal
  64. BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName)));
  65. LinkedHashMap<String, String> comments = new LinkedHashMap<>();
  66. StringBuilder temp = new StringBuilder();
  67. String line;
  68. while ((line = reader.readLine()) != null) {
  69. if (line.contains("#")) {
  70. temp.append(line).append("\n");
  71. }
  72. else if (line.contains(":")) {
  73. line = line.substring(0, line.indexOf(":") + 1);
  74. if (temp.length() > 0) {
  75. if(comments.containsKey(line)) {
  76. int index = 0;
  77. while(comments.containsKey(line + index)) {
  78. index++;
  79. }
  80. line = line + index;
  81. }
  82. comments.put(line, temp.toString());
  83. temp = new StringBuilder();
  84. }
  85. }
  86. }
  87. // Dump to the new one
  88. HashMap<String, Integer> indexed = new HashMap<>();
  89. for (String key : comments.keySet()) {
  90. String actualkey = key.substring(0, key.indexOf(":") + 1);
  91. int index = 0;
  92. if(indexed.containsKey(actualkey)) {
  93. index = indexed.get(actualkey);
  94. }
  95. boolean isAtTop = !output.contains("\n" + actualkey);
  96. index = output.indexOf((isAtTop ? "" : "\n") + actualkey, index);
  97. if (index >= 0) {
  98. output = output.substring(0, index) + "\n" + comments.get(key) + output.substring(isAtTop ? index : index + 1);
  99. indexed.put(actualkey, index + comments.get(key).length() + actualkey.length() + 1);
  100. }
  101. }
  102. }
  103. catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. // Save it
  107. try {
  108. String saveName = fileName;
  109. // At this stage we cannot guarantee that Config has been loaded, so we do the check directly here
  110. if (!plugin.getConfig().getBoolean("General.Config_Update_Overwrite", true)) {
  111. saveName += ".new";
  112. }
  113. BufferedWriter writer = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), saveName)));
  114. writer.write(output);
  115. writer.flush();
  116. writer.close();
  117. }
  118. catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. }
  123. }