Convar print commands (#539)

Adds various concommands such as 
- `convar_findByFlags`
- `convar_list`
- `convar_differences`
- `convar_find`

The first 3 listed above are already registered as concommands natively but didn't seem to do anything when tested.
`convar_findByFlags` and `convar_find` were already implemented by Bob under the names `findflags` and `find` respectively but the names have been changed to reflect already existing convars.
This commit is contained in:
H0L0 2023-11-05 22:36:06 +10:00 committed by GitHub
parent 35581f1ff2
commit 2b269d22a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 118 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#include "printcommands.h"
#include "core/convar/cvar.h"
#include "core/convar/convar.h"
#include "core/convar/concommand.h"
@ -94,7 +95,20 @@ void ConCommand_find(const CCommand& arg)
char pTempName[256];
char pTempSearchTerm[256];
for (auto& map : R2::g_pCVar->DumpToMap())
ConCommandBase* var;
CCVarIteratorInternal* itint = R2::g_pCVar->FactoryInternalIterator();
std::map<std::string, ConCommandBase*> sorted;
for (itint->SetFirst(); itint->IsValid(); itint->Next())
{
var = itint->Get();
if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
{
sorted.insert({var->m_pszName, var});
}
}
delete itint;
for (auto& map : sorted)
{
bool bPrintCommand = true;
for (int i = 0; i < arg.ArgC() - 1; i++)
@ -150,8 +164,20 @@ void ConCommand_findflags(const CCommand& arg)
}
}
// print cvars
for (auto& map : R2::g_pCVar->DumpToMap())
ConCommandBase* var;
CCVarIteratorInternal* itint = R2::g_pCVar->FactoryInternalIterator();
std::map<std::string, ConCommandBase*> sorted;
for (itint->SetFirst(); itint->IsValid(); itint->Next())
{
var = itint->Get();
if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
{
sorted.insert({var->m_pszName, var});
}
}
delete itint;
for (auto& map : sorted)
{
if (map.second->m_nFlags & resolvedFlag)
PrintCommandHelpDialogue(map.second, map.second->m_pszName);
@ -160,14 +186,100 @@ void ConCommand_findflags(const CCommand& arg)
delete[] upperFlag;
}
void ConCommand_list(const CCommand& arg)
{
ConCommandBase* var;
CCVarIteratorInternal* itint = R2::g_pCVar->FactoryInternalIterator();
std::map<std::string, ConCommandBase*> sorted;
for (itint->SetFirst(); itint->IsValid(); itint->Next())
{
var = itint->Get();
if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
{
sorted.insert({var->m_pszName, var});
}
}
delete itint;
for (auto& map : sorted)
{
PrintCommandHelpDialogue(map.second, map.second->m_pszName);
}
spdlog::info("{} total convars/concommands", sorted.size());
}
void ConCommand_differences(const CCommand& arg)
{
CCVarIteratorInternal* itint = R2::g_pCVar->FactoryInternalIterator();
std::map<std::string, ConCommandBase*> sorted;
for (itint->SetFirst(); itint->IsValid(); itint->Next())
{
ConCommandBase* var = itint->Get();
if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
{
sorted.insert({var->m_pszName, var});
}
}
delete itint;
for (auto& map : sorted)
{
ConVar* cvar = R2::g_pCVar->FindVar(map.second->m_pszName);
if (!cvar)
{
continue;
}
if (strcmp(cvar->GetString(), "FCVAR_NEVER_AS_STRING") == NULL)
{
continue;
}
if (strcmp(cvar->GetString(), cvar->m_pszDefaultValue) == NULL)
{
continue;
}
std::string formatted =
fmt::format("\"{}\" = \"{}\" ( def. \"{}\" )", cvar->GetBaseName(), cvar->GetString(), cvar->m_pszDefaultValue);
if (cvar->m_bHasMin)
{
formatted.append(fmt::format(" min. {}", cvar->m_fMinVal));
}
if (cvar->m_bHasMax)
{
formatted.append(fmt::format(" max. {}", cvar->m_fMaxVal));
}
formatted.append(fmt::format(" - {}", cvar->GetHelpText()));
spdlog::info(formatted);
}
}
void InitialiseCommandPrint()
{
RegisterConCommand("find", ConCommand_find, "Find concommands with the specified string in their name/help text.", FCVAR_NONE);
RegisterConCommand("findflags", ConCommand_findflags, "Find concommands by flags.", FCVAR_NONE);
RegisterConCommand(
"convar_find", ConCommand_find, "Find convars/concommands with the specified string in their name/help text.", FCVAR_NONE);
// help is already a command, so we need to modify the preexisting command to use our func instead
// these commands already exist, so we need to modify the preexisting command to use our func instead
// and clear the flags also
ConCommand* helpCommand = R2::g_pCVar->FindCommand("help");
helpCommand->m_nFlags = FCVAR_NONE;
helpCommand->m_pCommandCallback = ConCommand_help;
ConCommand* findCommand = R2::g_pCVar->FindCommand("convar_findByFlags");
findCommand->m_nFlags = FCVAR_NONE;
findCommand->m_pCommandCallback = ConCommand_findflags;
ConCommand* listCommand = R2::g_pCVar->FindCommand("convar_list");
listCommand->m_nFlags = FCVAR_NONE;
listCommand->m_pCommandCallback = ConCommand_list;
ConCommand* diffCommand = R2::g_pCVar->FindCommand("convar_differences");
diffCommand->m_nFlags = FCVAR_NONE;
diffCommand->m_pCommandCallback = ConCommand_differences;
}