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

[SNIP] Add a tag for your plugin.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
API
Veteran Member
Join Date: May 2006
Old 09-07-2010 , 15:37   [SNIP] Add a tag for your plugin.
Reply With Quote #1

Today I was looking through my server browser, and noticed the lack of tags for game mode indication. I would assume this primarily comes from the fact that sv_tags is a cvar, and I think people are scared of messing with it. With the code here, you will be able to ensure that a tag is always included for your game mode or plugin. Please note this isn't the only way to do this, and probably isn't the most efficient either, but it works.
I didn't code in support for multiple tags per plugin (or at least I didn't intend to)... this can be accomplished easily, but I didn't design this specific example with it in mind. To be honest, one tag is all a plugin should really allocate in order to show it's presence.

Code:
#define CUSTOM_TAG "mycoolplugin"
new bool:KeepMyMouthShut;
new Handle:hTagsCvar;

public OnPluginStart()
{
    hTagsCvar = FindConVar("sv_tags");
    if(hTagsCvar)
    {
        HookConVarChange(hTagsCvar, CallbackTags);
        KeepMyMouthShut = false;
    }
    else
    {
        SetFailState("Error attempting to hook sv_tags! This game doesn't support it?");
    }
}

EnsureTagsInclusion(const String:IncludeMe[], const String:CurrentValue[])
{
    new String:CurVal[512];
    strcopy(CurVal,512,CurrentValue);
    // Always just fix their tiny formatting errors.
    ReplaceString(CurVal, 512, " ,",",",false);
    ReplaceString(CurVal, 512, ", ",",",false);
    // Trim it too.
    TrimString(CurVal);
    // Are we empty? If we are, then automatically append.
    // Otherwise, check it and append only if necessary.
    if(strlen(CurVal)==0)
    {
        SetConVarString(hTagsCvar, IncludeMe);
    }
    else
    {
        // Now lets get all the current values.
        // An easy way to do this is to explode by the comma.
        new String:TheStrings[64][64];
        new ExpCount = ExplodeString(CurVal, ",", TheStrings, 64, 64);
        new bool:IsFound = false;
        for(new i=0;i<ExpCount;i++)
        {
            if(StrEqual(TheStrings[i], IncludeMe, false))
            {
                IsFound = true;
                break;
            }
        }
        if(!IsFound)
        {
            Format(CurVal, 512, "%s, %s", CurVal, IncludeMe);
            SetConVarString(hTagsCvar, CurVal);
        }
    }
}

public CallbackTags(Handle:convar, const String:oldValue[], const String:newValue[])
{
    if(!KeepMyMouthShut)
    {
        KeepMyMouthShut = true;
        EnsureTagsInclusion(CUSTOM_TAG, newValue);
        KeepMyMouthShut = false;
    }
}

public OnMapStart()
{
    new String:CurrentValue[512];
    GetConVarString(hTagsCvar, CurrentValue, 512);
    EnsureTagsInclusion(CUSTOM_TAG, CurrentValue);
}
Oh... by the way... I haven't tested it. Maybe someone else can?
__________________

Last edited by API; 09-07-2010 at 15:59.
API is offline
Send a message via AIM to API
psychonic

BAFFLED
Join Date: May 2008
Old 09-07-2010 , 15:48   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #2

This is what I use.

You still have to add logic to the below if you are concerned with something else clobbering your tag. This shouldn't be necessary as just manually changing cvars that add/remove tags should still leave it in tact.

By the way, if I'm not mistaken, I'm pretty sure sv_tags has a limit of 128 characters (inc. null).

Global
PHP Code:
new Handle:sv_tags
in OnPluginStart
PHP Code:
sv_tags FindConVar("sv_tags"); 
PHP Code:
stock MyAddServerTag(const String:tag[])
{
    
decl String:currtags[128];
    if (
sv_tags == INVALID_HANDLE)
    {
        return;
    }
    
    
GetConVarString(sv_tagscurrtagssizeof(currtags));
    if (
StrContains(currtagstag) > -1)
    {
        
// already have tag
        
return;
    }
    
    
decl String:newtags[128];
    
Format(newtagssizeof(newtags), "%s%s%s"currtags, (currtags[0]!=0)?",":""tag);
    new 
flags GetConVarFlags(sv_tags);
    
SetConVarFlags(sv_tagsflags & ~FCVAR_NOTIFY);
    
SetConVarString(sv_tagsnewtags);
    
SetConVarFlags(sv_tagsflags);
}

