Raised This Month: $ Target: $400
 0% 

[SNIP] Add a tag for your plugin.


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



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 17:32.


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