Updater/GamerService Updater/Config.cs

201 lines
6.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MrAG
{
public class Config
{
private static Hashtable Data = new Hashtable();
private static bool Busy;
public static void New(){
Data.Clear();
}
public static void Write(string path){
while (Busy) { };
Busy = true;
StreamWriter writer = new StreamWriter(File.OpenWrite(path));
foreach( DictionaryEntry cat in Data)
{
writer.WriteLine("[\"" + cat.Key + "\"] = {");
Hashtable values = (Hashtable)Data[cat.Key];
foreach( DictionaryEntry entry in values)
{
writer.WriteLine(" [\"" + entry.Key + "\"] = \"" + entry.Value + "\";");
}
writer.WriteLine("}");
writer.WriteLine("");
}
Busy = false;
writer.Close();
}
public static bool Read(string file){
while (Busy) { };
Busy = true;
Data.Clear();
string realfile = Directory.GetCurrentDirectory() + "/" + file;
if (File.Exists(realfile)){
StreamReader reader = new StreamReader(File.OpenRead(realfile));
string[] content = reader.ReadToEnd().Replace("\r", "").Split('\n');
reader.Close();
int entrys = 0;
string CurrentSection = "";
string CurrentName = "";
foreach(string line in content){
if (line.Length > 0){
if (line.Replace(" ","").Replace(" ", "").StartsWith("#")){ // comment
}else if (line.StartsWith("[\"")){ // section
CurrentSection = line.Substring(2, line.Length - 8);
Data[CurrentSection] = new Hashtable();
}else if (line == "}"){ // end-section
CurrentSection = "";
}else if (line.StartsWith(" [\"") || line.StartsWith(" [\"")){ // name/value
int splitpos = line.IndexOf("\"] = \"");
if (line.EndsWith("\";")){
entrys++;
(Data[CurrentSection] as Hashtable)[line.Substring(3, splitpos - 3)] = line.Substring(splitpos + 6, line.Length - splitpos - 8);
}else{
CurrentName = line.Substring(3, splitpos - 3);
(Data[CurrentSection] as Hashtable)[CurrentName] = line.Substring(splitpos + 6, line.Length - splitpos - 6);
}
}else{ // resume value on next line (multilined crap :3)
if (line.EndsWith("\";")){
(Data[CurrentSection] as Hashtable)[CurrentName] += "\n" + line.Substring(1, line.Length - 3);
CurrentName = "";
}else{
(Data[CurrentSection] as Hashtable)[CurrentName] += "\n" + line;
}
}
}
}
Console.WriteLine("Finished reading '" + file + "', " + entrys + " entrys");
Busy = false;
return true;
}
Busy = false;
return false;
}
public static bool IsBusy(){
return Busy;
}
public static bool Value_Exist(string section, string name){
while (Busy) { };
Busy = true;
if (Data[section] == null){
Busy = false;
return false;
}
if ((Data[section] as Hashtable)[name] == null){
Busy = false;
return false;
}
Busy = false;
return true;
}
public static Hashtable Section_Get(string name){
while (Busy) { };
Busy = true;
if (Data[name] == null) {
Busy = false;
return new Hashtable();
}
Busy = false;
return (Hashtable)Data[name];
}
public static bool Section_Exists(string name){
while (Busy) { };
return Data[name] != null;
}
public static void Section_Set(string name, Hashtable data){
while (Busy) { };
Busy = true;
Data[name] = data;
Busy = false;
}
public static string Value_Get(string section, string name){
while (Busy) { };
Busy = true;
if (Data[section] == null) {
Busy = false;
return "";
}
Busy = false;
return (string)(Data[section] as Hashtable)[name];
}
public static string Value_Get(string section, string name, string def){
while (Busy) { };
Busy = true;
if (Data[section] == null) {
Busy = false;
return def;
}
Busy = false;
return (string)(Data[section] as Hashtable)[name];
}
public static void Value_Set(string section, string name, string value){
while (Busy) { };
Busy = true;
if (Data[section] == null)
Data[section] = new Hashtable();
(Data[section] as Hashtable)[name] = value;
Busy = false;
}
public static void PrintContent(){
while (Busy) { };
Busy = true;
foreach( DictionaryEntry cat in Data )
{
Console.WriteLine("[\"" + cat.Key + "\"] = {");
Hashtable values = (Hashtable)Data[cat.Key];
foreach( DictionaryEntry entry in values)
{
Console.WriteLine(" [\"" + entry.Key + "\"] = \"" + entry.Value + "\";");
}
Console.WriteLine("}");
Console.WriteLine("");
}
Busy = false;
}
}
}