added command line, added basic character
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
#include "console.h"
|
||||
#include "tier1/utlstring.h"
|
||||
#include "tier1/utlvector.h"
|
||||
|
||||
CUtlVector<ConVar*> g_convars;
|
||||
CUtlVector<ConCommand*> g_commands;
|
||||
|
||||
void IConsole::RegisterVar( ConVar *cvar )
|
||||
{
|
||||
|
||||
g_convars.AppendTail(cvar);
|
||||
}
|
||||
void IConsole::UnRegisterVar( ConVar *cvar )
|
||||
{
|
||||
@@ -11,35 +15,112 @@ void IConsole::UnRegisterVar( ConVar *cvar )
|
||||
}
|
||||
ConVar *IConsole::FindVar( const char *pName )
|
||||
{
|
||||
|
||||
for (auto &var: g_convars)
|
||||
{
|
||||
if (!V_strcmp(var->GetName(), pName))
|
||||
return var;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void IConsole::RegisterCommand( ConVar *cvar )
|
||||
void IConsole::RegisterCommand( ConCommand *cvar )
|
||||
{
|
||||
|
||||
g_commands.AppendTail(cvar);
|
||||
}
|
||||
void IConsole::UnRegisterCommand( ConVar *cvar )
|
||||
void IConsole::UnRegisterCommand( ConCommand *cvar )
|
||||
{
|
||||
|
||||
}
|
||||
ConCommand *IConsole::FindCommand( const char *pName )
|
||||
{
|
||||
for (auto &var: g_commands)
|
||||
{
|
||||
if (!V_strcmp(var->GetName(), pName))
|
||||
return var;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
CUtlString g_commandBuffer;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Executes arguments from IConsole::Execute
|
||||
//-----------------------------------------------------------------------------
|
||||
void IConsole::Execute2( CUtlVector<CUtlString> &args )
|
||||
{
|
||||
if (args.GetSize()<1)
|
||||
return;
|
||||
|
||||
ConCommand *cmd = IConsole::FindCommand(args[0]);
|
||||
if (!cmd)
|
||||
{
|
||||
V_printf("%s not found\n", args[0].GetString());
|
||||
return;
|
||||
}
|
||||
CUtlBuffer<char*> strbuffer(args.GetSize());
|
||||
for ( size_t i = 0; i < args.GetSize(); i++)
|
||||
{
|
||||
strbuffer[i] = args[i].GetString();
|
||||
};
|
||||
(cmd->GetCallback())(args.GetSize(), strbuffer);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Parses command buffer.
|
||||
//-----------------------------------------------------------------------------
|
||||
void IConsole::Execute( void )
|
||||
{
|
||||
CUtlVector<CUtlString> arguments;
|
||||
CUtlString szArgument;
|
||||
bool bIsQuote = false;
|
||||
for (auto &c: (CUtlVector<char>&)g_commandBuffer)
|
||||
{
|
||||
if (c == '\"')
|
||||
{
|
||||
bIsQuote = !bIsQuote;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == ';' || c == '\n' || c == '\0')
|
||||
{
|
||||
if (bIsQuote)
|
||||
{
|
||||
if (c != '\0')
|
||||
szArgument.AppendTail(c);
|
||||
continue;
|
||||
}
|
||||
if (szArgument != 0)
|
||||
arguments.AppendTail(szArgument);
|
||||
IConsole::Execute2(arguments);
|
||||
szArgument = 0;
|
||||
arguments = {};
|
||||
continue;
|
||||
}
|
||||
if ( c == '\t' || c == ' ' )
|
||||
{
|
||||
if (bIsQuote)
|
||||
{
|
||||
szArgument.AppendTail(c);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (szArgument != 0)
|
||||
arguments.AppendTail(szArgument);
|
||||
szArgument = 0;
|
||||
continue;
|
||||
}
|
||||
szArgument.AppendTail(c);
|
||||
};
|
||||
g_commandBuffer = 0;
|
||||
}
|
||||
|
||||
void IConsole::AddCommand( const char *psz )
|
||||
{
|
||||
|
||||
g_commandBuffer.AppendTail(psz);
|
||||
}
|
||||
|
||||
void IConsole::InsertCommand( const char *psz )
|
||||
{
|
||||
|
||||
g_commandBuffer.AppendHead(psz);
|
||||
};
|
||||
|
||||
ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags )
|
||||
@@ -55,7 +136,7 @@ ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags,
|
||||
ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags,
|
||||
const char *pHelpString, ConCommandFn callback )
|
||||
{
|
||||
m_szName = CUtlString(pName);
|
||||
m_szName = pName;
|
||||
m_flags = flags;
|
||||
m_szValue = pDefaultValue;
|
||||
m_fValue = V_atof(pDefaultValue);
|
||||
@@ -69,7 +150,7 @@ bool ConVar::IsFlagSet( int flag )
|
||||
}
|
||||
const char *ConVar::GetHelpText( void )
|
||||
{
|
||||
|
||||
return m_szHelpString;
|
||||
}
|
||||
bool ConVar::IsRegistered( void )
|
||||
{
|
||||
@@ -77,7 +158,7 @@ bool ConVar::IsRegistered( void )
|
||||
}
|
||||
const char *ConVar::GetName( void )
|
||||
{
|
||||
|
||||
return m_szName;
|
||||
}
|
||||
void ConVar::AddFlags( int flags )
|
||||
{
|
||||
@@ -129,4 +210,25 @@ void ConVar::SetValue( int iValue )
|
||||
m_fValue = iValue;
|
||||
m_nValue = iValue;
|
||||
m_szValue = CUtlString("%i\n",iValue);
|
||||
}
|
||||
}
|
||||
|
||||
ConCommand::ConCommand(const char *pName, ConCommandFn callback,
|
||||
const char *pHelpString, int flags)
|
||||
{
|
||||
m_szName = pName;
|
||||
m_callback = callback;
|
||||
m_flags = flags;
|
||||
IConsole::RegisterCommand(this);
|
||||
};
|
||||
const char *ConCommand::GetHelpText( void )
|
||||
{
|
||||
return m_szHelpString;
|
||||
}
|
||||
const char *ConCommand::GetName( void )
|
||||
{
|
||||
return m_szName;
|
||||
}
|
||||
ConCommandFn ConCommand::GetCallback( void )
|
||||
{
|
||||
return m_callback;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user