Raised This Month: $32 Target: $400
 8% 

[EXTENSION] Config (1.0.2)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 03-29-2008 , 21:41   [EXTENSION] Config (1.0.2)
Reply With Quote #1

This extension provides a simple and comfortable configuration storage system suitable for both simple and advanced configurations. It is a wrapper for libconfig.

An extensive example for the format is available at http://www.hyperrealm.com/libconfig/test.cfg.txt


Installation

extract the attached config.zip file to your mod folder.


Configuration file format documentation

Located at http://www.hyperrealm.com/libconfig/...guration-Files


Configuration file grammar

Located at http://www.hyperrealm.com/libconfig/...n-File-Grammar


Examples

Load a simple configuration file with a few scalar settings (convar style, access setting values directly)

Example configuration file (addons/sourcemod/configs/IRC_Relay_-_Core_Plugin_Olly.conf (auto-generated from plugin name+author)):
PHP Code:
//********************************************************* 
//*  IRC Relay Core 
//********************************************************* 
irc_server "irc.gamesurge";
irc_port 6667;
irc_password "";
irc_nickname "IRCRelay";
 
// NAME CANNOT CONTAIN SPACES, OR QUOTES (This will be the bot trigger too!) 
irc_name "eu.test";
 
// The delay from each message dispatch from the message queue 
irc_flood_limit 0.5;
// [...] 
Example SourceMod script:
PHP Code:
// We can use OnPluginStart() since it doesn't depend on the Source config+
// convar stuff
public OnPluginStart() {
    
// create a new empty config object
    
new Handle:config ConfigCreate();

    
// Load a config from a file
    // The filename is optional, the extension will automatically generate it
    // from the plugin's name+author
    
if (!ConfigReadFile(config)) {
        
LogError("can't read config file");
        return;
    }

    
decl String:IRCServer[30];
    
// retrieve a string from the config
    
ConfigLookupString(config"irc_server"IRCServersizeof(IRCServer));
    
// retrieve a cell (integer) from the config
    
new Port ConfigLookupInt(config"irc_port");
    
// retrieve a float from the config
    
new Float:FloodLimit ConfigLookupFloat(config"irc_flood_limit");
    
// [...] (load more config settings here)

    // close the config handle
    
CloseHandle(config);


Load a more advanced configuration file

Example configuration file (from message relay):
PHP Code:
/*
 * Message relay configuration file
 */

// prefix required to relay messages, set to "" for all
say_prefix "+";

// output formatting
output "[%remotehost]  %nick: %message";

// your server's ip
localhost = {
    
host "1.2.3.4";
    
port 48476;
};

// remote hosts to relay to
remotehosts = (
    { 
host "3.4.5.6";
      
port 48476; },
    { 
host "example.player.to";
      
port 48476; },
    { 
host "5.6.7.8";
      
port 48476; }
); 
Example SourceMod script:
PHP Code:
public OnPluginStart() {
    new 
Handle:config ConfigCreate();

    if (!
ConfigReadFile(config)) {
        
LogError("can't read config file");
        return;
    }

    
decl String:prefix[20];
    
ConfigLookupString(config"say_prefix"prefixsizeof(prefix));

    
decl String:outputFormat[100];
    
ConfigLookupString(config"output"outputFormatsizeof(outputFormat));

    
decl String:localHost[25];
    
// . separates setting names in a path
    
ConfigLookupString(config"localhost.host",localHost,sizeof(localHost));

    new 
localPort ConfigLookupInt(config"localhost.port");

    
// get parent setting containing the remote host groups
    
new Handle:parent ConfigLookup(config"remotehosts");

    
// get amount of children
    
new length ConfigSettingLength(parent);

    
// iterate through children
    
for (new i=0i<lengthi++) {
        
// retrieve child setting (the group with remoteHost's host+port)
        
new Handle:child ConfigSettingGetElement(parenti);

        
// retrieve child "host" from the group
        
new Handle:childHost ConfigSettingGetMember(child"host");
        
// store host in a string
        
decl String:remoteHost[25];
        
ConfigSettingGetString(childHostremoteHostsizeof(remoteHost));

        
// retrieve child "port" from the group
        
new Handle:childPort ConfigSettingGetMember(child"port");
        
// get the port
        
new remotePort ConfigSettingGetInt(childPort);

        
// [...] do something with remoteHost+remotePort
        // don't close setting handles, only config handles!
    
}

     
// close the config handle
    
CloseHandle(config);




Usage (Note: You won't need all of these natives)

A configuration will be represented by a config object which consists of several setting objects arranged in a tree structure. There is always an implicit root setting which is a group (type).

You always have to close handles to config objects (only created by ConfigCreate()) but must not close handles to setting objects which will be closed by the extension as soon as the config object or linked setting object is destroyed.

Settings with an aggregate type can have child (=sub) settings. Aggregate types are group, array and list, scalar types are int/cell, float, bool and string.

Refer to http://www.hyperrealm.com/libconfig/...guration-Files for explaination of the individual types.

General
PHP Code:
native Handle:ConfigCreate(); 
Create a new empty config object, returns a config handle. This handle has to be closed with CloseHandle().

File import/export

The FileName parameter for these natives is optional. The extension will automatically generate one from the plugin's name and author if it's missing.

PHP Code:
native bool:ConfigFileExists(const String:FileName[]=""); 
Returns whether a configuration file exists.

PHP Code:
native bool:ConfigReadFile(Handle:Config, const String:FileName[]=""String:ErrorMsg[]=""MaxLength=0, &ErrorLine=0); 
Reads a configuration file into a config object. It'll clear the config object before.

PHP Code:
native bool:ConfigWriteFile(Handle:Config, const String:FileName[]=""); 
Writes a config object to a file.

Retrieve the value directly from a config object

PHP Code:
native ConfigLookupInt(Handle:Config, const String:Path[]);
native Float:ConfigLookupFloat(Handle:Config, const String:Path[]);
native bool:ConfigLookupBool(Handle:Config, const String:Path[]); 
Returns the requested value or 0 on failure.

PHP Code:
native bool:ConfigLookupString(Handle:Config, const String:Path[], String:Result[], MaxLength); 
Sets Result to the requested value, returns whether the path lookup+type check was successful.

Retrieve a setting object from a config object

PHP Code:
native Handle:ConfigLookup(Handle:Config, const String:Path[]); 
Returns the setting object or INVALID_HANDLE on failure (invalid path). This handle must not be freed, the extension will free it after the config handle has been destroyed.

Retrieve the value from a scalar setting object

PHP Code:
native ConfigSettingGetIntElement(Handle:SettingIndex);
native Float:ConfigSettingGetFloatElement(Handle:SettingIndex);
native bool:ConfigSettingGetBoolElement(Handle:SettingIndex); 
Returns the requested value or 0 on failure.

PHP Code:
native bool:ConfigSettingGetString(Handle:SettingString:Result[], MaxLength); 
Sets Result to the requested value, returns whether the type check was successful.


Set the value of a (scalar) setting object

PHP Code:
native bool:ConfigSettingSetInt(Handle:SettingValue);
native bool:ConfigSettingSetFloat(Handle:SettingFloat:Value);
native bool:ConfigSettingSetBool(Handle:Settingbool:Value);
native bool:ConfigSettingSetString(Handle:Setting, const String:Value[]); 
Sets the setting object's value to a Value, returns whether the type check was successful.

Retrieve a child setting object handle of a (aggregate) setting object (Note: Usually these may be not used in conjunction with the *Element natives)

PHP Code:
native Handle:ConfigSettingGetMember(Handle:SettingString:Name[]); 
Returns the handle of a child setting with a specific name or INVALID_HANDLE on failure.

PHP Code:
native Handle:ConfigSettingGetElement(Handle:SettingIndex); 
Returns handle of the nth child setting where n=Index starting from 0 or INVALID_HANDLE on failure.

Retrieve the value of the nth child setting object of an aggregate setting object

PHP Code:
native ConfigSettingGetIntElement(Handle:SettingIndex);
native Float:ConfigSettingGetFloatElement(Handle:SettingIndex);
native bool:ConfigSettingGetBoolElement(Handle:SettingIndex); 
Returns the requested value or 0 on failure.

PHP Code:
native bool:ConfigSettingGetStringElement(Handle:SettingIndexString:Result[], MaxLength); 
Sets Result to the requested value, returns whether the lookup+type check was successful.

Set the value of the nth child setting object of an aggregate setting object

PHP Code:
native bool:ConfigSettingSetIntElement(Handle:SettingIndexValue);
native bool:ConfigSettingSetFloatElement(Handle:SettingIndexFloat:Value);
native bool:ConfigSettingSetBoolElement(Handle:SettingIndexbool:Value);
native bool:ConfigSettingSetStringElement(Handle:SettingIndex, const String:Value[]); 
Sets the setting object's Indexth child's value to a Value, returns whether the lookup+type check was successful.

Add+Remove settings

PHP Code:
native Handle:ConfigSettingAdd(Handle:ParentSettingString:Name[], SettingType:Type); 
Adds a new child setting to ParentSetting. Name will not be used if ParentSetting is an array or a list. Returns the newly created setting object's handle or INVALID_HANDLE on failure (parent is no aggregate setting, name ambiguous or invalid type)

PHP Code:
native bool:ConfigSettingRemove(Handle:ParentSettingString:Name[]); 
Removes and destroys the childsetting named Name from ParentSetting recursively. All handles to destroyed settings will be closed. Returns whether the operation was successful.

Root setting

PHP Code:
native Handle:ConfigRootSetting(Handle:Config); 
Returns a handle to the root setting object which is always a group and implicitly defined.

Setting information

PHP Code:
native ConfigSettingName(Handle:SettingString:Result[], MaxLength); 
Sets Result to the Name of a Setting object.

PHP Code:
native ConfigSettingLength(Handle:Setting); 
Returns the amount of child settings under Setting. Use it for iterating.

PHP Code:
native SettingType:ConfigSettingType(Handle:Setting); 
Returns the type of a Setting object.

PHP Code:
native bool:ConfigSettingIsGroup(Handle:Setting);
native bool:ConfigSettingIsArray(Handle:Setting);
native bool:ConfigSettingIsList(Handle:Setting); 
Returns whether Setting is a group, array and list respectively.

PHP Code:
native bool:ConfigSettingIsAggregate(Handle:Setting);
native bool:ConfigSettingIsScalar(Handle:Setting);
native bool:ConfigSettingIsNumber(Handle:Setting); 
Returns whether Setting is aggregate (group, array or list), scalar (int/cell, float, bool or string) and a number (int/cell or float) respectively.

PHP Code:
native ConfigSettingSourceLine(Handle:Setting); 
Returns the line in the configuration file where Setting has been parsed from.

Sourcecode repository

http://player.to/gitweb/index.cgi?p=sm-ext-config.git

Backup:
http://forums.alliedmods.net/showpos...5&postcount=34
Attached Files
File Type: sp Get Plugin or Get Source (example2.sp - 1524 views - 2.0 KB)
File Type: zip config.ext.zip (108.0 KB, 4293 views)

Last edited by sfPlayer; 05-07-2011 at 18:10. Reason: Re-upload binary package (thx to berni)
sfPlayer is offline
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 03-29-2008 , 21:42   Re: [EXTENSION] Config
Reply With Quote #2

<reserved>
sfPlayer is offline
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 03-29-2008 , 21:43   Re: [EXTENSION] Config
Reply With Quote #3

<reserved 2>
sfPlayer is offline
Fredd
Veteran Member
Join Date: Jul 2007
Old 03-31-2008 , 19:25   Re: [EXTENSION] Config
Reply With Quote #4

sounds sweet man. gj
__________________
Need a private coder? AMXX, SourceMOD, MMS? PM me!
Fredd is offline
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 04-04-2008 , 13:44   Re: [EXTENSION] Config (1.0.0)
Reply With Quote #5

finally released after fixing my hdd
sfPlayer is offline
Fredd
Veteran Member
Join Date: Jul 2007
Old 06-08-2008 , 01:41   Re: [EXTENSION] Config (1.0.0)
Reply With Quote #6

so when i use ConfigSettingGetMember could i use ConfigSettingGetElement again on the element if i had a big nested config for example something like
Code:
servers = (     { host = "#####";       port = 2715;       settings = (       { ff = 1;         type = 3; }      );      } );
erm one more thing if the info under servers had a parent name like "ServerName = ( { host =......" could i still use integers to get the elements like how you used "ConfigSettingLength" ?
__________________
Need a private coder? AMXX, SourceMOD, MMS? PM me!

Last edited by Fredd; 06-08-2008 at 01:44.
Fredd is offline
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 06-11-2008 , 07:08   Re: [EXTENSION] Config (1.0.0)
Reply With Quote #7

yes, yes
sfPlayer is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 02-14-2010 , 19:57   Re: [EXTENSION] Config (1.0.0)
Reply With Quote #8

Hey,

I have a problem with ConfigReadFile(), it only accepts an absolute path, when I specify a path relative to the game folder, it returns false
Can this be fixed ?
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Sammy-ROCK!
Senior Member
Join Date: Jun 2008
Location: Near Mrs.Lag
Old 02-14-2010 , 20:05   Re: [EXTENSION] Config (1.0.0)
Reply With Quote #9

Awesome!
A instant load config file.
Sammy-ROCK! is offline
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 02-14-2010 , 22:11   Re: [EXTENSION] Config (1.0.0)
Reply With Quote #10

added support for paths relative to the game directory (compatible to BuildPath)

updated embedded libconfig to version 1.4.3

please test it

Last edited by psychonic; 03-05-2011 at 23:39. Reason: removed attachments due to source code not being available
sfPlayer 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 01:30.


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