Wrote this while being bored in the train, it provides really easy access to CFG files from all COD's
You can read and write stuff from CFG files with it
ReadDvar will return 'nope' when the cfg can't be found, trying to find a better solution for this
Fixed all problems, in case you want to know if the dvar wasn't found:
You can use myCFG.NotFound (it will be true when it's not found, don't forget to change it back to false before you call readdvar again!)
Example:
The class:
You can read and write stuff from CFG files with it
ReadDvar will return 'nope' when the cfg can't be found, trying to find a better solution for this
Fixed all problems, in case you want to know if the dvar wasn't found:
You can use myCFG.NotFound (it will be true when it's not found, don't forget to change it back to false before you call readdvar again!)
Example:
CSHARP Code
- //Create a new CFGFile instance
- //Read the rcon password from server.cfg
- string rcon_password = myCFG.ReadDvar("rcon_password");
- //Set the rcon password to 'bananas'
- myCFG.WriteDvar("rcon_password", "bananas", false);
The class:
CSHARP Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
-
- namespace ServerConfigurator
- {
- class CFGFile
- {
- string path;
- public CFGFile(string file)
- {
- path = file;
- }
-
- public string ReadDvar(string dvar)
- {
- string[] lines = File.ReadAllLines(path);
- foreach (string line in lines)
- {
- try
- {
- if (split[1] == dvar)
- {
- string temp = "";
- if (split.Length > 3)
- {
- int i = -1;
- foreach (string f in split)
- {
- i++;
-
- if (i == split.Length+1)
- temp += f;
- else if(i > 1)
- {
- temp += f;
- temp += " ";
- }
- }
- }
- else temp = split[2];
-
- if (temp.StartsWith("\""))
- return temp.Replace("\"", "");
- else return temp;
- }
- }
- catch { }
- }
- NotFound = true;
- return "";
- }
- public bool NotFound = false;
- public void WriteDvar(string dvar, string value, bool integer)
- {
- ReadDvar(dvar);
- if (NotFound)
- {
- if (!integer) sw.WriteLine(string.Format("seta {0} \"{1}\"", dvar, value));
- else sw.WriteLine(string.Format("seta {0} {1}", dvar, value));
- sw.Close();
- }
- else
- {
- string[] lines = File.ReadAllLines(path);
- int i = -1;
- foreach (string line in lines)
- {
- i++;
- try
- {
- if (split[1] == dvar || "\""+split[1]+"\"" == dvar)
- {
- if (integer) split[2] = value;
- else split[2] = "\"" + value + "\"";
-
- lines[i] = string.Format("{0} {1} {2}", split[0], split[1], split[2]);
- File.Delete(path);
- foreach (string l33t in lines)
- {
- sw.WriteLine(l33t);
- }
- sw.Close();
- break;
- }
- }
- catch
- {
- }
- }
- }
-
- NotFound = false;
- }
- }
- }