// BeamMP, the BeamNG.drive multiplayer mod. // Copyright (C) 2024 BeamMP Ltd., BeamMP team and contributors. // // BeamMP Ltd. can be contacted by electronic mail via contact@beammp.com. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . #pragma once #include "Sync.h" #include #include #include #include #include #include #include #include #include struct ComposedKey { std::string Category; std::string Key; bool operator==(const ComposedKey& rhs) const { return (this->Category == rhs.Category && this->Key == rhs.Key); } }; template <> struct fmt::formatter : formatter { auto format(ComposedKey key, format_context& ctx) const; }; inline auto fmt::formatter::format(ComposedKey key, fmt::format_context& ctx) const { std::string key_metadata = fmt::format("{}::{}", key.Category, key.Key); return formatter::format(key_metadata, ctx); } namespace std { template <> class hash { public: std::uint64_t operator()(const ComposedKey& key) const { std::hash hash_fn; return hash_fn(key.Category + key.Key); } }; } struct Settings { using SettingsTypeVariant = std::variant; Settings(); enum Key { // Keys that correspond to the keys set in TOML // Keys have their TOML section name as prefix // [Misc] Misc_ImScaredOfUpdates, Misc_UpdateReminderTime, // [General] General_Description, General_Tags, General_MaxPlayers, General_Name, General_Map, General_AuthKey, General_Private, General_IP, General_Port, General_MaxCars, General_LogChat, General_ResourceFolder, General_Debug, General_AllowGuests, General_InformationPacket, }; Sync> SettingsMap; enum SettingsAccessMask { READ_ONLY, // Value can be read from console READ_WRITE, // Value can be read and written to from console NO_ACCESS // Value is inaccessible from console (no read OR write) }; using SettingsAccessControl = std::pair< Key, // The Key's corresponding enum encoding SettingsAccessMask // Console read/write permissions >; Sync> InputAccessMapping; std::string getAsString(Key key); int getAsInt(Key key); bool getAsBool(Key key); SettingsTypeVariant get(Key key); void set(Key key, const std::string& value); template , bool> = true> void set(Key key, Integer value) { auto map = SettingsMap.synchronize(); if (!map->contains(key)) { throw std::logic_error { "Undefined setting key accessed in Settings::set(int)" }; } if (!std::holds_alternative(map->at(key))) { throw std::logic_error { fmt::format("Wrong value type in Settings::set(int): index {}", map->at(key).index()) }; } map->at(key) = value; } template , bool> = true> void set(Key key, Boolean value) { auto map = SettingsMap.synchronize(); if (!map->contains(key)) { throw std::logic_error { "Undefined setting key accessed in Settings::set(bool)" }; } if (!std::holds_alternative(map->at(key))) { throw std::logic_error { fmt::format("Wrong value type in Settings::set(bool): index {}", map->at(key).index()) }; } map->at(key) = value; } const std::unordered_map getAccessControlMap() const; SettingsAccessControl getConsoleInputAccessMapping(const ComposedKey& keyName); void setConsoleInputAccessMapping(const ComposedKey& keyName, const std::string& value); void setConsoleInputAccessMapping(const ComposedKey& keyName, int value); void setConsoleInputAccessMapping(const ComposedKey& keyName, bool value); };