From 40408a59c53ea6b43359e12fd7f372d59ec6ad62 Mon Sep 17 00:00:00 2001 From: Karrz1 <127596890+Karrz1@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:25:32 +0000 Subject: [PATCH] Core: a reload reads the config directory again LoadAdditionalDir kept the names of the files worldserver.conf.d held at startup, and Reload read only those. A file added to the directory later was never read by `.reload config` (it needed a restart), and a file removed from it made every later reload report an error. The directory itself is now kept, in its place in the load order, and every reload lists it again in name order: a new file is read, a removed one is dropped. Files added one by one with keepOnReload are kept as before. The order stays worldserver.conf, base files, additional files and directories in the order they were added, environment. --- src/common/Configuration/Config.cpp | 85 ++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index 1c73d0d50a..26300bfeb2 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -35,8 +35,45 @@ namespace fs = boost::filesystem; namespace { std::string _filename; - std::vector _additonalFiles; - std::vector _baseFiles; // LoadBaseFile: applied after the main file, before _additonalFiles + // Additional files and directories kept on reload, in load order. A directory (worldserver.conf.d) is listed again on every + // reload, so a file added to it after startup is read and a removed one is dropped; keeping only the file names it held at + // startup made a new conf.d file invisible until a restart. + struct ReloadEntry + { + std::string path; + bool isDir; + }; + std::vector _additionalEntries; + std::vector _baseFiles; // LoadBaseFile: applied after the main file, before _additionalEntries + + // The .conf files of a directory and its subdirectories, in name order, so a later file (zz-9-...) always overrides an + // earlier one. The order of the directory listing itself depends on the file system and is not alphabetical everywhere. + std::vector ListConfigFiles(std::string const& dir) + { + std::vector files; + fs::path dirPath = dir; + if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) + return files; + + std::vector configFiles; + for (fs::directory_entry const& f : fs::recursive_directory_iterator(dirPath)) + { + if (!fs::is_regular_file(f)) + continue; + + fs::path configFile = fs::absolute(f); + if (configFile.extension() != ".conf") + continue; + + configFiles.push_back(std::move(configFile)); + } + std::sort(configFiles.begin(), configFiles.end()); + + files.reserve(configFiles.size()); + for (fs::path const& configFile : configFiles) + files.push_back(configFile.generic_string()); + return files; + } std::vector _args; bpt::ptree _config; std::mutex _configLock; @@ -169,7 +206,7 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str _config.put_child(bpt::ptree::path_type(child.first, '/'), child.second); if (keepOnReload) - _additonalFiles.emplace_back(std::move(file)); + _additionalEntries.push_back({ std::move(file), false }); return true; } @@ -203,31 +240,17 @@ bool Trinity::Config::LoadBaseFile(std::string file, std::string& error) bool ConfigMgr::LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector& loadedFiles, std::vector& errors) { - fs::path dirPath = dir; - if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) - return true; - - // Load in name order, so a later file (zz-9-...) always overrides an earlier one. The order of the directory listing - // itself depends on the file system and is not alphabetical everywhere. - std::vector configFiles; - for (fs::directory_entry const& f : fs::recursive_directory_iterator(dirPath)) + // the directory itself is kept, not its files: Reload lists it again (see _additionalEntries) + if (keepOnReload) { - if (!fs::is_regular_file(f)) - continue; - - fs::path configFile = fs::absolute(f); - if (configFile.extension() != ".conf") - continue; - - configFiles.push_back(std::move(configFile)); + std::scoped_lock lock(_configLock); + _additionalEntries.push_back({ dir, true }); } - std::sort(configFiles.begin(), configFiles.end()); - for (fs::path const& configFile : configFiles) + for (std::string& fileName : ListConfigFiles(dir)) { - std::string fileName = configFile.generic_string(); std::string error; - if (LoadAdditionalFile(fileName, keepOnReload, error)) + if (LoadAdditionalFile(fileName, false, error)) loadedFiles.push_back(std::move(fileName)); else errors.push_back(std::move(error)); @@ -275,9 +298,19 @@ bool ConfigMgr::Reload(std::vector& errors) if (!LoadAdditionalFile(baseFile, false, error)) errors.push_back(std::move(error)); - for (std::string const& additionalFile : _additonalFiles) - if (!LoadAdditionalFile(additionalFile, false, error)) - errors.push_back(std::move(error)); + for (ReloadEntry const& entry : _additionalEntries) + { + if (!entry.isDir) + { + if (!LoadAdditionalFile(entry.path, false, error)) + errors.push_back(std::move(error)); + continue; + } + + for (std::string const& additionalFile : ListConfigFiles(entry.path)) + if (!LoadAdditionalFile(additionalFile, false, error)) + errors.push_back(std::move(error)); + } OverrideWithEnvVariablesIfAny();