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

[Stock] Auto Exec Config File


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 12-05-2010 , 07:10   [Stock] Auto Exec Config File
Reply With Quote #1

auto_exec_config(const szFileName[], bool:bAutoCreate=true))

Executes config file from a plugin, file has to be in configs folder.
If the file doesn't exist and autocreate set to 1 (default value),
a file is created with all commands listed (commented for obvious reasons) and all cvars.

PHP Code:
auto_exec_config(const szName[], bool:bAutoCreate=true)
{
    new 
szFileName[32]
    new 
iLen copy(szFileNamecharsmax(szFileName), szName)
    if( 
iLen <= || !equal(szFileName[iLen-4], ".cfg") )
    {
        
add(szFileNamecharsmax(szFileName), ".cfg")
    }

    new 
szConfigPath[96]
    
get_localinfo("amxx_configsdir"szConfigPathcharsmax(szConfigPath))
    
format(szConfigPathcharsmax(szConfigPath), "%s/%s"szConfigPathszFileName)

    if( 
file_exists(szConfigPath) )
    {
        
server_cmd("exec %s"szConfigPath)
        
server_exec()
        return 
1
    
}
    else if( 
bAutoCreate )
    {
        new 
fp fopen(szConfigPath"wt")
        if( !
fp )
        {
            return -
1
        
}
        new 
szPluginFileName[96], szPluginName[64], szAuthor[32], szVersion[32], szStatus[2]
        new 
iPlugin get_plugin(-1
                    
szPluginFileNamecharsmax(szPluginFileName), 
                    
szPluginNamecharsmax(szPluginName), 
                    
szVersioncharsmax(szVersion), 
                    
szAuthorcharsmax(szAuthor), 
                    
szStatuscharsmax(szStatus) )

        
server_print("Plugin id is %d"iPlugin)
        
fprintf(fp"; ^"%s^" configuration file^n"szPluginName)
        
fprintf(fp"; Author : ^"%s^"^n"szAuthor)
        
fprintf(fp"; Version : ^"%s^"^n"szVersion)
        
fprintf(fp"; File : ^"%s^"^n"szPluginFileName)

        new 
iMaxiszCommand[64], iCommandAccessszCmdInfo[128], szFlags[32]
        
iMax get_concmdsnum(-1, -1)
        
fprintf(fp"^n; Console Commands :^n")
        for(
i=0i<iMaxi++)
        {
            if( 
get_concmd_plid(i, -1, -1) == iPlugin )
            {
                
get_concmd(i
                        
szCommandcharsmax(szCommand), 
                        
iCommandAccess
                        
szCmdInfocharsmax(szCmdInfo), 
                        -
1, -1)
                
get_flags(iCommandAccessszFlagscharsmax(szFlags))
                
fprintf(fp"; %s | Access:^"%s^" | ^"%s^"^n"szCommandszFlagsszCmdInfo)
            }
        }

        
iMax get_plugins_cvarsnum()
        new 
iTempIdiPcvarszCvarName[256], szCvarValue[128]
        
fprintf(fp"^n; Cvars :^n")
        for(new 
ii<iMaxi++)
        {
            
get_plugins_cvar(iszCvarNamecharsmax(szCvarName), _iTempIdiPcvar)
            if( 
iTempId == iPlugin )
            {
                
get_pcvar_string(iPcvarszCvarValuecharsmax(szCvarValue))
                
fprintf(fp"%s ^"%s^"^n"szCvarNameszCvarValue)
            }
        }

        
fclose(fp)
    }
    return 
0


Plugin usage example :
PHP Code:
/*    Formatright © 2010, ConnorMcLeod

    This plugin is free software;
    you can redistribute it and/or modify it under the terms of the
    GNU General Public License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this plugin; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include <amxmodx>
#include <autoexecconfig>

#define VERSION "0.0.1"
#define PLUGIN "auto config test"

public plugin_init()
{
    
register_plugin(PLUGINVERSION"ConnorMcLeod")
    
register_cvar("osefblabla""1")
    
register_cvar("osefblabla1""2.050")
    
register_cvar("osefblabla2""You Failed !!")
    
register_clcmd("my_clcmd""my_clcmd"_"my clcmd comment")
    
register_concmd("my_concmd""my_concmd"ADMIN_CFG|ADMIN_KICK"my cocmd comment")
    
register_srvcmd("my_srvcmd""my_srvcmd"ADMIN_CFG"my servcmd comment")
}

public 
my_clcmd() {}
public 
my_concmd() {}
public 
my_srvcmd() {}

public 
plugin_cfg()
{
    
auto_exec_config("test")

Auto created configs/test.cfg if file doesn't exists :
Code:
; "auto config test" configuration file
; Author : "ConnorMcLeod"
; Version : "0.0.1"
; File : "auto_test.amxx"

; Console Commands :
; my_concmd | Access:"ch" | "my cocmd comment"
; my_srvcmd | Access:"h" | "my servcmd comment"

; Cvars :
osefblabla "0"
osefblabla1 "2.050"
osefblabla2 "You Failed !!"
Attached Files
File Type: inc autoexecconfig.inc (2.2 KB, 719 views)
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 12-05-2010 , 07:22   Re: [Stock] Auto Exec Config File
Reply With Quote #2

NIce, i will definitely use it
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 12-05-2010 , 07:23   Re: [Stock] Auto Exec Config File
Reply With Quote #3

It's to make beginners life easier.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 12-05-2010 , 09:08   Re: [Stock] Auto Exec Config File
Reply With Quote #4

I wish you've made this a month ago, it would've save me a lot of work.
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.

Last edited by drekes; 12-05-2010 at 09:08. Reason: typo
drekes is offline
Send a message via MSN to drekes
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 12-05-2010 , 09:56   Re: [Stock] Auto Exec Config File
Reply With Quote #5

BTW, conner it is also possible to make a similiar stock for ini files, right
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 12-05-2010 , 10:19   Re: [Stock] Auto Exec Config File
Reply With Quote #6

.cfg can be executed from server console, .ini files can't, so you would have to make your own system in your plugin to read the .ini file, so for cvars and commands, .cfg is just fine, and for .ini files, such a stock would be useless i think.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 12-05-2010 , 10:33   Re: [Stock] Auto Exec Config File
Reply With Quote #7

Quote:
Originally Posted by ConnorMcLeod View Post
.cfg can be executed from server console, .ini files can't, so you would have to make your own system in your plugin to read the .ini file, so for cvars and commands, .cfg is just fine, and for .ini files, such a stock would be useless i think.
Ok, thnx i got da point
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 12-05-2010 , 12:25   Re: [Stock] Auto Exec Config File
Reply With Quote #8

See, this is what I was talking about
Very nice work Connor.
Seta00 is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 12-14-2010 , 01:15   Re: [Stock] Auto Exec Config File
Reply With Quote #9

Connor -

In Galileo I had to alter the config loading code because some people were getting "segmentation fault" errors when it was loading the config like the way you have it setup in your code. Changing it to the following seemed to fix it, if I recall correctly. I can't recall what the problem was (too big maybe?) nor how I determined that this fixed it. I just have a vague memory that it did.

Code:
config_load() {     // we are loading the config like this in order to avoid the     // "segmentation fault" error that some servers get when trying     // to exec the config file // this is the old, easy, awesome way of loading the config file //  server_cmd("exec %s/galileo.cfg", DIR_CONFIGS); //  server_exec();     new configFilename[256];     formatex(configFilename, sizeof(configFilename)-1, "%s/galileo.cfg", DIR_CONFIGS);     new file = fopen(configFilename, "rt");     if (file)     {         new buffer[512], cvar[32], value[480];                 while (!feof(file))         {             fgets(file, buffer, sizeof(buffer)-1);             trim(buffer);                         if (buffer[0] && !equal(buffer, "//", 2) && !equal(buffer, ";", 1))             {                 strbreak(buffer, cvar, sizeof(cvar)-1, value, sizeof(value)-1);                 if (value[0] == 0 || isalpha(value[0]))                 {                     set_cvar_string(cvar, value);                 }                 else if (contain(value, "."))                 {                     set_cvar_float(cvar, floatstr(value));                 }                 else                 {                     set_cvar_num(cvar, str_to_num(value));                 }             }         }         fclose(file);     }     else     {         log_error(AMX_ERR_NOTFOUND, "%L", LANG_SERVER, "GAL_CONFIG_FILEMISSING", configFilename);     } }

Thought you might be interested. Take it for what it's worth.
__________________
Brad is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 12-14-2010 , 01:25   Re: [Stock] Auto Exec Config File
Reply With Quote #10

Just curious... Why didn't you add an optional 'folder' parameter to specify a sub-folder with the 'configs' directory, as they have it in SourceMod? It'd be helpful for any plugins that have multiple config files.
__________________
Brad 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:25.


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