Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Globalization;
- using System.Security.Cryptography;
- using System.Net;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Reflection;
- using System.Xml;
- using System.Xml.Serialization;
- namespace Ini
- {
- // Token: 0x020005A7 RID: 1447
- public class Ini
- {
- // Token: 0x06002906 RID: 10502 RVA: 0x001AAEF3 File Offset: 0x001A92F3
- public Ini(string filename) : this(filename, true)
- {
- }
- // Token: 0x06002907 RID: 10503 RVA: 0x001AAF00 File Offset: 0x001A9300
- public Ini(string filename, bool readonload)
- {
- this.ci = new CultureInfo("en-US");
- this.dict = new StringDictionary();
- this.filename = filename;
- this.currentsection = null;
- if (readonload && this.FileExists())
- {
- this.ReadFromDisk();
- }
- else if (readonload)
- {
- throw new IniException("Ini file does not exist");
- }
- }
- // Token: 0x06002908 RID: 10504 RVA: 0x001AAF6C File Offset: 0x001A936C
- public void WriteToDisk()
- {
- StreamWriter streamWriter;
- try
- {
- streamWriter = new StreamWriter(new FileStream(this.filename, FileMode.Create, FileAccess.Write));
- }
- catch (Exception e)
- {
- throw new IniException("Could not open file for writing: " + this.filename, e);
- }
- try
- {
- this.writeini(streamWriter);
- }
- catch (Exception e2)
- {
- throw new IniException("Could not write data to file: " + this.filename, e2);
- }
- streamWriter.Close();
- }
- // Token: 0x06002909 RID: 10505 RVA: 0x001AAFF0 File Offset: 0x001A93F0
- public bool FileExists()
- {
- return File.Exists(this.filename);
- }
- // Token: 0x0600290A RID: 10506 RVA: 0x001AAFFD File Offset: 0x001A93FD
- public void ChangeFileName(string newPath)
- {
- this.filename = newPath;
- }
- // Token: 0x0600290B RID: 10507 RVA: 0x001AB008 File Offset: 0x001A9408
- public void ReadFromDisk()
- {
- this.dict.Clear();
- StreamReader streamReader;
- try
- {
- streamReader = new StreamReader(new FileStream(this.filename, FileMode.Open, FileAccess.Read));
- }
- catch (Exception e)
- {
- throw new IniException("ini file \"" + this.filename + "\" could not acccessed", e);
- }
- try
- {
- this.parseini(streamReader);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Ini file parsed, but no entries found - corrupted ini file?" + ex.Message);
- }
- streamReader.Close();
- }
- // Token: 0x0600290C RID: 10508 RVA: 0x001AB0A0 File Offset: 0x001A94A0
- public void AddSection(string sectionname)
- {
- if (!this.dict.ContainsKey(sectionname))
- {
- this.dict.Add(sectionname, null);
- this.currentsection = sectionname;
- }
- }
- // Token: 0x0600290D RID: 10509 RVA: 0x001AB0CC File Offset: 0x001A94CC
- public void SetSection(string sectionname)
- {
- if (this.dict.ContainsKey(sectionname))
- {
- this.currentsection = sectionname;
- return;
- }
- throw new IniException("Section " + sectionname + " does not exist");
- }
- // Token: 0x0600290E RID: 10510 RVA: 0x001AB101 File Offset: 0x001A9501
- public bool ContainsSection(string sectionname)
- {
- return this.dict.ContainsKey(sectionname);
- }
- // Token: 0x0600290F RID: 10511 RVA: 0x001AB10F File Offset: 0x001A950F
- public bool ContainsKey(string keyname)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- return this.dict.ContainsKey(this.currentsection + ":" + keyname);
- }
- // Token: 0x06002910 RID: 10512 RVA: 0x001AB144 File Offset: 0x001A9544
- public int GetInteger(string key, int defaultvalue)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- try
- {
- return Convert.ToInt32(this.dict[key2], this.ci);
- }
- catch (Exception e)
- {
- throw new IniException("Value not a valid Int32", e);
- }
- return defaultvalue;
- }
- return defaultvalue;
- }
- // Token: 0x06002911 RID: 10513 RVA: 0x001AB1C8 File Offset: 0x001A95C8
- public void SetInteger(string key, int value)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- this.dict[key2] = string.Empty + value;
- }
- else
- {
- this.dict.Add(key2, string.Empty + value);
- }
- }
- // Token: 0x06002912 RID: 10514 RVA: 0x001AB24C File Offset: 0x001A964C
- public float GetFloat(string key, float defaultvalue)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- try
- {
- return (float)Convert.ToDouble(this.dict[key2], this.ci);
- }
- catch (Exception e)
- {
- throw new IniException("Value not a valid float/double", e);
- }
- return defaultvalue;
- }
- return defaultvalue;
- }
- // Token: 0x06002913 RID: 10515 RVA: 0x001AB2D0 File Offset: 0x001A96D0
- public void SetFloat(string key, float value)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- this.dict[key2] = string.Empty + value;
- }
- else
- {
- this.dict.Add(key2, string.Empty + value);
- }
- }
- // Token: 0x06002914 RID: 10516 RVA: 0x001AB354 File Offset: 0x001A9754
- public double GetDouble(string key, double defaultvalue)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- try
- {
- return Convert.ToDouble(this.dict[key2], this.ci);
- }
- catch (Exception e)
- {
- throw new IniException("Value not a valid float/double", e);
- }
- //return defaultvalue;
- }
- return defaultvalue;
- }
- // Token: 0x06002915 RID: 10517 RVA: 0x001AB3D8 File Offset: 0x001A97D8
- public void SetDouble(string key, double value)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- this.dict[key2] = string.Empty + value;
- }
- else
- {
- this.dict.Add(key2, string.Empty + value);
- }
- }
- // Token: 0x06002916 RID: 10518 RVA: 0x001AB45C File Offset: 0x001A985C
- public string GetString(string key, string defaultvalue)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- try
- {
- return this.dict[key2];
- }
- catch (Exception e)
- {
- throw new IniException("Value not a valid String", e);
- }
- //return defaultvalue;
- }
- return defaultvalue;
- }
- // Token: 0x06002917 RID: 10519 RVA: 0x001AB4D4 File Offset: 0x001A98D4
- public void SetString(string key, string value)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- this.dict[key2] = string.Empty + value;
- }
- else
- {
- this.dict.Add(key2, string.Empty + value);
- }
- }
- // Token: 0x06002918 RID: 10520 RVA: 0x001AB550 File Offset: 0x001A9950
- public bool GetBoolean(string key, bool defaultvalue)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- try
- {
- string text = this.dict[key2].ToLower();
- return text.Equals("true") || text.Equals("on") || text.Equals("yes");
- }
- catch (Exception e)
- {
- throw new IniException("Value not a valid boolean", e);
- }
- //return defaultvalue;
- }
- return defaultvalue;
- }
- // Token: 0x06002919 RID: 10521 RVA: 0x001AB5FC File Offset: 0x001A99FC
- public void SetBoolean(string key, bool value)
- {
- if (this.currentsection == null)
- {
- throw new IniException("No section set");
- }
- string key2 = this.currentsection + ":" + key;
- if (this.dict.ContainsKey(key2))
- {
- if (value)
- {
- this.dict[key2] = "true";
- }
- else
- {
- this.dict[key2] = "false";
- }
- }
- else if (value)
- {
- this.dict.Add(key2, "true");
- }
- else
- {
- this.dict.Add(key2, "false");
- }
- }
- // Token: 0x0600291A RID: 10522 RVA: 0x001AB6A4 File Offset: 0x001A9AA4
- private int getsectioncount()
- {
- int num = 0;
- IEnumerator enumerator = this.dict.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
- string EntryKey = (string)dictionaryEntry.Key;
- if (dictionaryEntry.Value == null && !(EntryKey.Contains(":")))
- {
- num++;
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- return num;
- }
- // Token: 0x0600291B RID: 10523 RVA: 0x001AB730 File Offset: 0x001A9B30
- private string getsection(int index)
- {
- int num = 0;
- IEnumerator enumerator = this.dict.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
- string EntryKey = (string)dictionaryEntry.Key;
- if (dictionaryEntry.Value == null && !(EntryKey.Contains(":")))
- {
- if (num == index)
- {
- return (string)dictionaryEntry.Key;
- }
- num++;
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- throw new IniException("Index exceeds number of sections");
- }
- // Token: 0x0600291C RID: 10524 RVA: 0x001AB7E0 File Offset: 0x001A9BE0
- private void parseini(StreamReader reader)
- {
- Regex regex = new Regex("^\\[([^\\]]+)\\]");
- Regex regex2 = new Regex("^([^;#]+?)=([^;#]*)");
- Regex regex3 = new Regex("^([^;#]+?)=\\\\s+'([^']+)'");
- Regex regex4 = new Regex("^([^;#]+?)=\\s+\"([^\"]+)\"");
- string text;
- while ((text = reader.ReadLine()) != null)
- {
- Console.WriteLine("[INI] text = " + text);
- text = text.Trim();
- if (text.StartsWith("["))
- {
- MatchCollection matchCollection = regex.Matches(text);
- GroupCollection groups = matchCollection[0].Groups;
- string sectionname = groups[1].Value.Trim();
- this.AddSection(sectionname);
- }
- else
- {
- MatchCollection matchCollection2 = regex2.Matches(text);
- if (matchCollection2.Count == 0)
- {
- matchCollection2 = regex3.Matches(text);
- if (matchCollection2.Count == 0)
- {
- matchCollection2 = regex4.Matches(text);
- }
- }
- if (matchCollection2.Count != 0)
- {
- if (this.currentsection == null)
- {
- throw new IniException("Corrupt ini file: entries defined without a section");
- }
- GroupCollection groups2 = matchCollection2[0].Groups;
- string str = groups2[1].Value.Trim();
- string value = groups2[2].Value.Trim();
- string key = this.currentsection + ":" + str;
- Console.WriteLine("[INI] str = " + str + " value = " + value + " key = " + key);
- if (!this.dict.ContainsKey(key))
- {
- this.dict.Add(key, value);
- }
- else
- {
- this.dict[key] = value;
- }
- }
- }
- }
- if (this.dict.Count == 0)
- {
- Console.WriteLine("Ini file parsed, but no entries found - corrupted ini file?");
- return;
- }
- }
- // Token: 0x0600291D RID: 10525 RVA: 0x001AB970 File Offset: 0x001A9D70
- private void writeini(StreamWriter writer)
- {
- int num = this.getsectioncount();
- for (int i = 0; i < num; i++)
- {
- string text = this.getsection(i);
- writer.WriteLine("[{0}]", text);
- IEnumerator enumerator = this.dict.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
- string text2 = (string)dictionaryEntry.Key;
- if (text2.StartsWith(text) && dictionaryEntry.Value != null)
- {
- string[] array = text2.Split(new char[]
- {
- ':'
- });
- writer.WriteLine("{0}={1}", array[1], (string)dictionaryEntry.Value);
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- }
- }
- // Token: 0x04003E66 RID: 15974
- private string filename;
- // Token: 0x04003E67 RID: 15975
- private StringDictionary dict;
- // Token: 0x04003E68 RID: 15976
- private string currentsection;
- // Token: 0x04003E69 RID: 15977
- private CultureInfo ci;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement