I had the same problem. I believe Dzienny had made XML examples in this forum... But I'm not sure.
Anyways I don't like XML, even thought I should learn it.
I have made my own config(I actually made 4 of sorts of configs, all of them are different, but I picked this one):
Don't expect much from it(read the first comment
). Feel free to edit it.
There are multiple types of get():
- Code: Select all
object get(string key) // Will check if it exists, returns it as a bool/string/object/float/int
void get(string key, int/string/float/bool defaultValue, ref thing) // checks it and will always return: if it doesn't exist, the default value will get set to the key and the default value will be passed to ref thing.
- Code: Select all
using System;
using System.IO;
namespace Config
{
// This is just awefull. Written in one night. But it works well!
class JConfig
{
public string path;
public string[] lines;
public string[] newFileLines;
public JConfig(string config, string[] startingLines)
{
this.path = Directory.GetCurrentDirectory() + "\\" + config;
this.newFileLines = startingLines;
checkFile();
initLines();
}
private void initLines()
{
using (StreamReader read = new StreamReader(path))
{
this.lines = read.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
}
}
// get("player", "player1", ref player);
#region get
public object get(string key)
{
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("//")) continue; // Not really needed, but...
string[] split = lines[i].Split('=');
if (split[0] == key && split.Length > 1)
{
if (split[1] == "true") return true;
else if (split[1] == "false") return false;
int num;
bool isNum = int.TryParse(split[1], out num);
if (isNum) return num;
float f;
bool isFloat = float.TryParse(split[1], out f);
if (isFloat) return f;
return split[1];
}
}
return null; // NULL NULL NULL NULL
}
public void get(string key, object defaultValue, ref object obj)
{
var thing = get(key);
if (thing == null)
{
set(key, defaultValue);
obj = defaultValue;
}
else if (thing.GetType() == defaultValue.GetType()) obj = thing;
else obj = defaultValue; // Computer will never come here
}
public void get(string key, int defaultValue, ref int obj)
{
object b = (object)obj;
get(key, defaultValue, ref b);
obj = (int)b;
}
public void get(string key, string defaultValue, ref string obj)
{
object b = (object)obj;
get(key, defaultValue, ref b);
obj = (string)b;
}
public void get(string key, bool defaultValue, ref bool obj)
{
object b = (object)obj;
get(key, defaultValue, ref b);
obj = (bool)b;
}
public void get(string key, float defaultValue, ref float obj)
{
object b = (object)obj;
get(key, defaultValue, ref b);
obj = (float)b;
}
#endregion
public void set(string key, object value)
{
checkFile();
byte minus = 1;
if (lines.Length == 1)
{
lines = newFileLines;
minus = 0;
}
using (StreamWriter write = new StreamWriter(path))
{
for (int i = 0; i < lines.Length - minus; i++) write.WriteLine(lines[i]);
write.WriteLine(key.ToString() + "=" + value.ToString());
}
initLines();
}
public void checkFile()
{
if (!File.Exists(path)) using (File.Create(path)) { }
}
}
}