OSDN Git Service

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