5 #include <config/Config.h>
9 CHIRA_CREATE_LOG(CONENTRY);
12 ConCommand about{
"about",
"Prints the description of the given convar(s) or concommand(s).", [](ConCommand::CallbackArgs args) {
13 for (
const auto& name : args) {
14 if (ConEntryRegistry::hasConCommand(name)) {
15 LOG_CONENTRY.infoImportant(std::string{*ConEntryRegistry::getConCommand(name)});
16 }
else if (ConEntryRegistry::hasConVar(name)) {
17 LOG_CONENTRY.infoImportant(std::string{*ConEntryRegistry::getConVar(name)});
19 LOG_CONENTRY.infoImportant(
"Unknown console entry \"{}\"", name);
25 ConCommand find{
"find",
"Finds convars and/or concommands from the given substring.", [](ConCommand::CallbackArgs args) {
26 bool resultFound =
false;
27 for (
const auto& substr : args) {
28 for (
const auto& concommand: ConEntryRegistry::getConCommandList()) {
29 if (concommand.find(substr) != std::string::npos) {
30 LOG_CONENTRY.infoImportant(std::string{*ConEntryRegistry::getConCommand(concommand)});
34 for (
const auto& convar: ConEntryRegistry::getConVarList()) {
35 if (convar.find(substr) != std::string::npos) {
36 LOG_CONENTRY.infoImportant(std::string{*ConEntryRegistry::getConVar(convar)});
42 LOG_CONENTRY.infoImportant(
"No results.");
47 ConCommand con_entries{
"con_entries",
"Prints the description of every convar and concommand currently registered.", [] {
48 LOG_CONENTRY.infoImportant(
"-- Commands --");
49 auto concommandList = ConEntryRegistry::getConCommandList();
50 std::sort(concommandList.begin(), concommandList.end());
51 for (
const auto& name : concommandList) {
52 if (
const auto* concommand = ConEntryRegistry::getConCommand(name); !concommand->hasFlag(CON_FLAG_HIDDEN)) {
53 LOG_CONENTRY.infoImportant(std::string{*concommand});
57 LOG_CONENTRY.infoImportant(
"-- Variables --");
58 auto convarList = ConEntryRegistry::getConVarList();
59 std::sort(convarList.begin(), convarList.end());
60 for (
const auto& name : convarList) {
61 if (
const auto* convar = ConEntryRegistry::getConVar(name); !convar->hasFlag(CON_FLAG_HIDDEN)) {
62 LOG_CONENTRY.infoImportant(std::string{*convar});
67 ConEntry::ConEntry(std::string name_, std::string description_,
int flags_)
68 : name(std::move(name_))
69 , description(std::move(description_))
72 std::string_view ConEntry::getName()
const {
76 std::string_view ConEntry::getDescription()
const {
77 return this->description;
80 bool ConEntry::hasFlag(ConFlags flag)
const {
81 return this->flags & flag;
84 ConCommand::ConCommand(std::string name_,
const std::function<
void()>& callback_,
int flags_)
85 :
ConCommand(std::move(name_),
"No description provided.", callback_, flags_) {}
87 ConCommand::ConCommand(std::string name_, std::function<
void(ConCommand::CallbackArgs)> callback_,
int flags_)
88 :
ConCommand(std::move(name_),
"No description provided.", std::move(callback_), flags_) {}
90 ConCommand::ConCommand(std::string name_, std::string description_,
const std::function<
void()>& callback_,
int flags_)
91 :
ConCommand(std::move(name_), std::move(description_), [callback_](
ConCommand::CallbackArgs) {callback_();}, flags_) {}
93 ConCommand::ConCommand(std::string name_, std::string description_, std::function<
void(ConCommand::CallbackArgs)> callback_,
int flags_)
94 :
ConEntry(std::move(name_), std::move(description_), flags_)
95 , callback(std::move(callback_)) {
96 runtime_assert(ConEntryRegistry::registerConCommand(
this),
"This ConCommand already exists!");
99 ConCommand::~ConCommand() {
100 ConEntryRegistry::deregisterConCommand(
this);
103 void ConCommand::fire(ConCommand::CallbackArgs args) {
105 LOG_CONENTRY.error(
"Cannot fire cheat-protected ConCommand with cheats disabled.");
108 this->callback(args);
111 bool ConEntryRegistry::hasConCommand(std::string_view name) {
112 for (
const auto* concommand : ConEntryRegistry::getConCommands()) {
113 if (concommand->getName() == name) {
120 ConCommand* ConEntryRegistry::getConCommand(std::string_view name) {
121 for (
auto* concommand : ConEntryRegistry::getConCommands()) {
122 if (concommand->getName() == name) {
129 std::vector<std::string> ConEntryRegistry::getConCommandList() {
130 std::vector<std::string> out;
131 for (
const auto* concommand : ConEntryRegistry::getConCommands()) {
132 out.emplace_back(concommand->getName().data());
137 std::vector<ConCommand*>& ConEntryRegistry::getConCommands() {
138 static std::vector<ConCommand*> concommands;
142 bool ConEntryRegistry::registerConCommand(
ConCommand* concommand) {
143 if (ConEntryRegistry::hasConCommand(concommand->getName()))
145 ConEntryRegistry::getConCommands().push_back(concommand);
149 void ConEntryRegistry::deregisterConCommand(
ConCommand* concommand) {
150 auto& concommands = ConEntryRegistry::getConCommands();
151 concommands.erase(std::remove_if(concommands.begin(), concommands.end(), [concommand](
ConCommand* other) {
152 return concommand->getName() == other->getName();
153 }), concommands.end());
156 bool ConEntryRegistry::hasConVar(std::string_view name) {
157 for (
const auto* convar : ConEntryRegistry::getConVars()) {
158 if (convar->getName() == name) {
165 ConVar* ConEntryRegistry::getConVar(std::string_view name) {
166 for (
auto* convar : ConEntryRegistry::getConVars()) {
167 if (convar->getName() == name) {
174 std::vector<std::string> ConEntryRegistry::getConVarList() {
175 std::vector<std::string> out;
176 for (
const auto* convar : ConEntryRegistry::getConVars()) {
177 out.emplace_back(convar->getName().data());
182 std::vector<ConVar*>& ConEntryRegistry::getConVars() {
183 static std::vector<ConVar*> convars;
192 bool ConEntryRegistry::registerConVar(
ConVar* convar) {
193 if (ConEntryRegistry::hasConVar(convar->getName()))
195 ConEntryRegistry::getConVars().push_back(convar);
197 if (convar->hasFlag(CON_FLAG_CACHE) && ConEntryRegistry::getConVarCache().hasValue(convar->getName().data())) {
199 switch (convar->getType()) {
200 using enum ConVarType;
202 auto value = convar->getValue<
bool>();
203 ConEntryRegistry::getConVarCache().getValue(convar->getName().data(), &value);
204 convar->setValue(value,
false);
208 auto value = convar->getValue<
int>();
209 ConEntryRegistry::getConVarCache().getValue(convar->getName().data(), &value);
210 convar->setValue(value,
false);
214 auto value = convar->getValue<
double>();
215 ConEntryRegistry::getConVarCache().getValue(convar->getName().data(), &value);
216 convar->setValue(value,
false);
220 auto value = convar->getValue<std::string>();
221 ConEntryRegistry::getConVarCache().getValue(convar->getName().data(), &value);
222 convar->setValue(value,
false);
230 void ConEntryRegistry::deregisterConVar(
ConVar* convar) {
232 if (convar->hasFlag(CON_FLAG_CACHE)) {
233 auto& cache = ConEntryRegistry::getConVarCache();
234 switch (convar->getType()) {
235 using enum ConVarType;
237 cache.setValue(convar->getName().data(), convar->getValue<
bool>(),
true,
true);
240 cache.setValue(convar->getName().data(), convar->getValue<
int>(),
true,
true);
243 cache.setValue(convar->getName().data(), convar->getValue<
double>(),
true,
true);
246 cache.setValue(convar->getName().data(), convar->getValue<std::string>(),
true,
true);
251 auto& convars = ConEntryRegistry::getConVars();
252 convars.erase(std::remove_if(convars.begin(), convars.end(), [convar](
ConVar* other) {
253 return convar->getName() == other->getName();
258 ConEntryRegistry::deregisterConVar(
this);
261 ConVarType ConVar::getType()
const {
265 std::string_view ConVar::getTypeAsString()
const {
266 switch (this->type) {
267 using enum ConVarType;
280 ConVar sv_cheats{
"sv_cheats",
false,
"Unlocks certain console entries that break gameplay."};
283 return sv_cheats.getValue<
bool>();
static bool areCheatsEnabled()
Convenience function to check the value of sv_cheats.