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

KeyValues Extension


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 03-26-2015 , 06:34   KeyValues Extension
Reply With Quote #1

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"
		}
	}
}
Attached Files
File Type: inc keyvaluesex.inc (17.9 KB, 215 views)

Last edited by Kailo; 03-30-2015 at 13:44.
Kailo is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 03-26-2015 , 09:45   Re: KeyValues Extension
Reply With Quote #2

KeyValueSex

sounds useful
__________________
Chdata is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 03-30-2015 , 13:43   Re: KeyValues Extension
Reply With Quote #3

Update:
Added DeleteKeyEx method.
Kailo is offline
spumer
Senior Member
Join Date: Aug 2011
Old 05-12-2015 , 12:29   Re: KeyValues Extension
Reply With Quote #4

This will be great to add helpers for getting random subkey or implement for this coroutines: "get subkeys count" and "get subkey by position"
__________________
spumer is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 05-12-2015 , 16:06   Re: KeyValues Extension
Reply With Quote #5

Have you considered doing an almost filesystem method of access? Either through kv.GetInt("max.info.money", 100)? Could be useful in some instances.
necavi is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 05-12-2015 , 19:18   Re: KeyValues Extension
Reply With Quote #6

Quote:
Originally Posted by necavi View Post
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.
__________________
asherkin is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 05-12-2015 , 19:41   Re: KeyValues Extension
Reply With Quote #7

Huh, never knew that, thanks!
necavi is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 05-13-2015 , 06:00   Re: KeyValues Extension
Reply With Quote #8

Quote:
Originally Posted by asherkin View Post
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 View Post
KeyValueSex

sounds useful
Just the name of this include KeyValueSexTension.

Last edited by KissLick; 05-13-2015 at 09:34.
KissLick is offline
Reply


Thread Tools
Display Modes

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 02:46.


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