Advertisement
lonksys

C# Ini Reader

Aug 6th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.26 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Globalization;
  6. using System.Security.Cryptography;
  7. using System.Net;
  8. using System.IO;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Reflection;
  13. using System.Xml;
  14. using System.Xml.Serialization;
  15.  
  16. namespace Ini
  17. {
  18. // Token: 0x020005A7 RID: 1447
  19. public class Ini
  20. {
  21. // Token: 0x06002906 RID: 10502 RVA: 0x001AAEF3 File Offset: 0x001A92F3
  22. public Ini(string filename) : this(filename, true)
  23. {
  24.  
  25. }
  26.  
  27. // Token: 0x06002907 RID: 10503 RVA: 0x001AAF00 File Offset: 0x001A9300
  28. public Ini(string filename, bool readonload)
  29. {
  30. this.ci = new CultureInfo("en-US");
  31. this.dict = new StringDictionary();
  32. this.filename = filename;
  33. this.currentsection = null;
  34. if (readonload && this.FileExists())
  35. {
  36. this.ReadFromDisk();
  37. }
  38. else if (readonload)
  39. {
  40. throw new IniException("Ini file does not exist");
  41. }
  42. }
  43.  
  44. // Token: 0x06002908 RID: 10504 RVA: 0x001AAF6C File Offset: 0x001A936C
  45. public void WriteToDisk()
  46. {
  47. StreamWriter streamWriter;
  48. try
  49. {
  50. streamWriter = new StreamWriter(new FileStream(this.filename, FileMode.Create, FileAccess.Write));
  51. }
  52. catch (Exception e)
  53. {
  54. throw new IniException("Could not open file for writing: " + this.filename, e);
  55. }
  56. try
  57. {
  58. this.writeini(streamWriter);
  59. }
  60. catch (Exception e2)
  61. {
  62. throw new IniException("Could not write data to file: " + this.filename, e2);
  63. }
  64. streamWriter.Close();
  65. }
  66.  
  67. // Token: 0x06002909 RID: 10505 RVA: 0x001AAFF0 File Offset: 0x001A93F0
  68. public bool FileExists()
  69. {
  70. return File.Exists(this.filename);
  71. }
  72.  
  73. // Token: 0x0600290A RID: 10506 RVA: 0x001AAFFD File Offset: 0x001A93FD
  74. public void ChangeFileName(string newPath)
  75. {
  76. this.filename = newPath;
  77. }
  78.  
  79. // Token: 0x0600290B RID: 10507 RVA: 0x001AB008 File Offset: 0x001A9408
  80. public void ReadFromDisk()
  81. {
  82. this.dict.Clear();
  83. StreamReader streamReader;
  84. try
  85. {
  86. streamReader = new StreamReader(new FileStream(this.filename, FileMode.Open, FileAccess.Read));
  87. }
  88. catch (Exception e)
  89. {
  90. throw new IniException("ini file \"" + this.filename + "\" could not acccessed", e);
  91. }
  92. try
  93. {
  94. this.parseini(streamReader);
  95. }
  96. catch (Exception ex)
  97. {
  98. Console.WriteLine("Ini file parsed, but no entries found - corrupted ini file?" + ex.Message);
  99. }
  100. streamReader.Close();
  101. }
  102.  
  103. // Token: 0x0600290C RID: 10508 RVA: 0x001AB0A0 File Offset: 0x001A94A0
  104. public void AddSection(string sectionname)
  105. {
  106. if (!this.dict.ContainsKey(sectionname))
  107. {
  108. this.dict.Add(sectionname, null);
  109. this.currentsection = sectionname;
  110. }
  111. }
  112.  
  113. // Token: 0x0600290D RID: 10509 RVA: 0x001AB0CC File Offset: 0x001A94CC
  114. public void SetSection(string sectionname)
  115. {
  116. if (this.dict.ContainsKey(sectionname))
  117. {
  118. this.currentsection = sectionname;
  119. return;
  120. }
  121. throw new IniException("Section " + sectionname + " does not exist");
  122. }
  123.  
  124. // Token: 0x0600290E RID: 10510 RVA: 0x001AB101 File Offset: 0x001A9501
  125. public bool ContainsSection(string sectionname)
  126. {
  127. return this.dict.ContainsKey(sectionname);
  128. }
  129.  
  130. // Token: 0x0600290F RID: 10511 RVA: 0x001AB10F File Offset: 0x001A950F
  131. public bool ContainsKey(string keyname)
  132. {
  133. if (this.currentsection == null)
  134. {
  135. throw new IniException("No section set");
  136. }
  137. return this.dict.ContainsKey(this.currentsection + ":" + keyname);
  138. }
  139.  
  140. // Token: 0x06002910 RID: 10512 RVA: 0x001AB144 File Offset: 0x001A9544
  141. public int GetInteger(string key, int defaultvalue)
  142. {
  143. if (this.currentsection == null)
  144. {
  145. throw new IniException("No section set");
  146. }
  147. string key2 = this.currentsection + ":" + key;
  148. if (this.dict.ContainsKey(key2))
  149. {
  150. try
  151. {
  152. return Convert.ToInt32(this.dict[key2], this.ci);
  153. }
  154. catch (Exception e)
  155. {
  156. throw new IniException("Value not a valid Int32", e);
  157. }
  158. return defaultvalue;
  159. }
  160. return defaultvalue;
  161. }
  162.  
  163. // Token: 0x06002911 RID: 10513 RVA: 0x001AB1C8 File Offset: 0x001A95C8
  164. public void SetInteger(string key, int value)
  165. {
  166. if (this.currentsection == null)
  167. {
  168. throw new IniException("No section set");
  169. }
  170. string key2 = this.currentsection + ":" + key;
  171. if (this.dict.ContainsKey(key2))
  172. {
  173. this.dict[key2] = string.Empty + value;
  174. }
  175. else
  176. {
  177. this.dict.Add(key2, string.Empty + value);
  178. }
  179. }
  180.  
  181. // Token: 0x06002912 RID: 10514 RVA: 0x001AB24C File Offset: 0x001A964C
  182. public float GetFloat(string key, float defaultvalue)
  183. {
  184. if (this.currentsection == null)
  185. {
  186. throw new IniException("No section set");
  187. }
  188. string key2 = this.currentsection + ":" + key;
  189. if (this.dict.ContainsKey(key2))
  190. {
  191. try
  192. {
  193. return (float)Convert.ToDouble(this.dict[key2], this.ci);
  194. }
  195. catch (Exception e)
  196. {
  197. throw new IniException("Value not a valid float/double", e);
  198. }
  199. return defaultvalue;
  200. }
  201. return defaultvalue;
  202. }
  203.  
  204. // Token: 0x06002913 RID: 10515 RVA: 0x001AB2D0 File Offset: 0x001A96D0
  205. public void SetFloat(string key, float value)
  206. {
  207. if (this.currentsection == null)
  208. {
  209. throw new IniException("No section set");
  210. }
  211. string key2 = this.currentsection + ":" + key;
  212. if (this.dict.ContainsKey(key2))
  213. {
  214. this.dict[key2] = string.Empty + value;
  215. }
  216. else
  217. {
  218. this.dict.Add(key2, string.Empty + value);
  219. }
  220. }
  221.  
  222. // Token: 0x06002914 RID: 10516 RVA: 0x001AB354 File Offset: 0x001A9754
  223. public double GetDouble(string key, double defaultvalue)
  224. {
  225. if (this.currentsection == null)
  226. {
  227. throw new IniException("No section set");
  228. }
  229. string key2 = this.currentsection + ":" + key;
  230. if (this.dict.ContainsKey(key2))
  231. {
  232. try
  233. {
  234. return Convert.ToDouble(this.dict[key2], this.ci);
  235. }
  236. catch (Exception e)
  237. {
  238. throw new IniException("Value not a valid float/double", e);
  239. }
  240. //return defaultvalue;
  241. }
  242. return defaultvalue;
  243. }
  244.  
  245. // Token: 0x06002915 RID: 10517 RVA: 0x001AB3D8 File Offset: 0x001A97D8
  246. public void SetDouble(string key, double value)
  247. {
  248. if (this.currentsection == null)
  249. {
  250. throw new IniException("No section set");
  251. }
  252. string key2 = this.currentsection + ":" + key;
  253. if (this.dict.ContainsKey(key2))
  254. {
  255. this.dict[key2] = string.Empty + value;
  256. }
  257. else
  258. {
  259. this.dict.Add(key2, string.Empty + value);
  260. }
  261. }
  262.  
  263. // Token: 0x06002916 RID: 10518 RVA: 0x001AB45C File Offset: 0x001A985C
  264. public string GetString(string key, string defaultvalue)
  265. {
  266. if (this.currentsection == null)
  267. {
  268. throw new IniException("No section set");
  269. }
  270. string key2 = this.currentsection + ":" + key;
  271. if (this.dict.ContainsKey(key2))
  272. {
  273. try
  274. {
  275. return this.dict[key2];
  276. }
  277. catch (Exception e)
  278. {
  279. throw new IniException("Value not a valid String", e);
  280. }
  281. //return defaultvalue;
  282. }
  283. return defaultvalue;
  284. }
  285.  
  286. // Token: 0x06002917 RID: 10519 RVA: 0x001AB4D4 File Offset: 0x001A98D4
  287. public void SetString(string key, string value)
  288. {
  289. if (this.currentsection == null)
  290. {
  291. throw new IniException("No section set");
  292. }
  293. string key2 = this.currentsection + ":" + key;
  294. if (this.dict.ContainsKey(key2))
  295. {
  296. this.dict[key2] = string.Empty + value;
  297. }
  298. else
  299. {
  300. this.dict.Add(key2, string.Empty + value);
  301. }
  302. }
  303.  
  304. // Token: 0x06002918 RID: 10520 RVA: 0x001AB550 File Offset: 0x001A9950
  305. public bool GetBoolean(string key, bool defaultvalue)
  306. {
  307. if (this.currentsection == null)
  308. {
  309. throw new IniException("No section set");
  310. }
  311. string key2 = this.currentsection + ":" + key;
  312. if (this.dict.ContainsKey(key2))
  313. {
  314. try
  315. {
  316. string text = this.dict[key2].ToLower();
  317. return text.Equals("true") || text.Equals("on") || text.Equals("yes");
  318. }
  319. catch (Exception e)
  320. {
  321. throw new IniException("Value not a valid boolean", e);
  322. }
  323. //return defaultvalue;
  324. }
  325. return defaultvalue;
  326. }
  327.  
  328. // Token: 0x06002919 RID: 10521 RVA: 0x001AB5FC File Offset: 0x001A99FC
  329. public void SetBoolean(string key, bool value)
  330. {
  331. if (this.currentsection == null)
  332. {
  333. throw new IniException("No section set");
  334. }
  335. string key2 = this.currentsection + ":" + key;
  336. if (this.dict.ContainsKey(key2))
  337. {
  338. if (value)
  339. {
  340. this.dict[key2] = "true";
  341. }
  342. else
  343. {
  344. this.dict[key2] = "false";
  345. }
  346. }
  347. else if (value)
  348. {
  349. this.dict.Add(key2, "true");
  350. }
  351. else
  352. {
  353. this.dict.Add(key2, "false");
  354. }
  355. }
  356.  
  357. // Token: 0x0600291A RID: 10522 RVA: 0x001AB6A4 File Offset: 0x001A9AA4
  358. private int getsectioncount()
  359. {
  360. int num = 0;
  361. IEnumerator enumerator = this.dict.GetEnumerator();
  362. try
  363. {
  364. while (enumerator.MoveNext())
  365. {
  366. DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
  367.  
  368. string EntryKey = (string)dictionaryEntry.Key;
  369. if (dictionaryEntry.Value == null && !(EntryKey.Contains(":")))
  370. {
  371. num++;
  372. }
  373. }
  374. }
  375. finally
  376. {
  377. IDisposable disposable;
  378. if ((disposable = (enumerator as IDisposable)) != null)
  379. {
  380. disposable.Dispose();
  381. }
  382. }
  383. return num;
  384. }
  385.  
  386. // Token: 0x0600291B RID: 10523 RVA: 0x001AB730 File Offset: 0x001A9B30
  387. private string getsection(int index)
  388. {
  389. int num = 0;
  390. IEnumerator enumerator = this.dict.GetEnumerator();
  391. try
  392. {
  393. while (enumerator.MoveNext())
  394. {
  395. DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
  396. string EntryKey = (string)dictionaryEntry.Key;
  397. if (dictionaryEntry.Value == null && !(EntryKey.Contains(":")))
  398. {
  399. if (num == index)
  400. {
  401. return (string)dictionaryEntry.Key;
  402. }
  403. num++;
  404. }
  405. }
  406. }
  407. finally
  408. {
  409. IDisposable disposable;
  410. if ((disposable = (enumerator as IDisposable)) != null)
  411. {
  412. disposable.Dispose();
  413. }
  414. }
  415. throw new IniException("Index exceeds number of sections");
  416. }
  417.  
  418. // Token: 0x0600291C RID: 10524 RVA: 0x001AB7E0 File Offset: 0x001A9BE0
  419. private void parseini(StreamReader reader)
  420. {
  421. Regex regex = new Regex("^\\[([^\\]]+)\\]");
  422. Regex regex2 = new Regex("^([^;#]+?)=([^;#]*)");
  423. Regex regex3 = new Regex("^([^;#]+?)=\\\\s+'([^']+)'");
  424. Regex regex4 = new Regex("^([^;#]+?)=\\s+\"([^\"]+)\"");
  425. string text;
  426. while ((text = reader.ReadLine()) != null)
  427. {
  428. Console.WriteLine("[INI] text = " + text);
  429. text = text.Trim();
  430. if (text.StartsWith("["))
  431. {
  432. MatchCollection matchCollection = regex.Matches(text);
  433. GroupCollection groups = matchCollection[0].Groups;
  434. string sectionname = groups[1].Value.Trim();
  435. this.AddSection(sectionname);
  436. }
  437. else
  438. {
  439. MatchCollection matchCollection2 = regex2.Matches(text);
  440. if (matchCollection2.Count == 0)
  441. {
  442. matchCollection2 = regex3.Matches(text);
  443. if (matchCollection2.Count == 0)
  444. {
  445. matchCollection2 = regex4.Matches(text);
  446. }
  447. }
  448. if (matchCollection2.Count != 0)
  449. {
  450. if (this.currentsection == null)
  451. {
  452. throw new IniException("Corrupt ini file: entries defined without a section");
  453. }
  454. GroupCollection groups2 = matchCollection2[0].Groups;
  455. string str = groups2[1].Value.Trim();
  456. string value = groups2[2].Value.Trim();
  457. string key = this.currentsection + ":" + str;
  458. Console.WriteLine("[INI] str = " + str + " value = " + value + " key = " + key);
  459. if (!this.dict.ContainsKey(key))
  460. {
  461. this.dict.Add(key, value);
  462. }
  463. else
  464. {
  465. this.dict[key] = value;
  466. }
  467. }
  468. }
  469. }
  470. if (this.dict.Count == 0)
  471. {
  472. Console.WriteLine("Ini file parsed, but no entries found - corrupted ini file?");
  473. return;
  474. }
  475. }
  476.  
  477. // Token: 0x0600291D RID: 10525 RVA: 0x001AB970 File Offset: 0x001A9D70
  478. private void writeini(StreamWriter writer)
  479. {
  480. int num = this.getsectioncount();
  481. for (int i = 0; i < num; i++)
  482. {
  483. string text = this.getsection(i);
  484. writer.WriteLine("[{0}]", text);
  485. IEnumerator enumerator = this.dict.GetEnumerator();
  486. try
  487. {
  488. while (enumerator.MoveNext())
  489. {
  490. DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
  491. string text2 = (string)dictionaryEntry.Key;
  492. if (text2.StartsWith(text) && dictionaryEntry.Value != null)
  493. {
  494. string[] array = text2.Split(new char[]
  495. {
  496. ':'
  497. });
  498. writer.WriteLine("{0}={1}", array[1], (string)dictionaryEntry.Value);
  499. }
  500. }
  501. }
  502. finally
  503. {
  504. IDisposable disposable;
  505. if ((disposable = (enumerator as IDisposable)) != null)
  506. {
  507. disposable.Dispose();
  508. }
  509. }
  510. }
  511. }
  512.  
  513. // Token: 0x04003E66 RID: 15974
  514. private string filename;
  515.  
  516. // Token: 0x04003E67 RID: 15975
  517. private StringDictionary dict;
  518.  
  519. // Token: 0x04003E68 RID: 15976
  520. private string currentsection;
  521.  
  522. // Token: 0x04003E69 RID: 15977
  523. private CultureInfo ci;
  524. }
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement