ItsMods

Full Version: A code like GetServerCFG
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
guys i need a code like "GetServerCFG" that Addon use for read data from sv_config.ini

CSHARP Code
  1. public unsafe string GetServerCFG(string Section, string Key, string Default)

i need that to read other extra data form other files as same way Addon use on GetServerCFG .

Thanks
(11-10-2012, 09:20)raminr63 Wrote: [ -> ]hi
guys i need a code like "GetServerCFG" that Addon use for read data from sv_config.ini

CSHARP Code
  1. public unsafe string GetServerCFG(string Section, string Key, string Default)

i need that to read other extra data form other files as same way Addon use on GetServerCFG .

Thanks

EDIT:
Found it (can't edit my post on 4D1. also forgot the passwort Big Grin )

First of all:
This is NOT a Mod, it is a tool for Mod-Creators.

I made this to sum up every config for a mod into one File. Also it is now reall easy to read from text-files.
Also because it was a great function in the ServerAddon @itsmods


How to use it(Instructions for the File-Layout):
There is only one File: mod_config.ini . It is located in the admin Folder of your Server
If there is none, create it
Use the following Layout:
CODE: SELECT ALL
[Section]
Variablename=Value
Variablename2=Value45

[MyHighJumpMod]
JumpHeight=453
PlayerSpeed=53453

[YourAwsomeMod]
YourMoreAwsomeVariableName=1337

How to add it in your Mod:
Copy this code inside your class:
CODE: SELECT ALL
/**Searched through the mod_config.ini.
*@ param Section The Section where the Variablename is located
*@ param VariableName The Variablename
*@ param DefaultValue This Value is beeing returned when there is no mod_config.ini or the Section could not be found or the VariableName could not be found
*/
static public String getServerConfig(String Section, String VariableName, String DefaultValue)
{
string Path = "admin\\mod_config.ini";

if (!File.Exists(Path))
return DefaultValue;

StreamReader sr = new StreamReader(Path);

bool InSection = false;
for (String line = sr.ReadLine(); line != null; line = sr.ReadLine())
{
//***** SEARCHING THE CORRECT SECTION
if (!InSection && line.StartsWith("["))
{
line = line.Substring(1, line.Length - 2);
if (line.CompareTo(Section) != 0)
continue;
InSection = true;
continue;
}
//***** SEARCHING THE CORRECT SECTION

if (!InSection)
continue;

//***** EXITING WHEN ANOTHER SECTION BEGINS
if (InSection && line.StartsWith("["))
{
return DefaultValue;
}
//***** EXITING WHEN ANOTHER SECTION BEGINS

//***** SEARCHING FOR THE VARIABLE NAME AND GRABBING THE VALUE
if (InSection)
{
String[] splitted = line.Split('=');
if (splitted.Length != 2)
continue;

if (splitted[0].CompareTo(VariableName) != 0)
continue;

return splitted[1];
}
//***** SEARCHING FOR THE VARIABLE NAME AND GRABBING THE VALUE
}
return DefaultValue;
}
Usage in your code:
CODE: SELECT ALL
int jumpheight=getServerConfig("MyHighJumpMod","JumpHeight","45");
relay nice one exactly i need that source
very thanks

So bro Can you Change this source or create a new one and same for "SetServerCFG"

very thanks for useful source.
I have problem with line starter with "//"
as you know to disable a line we have to use "//"

So can someone add a section to this code to skip that line started with "//"

Thank you.
In pure INI files you use # as a comment, not //.
You might also be interested in http://easycaptu.re/z0tjQ
(11-10-2012, 18:24)JariZ Wrote: [ -> ]In pure INI files you use # as a comment, not //.
You might also be interested in http://easycaptu.re/z0tjQ

I just wanted to post this Undecided