WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
Config.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Xml.Serialization;
6 using System.Reflection;
7 
8 namespace PinballX
9 {
10 
11 
12 
13  public class Config
14  {
15 
16  public bool EnableLogging { get; set; }
17 
18 
19 
20 
21 
22  #region Serialization
23 
24 
25  public static string ConfigFileName
26  {
27  get
28  {
29  FileInfo PluginAssemblyFileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
30  return PluginAssemblyFileInfo.FullName.Substring(0, PluginAssemblyFileInfo.FullName.Length - PluginAssemblyFileInfo.Extension.Length) + ".xml";
31  }
32  }
33 
38  public string GetConfigXml()
39  {
40  string Xml = "";
41  using (MemoryStream ms = new MemoryStream())
42  {
43  new XmlSerializer(typeof(Config)).Serialize(ms, this);
44  ms.Position = 0;
45  using (StreamReader sr = new StreamReader(ms, Encoding.Default))
46  {
47  Xml = sr.ReadToEnd();
48  sr.Dispose();
49  }
50  }
51 
52  return Xml;
53  }
54 
55 
60  public void SaveConfigXmlFile(string FileName=null)
61  {
62 
63  string C = GetConfigXml();
64 
65  TextWriter tw = null;
66  try
67  {
68  tw = new StreamWriter((FileName == null ? ConfigFileName : FileName), false);
69  tw.Write(C);
70  tw.Close();
71  }
72  catch (Exception e)
73  {
74 
75  if (tw != null)
76  {
77  tw.Close();
78  }
79  throw new Exception("Could not save PinballX DirectOutput Plugin config to " + (FileName == null ? ConfigFileName : FileName), e);
80  }
81 
82  tw = null;
83 
84  }
85 
86 
92  public static Config GetConfigFromXmlFile(string FileName=null)
93  {
94  string Xml;
95 
96  try
97  {
98  using (StreamReader streamReader = new StreamReader((FileName==null?ConfigFileName:FileName)))
99  {
100  Xml = streamReader.ReadToEnd();
101  streamReader.Close();
102  }
103  }
104  catch (Exception E)
105  {
106  throw new Exception("Could not read PinballX DirectOutput Plugin config file" + (FileName == null ? ConfigFileName : FileName), E);
107  }
108 
109  return GetConfigFromXml(Xml);
110  }
111 
112 
113 
119  public static Config GetConfigFromXml(string ConfigXml)
120  {
121  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
122  using (MemoryStream ms = new MemoryStream(xmlBytes))
123  {
124  try
125  {
126  return (Config)new XmlSerializer(typeof(Config)).Deserialize(ms);
127  }
128  catch (Exception E)
129  {
130 
131  Exception Ex = new Exception("Could not deserialize the PinballX DirectOutput Plugin config from XML data.", E);
132  Ex.Data.Add("XML Data", ConfigXml);
133 
134  throw Ex;
135  }
136  }
137  }
138  #endregion
139 
140 
141 
142  }
143 
144 }
string GetConfigXml()
Returns a serialized XML representation of the configuration.
Definition: Config.cs:38
bool EnableLogging
Definition: Config.cs:16
static Config GetConfigFromXml(string ConfigXml)
Instanciates a Cabinet object from a cabinet configuration in a XML string.
Definition: Config.cs:119
static Config GetConfigFromXmlFile(string FileName=null)
Instanciates a config object from a cabinet configuration in a XML file.
Definition: Config.cs:92
static string ConfigFileName
Definition: Config.cs:26
void SaveConfigXmlFile(string FileName=null)
Serializes the configuration to a XML file.
Definition: Config.cs:60