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

Add string to current hostname


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
farawayf
Senior Member
Join Date: Jan 2019
Old 03-04-2019 , 15:11   Add string to current hostname
Reply With Quote #1

Hello. How to get current hostname and add specific string to at start of hostname.

I don't know how to use Format or StrCat

PHP Code:

public void OnPluginStart()
{
    
HookEntityOutput("logic_relay""OnTrigger"Trigger_Relay);
}

public 
Trigger_Relay(const String:output[], callerattackerFloat:delay)
{
    
char lvlt3[64];

    
GetEntPropString(callerProp_Data"m_iName"lvlt3sizeof(lvlt3));
    
    
    
    if (
StrEqual(lvlt3"Mode_Stage1")) 
    {
        
//here need to add string [Stage 1] to at start of hostname. Example "[Stage 1] Server Hostname"     
    
}
    
    if (
StrEqual(lvlt3"Mode_Stage2")) 
    {
        
//here need to add string [Stage 2] to at start of hostname. Example "[Stage 2] Server Hostname" 
    
}
    

farawayf is offline
Kolapsicle
Senior Member
Join Date: Oct 2014
Old 03-04-2019 , 16:14   Re: Add string to current hostname
Reply With Quote #2

You may be able to get the 'hostname' console variable and read it into a char buffer. You'll need FindConVar and GetConVarString. To easily format a string, I suggest you use Format as you can merge multiple strings into a new char variable.

FindConVar example:
Spoiler


GetConVarString example:
Spoiler


Format example:
Spoiler


None of the above code has been tested.

Last edited by Kolapsicle; 03-04-2019 at 16:18.
Kolapsicle is offline
impossible_cc
Senior Member
Join Date: Sep 2018
Location: Ukraine
Old 03-04-2019 , 16:15   Re: Add string to current hostname
Reply With Quote #3

I'm not sure that it is possible to get access to hostname this way, but if that's true, it should work.


PHP Code:
ConVar cv_Name;
public 
void OnPluginStart() 

    
HookEntityOutput("logic_relay""OnTrigger"Trigger_Relay);
    
cv_Name FindConVar("hostname");


public 
Trigger_Relay(const String:output[], callerattackerFloat:delay

    
char lvlt3[64]; 

    
GetEntPropString(callerProp_Data"m_iName"lvlt3sizeof(lvlt3));
    
    if (
StrEqual(lvlt3"Mode_Stage1"))  
    {
        
char buffer[32];
        
cv_Name.GetString(buffer32);
        
Format(buffer32"[Stage 1] %s"buffer);
        
        
cv_Name.SetString(buffertruefalse);//here we add string [Stage 1] to at start of hostname. Example "[Stage 1] Server Hostname"  
        
        
    
}
     
    if (
StrEqual(lvlt3"Mode_Stage2"))  
    {
        
char buffer[32];
        
cv_Name.GetString(buffer32);
        
Format(buffer32"[Stage 2] %s"buffer);
        
        
cv_Name.SetString(buffertruefalse);//here we add string [Stage 2] to at start of hostname. Example "[Stage 2] Server Hostname"  
        
        
    

     


Last edited by impossible_cc; 03-04-2019 at 16:20.
impossible_cc is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 03-05-2019 , 06:20   Re: Add string to current hostname
Reply With Quote #4

Thank you Kolapsicle for information.

impossible_cc yes it works, thank you.
farawayf is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 03-05-2019 , 15:14   Re: Add string to current hostname
Reply With Quote #5

Quote:
Originally Posted by impossible_cc View Post
I'm not sure that it is possible to get access to hostname this way, but if that's true, it should work.


PHP Code:
ConVar cv_Name;
public 
void OnPluginStart() 

    
HookEntityOutput("logic_relay""OnTrigger"Trigger_Relay);
    
cv_Name FindConVar("hostname");


public 
Trigger_Relay(const String:output[], callerattackerFloat:delay

    
char lvlt3[64]; 

    
GetEntPropString(callerProp_Data"m_iName"lvlt3sizeof(lvlt3));
    
    if (
StrEqual(lvlt3"Mode_Stage1"))  
    {
        
char buffer[32];
        
cv_Name.GetString(buffer32);
        
Format(buffer32"[Stage 1] %s"buffer);
        
        
cv_Name.SetString(buffertruefalse);//here we add string [Stage 1] to at start of hostname. Example "[Stage 1] Server Hostname"  
        
        
    
}
     
    if (
StrEqual(lvlt3"Mode_Stage2"))  
    {
        
char buffer[32];
        
cv_Name.GetString(buffer32);
        
Format(buffer32"[Stage 2] %s"buffer);
        
        
cv_Name.SetString(buffertruefalse);//here we add string [Stage 2] to at start of hostname. Example "[Stage 2] Server Hostname"  
        
        
    

     

This implentation would end up with "[Stage 2] [Stage 1] ..."

You should store the hostname string during OnConfigsExecuted and always use it when formatting to avoid the above.

The buffer you use to write the level and hostname is to small and should be declared once at the start of the callback instead of multiple times.

When calling format use sizeof(buffer) instead of manually putting in the char array size.
__________________
Neuro Toxin is offline
impossible_cc
Senior Member
Join Date: Sep 2018
Location: Ukraine
Old 03-05-2019 , 15:40   Re: Add string to current hostname
Reply With Quote #6

Quote:
Originally Posted by Neuro Toxin View Post
You should store the hostname string during OnConfigsExecuted and always use it when formatting to avoid the above.
Nice catch. You are right here.

Quote:
Originally Posted by Neuro Toxin View Post
When calling format use sizeof(buffer) instead of manually putting in the char array size.
Such metod will work slower.

Quote:
Originally Posted by Neuro Toxin View Post
The buffer you use to write the level and hostname is to small and should be declared once at the start of the callback instead of multiple times.
I wrote it like this because I don't know is there any Trigger_Relay action, that will be just ignored (We don't need it). If so, we will declare char [], and don't use it sometimes.


I know that we will NEVER see the difference, because it works very fast anyways, so probably I'm too mad on optimizing, I rather do a bit more optimized than understandable, sorry for that.

I post it here, probably it is better
PHP Code:
ConVar cv_Name;

#define HOSTNAME_MAX_LENGTH 60

char s_Hostname[HOSTNAME_MAX_LENGTH];

public 
void OnPluginStart()  
{  
    
HookEntityOutput("logic_relay""OnTrigger"Trigger_Relay); 
    
cv_Name FindConVar("hostname"); 
}  

public 
void OnConfigsExecuted()
{
    
cv_Name.GetString(s_HostnameHOSTNAME_MAX_LENGTH);
}

public 
Trigger_Relay(const String:output[], callerattackerFloat:delay)  
{  
    
char lvlt3[64];
    
GetEntPropString(callerProp_Data"m_iName"lvlt3sizeof(lvlt3));
    
    if(
StrContains(lvlt3"Mode_Stage"false) > -1)
    {
//Any Mode_Stage will change the hostname, so you can do a restriction here
        
char buffer[72];
        
Format(buffersizeof(buffer), "[Stage %s] %s"lvlt3[10], s_Hostname);
        
cv_Name.SetString(buffertruefalse);
    }


Last edited by impossible_cc; 03-06-2019 at 16:37. Reason: sizeof() instead of literal, removed OnMapEnd()
impossible_cc is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 03-05-2019 , 16:00   Re: Add string to current hostname
Reply With Quote #7

anyway bcs of 2 plugins which conflicting with this plugin i put timer 0.5 .
buffer[32] changed to buffer[64]. working good.
farawayf is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 03-06-2019 , 00:56   Re: Add string to current hostname
Reply With Quote #8

Your comment in map end is right. Server.cfg will override as it re-executes before the next configs execute callback is fired.
__________________
Neuro Toxin is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-06-2019 , 04:55   Re: Add string to current hostname
Reply With Quote #9

Quote:
Originally Posted by impossible_cc View Post
Such metod will work slower.
No it wont, sizeof is compile time, there is no runtime penalty as there is no runtime code, it just gets replaced with the correct number.
__________________
asherkin is offline
impossible_cc
Senior Member
Join Date: Sep 2018
Location: Ukraine
Old 03-06-2019 , 16:35   Re: Add string to current hostname
Reply With Quote #10

Quote:
Originally Posted by asherkin View Post
No it wont, sizeof is compile time, there is no runtime penalty as there is no runtime code, it just gets replaced with the correct number.
Oh, that's nice.

For some reason, when I think about sizeof, I imagine strlen, my fault.
impossible_cc 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 09:13.


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