AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [SNIP] Add a tag for your plugin. (https://forums.alliedmods.net/showthread.php?t=137626)

API 09-07-2010 15:37

[SNIP] Add a tag for your plugin.
 
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?

psychonic 09-07-2010 15:48

Re: [SNIP] Add a tag for your plugin.
 
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);



API 09-07-2010 15:52

Re: [SNIP] Add a tag for your plugin.
 
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.

psychonic 09-07-2010 15:53

Re: [SNIP] Add a tag for your plugin.
 
Quote:

Originally Posted by pimpinjuice (Post 1294227)
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.

API 09-07-2010 15:56

Re: [SNIP] Add a tag for your plugin.
 
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.

psychonic 09-07-2010 16:03

Re: [SNIP] Add a tag for your plugin.
 
Quote:

Originally Posted by pimpinjuice (Post 1294232)
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[]) 


API 09-07-2010 16:05

Re: [SNIP] Add a tag for your plugin.
 
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()

psychonic 09-07-2010 16:07

Re: [SNIP] Add a tag for your plugin.
 
Quote:

Originally Posted by pimpinjuice (Post 1294244)
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.

API 09-07-2010 16:10

Re: [SNIP] Add a tag for your plugin.
 
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.

psychonic 09-07-2010 16:43

Re: [SNIP] Add a tag for your plugin.
 
Quote:

Originally Posted by pimpinjuice (Post 1294250)
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.


All times are GMT -4. The time now is 20:34.

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