WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
ToyList.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 using System.Xml;
4 using System.Xml.Serialization;
6 
7 namespace DirectOutput.Cab.Toys
8 {
9 
13  public class ToyList : NamedItemList<IToy>, IXmlSerializable
14  {
15 
16  #region IXmlSerializable implementation
17  public void WriteXml(XmlWriter writer)
23  {
24 
25  XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces();
26  Namespaces.Add(string.Empty, string.Empty);
27  foreach (IToy T in this)
28  {
29  XmlSerializer serializer = new XmlSerializer(T.GetType());
30  serializer.Serialize(writer, T, Namespaces);
31  }
32  }
33 
34 
40  public void ReadXml(XmlReader reader)
41  {
42  if (reader.IsEmptyElement)
43  {
44  reader.ReadStartElement();
45  return;
46  }
47  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IToy).IsAssignableFrom(p) && !p.IsAbstract));
48 
49  reader.Read();
50 
51  while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
52  {
53  if (reader.NodeType == XmlNodeType.Element)
54  {
55  Type T = Types[reader.LocalName];
56 
57  if (T != null)
58  {
59  XmlSerializer serializer = new XmlSerializer(T);
60  IToy Toy = (IToy)serializer.Deserialize(reader);
61  if (!Contains(Toy.Name))
62  {
63  Add(Toy);
64  }
65  }
66  else
67  {
68  Log.Warning("Toy type {0} not found during deserialization of cabinet data.".Build(reader.LocalName));
69  reader.Skip();
70  }
71  }
72  else
73  {
74  //Not a xml element. Probably a comment. Skip.
75  reader.Skip();
76  }
77 
78  }
79  reader.ReadEndElement();
80  }
81 
82 
87  public System.Xml.Schema.XmlSchema GetSchema() { return (null); }
88  #endregion
89 
94  public void Init(Cabinet Cabinet)
95  {
96  foreach (IToy T in this)
97  {
98  T.Init(Cabinet);
99  }
100 
101  }
102 
106  public void Reset()
107  {
108  foreach (IToy T in this)
109  {
110  T.Reset();
111  }
112  }
113 
114 
115 
119  public void Finish()
120  {
121  foreach (IToy T in this)
122  {
123  T.Finish();
124  }
125  }
126 
127 
131  public void UpdateOutputs()
132  {
133  foreach (IToy T in this)
134  {
135  if (T is IToyUpdatable)
136  {
137  ((IToyUpdatable)T).UpdateOutputs();
138  }
139  }
140  }
141  }
142 }
void Finish()
Finishes all toys in the list.
Definition: ToyList.cs:119
void Reset()
Resets all toys in the list.
Definition: ToyList.cs:106
The Cabinet object describes the parts of a pinball cabinet (toys, outputcontrollers, outputs and more).
Definition: Cabinet.cs:17
void Reset()
Must reset the state of the IToy to its default state (off).
void Init(Cabinet Cabinet)
Must initialize the IToy.
void ReadXml(XmlReader reader)
Deserializes the IToy objects in the XmlReader The IToy objects are deserialized using the object nam...
Definition: ToyList.cs:40
Interface for toys which need a update method which is used to update their outputs.
Definition: IToyUpdatable.cs:7
static void Warning(string Message)
Writes a warning message to the log.
Definition: Log.cs:134
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
void Init(Cabinet Cabinet)
Initializes all IToy objects in the list
Definition: ToyList.cs:94
System.Xml.Schema.XmlSchema GetSchema()
Method is required by the IXmlSerializable interface
Definition: ToyList.cs:87
Common interface for all toy implementations. The abstract class ToyBase implements this interface...
Definition: IToy.cs:9
void Finish()
Must finish the IToy, do all necessary clean up work and reset the IToy to its default state (off)...
void UpdateOutputs()
Updates the outputs of all toys implementing the IToyUpdatable interface.
Definition: ToyList.cs:131
new string Name
Gets or sets the Name of the IToy.
Definition: IToy.cs:17
List of IToy objects
Definition: ToyList.cs:13
A list of uniquely named items which can be referenced by their name.
The namespace DirectOutput.General contains classes for general use.