/
ConfigUpdater.java
81 lines (75 loc) · 3.32 KB
/
ConfigUpdater.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package net.aufdemrand.denizencore.utilities.text;
import com.freneticllc.freneticutilities.freneticdatasyntax.FDSData;
import com.freneticllc.freneticutilities.freneticdatasyntax.FDSSection;
import java.util.ArrayList;
import java.util.Arrays;
public class ConfigUpdater {
/**
* Adds missing entries to a config file, based on a modern config file.
* Returns the patched config if patches were made, otherwise returns null.
*/
public static String updateConfig(String configFile, String correctConfig) {
FDSSection fileSection = new FDSSection(configFile);
FDSSection correctSection = new FDSSection(correctConfig);
if (transferKeys(fileSection, correctSection)) {
int changes = fileSection.getInt("times_changed", 0);
changes++;
FDSData data = new FDSData(changes, new ArrayList<>(Arrays.asList(
"----------- Important! -----------",
"Your configuration file was outdated.",
"It has been automatically updated.",
"You may notice some duplicated comments or other minor inconsistencies,",
"if these annoy you, you may remove the duplicate comments OR",
"delete the config file to allow it to regenerate.",
"",
"The below value is how many times automatic updates have been applied",
"since you last cleaned this file.",
"----------- End Important! -----------"
)));
fileSection.setRootData("times_changed", data);
return fileSection.savetoString();
}
return null;
}
public static boolean fixComments(FDSData user, FDSData correct) {
boolean changed = false;
for (String comment : correct.precedingComments) {
if (!user.precedingComments.contains(comment)) {
user.precedingComments.add(comment);
changed = true;
}
}
return changed;
}
public static boolean transferKeys(FDSSection fileSection, FDSSection correctSection) {
boolean changed = false;
for (String rootKey : correctSection.getRootKeys()) {
FDSData data = correctSection.getRootData(rootKey);
if (!fileSection.hasKey(rootKey)) {
fileSection.setRootData(rootKey, data);
changed = true;
}
else if (data.internal instanceof FDSSection) {
FDSData dataTwo = fileSection.getRootData(rootKey);
if (dataTwo.internal instanceof FDSSection) {
if (transferKeys((FDSSection) dataTwo.internal, (FDSSection) data.internal)) {
changed = true;
}
if (fixComments(dataTwo, data)) {
changed = true;
}
}
else {
fileSection.setRootData(rootKey, data);
changed = true;
}
}
else {
if (fixComments(fileSection.getRootData(rootKey), data)) {
changed = true;
}
}
}
return changed;
}
}