ItsMods

Full Version: CFGFile class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
CSHARP Code
  1. //Create a new CFGFile instance
  2. CFGFile myCFG = new CFGFile("players2\\server.cfg");
  3. //Read the rcon password from server.cfg
  4. string rcon_password = myCFG.ReadDvar("rcon_password");
  5. //Set the rcon password to 'bananas'
  6. myCFG.WriteDvar("rcon_password", "bananas", false);


The class:
CSHARP Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace ServerConfigurator
  8. {
  9. class CFGFile
  10. {
  11. string path;
  12. public CFGFile(string file)
  13. {
  14. path = file;
  15. }
  16.  
  17. public string ReadDvar(string dvar)
  18. {
  19. string[] lines = File.ReadAllLines(path);
  20. foreach (string line in lines)
  21. {
  22. try
  23. {
  24. string[] split = line.Split(new string[] { " " }, StringSplitOptions.None);
  25. if (split[1] == dvar)
  26. {
  27. string temp = "";
  28. if (split.Length > 3)
  29. {
  30. int i = -1;
  31. foreach (string f in split)
  32. {
  33. i++;
  34.  
  35. if (i == split.Length+1)
  36. temp += f;
  37. else if(i > 1)
  38. {
  39. temp += f;
  40. temp += " ";
  41. }
  42. }
  43. }
  44. else temp = split[2];
  45.  
  46. if (temp.StartsWith("\""))
  47. return temp.Replace("\"", "");
  48. else return temp;
  49. }
  50. }
  51. catch { }
  52. }
  53. NotFound = true;
  54. return "";
  55. }
  56. public bool NotFound = false;
  57. public void WriteDvar(string dvar, string value, bool integer)
  58. {
  59. ReadDvar(dvar);
  60. if (NotFound)
  61. {
  62. StreamWriter sw = new StreamWriter(path, true);
  63. if (!integer) sw.WriteLine(string.Format("seta {0} \"{1}\"", dvar, value));
  64. else sw.WriteLine(string.Format("seta {0} {1}", dvar, value));
  65. sw.Close();
  66. }
  67. else
  68. {
  69. string[] lines = File.ReadAllLines(path);
  70. int i = -1;
  71. foreach (string line in lines)
  72. {
  73. i++;
  74. try
  75. {
  76. string[] split = line.Split(new string[] { " " }, StringSplitOptions.None);
  77. if (split[1] == dvar || "\""+split[1]+"\"" == dvar)
  78. {
  79. if (integer) split[2] = value;
  80. else split[2] = "\"" + value + "\"";
  81.  
  82. lines[i] = string.Format("{0} {1} {2}", split[0], split[1], split[2]);
  83. File.Delete(path);
  84. StreamWriter sw = new StreamWriter(path);
  85. foreach (string l33t in lines)
  86. {
  87. sw.WriteLine(l33t);
  88. }
  89. sw.Close();
  90. break;
  91. }
  92. }
  93. catch
  94. {
  95. }
  96. }
  97. }
  98.  
  99. NotFound = false;
  100. }
  101. }
  102. }
rcon password "bananas" Big Grin
What happens when 2 plugins write to the config at the same time?
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
(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
(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
@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
(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