1
0
Fork 0
fgdata/Nasal/modules/repair_config.nas
Henning Stahlke c6e5244b13 update defaults.xml
add repair_config.nas to fix old userarchive flags
2020-02-15 14:39:36 +01:00

82 lines
2.9 KiB
Text

#-------------------------------------------------------------------------------
# repair_config.mod
# This code repairs the userarchive flags for core nasal modules listed in
# defaults.xml, they may be wrongly set in the users home directory by earlier
# versions of FlightGear
#-------------------------------------------------------------------------------
var defaults_path = getprop("/sim/fg-root")~"/defaults.xml";
var printNodeInfo = func(n) {
print(sprintf("%-20s enabled=%s, userarchive=%s",
n.getName(),
n.getNode("enabled").getValue(),
n.getNode("enabled").getAttribute("userarchive")
));
}
var printConfig = func(name, cfg) {
print("== "~name~" ==");
foreach (var mod; cfg.getChildren()) {
if (mod.getChild("enabled") != nil)
printNodeInfo(mod);
}
}
var loadDefaultsNasal = func {
var defaults = io.readxml(defaults_path);
if (defaults != nil) {
defaults = defaults.getNode("PropertyList/nasal");
foreach (var mod; defaults.getChildren()) {
var enabled = mod.getNode("enabled");
if (string.lc(enabled.getValue()) == "true") enabled.setIntValue(1);
else enabled.setIntValue(0);
enabled.setAttribute("userarchive",
(mod.getNode("enabled/___userarchive").getValue() == "y")
);
}
}
return defaults;
}
var compareConfigs = func(ref_cfg, check_cfg) {
var mismatch = 0;
# printConfig("default", mod_default_cfg);
# printConfig("actual", mod_runtime_cfg);
foreach (var mod; ref_cfg.getChildren()) {
var name = mod.getName();
var ref_ena = mod.getChild("enabled");
var ref_ua = ref_ena.getAttribute("userarchive");
var check_mod = check_cfg.getChild(name);
if (check_mod != nil) {
var check_ena = check_mod.getChild("enabled");
var check_ua = check_ena.getAttribute("userarchive");
# if (ref_ena.getValue() != check_ena.getValue())
# printf("%-20s enable flag mismatch", name);
if (ref_ua != check_ua) {
printf("%-20s userarchive mismatch", name);
mismatch = 1;
}
}
}
return mismatch;
}
var repairUserArchiveFlag = func(ref_cfg, check_cfg) {
foreach (var mod; ref_cfg.getChildren()) {
var name = mod.getName();
var ref_ua = mod.getChild("enabled").getAttribute("userarchive");
var check_mod = check_cfg.getChild(name);
if (check_mod != nil) {
check_mod.getChild("enabled").setAttribute("userarchive", ref_ua);
check_mod.setAttribute("userarchive", ref_ua);
}
}
}
var mod_default_cfg = loadDefaultsNasal();
var mod_runtime_cfg = props.getNode("/nasal");
if (compareConfigs(mod_default_cfg, mod_runtime_cfg)) {
print("-- Resetting userarchive flags to defaults --");
repairUserArchiveFlag(mod_default_cfg, mod_runtime_cfg);
}