Release CFGFile class
#1
Lightbulb 
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:
Code:
//Create a new CFGFile instance CFGFile myCFG = new CFGFile("players2\\server.cfg"); //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:
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 { string[] split = line.Split(new string[] { " " }, StringSplitOptions.None); 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) { StreamWriter sw = new StreamWriter(path, true); 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 { string[] split = line.Split(new string[] { " " }, StringSplitOptions.None); 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); StreamWriter sw = new StreamWriter(path); foreach (string l33t in lines) { sw.WriteLine(l33t); } sw.Close(); break; } } catch { } } } NotFound = false; } } }
Reply

#2
rcon password "bananas" Big Grin
Reply

#3
What happens when 2 plugins write to the config at the same time?
[Image: MaEIQ.png]
Reply

#4
It will probably throw an error, but the chances of this happening are very unlikely (because reading takes like 1 ms), you can just run it in a try { } if you're afraid of this happening
Reply

#5
(01-27-2012, 09:37)jariz Wrote: It will probably throw an error, but the chances of this happening are very unlikely (because reading takes like 1 ms), you can just run it in a try { } if you're afraid of this happening

if 2 plugins do this on loadserver() the chance is pretty high
[Image: MaEIQ.png]
Reply

#6
(01-27-2012, 14:58)Pozzuh Wrote:
(01-27-2012, 09:37)jariz Wrote: It will probably throw an error, but the chances of this happening are very unlikely (because reading takes like 1 ms), you can just run it in a try { } if you're afraid of this happening

if 2 plugins do this on loadserver() the chance is pretty high

true
Reply

#7
@kokole you don't know anything about it, so don't pretend you are
@Pozzuh no because the plugins get called one by one and not all at the same time, therefore your argument is invalid
Reply

#8
(01-27-2012, 17:39)jariz Wrote: @kokole you don't know anything about it, so don't pretend you are
@Pozzuh no because the plugins get called one by one and not all at the same time, therefore your argument is invalid

that I dont know anything about it? Dodgy ok,ok. lets see Angry
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  W@W: Making player spawn with a custom default class. koil 9 7,569 01-13-2013, 12:10
Last Post: koil
  [Request] Disabling Custom Classes NOT CHOOSE CLASS DidUknowiPwn 5 5,372 01-10-2013, 04:32
Last Post: banz
  How to disable "Choose Class"? DidUknowiPwn 8 7,823 10-07-2012, 21:09
Last Post: JariZ
  [News] Black Ops 2 - Create A Class(LEAK) Tomsen1410 5 5,501 09-20-2012, 09:51
Last Post: Tomsen1410
  Mage class Lemon 23 17,837 04-22-2012, 22:46
Last Post: frisky
  Weapon Menu, create ingame custom class Gamemaster20 4 5,490 04-20-2012, 08:13
Last Post: rotceh_dnih
  Class Plugin pollarpart 5 6,298 04-16-2012, 13:40
Last Post: OzonE
  Disable Class Choice for one team kobsen 7 6,661 03-19-2012, 19:04
Last Post: Lemon
  [Release] BF1942 Class Selection Menu Style Yamato 14 12,380 12-30-2011, 18:01
Last Post: GscGunner
  [Tutorial] MW3 Class Loadouts Yamato 2 3,732 11-30-2011, 18:02
Last Post: Pozzuh

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2026 Melroy van den Berg.