WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
AvailableToysInfo.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using DirectOutput.Cab.Toys;
10 using System.Xml.Serialization;
11 using System.IO;
12 using System.Reflection;
13 
14 namespace DirectOutput.Frontend
15 {
16  public partial class AvailableToysInfo : Form
17  {
18  private void UpdateAvailableTypes()
19  {
20  DataTable DT = new DataTable();
21  DT.Columns.Add("Toy type", typeof(string));
22  DT.Columns.Add("Source", typeof(string));
23  Assembly A = System.Reflection.Assembly.GetExecutingAssembly();
24 
25  foreach (Type T in AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IToy).IsAssignableFrom(p) && !p.IsAbstract))
26  {
27  DT.Rows.Add(T.Name, (A == T.Assembly ? "Built in" : "Scripted"));
28  }
29 
30  AvailableToys.ClearSelection();
31  AvailableToys.Columns.Clear();
32  AvailableToys.AutoGenerateColumns = true;
33  AvailableToys.DataSource = DT;
34  AvailableToys.Refresh();
35  AvailableToys.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
36 
37  UpdateTypeXml();
38  }
39 
40 
41  private void UpdateTypeXml()
42  {
43  string S = "";
44  if (AvailableToys.SelectedRows.Count > 0)
45  {
46  string N = AvailableToys.SelectedRows[0].Cells[0].Value.ToString();
47  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IToy).IsAssignableFrom(p) && !p.IsAbstract));
48  if (Types.Contains(N))
49  {
50  Type T = Types[N];
51 
52 
53  object O = Activator.CreateInstance(T);
54 
55 
56  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
57  {
58  if (PI.CanWrite)
59  {
60  if (PI.PropertyType == typeof(string) && PI.Name == "Name")
61  {
62  PI.SetValue(O, "Toy name", null);
63  }
64  else if (PI.PropertyType.IsNumber())
65  {
66  // PI.SetValue(O, 0, null);
67  }
68  else if (PI.PropertyType == typeof(bool))
69  {
70  PI.SetValue(O, false, null);
71  }
72  else if (PI.PropertyType == typeof(string) && PI.Name.ToLower().Contains("output"))
73  {
74  PI.SetValue(O, "Name of a output", null);
75  }
76  else if (PI.PropertyType == typeof(string))
77  {
78  string V = (string)PI.GetValue(O, null);
79  if (V.IsNullOrWhiteSpace())
80  {
81  PI.SetValue(O, "{0} value".Build(PI.Name), null);
82  }
83  }
84  else if (PI.PropertyType == typeof(DateTime))
85  {
86  PI.SetValue(O, DateTime.MaxValue, null);
87  }
88  }
89  }
90 
91  try
92  {
93  XmlSerializerNamespaces EmptyNamepsaces = new XmlSerializerNamespaces(new[] { System.Xml.XmlQualifiedName.Empty });
94  var Serializer = new XmlSerializer(T);
95  var Settings = new System.Xml.XmlWriterSettings();
96  Settings.Indent = true;
97  Settings.OmitXmlDeclaration = true;
98 
99 
100  using (var Stream = new StringWriter())
101  using (var Writer = System.Xml.XmlWriter.Create(Stream, Settings))
102  {
103  Serializer.Serialize(Writer, O, EmptyNamepsaces);
104  S = Stream.ToString();
105  }
106 
107  }
108  catch (Exception E)
109  {
110  S = "XML Serialization failed.\nException:\n{0}".Build(E.Message);
111  }
112 
113  }
114  else
115  {
116  S = "Type not found";
117  }
118 
119  }
120  TypeXml.Text = S;
121  }
123  {
124  InitializeComponent();
125  UpdateAvailableTypes();
126  }
127 
128  private void AvailableToys_SelectionChanged(object sender, EventArgs e)
129  {
130  UpdateTypeXml();
131  }
132 
133  private void CopyToClipboard_Click(object sender, EventArgs e)
134  {
135  if (!TypeXml.Text.IsNullOrWhiteSpace())
136  {
137  Clipboard.SetText(TypeXml.Text);
138  }
139  }
140 
141  }
142 }
The namespace DirectOutput.Cab.Toys contains all toy related classes.
Common interface for all toy implementations. The abstract class ToyBase implements this interface...
Definition: IToy.cs:9
The namespace DirectOutput.Cab contains all cabinet related classes like the Cabinet class itself...
Definition: Cab.cs:16