AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   KeyValues Extension (https://forums.alliedmods.net/showthread.php?t=260486)

Kailo 03-26-2015 06:34

KeyValues Extension
 
1 Attachment(s)
This plugin add methods for easily tree managment.

Features:
New class KeyValuesEx;
New functions:
Code:

// Creates a new KeyValues structure.  The Handle must be closed with
// CloseHandle() or delete.
//
// @param name          Name of the root section.
// @param firstKey      If non-empty, specifies the first key value.
// @param firstValue    If firstKey is non-empty, specifies the first key's value.
public KeyValuesEx(const char[] name, const char[] firstKey="", const char[] firstValue="");

// Sets a string value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param value        String value.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetStringEx(const char[] key, const char[] value, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Sets an integer value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param value        Value number.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetNumEx(const char[] key, int value, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Sets a large integer value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param value        Floating point value.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetUInt64Ex(const char[] key, const int value[2], const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Sets a floating point value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param value        Floating point value.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetFloatEx(const char[] key, float value, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Sets a set of color value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param r            Red value.
// @param g            Green value.
// @param b            Blue value.
// @param a            Alpha value.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetColorEx(const char[] key, int r, int g, int b, int a=0, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Sets a set of color value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param color        Red, green, blue and alpha channels.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetColor4Ex(const char[] key, const int color[4], const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Sets a vector value of a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param vec          Vector value.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void SetVectorEx(const char[] key, const float vec[3], const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves a string value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param value        Buffer to store key value in.
// @param maxlength    Maximum length of the value buffer.
// @param defvalue      Optional default value to use if the key is not found.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void GetStringEx(const char[] key, char[] value, int maxlength, const char[] defvalue="", const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves an integer value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param defvalue      Optional default value to use if the key is not found.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public int GetNumEx(const char[] key, int defvalue=0, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves a floating point value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param defvalue      Optional default value to use if the key is not found.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public float GetFloatEx(const char[] key, float defvalue=0.0, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves a set of color value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param r            Red value, set by reference.
// @param g            Green value, set by reference.
// @param b            Blue value, set by reference.
// @param a            Alpha value, set by reference.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void GetColorEx(const char[] key, int &r, int &g, int &b, int &a, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves a set of color value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param color        Red, green, blue, and alpha channels.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void GetColor4Ex(const char[] key, int color[4], const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves a large integer value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param value        Array to represent the large integer.
// @param defvalue      Optional default value to use if the key is not found.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void GetUInt64Ex(const char[] key, int value[2], int defvalue[2]={0,0}, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Retrieves a vector value from a KeyValues key with indication of tree level.
//
// @param key          Name of the key, or NULL_STRING.
// @param vec          Destination vector to store the value in.
// @param defvalue      Optional default value to use if the key is not found.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void GetVectorEx(const char[] key, float vec[3], const float defvalue[3]={0.0, 0.0, 0.0}, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

// Removes the given key with indication of tree level.
//
// @param key          Name of the key.
// @param tier1        If non-empty, specifies the first tree level.
// @param tier2        If non-empty, specifies the second tree level.
// @param tier3        If non-empty, specifies the third tree level.
// @param tier4        If non-empty, specifies the fourth tree level.
// @param tier5        If non-empty, specifies the fifth tree level.
public void DeleteKeyEx(const char[] key, const char[] tier1="", const char[] tier2="", const char[] tier3="", const char[] tier4="", const char[] tier5="");

Plugin Sample:
Code:

#include <keyvaluesex>

#define KVFILE                "pluginvip.txt"

KeyValuesEx kv;

public void OnPluginStart()
{
        kv = new KeyValuesEx("data");
        kv.ImportFromFile(KVFILE);
        if (!FileExists(KVFILE))
                kv.ExportToFile(KVFILE);
       
        kv.SetNumEx("money", 0, "Max", "info");
        kv.SetStringEx("playertype", "common", "Max", "info");
       
        kv.SetNumEx("money", 1000, "Bob", "info");
        kv.SetStringEx("playertype", "vip", "Bob", "info");
       
        RegConsoleCmd("sm_vip_money", Command_Info);
        RegConsoleCmd("sm_vip_givemoney", Command_Give);
}

public void OnPluginEnd()
{
        kv.ExportToFile(KVFILE);
        delete kv;
}

public Action Command_Info(int client, int args)
{
        char name[MAX_NAME_LENGTH+1];
        GetCmdArgString(name, sizeof(name));
        ReplyToCommand(client, "[VIP] %s's money: %d", name, kv.GetNumEx("money", _, name, "info"));
       
        return Plugin_Handled;
}

public Action Command_Give(int client, int args)
{
        if (GetCmdArgs() != 2)
                ReplyToCommand(client, "[VIP] Bad args count!");
        char name[MAX_NAME_LENGTH+1], sMoney[10];
        GetCmdArg(1, name, sizeof(name));
        GetCmdArg(2, sMoney, sizeof(sMoney));
        int money = StringToInt(sMoney) + kv.GetNumEx("money", _, name, "info");
        kv.SetNumEx("money", money, name, "info");
        kv.ExportToFile(KVFILE);
       
        return Plugin_Handled;
}

pluginvip.txt will look like:
Code:

"data"
{
        "Max"
        {
                "info"
                {
                        "money"                "0"
                        "playertype"                "common"
                }
        }
        "bob"
        {
                "info"
                {
                        "money"                "1000"
                        "playertype"                "vip"
                }
        }
}


Chdata 03-26-2015 09:45

Re: KeyValues Extension
 
KeyValueSex

sounds useful

Kailo 03-30-2015 13:43

Re: KeyValues Extension
 
Update:
Added DeleteKeyEx method.

spumer 05-12-2015 12:29

Re: KeyValues Extension
 
This will be great to add helpers for getting random subkey or implement for this coroutines: "get subkeys count" and "get subkey by position"

necavi 05-12-2015 16:06

Re: KeyValues Extension
 
Have you considered doing an almost filesystem method of access? Either through kv.GetInt("max.info.money", 100)? Could be useful in some instances.

asherkin 05-12-2015 19:18

Re: KeyValues Extension
 
Quote:

Originally Posted by necavi (Post 2296001)
Have you considered doing an almost filesystem method of access? Either through kv.GetInt("max.info.money", 100)? Could be useful in some instances.

That is already a thing in the native SourceMod interface, use / to separate levels.

necavi 05-12-2015 19:41

Re: KeyValues Extension
 
Huh, never knew that, thanks!

KissLick 05-13-2015 06:00

Re: KeyValues Extension
 
Quote:

Originally Posted by asherkin (Post 2296062)
That is already a thing in the native SourceMod interface, use / to separate levels.

Hell nice! You should write it to somewhere (wiki or docs page), I am already looking forward to try this feature! :-D

Btw.
Quote:

Originally Posted by Chdata (Post 2278234)
KeyValueSex

sounds useful

Just the name of this include KeyValueSexTension.


All times are GMT -4. The time now is 08:40.

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