Raised This Month: $ Target: $400
 0% 

How to read from the server.cfg?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 02-17-2011 , 11:55   How to read from the server.cfg?
Reply With Quote #1

Hey guys,

the headline says it all. I want to read the server.cfg file and find out what values are stored there (e.g. sv_password blalba). How to?

Last edited by MightyMalcolm; 02-17-2011 at 12:15.
MightyMalcolm is offline
Steell
SourceMod Donor
Join Date: Mar 2009
Old 02-17-2011 , 18:38   Re: How to read from the server.cfg?
Reply With Quote #2

This is pretty basic, but:
Code:
//Finds the value of a given cvar in the given cfg file. Returns true on success, false on failure. //    fileName: the path to the cfg file //    cvar: the name of the cvar we're looking for //    buffer: string we're storing the value in //    buffersize: maximum length of the result string bool:FindConVarInCfg(const String:fileName[], const String:cvar[], String:buffer[], buffersize) {     //Try to open the cfg file.     new Handle:file = OpenFile(fileName, "rt");     if (file == INVALID_HANDLE)         return false;     new bool:result = false;     //Setup the regex we'll use to find the cvar     decl String:pattern[256];     Format(pattern, sizeof(pattern), "^\\s*%s\\s+(\"[^\"]*\"|\\S+)\\s*(?://.*)?$", cvar);     new Handle:reCvar = CompileRegex(pattern);     //Search each line of the cfg file for the cvar     decl String:line[256];     while (!IsEndOfFile(file) && ReadFileLine(file, line, sizeof(line)))     {         //if we find the correct cvar...         if (MatchRegex(reCvar, line) != -1)         {             //Store the value.             GetRegexSubString(reCvar, 1, buffer, buffersize);             result = true;         }     }         //Strip any quotes from the stored value.     if (result)     {         StripQuotes(buffer);     }         //And we're done here.     CloseHandle(file);     CloseHandle(reCvar);     return result; }

And so in your plugin, if you had this function, you would:
Code:
decl String:password[256]; FindConVarInCfg("cfg/server.cfg", "sv_password", password, sizeof(password));

Last edited by Steell; 02-18-2011 at 12:08.
Steell is offline
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 02-18-2011 , 05:21   Re: How to read from the server.cfg?
Reply With Quote #3

Thx Steell, but my compiler is crying out loud:

Quote:
SourcePawn Compiler 1.3.6
Copyright (c) 1997-2006, ITB CompuPhase, (C)2004-2008 AlliedModders, LLC

F:\Dokumente\Dark Messiah\Server\sourcemod-1.3.6-windows\addons\sourcemod\scripting\mm_test.sp (47) : error 017: undefined symbol "CompileRegex"
F:\Dokumente\Dark Messiah\Server\sourcemod-1.3.6-windows\addons\sourcemod\scripting\mm_test.sp (47) : warning 213: tag mismatch
F:\Dokumente\Dark Messiah\Server\sourcemod-1.3.6-windows\addons\sourcemod\scripting\mm_test.sp (52) : error 001: expected token: ")", but found "{"
F:\Dokumente\Dark Messiah\Server\sourcemod-1.3.6-windows\addons\sourcemod\scripting\mm_test.sp (54) : error 017: undefined symbol "MatchRegex"
F:\Dokumente\Dark Messiah\Server\sourcemod-1.3.6-windows\addons\sourcemod\scripting\mm_test.sp (57) : error 017: undefined symbol "GetRegexSubString"
F:\Dokumente\Dark Messiah\Server\sourcemod-1.3.6-windows\addons\sourcemod\scripting\mm_test.sp (35) : warning 203: symbol is never used: "buffersize"

4 Errors.

Compilation Time: 0,42 sec
Any includes i need to add? (sry me nub :<)
MightyMalcolm is offline
Steell
SourceMod Donor
Join Date: Mar 2009
Old 02-18-2011 , 06:15   Re: How to read from the server.cfg?
Reply With Quote #4

Code:
#include <regex>

Last edited by Steell; 02-18-2011 at 12:09.
Steell is offline
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 02-18-2011 , 20:24   Re: How to read from the server.cfg?
Reply With Quote #5

thx, works, love you guys ;)
MightyMalcolm is offline
Despirator
Senior Member
Join Date: Jun 2011
Location: Kazakhstan ->Shymkent
Old 11-24-2011 , 12:57   Re: How to read from the server.cfg?
Reply With Quote #6

result = GetRegexSubString(reCvar, 1, buffer, buffersize);
Despirator is offline
Reply



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 09:28.


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