OSDN Git Service

refactored
[tdcgexplorer/tso2mqo.git] / Config.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.IO;\r
4 using System.Text;\r
5 using System.Xml.Serialization;\r
6 \r
7 namespace Tso2MqoGui\r
8 {\r
9     [XmlRoot("TsoMqoConfig")]\r
10     [Serializable]\r
11     public class Config\r
12     {\r
13         public struct KeyAndValue\r
14         {\r
15             [XmlAttribute("key")]   public string   Key;\r
16             [XmlAttribute("value")] public string   Value;\r
17 \r
18             public KeyAndValue(string k, string v)\r
19             {\r
20                 Key     = k;\r
21                 Value   = v;\r
22             }\r
23         }\r
24 \r
25         public static Config    Instance;\r
26 \r
27         public static string    AssemblyPath    { get { return Path.GetDirectoryName(Type.Assembly.Location); } }\r
28         public static string    ConfigFile      { get { return Path.Combine(AssemblyPath, "config.xml"); } }\r
29         public static Type      Type            { get { return typeof(Config); } }\r
30 \r
31         [XmlElement("object_bone_list")] public List<KeyAndValue>   object_bone_list    = new List<KeyAndValue>();\r
32         [XmlIgnore] public Dictionary<string, string>               object_bone_map     = new Dictionary<string,string>();\r
33 \r
34         static Config()\r
35         {\r
36             Load();\r
37         }\r
38 \r
39         public static void Load()\r
40         {\r
41             try\r
42             {\r
43                 using(FileStream fs= File.OpenRead(ConfigFile))\r
44                 {\r
45                     XmlSerializer   s   = new XmlSerializer(Type);\r
46                     Instance            = s.Deserialize(fs) as Config;\r
47                     Instance.AfterDeserialize();\r
48                 }\r
49             } catch(Exception e)\r
50             {\r
51                 System.Diagnostics.Debug.WriteLine(e.ToString());\r
52                 Instance                = new Config();\r
53             }\r
54         }\r
55 \r
56         public static void Save()\r
57         {\r
58             try\r
59             {\r
60                 using(FileStream fs= File.OpenWrite(ConfigFile))\r
61                 {\r
62                     fs.SetLength(0);\r
63                     XmlSerializer   s   = new XmlSerializer(Type);\r
64                     Instance.BeforeSerialize();\r
65                     s.Serialize(fs, Instance);\r
66                     fs.Flush();\r
67                 }\r
68             } catch(Exception e)\r
69             {\r
70                 System.Diagnostics.Debug.WriteLine(e.ToString());\r
71             }\r
72         }\r
73 \r
74         public void BeforeSerialize()\r
75         {\r
76             object_bone_list.Clear();\r
77 \r
78             foreach(string i in object_bone_map.Keys)\r
79                 object_bone_list.Add(new KeyAndValue(i, object_bone_map[i]));\r
80         }\r
81 \r
82         public void AfterDeserialize()\r
83         {\r
84             object_bone_map.Clear();\r
85 \r
86             foreach(KeyAndValue i in object_bone_list)\r
87                 object_bone_map.Add(i.Key, i.Value);\r
88         }\r
89     }\r
90 }\r