Raised This Month: $51 Target: $400
 12% 

How to execute server command and get the output in extension?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CDboy
Junior Member
Join Date: Aug 2020
Location: China
Old 12-11-2021 , 05:56   How to execute server command and get the output in extension?
Reply With Quote #1

Hi I am writing an extension and I want to execute server commands using C++.
I know that
Code:
IGameHelpers::ServerCommand()
can be used for executing, but I also want to get the command output.
How can I get this done?
CDboy is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-12-2021 , 03:55   Re: How to execute server command and get the output in extension?
Reply With Quote #2

Try look ServerCommandEx ? https://sm.alliedmods.net/new-api/co...erverCommandEx
https://github.com/alliedmodders/sou...erverCommandEx
__________________
Do not Private Message @me

Last edited by Bacardi; 12-12-2021 at 03:55.
Bacardi is offline
CDboy
Junior Member
Join Date: Aug 2020
Location: China
Old 12-13-2021 , 00:01   Re: How to execute server command and get the output in extension?
Reply With Quote #3

Well, I think I finally figured out the machanism of
Code:
static cell_t sm_ServerCommandEx(IPluginContext *pContext, const cell_t *params)
and I have written the code to get the output of a console command.
However during my test I found that any command would get a result of "Error, bad server command ...". For example, command status results in "Error, bad server command status".
Then I tested
Code:
IGameHelpers::ServerCommand
and I got the same result (Error...).
Then I tested ServerCommand() in SourcePawn, which worked.
Now I am really confused...
CDboy is offline
Ciallo
New Member
Join Date: Jan 2021
Old 12-13-2021 , 00:58   Re: How to execute server command and get the output in extension?
Reply With Quote #4

Code:
virtual void ServerCommand( const char *str )
{
	if ( !str )
	{
		Sys_Error( "ServerCommand with NULL string\n" );
	}
	if ( ValidCmd( str ) )
	{
		Cbuf_AddText( CBUF_SERVER, str );
	}
	else
	{
		ConMsg( "Error, bad server command %s\n", str );
	}
}

static bool ValidCmd( const char *pCmd )
{
	int len;

	len = strlen(pCmd);

	// Valid commands all have a ';' or newline '\n' as their last character
	if ( len && (pCmd[len-1] == '\n' || pCmd[len-1] == ';') )
		return true;

	return false;
}
make sure your command string have a ';' or newline '\n' as their last character

For example:
Code:
gamehelpers->ServerCommand("bot_kick\n");
Because the function does not auto \n terminate
Code:
/**
 * @brief Wraps IVEngineServer::ServerCommand. 
 *
 * @param buffer		Command buffer (does not auto \n terminate).
 */
virtual void ServerCommand(const char *buffer) =0;

Last edited by Ciallo; 12-13-2021 at 01:04. Reason: add code
Ciallo is offline
CDboy
Junior Member
Join Date: Aug 2020
Location: China
Old 12-13-2021 , 01:39   Re: How to execute server command and get the output in extension?
Reply With Quote #5

Quote:
Originally Posted by Ciallo View Post
Code:
virtual void ServerCommand( const char *str )
{
	if ( !str )
	{
		Sys_Error( "ServerCommand with NULL string\n" );
	}
	if ( ValidCmd( str ) )
	{
		Cbuf_AddText( CBUF_SERVER, str );
	}
	else
	{
		ConMsg( "Error, bad server command %s\n", str );
	}
}

static bool ValidCmd( const char *pCmd )
{
	int len;

	len = strlen(pCmd);

	// Valid commands all have a ';' or newline '\n' as their last character
	if ( len && (pCmd[len-1] == '\n' || pCmd[len-1] == ';') )
		return true;

	return false;
}
make sure your command string have a ';' or newline '\n' as their last character

For example:
Code:
gamehelpers->ServerCommand("bot_kick\n");
Because the function does not auto \n terminate
Code:
/**
 * @brief Wraps IVEngineServer::ServerCommand. 
 *
 * @param buffer		Command buffer (does not auto \n terminate).
 */
virtual void ServerCommand(const char *buffer) =0;
Thanks, this solved the problem!
CDboy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:55.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode