DirectOutputR1
DirectOutput framework R1 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.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml.Serialization;
6 using System.Reflection;
7 using System.IO;
8 using DirectOutput.FX;
9 using DirectOutput.Cab.Out.LW;
10 using DirectOutput.Cab.Toys;
11 using DirectOutput.Cab.Out;
12 using DirectOutput.Cab.Toys.LWEquivalent;
13 using DirectOutput.Cab.Color;
14 
15 
16 namespace DirectOutput.Cab
17 {
21  public class Cabinet
22  {
26  public void AutoConfig()
27  {
28  Log.Write("Cabinet auto configuration started");
29 
30  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IAutoConfigOutputController).IsAssignableFrom(p) && !p.IsAbstract));
31  foreach (Type T in Types)
32  {
33  IAutoConfigOutputController AutoConfig = (IAutoConfigOutputController)Activator.CreateInstance(T);
34  AutoConfig.AutoConfig(this);
35  }
36 
37 
38 
39  Log.Write("Cabinet auto configuration finished");
40  }
41 
42 
43 
44  #region Properties
45 
46 
47 
48 
49 
50 
51  [XmlIgnore]
52  public Pinball Pinball { get; private set; }
53 
57  [XmlElementAttribute(Order = 1)]
58  public string Name { get; set; }
59 
66  [XmlIgnoreAttribute]
67  public string CabinetConfigurationFilename { get; set; }
68 
69  private Toys.ToyList _Toys;
70 
74  [XmlElementAttribute(Order = 3)]
75  public DirectOutput.Cab.Toys.ToyList Toys
76  {
77  get { return _Toys; }
78  set { _Toys = value; }
79 
80  }
81 
82 
83 
84 
85 
86 
87  private DirectOutput.Cab.Color.ColorList _Colors;
88 
92  [XmlElementAttribute(Order = 4)]
93  public DirectOutput.Cab.Color.ColorList Colors
94  {
95  get { return _Colors; }
96  set { _Colors = value; }
97  }
98 
99 
100  private bool _AutoConfigEnabled=true;
108  [XmlElementAttribute(Order = 5)]
109  public bool AutoConfigEnabled
110  {
111  get { return _AutoConfigEnabled; }
112  set { _AutoConfigEnabled = value; }
113  }
114 
115 
116  private CabinetOutputList _Outputs;
120  [XmlIgnoreAttribute]
121  public CabinetOutputList Outputs
122  {
123  get
124  {
125  return _Outputs;
126 
127  }
128  }
129 
130  private Out.OutputControllerList _OutputControllers;
131 
135  [XmlElementAttribute(Order = 2)]
136  public Out.OutputControllerList OutputControllers
137  {
138  get { return _OutputControllers; }
139  set { _OutputControllers = value; }
140  }
141  #endregion
142 
143  #region Serialization
144 
149  public string GetConfigXml()
150  {
151  string Xml = "";
152  using (MemoryStream ms = new MemoryStream())
153  {
154  new XmlSerializer(typeof(Cabinet)).Serialize(ms, this);
155  ms.Position = 0;
156  using (StreamReader sr = new StreamReader(ms, Encoding.Default))
157  {
158  Xml = sr.ReadToEnd();
159  sr.Dispose();
160  }
161  }
162 
163  return Xml;
164  }
165 
166 
171  public void SaveConfigXmlFile(string FileName)
172  {
173  GetConfigXml().WriteToFile(FileName);
174  }
175 
176 
182  public static Cabinet GetCabinetFromConfigXmlFile(string FileName)
183  {
184  string Xml;
185  try
186  {
187  Xml = General.FileReader.ReadFileToString(FileName);
188  }
189  catch (Exception E)
190  {
191  Log.Exception("Could not load cabinet config from {0}.".Build(FileName), E);
192  throw new Exception("Could not read cabinet config file {0}.".Build(FileName), E);
193  }
194 
195  return GetCabinetFromConfigXml(Xml);
196  }
197 
198 
204  public static bool TestCabinetConfigXmlFile(string FileName)
205  {
206  Cabinet C = null;
207  try
208  {
209  C = GetCabinetFromConfigXmlFile(FileName);
210  }
211  catch
212  {
213  return false;
214  }
215  return C != null;
216 
217  }
218 
224  public static Cabinet GetCabinetFromConfigXmlFile(FileInfo CabinetConfigFile)
225  {
226  return GetCabinetFromConfigXmlFile(CabinetConfigFile.FullName);
227  }
228 
234  public static Cabinet GetCabinetFromConfigXml(string ConfigXml)
235  {
236  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
237  using (MemoryStream ms = new MemoryStream(xmlBytes))
238  {
239  try
240  {
241  return (Cabinet)new XmlSerializer(typeof(Cabinet)).Deserialize(ms);
242  }
243  catch (Exception E)
244  {
245 
246  Exception Ex = new Exception("Could not deserialize the cabinet config from XML data.", E);
247  Ex.Data.Add("XML Data", ConfigXml);
248  Log.Exception("Could not load cabinet config from XML data.", Ex);
249  throw Ex;
250  }
251  }
252  }
253  #endregion
254 
255 
260  public void Init(Pinball Pinball)
261  {
262  Log.Write("Initializing cabinet");
263  this.Pinball = Pinball;
264  OutputControllers.Init(this);
265  Toys.Init(this);
266 
267  Log.Write("Cabinet initialized");
268  }
269 
270 
274  public void Update()
275  {
276  Toys.UpdateOutputs();
277  OutputControllers.Update();
278  }
279 
280 
281 
285  public void Finish()
286  {
287  Log.Write("Finishing cabinet");
288 
289  Toys.Finish();
290  OutputControllers.Finish();
291  Log.Write("Cabinet finished");
292  }
293 
294 
295 
296  #region Constructor
297 
298 
299 
300  public Cabinet()
301  {
302  _OutputControllers = new Out.OutputControllerList();
303  _Outputs = new CabinetOutputList(this);
304  _Toys = new Toys.ToyList();
305  _Colors = new ColorList();
306  }
307  #endregion
308 
309  }
310 }