public class PlayerInfo
{
public string Name { get; set; }
public string Title { get; set; }
}
static readonly string TitlePath = "title.xml";
...
// Create a PlayerInfo instance.
var pi = new PlayerInfo { Name = "LeeIzaZombie", Title = "Zombie" };
// Initialize XmlSerializer.
var s = new XmlSerializer(pi.GetType());
// Create a TitlePath file to store the serialized data.
using (var xw = XmlWriter.Create(TitlePath, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true }))
{
// Serialize to a TitlePath file.
s.Serialize(xw, pi);
}
// Create a variable to store retrieved data.
PlayeInfo retrieved = null;
// Open a TitlePath file.
using (var xr = XmlReader.Create(TitlePath))
{
// Deserialize a TitlePath file.
retrieved = (PlayerInfo)s.Deserialize(xr);
}
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.
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)) { }
}
}
}
Users browsing this forum: No registered users and 3 guests