LCOV - code coverage report
Current view: top level - source/ps - ConfigDB.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 13 13 100.0 %
Date: 2026-12-03 01:19:39 Functions: 2 5 40.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : /*
      19             :     CConfigDB - Load, access and store configuration variables
      20             : 
      21             :     TDD     :   http://www.wildfiregames.com/forum/index.php?showtopic=1125
      22             :     OVERVIEW:
      23             : 
      24             :     JavaScript: Check this documentation: https://gitea.wildfiregames.com/0ad/0ad/wiki/Exposed_ConfigDB_Functions
      25             : */
      26             : 
      27             : #ifndef INCLUDED_CONFIGDB
      28             : #define INCLUDED_CONFIGDB
      29             : 
      30             : #include "lib/file/vfs/vfs_path.h"
      31             : #include "ps/CStr.h"
      32             : 
      33             : #include <array>
      34             : #include <functional>
      35             : #include <map>
      36             : #include <memory>
      37             : #include <mutex>
      38             : #include <string>
      39             : #include <string_view>
      40             : #include <utility>
      41             : #include <vector>
      42             : 
      43             : /**
      44             :  * Namespace priorities:
      45             :  * - Command line args override everything
      46             :  * - User supersedes HWDetect (let the user try crashing his system).
      47             :  * - HWDetect supersedes mods & default -> mods can mod hwdetect itself.
      48             :  * - SYSTEM is used for local.cfg and is basically for setting custom defaults.
      49             :  */
      50             : enum EConfigNamespace
      51             : {
      52             :     CFG_DEFAULT,
      53             :     CFG_MOD,
      54             :     CFG_SYSTEM,
      55             :     CFG_HWDETECT,
      56             :     CFG_USER,
      57             :     CFG_COMMAND,
      58             :     CFG_LAST
      59             : };
      60             : 
      61             : using CConfigValueSet = std::vector<CStr>;
      62             : 
      63             : // Opaque data type so that callers that hook into ConfigDB can delete their hooks.
      64             : // Would be defined in CConfigDB but then it couldn't be forward-declared, which is rather annoying.
      65             : // Actually defined below - requires access to CConfigDB.
      66             : class CConfigDBHook;
      67             : 
      68             : class CConfigDB
      69             : {
      70             :     friend CConfigDBHook;
      71             : public:
      72             :     CConfigDB();
      73             :     ~CConfigDB();
      74             :     CConfigDB(const CConfigDB&) = delete;
      75             :     CConfigDB(CConfigDB&&) = delete;
      76             : 
      77             :     static void Initialise();
      78             :     static void Shutdown();
      79             :     static bool IsInitialised();
      80             :     static CConfigDB* Instance();
      81             : 
      82             :     /**
      83             :      * Attempt to retrieve the value of a config variable with the given name;
      84             :      * will search CFG_COMMAND first, and then all namespaces from the specified
      85             :      * namespace down.
      86             :      */
      87             :     void GetValue(EConfigNamespace ns, const std::string_view name, bool& value);
      88             :     ///@copydoc CConfigDB::GetValue
      89             :     void GetValue(EConfigNamespace ns, const std::string_view name, int& value);
      90             :     ///@copydoc CConfigDB::GetValue
      91             :     void GetValue(EConfigNamespace ns, const std::string_view name, std::uint32_t& value);
      92             :     ///@copydoc CConfigDB::GetValue
      93             :     void GetValue(EConfigNamespace ns, const std::string_view name, float& value);
      94             :     ///@copydoc CConfigDB::GetValue
      95             :     void GetValue(EConfigNamespace ns, const std::string_view name, double& value);
      96             :     ///@copydoc CConfigDB::GetValue
      97             :     void GetValue(EConfigNamespace ns, const std::string_view name, std::string& value);
      98             : 
      99             :     template<typename T>
     100         239 :     [[nodiscard]] T Get(const std::string_view name, T value, const EConfigNamespace ns = CFG_USER)
     101             :     {
     102         239 :         GetValue(ns, name, value);
     103         209 :         return value;
     104             :     }
     105             : 
     106             :     template<typename T>
     107          31 :     [[nodiscard]] static T GetIfInitialised(const std::string_view name, T defaultValue,
     108             :         const EConfigNamespace ns = CFG_USER)
     109             :     {
     110          31 :         return IsInitialised() ? Instance()->Get(name, std::move(defaultValue), ns) : defaultValue;
     111             :     }
     112             : 
     113             :     /**
     114             :      * Returns true if changed with respect to last write on file
     115             :      */
     116             :     bool HasChanges(EConfigNamespace ns) const;
     117             : 
     118             :     void SetChanges(EConfigNamespace ns, bool value);
     119             : 
     120             :     /**
     121             :      * Attempt to retrieve a vector of values corresponding to the given setting;
     122             :      * will search CFG_COMMAND first, and then all namespaces from the specified
     123             :      * namespace down.
     124             :      */
     125             :     void GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const;
     126             : 
     127             :     /**
     128             :      * Returns the namespace that the value returned by GetValues was defined in,
     129             :      * or CFG_LAST if it wasn't defined at all.
     130             :      */
     131             :     EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr& name) const;
     132             : 
     133             :     /**
     134             :      * Retrieve a map of values corresponding to settings whose names begin
     135             :      * with the given prefix;
     136             :      * will search all namespaces from default up to the specified namespace.
     137             :      */
     138             :     std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const;
     139             : 
     140             :     /**
     141             :      * Save a config value in the specified namespace. If the config variable
     142             :      * existed the value is replaced.
     143             :      */
     144             :     void SetValueString(EConfigNamespace ns, const CStr& name, const CStr& value);
     145             : 
     146             :     void SetValueBool(EConfigNamespace ns, const CStr& name, const bool value);
     147             : 
     148             :     void SetValueList(EConfigNamespace ns, const CStr& name, std::vector<CStr> values);
     149             : 
     150             :     /**
     151             :      * Remove a config value in the specified namespace.
     152             :      */
     153             :     bool RemoveValue(EConfigNamespace ns, const CStr& name);
     154             : 
     155             :     /**
     156             :      * Set the path to the config file used to populate the specified namespace
     157             :      * Note that this function does not actually load the config file. Use
     158             :      * the Reload() method if you want to read the config file at the same time.
     159             :      *
     160             :      * 'path': The path to the config file.
     161             :      */
     162             :     void SetConfigFile(EConfigNamespace ns, const VfsPath& path);
     163             : 
     164             :     /**
     165             :      * Reload the config file associated with the specified config namespace
     166             :      * (the last config file path set with SetConfigFile)
     167             :      *
     168             :      * Returns:
     169             :      *  true:   if the reload succeeded,
     170             :      *  false:  if the reload failed
     171             :      */
     172             :     bool Reload(EConfigNamespace);
     173             : 
     174             :     /**
     175             :      * Write the current state of the specified config namespace to the file
     176             :      * specified by 'path'
     177             :      *
     178             :      * Returns:
     179             :      *  true:   if the config namespace was successfully written to the file
     180             :      *  false:  if an error occurred
     181             :      */
     182             :     bool WriteFile(EConfigNamespace ns, const VfsPath& path) const;
     183             : 
     184             :     /**
     185             :      * Write the current state of the specified config namespace to the file
     186             :      * it was originally loaded from.
     187             :      *
     188             :      * Returns:
     189             :      *  true:   if the config namespace was successfully written to the file
     190             :      *  false:  if an error occurred
     191             :      */
     192             :     bool WriteFile(EConfigNamespace ns) const;
     193             : 
     194             :     /**
     195             :      * Write a config value to the file specified by 'path'
     196             :      *
     197             :      * Returns:
     198             :      *  true:   if the config value was successfully saved and written to the file
     199             :      *  false:  if an error occurred
     200             :      */
     201             :     bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path);
     202             : 
     203             :     bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value);
     204             : 
     205             :     /**
     206             :      * Register a simple lambda that will be called anytime the value changes in any namespace
     207             :      * This is simple on purpose, the hook is responsible for checking if it should do something.
     208             :      * When RegisterHookAndCall is called, the hook is immediately triggered.
     209             :      * NB: CConfigDBHook will auto-unregister the hook when destroyed,
     210             :      * so you can use it to tie the lifetime of the hook to your object.
     211             :      * The hook will be deleted alongside ConfigDB anyways.
     212             :      */
     213             :     [[nodiscard]] CConfigDBHook RegisterHookAndCall(const CStr& name, std::function<void()> hook);
     214             : 
     215             :     void UnregisterHook(CConfigDBHook&& hook);
     216             :     void UnregisterHook(std::unique_ptr<CConfigDBHook> hook);
     217             : 
     218             : private:
     219             :     using TConfigMap = std::map<CStr, CConfigValueSet, std::less<>>;
     220             :     std::array<TConfigMap, CFG_LAST> m_Map;
     221             :     std::multimap<CStr, std::function<void()>> m_Hooks;
     222             :     std::array<VfsPath, CFG_LAST> m_ConfigFile;
     223             :     std::array<bool, CFG_LAST> m_HasChanges;
     224             : 
     225             :     mutable std::recursive_mutex m_Mutex;
     226             : };
     227             : 
     228             : class CConfigDBHook
     229             : {
     230             :     friend class CConfigDB;
     231             : public:
     232             :     CConfigDBHook() = delete;
     233             :     CConfigDBHook(const CConfigDBHook&) = delete;
     234             :     // Point the moved-from hook to end, which is checked for in UnregisterHook,
     235             :     // to avoid a double-erase error.
     236          12 :     CConfigDBHook(CConfigDBHook&& h) : m_ConfigDB(h.m_ConfigDB)
     237             :     {
     238          12 :         m_Ptr = std::move(h.m_Ptr);
     239          12 :         h.m_Ptr = m_ConfigDB.m_Hooks.end();
     240             :     }
     241             :     // Unregisters the hook. Must be called before the original ConfigDB gets deleted.
     242          24 :     ~CConfigDBHook()
     243             :     {
     244          24 :         m_ConfigDB.UnregisterHook(std::move(*this));
     245          24 :     }
     246             : private:
     247          12 :     CConfigDBHook(CConfigDB& cdb, std::multimap<CStr, std::function<void()>>::iterator p)
     248          12 :         : m_ConfigDB(cdb), m_Ptr(p)
     249             :     {};
     250             : 
     251             :     std::multimap<CStr, std::function<void()>>::iterator m_Ptr;
     252             :     CConfigDB& m_ConfigDB;
     253             : };
     254             : 
     255             : #define g_ConfigDB (*CConfigDB::Instance())
     256             : 
     257             : #endif // INCLUDED_CONFIGDB

Generated by: LCOV version 1.16