Addon-config: don’t overwrite autosaved props
Fix for: https://sourceforge.net/p/flightgear/codetickets/2059/ When copying addon-config into the main property tree, skip props which already exist and which are marked ARCHIVE, on the assumption they came from autosave.xml.
This commit is contained in:
parent
134685b527
commit
9d2e50fa56
3 changed files with 52 additions and 2 deletions
|
@ -96,13 +96,33 @@ AddonManager::loadConfigFileIfExists(const SGPath& configFile)
|
|||
return;
|
||||
}
|
||||
|
||||
SGPropertyNode_ptr configProps(new SGPropertyNode);
|
||||
try {
|
||||
readProperties(configFile, globals->get_props());
|
||||
readProperties(configFile, configProps);
|
||||
} catch (const sg_exception &e) {
|
||||
throw errors::error_loading_config_file(
|
||||
"unable to load add-on config file '" + configFile.utf8Str() + "': " +
|
||||
e.getFormattedMessage());
|
||||
}
|
||||
|
||||
// bug https://sourceforge.net/p/flightgear/codetickets/2059/
|
||||
// since we're loading this after autosave.xml is loaded, the defaults
|
||||
// always take precedence. To fix this, only copy a value from the
|
||||
// addon-config if it's not makred as ARCHIVE. (We assume ARCHIVE props
|
||||
// came from autosave.xml)
|
||||
copyPropertiesIf(configProps, globals->get_props(), [](const SGPropertyNode* src) {
|
||||
if (src->nChildren() > 0)
|
||||
return true;
|
||||
|
||||
// find the correspnding destination node
|
||||
auto dstNode = globals->get_props()->getNode(src->getPath());
|
||||
if (!dstNode)
|
||||
return true; // easy, just copy it
|
||||
|
||||
// copy if it's NOT marked archive. In other words, we can replace
|
||||
// values from defaults, but not autosave
|
||||
return dstNode->getAttribute(SGPropertyNode::ARCHIVE) == false;
|
||||
});
|
||||
|
||||
SG_LOG(SG_GENERAL, SG_INFO,
|
||||
"Loaded add-on config file: '" << configFile.utf8Str() + "'");
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include "test_props.hxx"
|
||||
|
||||
#include <simgear/props/props_io.hxx>
|
||||
|
||||
// Set up function for each test.
|
||||
void SimgearPropsTests::setUp()
|
||||
|
@ -50,3 +51,31 @@ void SimgearPropsTests::testAliasLeak()
|
|||
alias = tree->getNode("test-alias", true);
|
||||
alias->alias("test-node");
|
||||
}
|
||||
|
||||
void SimgearPropsTests::testPropsCopyIf()
|
||||
{
|
||||
// dummy property tree structure
|
||||
tree->setIntValue("a/a/a", 42);
|
||||
tree->setStringValue("a/a/b", "foo");
|
||||
tree->setIntValue("a/a/c[2]", 99);
|
||||
tree->setIntValue("a/a/d", 1);
|
||||
tree->setIntValue("a/b/a[0]", 50);
|
||||
tree->setIntValue("a/b/a[1]", 100);
|
||||
tree->setStringValue("a/b/b", "foo");
|
||||
|
||||
SGPropertyNode_ptr destA(new SGPropertyNode);
|
||||
|
||||
copyPropertiesIf(tree, destA, [](const SGPropertyNode* src) {
|
||||
// always copy non-leaf nodes
|
||||
if (src->nChildren() > 0)
|
||||
return true;
|
||||
|
||||
return (src->getType() == simgear::props::INT) &&
|
||||
src->getIntValue() > 50;
|
||||
});
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, destA->getNode("a/a")->nChildren()); // only 99
|
||||
CPPUNIT_ASSERT_EQUAL(99, destA->getIntValue("a/a/c[2]"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, destA->getNode("a/b")->nChildren()); // only 100
|
||||
CPPUNIT_ASSERT_EQUAL(100, destA->getIntValue("a/b/a[1]"));
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ class SimgearPropsTests : public CppUnit::TestFixture
|
|||
// Set up the test suite.
|
||||
CPPUNIT_TEST_SUITE(SimgearPropsTests);
|
||||
CPPUNIT_TEST(testAliasLeak);
|
||||
CPPUNIT_TEST(testPropsCopyIf);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
|
@ -45,7 +46,7 @@ public:
|
|||
|
||||
// The tests.
|
||||
void testAliasLeak();
|
||||
|
||||
void testPropsCopyIf();
|
||||
private:
|
||||
// A property tree.
|
||||
SGPropertyNode *tree;
|
||||
|
|
Loading…
Add table
Reference in a new issue