mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-25 21:40:09 +00:00
Add get and set to console command settings
Signed-off-by: Lucca Jiménez Könings <development@jimkoen.com>
This commit is contained in:
1 parent
a357ff8ca3
commit
3989961ff9
3 files changed
+167
-2
No files matched your search
@@ -171,6 +171,70 @@ struct Settings {
|
|||||||
}
|
}
|
||||||
SettingsMap.at(key) = value;
|
SettingsMap.at(key) = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::unordered_map<std::string, SettingsAccessControl>& getACLMap() const {
|
||||||
|
return InputAccessMapping;
|
||||||
|
}
|
||||||
|
SettingsAccessControl getConsoleInputAccessMapping(const std::string& keyName) {
|
||||||
|
if (!InputAccessMapping.contains(keyName)) {
|
||||||
|
throw std::logic_error { "Unknown key name accessed in Settings::getConsoleInputAccessMapping" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::noaccess) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not accessible from within the runtime!" };
|
||||||
|
}
|
||||||
|
return InputAccessMapping.at(keyName);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setConsoleInputAccessMapping(const std::string& keyName, std::string value) {
|
||||||
|
if (!InputAccessMapping.contains(keyName)) {
|
||||||
|
throw std::logic_error { "Unknown key name accessed in Settings::setConsoleInputAccessMapping" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::noaccess) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not accessible from within the runtime!" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::read) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not writeable from within the runtime!" };
|
||||||
|
}
|
||||||
|
|
||||||
|
Key key = InputAccessMapping.at(keyName).first;
|
||||||
|
|
||||||
|
if (!std::holds_alternative<std::string>(SettingsMap.at(key))) {
|
||||||
|
throw std::logic_error { "Wrong value type in Settings::setConsoleInputAccessMapping: expected std::string" };
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsMap.at(key) = value;
|
||||||
|
}
|
||||||
|
void setConsoleInputAccessMapping(const std::string& keyName, int value) {
|
||||||
|
if (!InputAccessMapping.contains(keyName)) {
|
||||||
|
throw std::logic_error { "Unknown key name accessed in Settings::setConsoleInputAccessMapping" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::noaccess) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not accessible from within the runtime!" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::read) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not writeable from within the runtime!" };
|
||||||
|
}
|
||||||
|
|
||||||
|
Key key = InputAccessMapping.at(keyName).first;
|
||||||
|
|
||||||
|
if (!std::holds_alternative<int>(SettingsMap.at(key))) {
|
||||||
|
throw std::logic_error { "Wrong value type in Settings::setConsoleInputAccessMapping: expected int" };
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsMap.at(key) = value;
|
||||||
|
}
|
||||||
|
void setConsoleInputAccessMapping(const std::string& keyName, bool value) {
|
||||||
|
if (!InputAccessMapping.contains(keyName)) {
|
||||||
|
throw std::logic_error { "Unknown key name accessed in Settings::setConsoleInputAccessMapping" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::noaccess) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not accessible from within the runtime!" };
|
||||||
|
} else if (InputAccessMapping.at(keyName).second == SettingsAccessMask::read) {
|
||||||
|
throw std::logic_error { "Key " + keyName + " is not writeable from within the runtime!" };
|
||||||
|
}
|
||||||
|
|
||||||
|
Key key = InputAccessMapping.at(keyName).first;
|
||||||
|
|
||||||
|
if (!std::holds_alternative<bool>(SettingsMap.at(key))) {
|
||||||
|
throw std::logic_error { "Wrong value type in Settings::setConsoleInputAccessMapping: expected bool" };
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsMap.at(key) = value;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*struct TSettings {
|
/*struct TSettings {
|
||||||
|
|||||||
+1
-1
@@ -238,7 +238,7 @@ template <class... Ts>
|
|||||||
overloaded(Ts...) -> overloaded<Ts...>;
|
overloaded(Ts...) -> overloaded<Ts...>;
|
||||||
|
|
||||||
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, Settings::Key key) {
|
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, Settings::Key key) {
|
||||||
|
beammp_infof("SettingsSingletonMap Size: {}", Application::SettingsSingleton.SettingsMap.size());
|
||||||
if (!Env.empty()) {
|
if (!Env.empty()) {
|
||||||
if (const char* envp = std::getenv(Env.data()); envp != nullptr && std::strcmp(envp, "") != 0) {
|
if (const char* envp = std::getenv(Env.data()); envp != nullptr && std::strcmp(envp, "") != 0) {
|
||||||
|
|
||||||
|
|||||||
+102
-1
@@ -28,6 +28,7 @@
|
|||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
static inline bool StringStartsWith(const std::string& What, const std::string& StartsWith) {
|
static inline bool StringStartsWith(const std::string& What, const std::string& StartsWith) {
|
||||||
return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith;
|
return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith;
|
||||||
@@ -366,8 +367,108 @@ std::tuple<std::string, std::vector<std::string>> TConsole::ParseCommand(const s
|
|||||||
return { Command, Args };
|
return { Command, Args };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class... Ts>
|
||||||
|
struct overloaded : Ts... {
|
||||||
|
using Ts::operator()...;
|
||||||
|
};
|
||||||
|
template <class... Ts>
|
||||||
|
overloaded(Ts...) -> overloaded<Ts...>;
|
||||||
|
|
||||||
void TConsole::Command_Settings(const std::string&, const std::vector<std::string>& args) {
|
void TConsole::Command_Settings(const std::string&, const std::vector<std::string>& args) {
|
||||||
if (!EnsureArgsCount(args, 1, 2)) {
|
|
||||||
|
for (const std::string& arg : args)
|
||||||
|
beammp_infof("Argument: {}", arg);
|
||||||
|
|
||||||
|
static constexpr const char* sHelpString = R"(
|
||||||
|
Settings:
|
||||||
|
settings help displays this help
|
||||||
|
settings list lists all settings
|
||||||
|
settings get <setting> prints current value of specified setting
|
||||||
|
settings set <setting> <value> sets specified setting to value
|
||||||
|
)";
|
||||||
|
|
||||||
|
if (args.size() == 0) {
|
||||||
|
beammp_errorf("No arguments specified for command 'settings'!");
|
||||||
|
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.front() == "help") {
|
||||||
|
|
||||||
|
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
||||||
|
return;
|
||||||
|
} else if (args.front() == "get") {
|
||||||
|
if (args.size() == 1) {
|
||||||
|
beammp_errorf("'settings get' needs at least one argument!");
|
||||||
|
|
||||||
|
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Settings::SettingsAccessControl acl = Application::SettingsSingleton.getConsoleInputAccessMapping(args.at(1));
|
||||||
|
Settings::SettingsTypeVariant keyType = Application::SettingsSingleton.get(acl.first);
|
||||||
|
|
||||||
|
std::visit(
|
||||||
|
overloaded {
|
||||||
|
[&args](std::string keyValue) {
|
||||||
|
Application::Console().WriteRaw(fmt::format("{} = {}", args.at(1), keyValue));
|
||||||
|
},
|
||||||
|
[&args](int keyValue) {
|
||||||
|
Application::Console().WriteRaw(fmt::format("{} = {}", args.at(1), keyValue));
|
||||||
|
},
|
||||||
|
[&args](bool keyValue) {
|
||||||
|
Application::Console().WriteRaw(fmt::format("{} = {}", args.at(1), keyValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
keyType);
|
||||||
|
|
||||||
|
} catch (std::logic_error& e) {
|
||||||
|
beammp_errorf("Error when getting key: {}", e.what());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (args.front() == "set") {
|
||||||
|
if (args.size() == 1) {
|
||||||
|
beammp_errorf("'settings set' needs at least two arguments!");
|
||||||
|
|
||||||
|
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Settings::SettingsAccessControl acl = Application::SettingsSingleton.getConsoleInputAccessMapping(args.at(1));
|
||||||
|
Settings::SettingsTypeVariant keyType = Application::SettingsSingleton.get(acl.first);
|
||||||
|
|
||||||
|
std::visit(
|
||||||
|
overloaded {
|
||||||
|
[&args](std::string keyValue) {
|
||||||
|
Application::SettingsSingleton.setConsoleInputAccessMapping(args.at(1), std::string(args.at(2)));
|
||||||
|
Application::Console().WriteRaw(fmt::format("{} := {}", args.at(1), std::string(args.at(2))));
|
||||||
|
},
|
||||||
|
[&args](int keyValue) {
|
||||||
|
Application::SettingsSingleton.setConsoleInputAccessMapping(args.at(1), std::stoi(args.at(2)));
|
||||||
|
Application::Console().WriteRaw(fmt::format("{} := {}", args.at(1), std::stoi(args.at(2))));
|
||||||
|
},
|
||||||
|
[&args](bool keyValue) {
|
||||||
|
// todo: implement other way to convert from string to bool
|
||||||
|
Application::SettingsSingleton.setConsoleInputAccessMapping(args.at(1), std::stoi(args.at(2)));
|
||||||
|
Application::Console().WriteRaw(fmt::format("{} := {}", args.at(1), std::stoi(args.at(2))));
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
keyType);
|
||||||
|
|
||||||
|
} catch (std::logic_error& e) {
|
||||||
|
beammp_errorf("Error when setting key: {}", e.what());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
beammp_errorf("Unknown argument for cammand 'settings': {}", args.front());
|
||||||
|
|
||||||
|
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user