PDA

View Full Version : [Stock] Auto Exec Config File


ConnorMcLeod
12-05-2010, 07:10
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.

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

new szConfigPath[96]
get_localinfo("amxx_configsdir", szConfigPath, charsmax(szConfigPath))
format(szConfigPath, charsmax(szConfigPath), "%s/%s", szConfigPath, szFileName)

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,
szPluginFileName, charsmax(szPluginFileName),
szPluginName, charsmax(szPluginName),
szVersion, charsmax(szVersion),
szAuthor, charsmax(szAuthor),
szStatus, charsmax(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 iMax, i, szCommand[64], iCommandAccess, szCmdInfo[128], szFlags[32]
iMax = get_concmdsnum(-1, -1)
fprintf(fp, "^n; Console Commands :^n")
for(i=0; i<iMax; i++)
{
if( get_concmd_plid(i, -1, -1) == iPlugin )
{
get_concmd(i,
szCommand, charsmax(szCommand),
iCommandAccess,
szCmdInfo, charsmax(szCmdInfo),
-1, -1)
get_flags(iCommandAccess, szFlags, charsmax(szFlags))
fprintf(fp, "; %s | Access:^"%s^" | ^"%s^"^n", szCommand, szFlags, szCmdInfo)
}
}

iMax = get_plugins_cvarsnum()
new iTempId, iPcvar, szCvarName[256], szCvarValue[128]
fprintf(fp, "^n; Cvars :^n")
for(new i; i<iMax; i++)
{
get_plugins_cvar(i, szCvarName, charsmax(szCvarName), _, iTempId, iPcvar)
if( iTempId == iPlugin )
{
get_pcvar_string(iPcvar, szCvarValue, charsmax(szCvarValue))
fprintf(fp, "%s ^"%s^"^n", szCvarName, szCvarValue)
}
}

fclose(fp)
}
return 0
}


Plugin usage example :
/* 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(PLUGIN, VERSION, "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 :
; "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 !!"

abdul-rehman
12-05-2010, 07:22
NIce, i will definitely use it :up:

ConnorMcLeod
12-05-2010, 07:23
It's to make beginners life easier.

drekes
12-05-2010, 09:08
I wish you've made this a month ago, it would've save me a lot of work.

abdul-rehman
12-05-2010, 09:56
BTW, conner it is also possible to make a similiar stock for ini files, right

ConnorMcLeod
12-05-2010, 10:19
.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.

abdul-rehman
12-05-2010, 10:33
.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 :D

Seta00
12-05-2010, 12:25
See, this is what I was talking about :D
Very nice work Connor.

Brad
12-14-2010, 01:15
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.

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
12-14-2010, 01:25
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.

ConnorMcLeod
12-14-2010, 01:39
Obviously because i'm too lazy, i may do it though.
Gonna see what you said in previous post later...

-edit-
Your way only seem to set cvars, i want to send commands as well.

Seta00
12-14-2010, 09:13
Maybe if you do a server_exec() before exec'ing your config file to be sure it will only execute your command on that frame?
This is just a guess but I'd give it a try, since I don't know how the engine exec's config files internally.

ConnorMcLeod
12-14-2010, 11:17
I don't remember what is the problem when you don't send server_exec, if it is only that the execution can be delayed, then i should let it optional.

Brad
12-14-2010, 13:00
Your way only seem to set cvars, i want to send commands as well.
Ah yes, I missed that bit.

Exolent[jNr]
12-14-2010, 18:34
Your way only seem to set cvars, i want to send commands as well.

You can still do that.

new pointer = get_cvar_pointer(cvar)
if(pointer)
{
// do cvar stuff
}
else
{
// send as command
}

Brad
12-14-2010, 20:45
I don't remember what is the problem when you don't send server_exec, if it is only that the execution can be delayed, then i should let it optional.
When you don't send it, you have no idea when your CVARs will be set. Thus, you read the config file and assume the CVARs are set, so you try to use them and WTFAPPLESAUCE they're not set. Using server_exec() forces the server to do it's thing and you don't get any WTFAPPLESAUCEs unless, you know... You suck at programming.

Exolent[jNr]
12-14-2010, 21:30
Also, if you wanted support for checking commands, I wrote this stock awhile ago:

bool:servcmd_exists(const command[])
bool:clcmd_exists(const command[])
bool:concmd_exists(const command[])
bool:user_can_use_cmd(id, const command[])
bool:xcmd_exists(const command[], id=-1, required_flags=0)

#define servcmd_exists(%1) xcmd_exists(%1, 0)
#define clcmd_exists(%1) xcmd_exists(%1, 1)
#define concmd_exists(%1) xcmd_exists(%1)
#define user_can_use_cmd(%1,%2) xcmd_exists(%2, %1, get_user_flags(%1))

stock bool:xcmd_exists(const command[], id=-1, const required_flags=0)
{
new i, _command[32], flags
while(get_concmd(i++, _command, charsmax(_command), flags, "", 0, required_flags, id))
{
if(equali(command, _command))
{
return true
}
}
return false
}

ConnorMcLeod
12-15-2010, 01:35
When you don't send it, you have no idea when your CVARs will be set. Thus, you read the config file and assume the CVARs are set, so you try to use them and WTFAPPLESAUCE they're not set. Using server_exec() forces the server to do it's thing and you don't get any WTFAPPLESAUCEs unless, you know... You suck at programming.

That's why i could let the server_exec optional and disabled by default, so depending on what you put in your plugin, you can set that param ON.
amxx.cfg is executed at plugin_init without server_exec for example.
Also, anyone have an idea on what max can be that delay ?

Seta00
12-15-2010, 06:30
Also, anyone have an idea on what max can be that delay ?

In time? 1/FPS seconds :P

bibu
12-18-2011, 06:34
Want to bring this up, since this is pretty useful. What about Brads situation with seg fault, is that fixed now?

Good job on this. :)

ConnorMcLeod
12-18-2011, 06:42
If you experience any server crash, feel free to remove the server_exec line.
System will still be as good as default amxx system with amxx.cfg and specific maps cfg files.

GuskiS
10-18-2013, 10:46
Can you add a support for newly added cvars/commands? For example, if you use an API, call the stock, check if cvar exists in file, if not, add it at the end.

CrazY.
08-05-2015, 10:37
Nice plugin :p