stock MyRemoveServerTag(const String:tag[])
{
    
decl String:newtags[128];
    if (
sv_tags == INVALID_HANDLE)
    {
        return;
    }
    
    
GetConVarString(sv_tagsnewtagssizeof(newtags));
    if (
StrContains(newtagstag) == -1)
    {
        
// tag isn't on here, just bug out
        
return;
    }
    
    
ReplaceString(newtagssizeof(newtags), tag"");
    
ReplaceString(newtagssizeof(newtags), ",,""");
    new 
flags GetConVarFlags(sv_tags);
    
SetConVarFlags(sv_tagsflags & ~FCVAR_NOTIFY);
    
SetConVarString(sv_tagsnewtags);
    
SetConVarFlags(sv_tagsflags);


Last edited by psychonic; 09-07-2010 at 15:53.
psychonic is offline
API
Veteran Member
Join Date: May 2006
Old 09-07-2010 , 15:52   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #3

Thanks for contributing. What exactly am I missing?
Quote:
You still have to add logic if you are concerned with something else clobbering your tag.
__________________
API is offline
Send a message via AIM to API
psychonic

BAFFLED
Join Date: May 2008
Old 09-07-2010 , 15:53   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #4

Quote:
Originally Posted by pimpinjuice View Post
Thanks for contributing. What exactly am I missing?
Sorry if I wasn't clear. That was directed at my example, not yours. I intentionally left it out as it shouldn't be necessary unless another plugin is wiping sv_tags. The game/engine shouldn't be clobbering custom ones on its own.
psychonic is offline
API
Veteran Member
Join Date: May 2006
Old 09-07-2010 , 15:56   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #5

Oh gotcha. Yeah sorry that makes more sense now that I re-read your post.
On a side note, is there enough interest in this to make it an interface? Something as simple as: AddPluginTag("blah"); RemovePluginTag("blah");
You could handle all the logic is one plugin.
Let me know.
__________________
API is offline
Send a message via AIM to API
psychonic

BAFFLED
Join Date: May 2008
Old 09-07-2010 , 16:03   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #6

Quote:
Originally Posted by pimpinjuice View Post
On a side note, is there enough interest in this to make it an interface? Something as simple as: AddPluginTag("blah"); RemovePluginTag("blah");
You could handle all the logic is one plugin.
Let me know.
It surely wouldn't hurt, but I can see many plugins not wanting to implement a new dependency for something small that they can just copy/paste in or at least include from file. It might make most sense as a self-contained include, like attached.

PHP Code:
stock AddCustomServerTag(const String:tag[])

stock RemoveCustomServerTag(const String:tag[]) 

Last edited by psychonic; 09-08-2010 at 11:17.
psychonic is offline
API
Veteran Member
Join Date: May 2006
Old 09-07-2010 , 16:05   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #7

Yeah, however if sv_tags gets manually changed the hook isn't in place. The include would be perfect if we added one more stock: InitAndHookTags()
__________________
API is offline
Send a message via AIM to API
psychonic

BAFFLED
Join Date: May 2008
Old 09-07-2010 , 16:07   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #8

Quote:
Originally Posted by pimpinjuice View Post
Yeah, however if sv_tags gets manually changed the hook isn't in place. The include would be perfect if we added one more stock: InitAndHookTags()
Feel free to add whatever you think is necessary.
psychonic is offline
API
Veteran Member
Join Date: May 2006
Old 09-07-2010 , 16:10   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #9

Sorry didn't mean it like that, I just meant that with the current include people should be aware that they should probably re-add the tag any time sv_tags is changed. Your code is great as you already know.
__________________
API is offline
Send a message via AIM to API
psychonic

BAFFLED
Join Date: May 2008
Old 09-07-2010 , 16:43   Re: [SNIP] Add a tag for your plugin.
Reply With Quote #10

Quote:
Originally Posted by pimpinjuice View Post
Sorry didn't mean it like that, I just meant that with the current include people should be aware that they should probably re-add the tag any time sv_tags is changed. Your code is great as you already know.
No, you're right. I forgot that manually adding tags (like via server.cfg) would clobber tags set by plugins.
psychonic 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 19:46.


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