DirectOuput Framework R2
DirectOutput framework R2 for virtual pinball cabinets
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
Cabinet.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Text;
5 using System.Xml.Serialization;
6 using DirectOutput.Cab.Out;
7 using DirectOutput.General;
8 using DirectOutput.General.Color;
9 
10 
11 namespace DirectOutput.Cab
12 {
16  public class Cabinet
17  {
21  public void AutoConfig()
22  {
23  Log.Write("Cabinet auto configuration started");
24 
25  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IAutoConfigOutputController).IsAssignableFrom(p) && !p.IsAbstract));
26  foreach (Type T in Types)
27  {
28 
29  try
30  {
31  IAutoConfigOutputController AutoConfig = (IAutoConfigOutputController)Activator.CreateInstance(T);
32  AutoConfig.AutoConfig(this);
33 
34  }
35  catch (Exception E)
36  {
37  Log.Exception("A exception occured during auto configuration for output controller(s) of type {0}.".Build(T.Name), E);
38  }
39  }
40 
41 
42 
43  Log.Write("Cabinet auto configuration finished");
44  }
45 
46 
47 
48  #region Properties
49 
50 
51 
52 
53 
54 
55  [XmlIgnore]
56  public Pinball Pinball { get; private set; }
57 
61 
62  public string Name { get; set; }
63 
70  [XmlIgnoreAttribute]
71  public string CabinetConfigurationFilename { get; set; }
72 
73  private Toys.ToyList _Toys;
74 
78 
79  public DirectOutput.Cab.Toys.ToyList Toys
80  {
81  get { return _Toys; }
82  set { _Toys = value; }
83 
84  }
85 
86 
87 
88 
89 
90 
91  private ColorList _Colors = new ColorList();
92 
96 
97  public ColorList Colors
98  {
99  get { return _Colors; }
100  set { _Colors = value; }
101  }
102 
103  private CurveList _Curves=new CurveList();
104 
108 
109  public CurveList Curves
110  {
111  get { return _Curves; }
112  set { _Curves = value; }
113  }
114 
115  private bool _AutoConfigEnabled=true;
123 
124  public bool AutoConfigEnabled
125  {
126  get { return _AutoConfigEnabled; }
127  set { _AutoConfigEnabled = value; }
128  }
129 
130 
131  private CabinetOutputList _Outputs;
135  [XmlIgnoreAttribute]
136  public CabinetOutputList Outputs
137  {
138  get
139  {
140  return _Outputs;
141 
142  }
143  }
144 
145  private Out.OutputControllerList _OutputControllers;
146 
150 
151  public Out.OutputControllerList OutputControllers
152  {
153  get { return _OutputControllers; }
154  set { _OutputControllers = value; }
155  }
156  #endregion
157 
158  #region Serialization
159 
164  public string GetConfigXml()
165  {
166  string Xml = "";
167  using (MemoryStream ms = new MemoryStream())
168  {
169  new XmlSerializer(typeof(Cabinet)).Serialize(ms, this);
170  ms.Position = 0;
171  using (StreamReader sr = new StreamReader(ms, Encoding.Default))
172  {
173  Xml = sr.ReadToEnd();
174  sr.Dispose();
175  }
176  }
177 
178  return Xml;
179  }
180 
181 
186  public void SaveConfigXmlFile(string FileName)
187  {
188  GetConfigXml().WriteToFile(FileName);
189  }
190 
191 
197  public static Cabinet GetCabinetFromConfigXmlFile(string FileName)
198  {
199  string Xml;
200  try
201  {
202  Xml = General.FileReader.ReadFileToString(FileName);
203  }
204  catch (Exception E)
205  {
206  Log.Exception("Could not load cabinet config from {0}.".Build(FileName), E);
207  throw new Exception("Could not read cabinet config file {0}.".Build(FileName), E);
208  }
209 
210  return GetCabinetFromConfigXml(Xml);
211  }
212 
213 
219  public static bool TestCabinetConfigXmlFile(string FileName)
220  {
221  Cabinet C = null;
222  try
223  {
224  C = GetCabinetFromConfigXmlFile(FileName);
225  }
226  catch
227  {
228  return false;
229  }
230  return C != null;
231 
232  }
233 
239  public static Cabinet GetCabinetFromConfigXmlFile(FileInfo CabinetConfigFile)
240  {
241  return GetCabinetFromConfigXmlFile(CabinetConfigFile.FullName);
242  }
243 
249  public static Cabinet GetCabinetFromConfigXml(string ConfigXml)
250  {
251  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
252  using (MemoryStream ms = new MemoryStream(xmlBytes))
253  {
254  try
255  {
256  return (Cabinet)new XmlSerializer(typeof(Cabinet)).Deserialize(ms);
257  }
258  catch (Exception E)
259  {
260 
261  Exception Ex = new Exception("Could not deserialize the cabinet config from XML data.", E);
262  Ex.Data.Add("XML Data", ConfigXml);
263  Log.Exception("Could not load cabinet config from XML data.", Ex);
264  throw Ex;
265  }
266  }
267  }
268  #endregion
269 
270 
275  public void Init(Pinball Pinball)
276  {
277  Log.Write("Initializing cabinet");
278  this.Pinball = Pinball;
279  OutputControllers.Init(this);
280  Toys.Init(this);
281 
282  Log.Write("Cabinet initialized");
283  }
284 
285 
289  public void Update()
290  {
291  Toys.UpdateOutputs();
292  OutputControllers.Update();
293  }
294 
295 
296 
300  public void Finish()
301  {
302  Log.Write("Finishing cabinet");
303 
304  Toys.Finish();
305  OutputControllers.Finish();
306  Log.Write("Cabinet finished");
307  }
308 
309 
310 
311  #region Constructor
312 
313 
314 
315  public Cabinet()
316  {
317  _OutputControllers = new Out.OutputControllerList();
318  _Outputs = new CabinetOutputList(this);
319  _Toys = new Toys.ToyList();
320  _Colors = new ColorList();
321  }
322  #endregion
323 
324  }
325 }