forked from mirrors/0ad
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
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
a = 10
|
||||
atext = "dummy"
|
||||
@@ -0,0 +1 @@
|
||||
b = 10
|
||||
@@ -0,0 +1,7 @@
|
||||
a = 8
|
||||
atext = "dummyreplaced"
|
||||
b = 8
|
||||
c = 8
|
||||
|
||||
[scoped]
|
||||
e = 8
|
||||
@@ -0,0 +1,2 @@
|
||||
[scoped]
|
||||
e = 10
|
||||
@@ -0,0 +1,8 @@
|
||||
a = 1
|
||||
b = 2
|
||||
c = 3
|
||||
d = 4
|
||||
|
||||
[scoped]
|
||||
e = 5
|
||||
f = 6
|
||||
@@ -310,6 +310,9 @@ bool CConfigDB::Reload(EConfigNamespace ns)
|
||||
}
|
||||
|
||||
TConfigMap newMap;
|
||||
if (ns == CFG_MOD)
|
||||
newMap.swap(m_Map[CFG_MOD]);
|
||||
|
||||
char *filebuf = (char*)buffer.get();
|
||||
char *filebufend = filebuf+buflen;
|
||||
|
||||
|
||||
@@ -151,6 +151,14 @@ void MountMods(const Paths& paths, const std::vector<CStr>& mods)
|
||||
g_VFS->Mount(L"", modPath / modName / "", baseFlags, priority);
|
||||
else
|
||||
g_VFS->Mount(L"", modUserPath / modName / "", userFlags, priority);
|
||||
|
||||
// If mod have a config/<modName>.cfg, load the configuration.
|
||||
VfsPath modConfigPath{fmt::format("config/{}.cfg", mods[i].c_str())};
|
||||
if (!VfsFileExists(modConfigPath))
|
||||
continue;
|
||||
|
||||
g_ConfigDB.SetConfigFile(CFG_MOD, modConfigPath);
|
||||
g_ConfigDB.Reload(CFG_MOD);
|
||||
}
|
||||
|
||||
// Mount the user mod last. In dev copy, mount it with a low priority. Otherwise, make it writable.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2021 Wildfire Games.
|
||||
/* 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
|
||||
@@ -32,8 +32,9 @@ public:
|
||||
void setUp()
|
||||
{
|
||||
g_VFS = CreateVfs();
|
||||
TS_ASSERT_OK(g_VFS->Mount(L"config", DataDir() / "_testconfig" / ""));
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
@@ -85,4 +86,56 @@ public:
|
||||
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");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user