Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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:
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. }
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
  [Release] TeamCounter class 8q4s8 3 5,254 10-15-2013, 18:14
Last Post: [HARD] Tony.
  W@W: Making player spawn with a custom default class. koil 9 5,438 01-13-2013, 12:10
Last Post: koil
  [Request] Disabling Custom Classes NOT CHOOSE CLASS DidUknowiPwn 5 3,701 01-10-2013, 04:32
Last Post: banz
  How to disable "Choose Class"? DidUknowiPwn 8 5,576 10-07-2012, 21:09
Last Post: JariZ
  [News] Black Ops 2 - Create A Class(LEAK) Tomsen1410 5 4,082 09-20-2012, 09:51
Last Post: Tomsen1410
  Mage class Lemon 23 12,809 04-22-2012, 22:46
Last Post: frisky
  Weapon Menu, create ingame custom class Gamemaster20 4 3,849 04-20-2012, 08:13
Last Post: rotceh_dnih
  Class Plugin pollarpart 5 4,299 04-16-2012, 13:40
Last Post: OzonE
  Disable Class Choice for one team kobsen 7 4,872 03-19-2012, 19:04
Last Post: Lemon
  [Release] BF1942 Class Selection Menu Style Yamato 14 9,084 12-30-2011, 18:01
Last Post: GscGunner

Forum Jump:


Users browsing this thread:
1 Guest(s)

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