/
Config.cs
74 lines (70 loc) · 2.13 KB
/
Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Xml.Serialization;
namespace MudkipzCraft
{
public class Config : XmlSerializer
{
const string CONFIG_FILE = "config.xml";
public string ListenIP { get; set; }
public int ListenPort { get; set; }
public bool OnlineMode { get; set; }
public int MaxPlayers { get; set; }
public bool SpawnAnimals { get; set; }
public bool SpawnMonsters { get; set; }
public Config()
{
ListenIP = "0.0.0.0";
ListenPort = 25565;
OnlineMode = false;
MaxPlayers = 16;
SpawnAnimals = true;
SpawnMonsters = true;
}
public Config Load()
{
if (File.Exists(CONFIG_FILE))
{
StreamReader sr = new StreamReader(CONFIG_FILE);
XmlTextReader xr = new XmlTextReader(sr);
XmlSerializer xs = new XmlSerializer(this.GetType());
object c;
if (xs.CanDeserialize(xr))
{
c = xs.Deserialize(xr);
Type t = this.GetType();
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo p in properties)
{
p.SetValue(this, p.GetValue(c, null), null);
}
}
xr.Close();
sr.Close();
}
else
{
Logger.Warn("Configuration file not found - loading defaults.");
Save();
}
return this;
}
public void Save()
{
try
{
StreamWriter w = new StreamWriter(CONFIG_FILE);
XmlSerializer s = new XmlSerializer(this.GetType());
s.Serialize(w, this);
w.Close();
}
catch
{
throw;
}
}
}
}