0ad/source/ps/tests/test_ConfigDB.h
trompetin17 8e255af185
Fix mods configuration stacking and mergin per mod
Previously, the engine only loaded the last mod’s `config/mod.cfg` file,
causing earlier mods' configuration settings to be ignored. This broke
the expectation of stackable mod behavior and affected features relying
on custom config, such as font overrides.

This commit updates the mod mounting and configuration process to:

- Mount each mod's files before attempting to load its config.
- Reload each mod's `config/modname.cfg` via `g_ConfigDB.Reload(CFG_MOD)`
  after mounting.
- Merge configuration keys into the CFG_MOD namespace:
  - If a key exists, its value is updated.
  - If not, the key is added.

This behavior now aligns with the VFS override system, where later mods
take precedence but earlier mods still contribute.

Also adds `_test.mods` for validation.

Fixes edge cases for mod authors who rely on consistent and layered
configuration overrides.

Related: #6383, #1810
Fixes: #8060
2025-06-16 11:03:09 -05:00

141 lines
4.8 KiB
C++

/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/self_test.h"
#include "lib/file/vfs/vfs.h"
#include "ps/ConfigDB.h"
#include <memory>
extern PIVFS g_VFS;
class TestConfigDB : public CxxTest::TestSuite
{
std::unique_ptr<CConfigDB> configDB;
public:
void setUp()
{
g_VFS = CreateVfs();
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.mods" / "", VFS_MOUNT_MUST_EXIST));
TS_ASSERT_OK(g_VFS->Mount(L"config", DataDir() / "_testconfig" / "", 0, VFS_MAX_PRIORITY));
configDB = std::make_unique<CConfigDB>();
}
void tearDown()
{
DeleteDirectory(DataDir()/"_testconfig");
g_VFS.reset();
configDB.reset();
}
void test_setting_int()
{
configDB->SetConfigFile(CFG_SYSTEM, "config/file.cfg");
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
configDB->SetValueString(CFG_SYSTEM, "test_setting", "5");
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
{
std::string res;
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
TS_ASSERT_EQUALS(res, "5");
}
{
int res;
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
TS_ASSERT_EQUALS(res, 5);
}
}
void test_setting_empty()
{
configDB->SetConfigFile(CFG_SYSTEM, "config/file.cfg");
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
configDB->SetValueList(CFG_SYSTEM, "test_setting", {});
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
{
std::string res = "toto";
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
// Empty config values don't overwrite
TS_ASSERT_EQUALS(res, "toto");
}
{
int res = 3;
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
// Empty config values don't overwrite
TS_ASSERT_EQUALS(res, 3);
}
}
void test_setting_mods()
{
configDB->SetConfigFile(CFG_DEFAULT, "config/start.cfg");
configDB->Reload(CFG_DEFAULT);
TS_ASSERT_EQUALS(configDB->Get("a", 0, CFG_MOD), 1);
TS_ASSERT_EQUALS(configDB->Get("b", 0, CFG_MOD), 2);
TS_ASSERT_EQUALS(configDB->Get("c", 0, CFG_MOD), 3);
TS_ASSERT_EQUALS(configDB->Get("d", 0, CFG_MOD), 4);
TS_ASSERT_EQUALS(configDB->Get("scoped.e", 0, CFG_MOD), 5);
TS_ASSERT_EQUALS(configDB->Get("scoped.f", 0, CFG_MOD), 6);
configDB->SetConfigFile(CFG_MOD, "config/moda.cfg");
configDB->Reload(CFG_MOD);
TS_ASSERT_EQUALS(configDB->Get("a", 0, CFG_MOD), 10);
TS_ASSERT_EQUALS(configDB->Get("b", 0, CFG_MOD), 2);
TS_ASSERT_EQUALS(configDB->Get("c", 0, CFG_MOD), 3);
TS_ASSERT_EQUALS(configDB->Get("d", 0, CFG_MOD), 4);
TS_ASSERT_EQUALS(configDB->Get("scoped.e", 0, CFG_MOD), 5);
TS_ASSERT_EQUALS(configDB->Get("scoped.f", 0, CFG_MOD), 6);
TS_ASSERT_EQUALS(configDB->Get("atext", std::string{}, CFG_MOD), "dummy");
configDB->SetConfigFile(CFG_MOD, "config/modb.cfg");
configDB->Reload(CFG_MOD);
TS_ASSERT_EQUALS(configDB->Get("a", 0, CFG_MOD), 10);
TS_ASSERT_EQUALS(configDB->Get("b", 0, CFG_MOD), 10);
TS_ASSERT_EQUALS(configDB->Get("c", 0, CFG_MOD), 3);
TS_ASSERT_EQUALS(configDB->Get("d", 0, CFG_MOD), 4);
TS_ASSERT_EQUALS(configDB->Get("scoped.e", 0, CFG_MOD), 5);
TS_ASSERT_EQUALS(configDB->Get("scoped.f", 0, CFG_MOD), 6);
TS_ASSERT_EQUALS(configDB->Get("atext", std::string{}, CFG_MOD), "dummy");
configDB->SetConfigFile(CFG_MOD, "config/modscoped.cfg");
configDB->Reload(CFG_MOD);
TS_ASSERT_EQUALS(configDB->Get("a", 0, CFG_MOD), 10);
TS_ASSERT_EQUALS(configDB->Get("b", 0, CFG_MOD), 10);
TS_ASSERT_EQUALS(configDB->Get("c", 0, CFG_MOD), 3);
TS_ASSERT_EQUALS(configDB->Get("d", 0, CFG_MOD), 4);
TS_ASSERT_EQUALS(configDB->Get("scoped.e", 0, CFG_MOD), 10);
TS_ASSERT_EQUALS(configDB->Get("scoped.f", 0, CFG_MOD), 6);
TS_ASSERT_EQUALS(configDB->Get("atext", std::string{}, CFG_MOD), "dummy");
configDB->SetConfigFile(CFG_MOD, "config/modreplace.cfg");
configDB->Reload(CFG_MOD);
TS_ASSERT_EQUALS(configDB->Get("a", 0, CFG_MOD), 8);
TS_ASSERT_EQUALS(configDB->Get("b", 0, CFG_MOD), 8);
TS_ASSERT_EQUALS(configDB->Get("c", 0, CFG_MOD), 8);
TS_ASSERT_EQUALS(configDB->Get("d", 0, CFG_MOD), 4);
TS_ASSERT_EQUALS(configDB->Get("scoped.e", 0, CFG_MOD), 8);
TS_ASSERT_EQUALS(configDB->Get("scoped.f", 0, CFG_MOD), 6);
TS_ASSERT_EQUALS(configDB->Get("atext", std::string{}, CFG_MOD), "dummyreplaced");
}
};