SDK/MrAG/Config.cs

239 lines
8.1 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 Clear(){
while (Busy) { };
Busy = true;
Data.Clear();
Busy = false;
}
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();
if (File.Exists(file)){
StreamReader reader = new StreamReader(File.OpenRead(file));
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;
}
string value = (string)(Data[section] as Hashtable)[name];
Busy = false;
return value == null ? def : value;
}
public static int Value_GetInt(string section, string name, int def){
int.TryParse(Value_Get(section, name), out def);
return def;
}
public static short Value_GetShort(string section, string name, short def){
short.TryParse(Value_Get(section, name), out def);
return def;
}
public static float Value_GetFloat(string section, string name, float def){
float.TryParse(Value_Get(section, name), out def);
return def;
}
public static double Value_GetDouble(string section, string name, double def){
double.TryParse(Value_Get(section, name), out def);
return def;
}
public static bool Value_GetBool(string section, string name, bool def){
return Value_Get(section, name, def.ToString()).ToLower() == "true";
}
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 Value_SetBool(string section, string name, bool value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetInt(string section, string name, int value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetShort(string section, string name, short value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetFloat(string section, string name, float value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetDouble(string section, string name, double value){ Value_Set(section, name, value.ToString()); }
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;
}
}
}