WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
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;
10 
11 
12 namespace DirectOutput.Cab
13 {
17  public class Cabinet
18  {
22  public void AutoConfig()
23  {
24  Log.Write("Cabinet auto configuration started");
25 
26  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IAutoConfigOutputController).IsAssignableFrom(p) && !p.IsAbstract));
27  foreach (Type T in Types)
28  {
29 
30  try
31  {
32  IAutoConfigOutputController AutoConfig = (IAutoConfigOutputController)Activator.CreateInstance(T);
33  AutoConfig.AutoConfig(this);
34 
35  }
36  catch (Exception E)
37  {
38  Log.Exception("A exception occured during auto configuration for output controller(s) of type {0}.".Build(T.Name), E);
39  }
40  }
41 
42 
43 
44  Log.Write("Cabinet auto configuration finished");
45  }
46 
47 
48 
49  #region Properties
50  //[XmlIgnore]
57  //public Pinball Pinball { get; private set; }
58 
59 
66  [XmlIgnore]
67  public ICabinetOwner Owner { get; set; }
68 
69 
70 
71 
78  [XmlIgnore]
79  public AlarmHandler Alarms
80  {
81  get { return Owner.Alarms; }
82  }
83 
84 
88 
89  public string Name { get; set; }
90 
97  [XmlIgnoreAttribute]
98  public string CabinetConfigurationFilename { get; set; }
99 
100  private Toys.ToyList _Toys;
101 
105 
106  public DirectOutput.Cab.Toys.ToyList Toys
107  {
108  get { return _Toys; }
109  set { _Toys = value; }
110 
111  }
112 
113 
114 
115 
116 
117 
118  private ColorList _Colors = new ColorList();
119 
123 
124  public ColorList Colors
125  {
126  get { return _Colors; }
127  set { _Colors = value; }
128  }
129 
130  private CurveList _Curves=new CurveList();
131 
135 
136  public CurveList Curves
137  {
138  get { return _Curves; }
139  set { _Curves = value; }
140  }
141 
142  private bool _AutoConfigEnabled=true;
150 
151  public bool AutoConfigEnabled
152  {
153  get { return _AutoConfigEnabled; }
154  set { _AutoConfigEnabled = value; }
155  }
156 
157 
158  private CabinetOutputList _Outputs;
162  [XmlIgnoreAttribute]
163  public CabinetOutputList Outputs
164  {
165  get
166  {
167  return _Outputs;
168 
169  }
170  }
171 
172  private Out.OutputControllerList _OutputControllers;
173 
177 
178  public Out.OutputControllerList OutputControllers
179  {
180  get { return _OutputControllers; }
181  set { _OutputControllers = value; }
182  }
183  #endregion
184 
185  #region Serialization
186 
191  public string GetConfigXml()
192  {
193  string Xml = "";
194  using (MemoryStream ms = new MemoryStream())
195  {
196  new XmlSerializer(typeof(Cabinet)).Serialize(ms, this);
197  ms.Position = 0;
198  using (StreamReader sr = new StreamReader(ms, Encoding.Default))
199  {
200  Xml = sr.ReadToEnd();
201  sr.Dispose();
202  }
203  }
204 
205  return Xml;
206  }
207 
208 
213  public void SaveConfigXmlFile(string FileName)
214  {
215  GetConfigXml().WriteToFile(FileName);
216  }
217 
218 
224  public static Cabinet GetCabinetFromConfigXmlFile(string FileName)
225  {
226  string Xml;
227  try
228  {
229  Xml = General.FileReader.ReadFileToString(FileName);
230  }
231  catch (Exception E)
232  {
233  Log.Exception("Could not load cabinet config from {0}.".Build(FileName), E);
234  throw new Exception("Could not read cabinet config file {0}.".Build(FileName), E);
235  }
236 
237  return GetCabinetFromConfigXml(Xml);
238  }
239 
240 
246  public static bool TestCabinetConfigXmlFile(string FileName)
247  {
248  Cabinet C = null;
249  try
250  {
251  C = GetCabinetFromConfigXmlFile(FileName);
252  }
253  catch
254  {
255  return false;
256  }
257  return C != null;
258 
259  }
260 
266  public static Cabinet GetCabinetFromConfigXmlFile(FileInfo CabinetConfigFile)
267  {
268  return GetCabinetFromConfigXmlFile(CabinetConfigFile.FullName);
269  }
270 
276  public static Cabinet GetCabinetFromConfigXml(string ConfigXml)
277  {
278  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
279  using (MemoryStream ms = new MemoryStream(xmlBytes))
280  {
281  try
282  {
283  return (Cabinet)new XmlSerializer(typeof(Cabinet)).Deserialize(ms);
284  }
285  catch (Exception E)
286  {
287 
288  Exception Ex = new Exception("Could not deserialize the cabinet config from XML data.", E);
289  Ex.Data.Add("XML Data", ConfigXml);
290  Log.Exception("Could not load cabinet config from XML data.", Ex);
291  throw Ex;
292  }
293  }
294  }
295  #endregion
296 
297 
303  {
304  Log.Write("Initializing cabinet");
305  this.Owner = CabinetOwner;
306  OutputControllers.Init(this);
307  Toys.Init(this);
308 
309  Log.Write("Cabinet initialized");
310  }
311 
312 
316  public void Update()
317  {
318  Toys.UpdateOutputs();
319  OutputControllers.Update();
320  }
321 
322 
323 
327  public void Finish()
328  {
329  Log.Write("Finishing cabinet");
330 
331  Toys.Finish();
332  OutputControllers.Finish();
333  Log.Write("Cabinet finished");
334  }
335 
336 
337 
338  #region Constructor
339  public Cabinet()
343  {
344  _OutputControllers = new Out.OutputControllerList();
345  _Outputs = new CabinetOutputList(this);
346  _Toys = new Toys.ToyList();
347  _Colors = new ColorList();
348  }
349  #endregion
350 
351  }
352 }
The AlarmHandler classed is used to execute scheduled events (e.g. regular updates on a effect) in th...
Definition: AlarmHandler.cs:14
The Cabinet object describes the parts of a pinball cabinet (toys, outputcontrollers, outputs and more).
Definition: Cabinet.cs:17
Interface for classes used to automatically detect and configure output controllers ...
The namespace DirectOutput.Cab.Toys contains all toy related classes.
static Cabinet GetCabinetFromConfigXmlFile(string FileName)
Instanciates a Cabinet object from a cabinet configuration in a XML file.
Definition: Cabinet.cs:224
static bool TestCabinetConfigXmlFile(string FileName)
Tests a cabinet config in a XML file.
Definition: Cabinet.cs:246
void SaveConfigXmlFile(string FileName)
Serializes the cabinet configuration to a XML file.
Definition: Cabinet.cs:213
void Update()
Calls the update method for toys and output controllers in the cabinet
Definition: Cabinet.cs:316
Readonly list containing all IOutput objects of all IOutputController objects in a cabinet...
static void Write(string Message)
Writes the specified message to the logfile.
Definition: Log.cs:99
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
The namespace DirectOutput.Cab contains all cabinet related classes like the Cabinet class itself...
Definition: Cab.cs:16
void Finish()
Finishes the cabinet
Definition: Cabinet.cs:327
DirectOutput.Cab.Out is the namespace for all output controller related classes like different output...
static Cabinet GetCabinetFromConfigXmlFile(FileInfo CabinetConfigFile)
Instanciates a Cabinet object from a cabinet configuration in a XML file.
Definition: Cabinet.cs:266
static Cabinet GetCabinetFromConfigXml(string ConfigXml)
Instanciates a Cabinet object from a cabinet configuration in a XML string.
Definition: Cabinet.cs:276
List for Color objects
Definition: ColorList.cs:12
void Init(ICabinetOwner CabinetOwner)
Initializes the cabinet.
Definition: Cabinet.cs:302
void AutoConfig()
This method finds all classes implementing the IAutoConfigOutputController interface and uses the mem...
Definition: Cabinet.cs:22
List of IToy objects
Definition: ToyList.cs:13
void AutoConfig(Cabinet Cabinet)
This method has to detect configure IOutputController object automatically.
The namespace DirectOutput.General contains classes for general use.
Support classes used by the Pinball object.
Definition: AlarmHandler.cs:6
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
Definition: Log.cs:144
string GetConfigXml()
Returns a serialized XML representation of the cabinet configuration.
Definition: Cabinet.cs:191