DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
ScriptInfo.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using DirectOutput.Scripting;
10 using DirectOutput.FX;
11 using DirectOutput.Cab.Toys;
12 using System.IO;
13 using System.Xml.Serialization;
14 using System.Reflection;
15 
16 namespace DirectOutput.Frontend
17 {
18  public partial class ScriptInfo : Form
19  {
20  private Pinball _Pinball = new Pinball();
21 
22  public Pinball Pinball
23  {
24  get { return _Pinball; }
25  set
26  {
27  _Pinball = value;
28  UpdateLoadedScripts();
29  }
30  }
31 
32 
33  private void UpdateScriptTypes()
34  {
35  DataTable DT = new DataTable();
36  DT.Columns.Add("Type", typeof(string));
37  DT.Columns.Add("Interface", typeof(string));
38 
39  if (LoadedScripts.SelectedRows.Count > 0)
40  {
41  string N = LoadedScripts.SelectedRows[0].Cells[0].Value.ToString();
42 
43  if (Pinball.Scripts.Any(sc => sc.File.FullName == N))
44  {
45  Script S = Pinball.Scripts[N];
46 
47  if (S.Assembly != null)
48  {
49  foreach (Type T in S.Assembly.GetTypes().Where(t => typeof(IEffect).IsAssignableFrom(t) && !t.IsAbstract))
50  {
51  DT.Rows.Add(T.Name, "IEffect");
52  }
53 
54  foreach (Type T in S.Assembly.GetTypes().Where(t => typeof(IToy).IsAssignableFrom(t) && !t.IsAbstract))
55  {
56  DT.Rows.Add(T.Name, "IToy");
57  }
58  }
59  }
60  }
61 
62 
63 
64 
65  ScriptTypes.ClearSelection();
66  ScriptTypes.Columns.Clear();
67  ScriptTypes.AutoGenerateColumns = true;
68  ScriptTypes.DataSource = DT;
69  ScriptTypes.Refresh();
70  ScriptTypes.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
71 
72  UpdateTypeXml();
73  }
74 
75 
76  private void UpdateLoadedScripts()
77  {
78  DataTable DT = new DataTable();
79  DT.Columns.Add("Filename", typeof(string));
80  DT.Columns.Add("Compilation Result", typeof(string));
81  DT.Columns.Add("Effect count", typeof(int));
82  DT.Columns.Add("Toy count", typeof(int));
83 
84  foreach (Script S in Pinball.Scripts)
85  {
86  int ToyCnt = 0;
87  int EffectCnt = 0;
88  string CompilationResult = "";
89  if (S.Compiled)
90  {
91  EffectCnt = S.Assembly.GetTypes().Count(p => typeof(IEffect).IsAssignableFrom(p) && !p.IsAbstract);
92  ToyCnt = S.Assembly.GetTypes().Count(p => typeof(IToy).IsAssignableFrom(p) && !p.IsAbstract);
93  CompilationResult = "OK";
94  }
95  else
96  {
97  if (S.CompilationException != null)
98  {
99  CompilationResult = S.CompilationException.Message;
100  }
101  else
102  {
103  CompilationResult = "Not compiled";
104  }
105  }
106  DT.Rows.Add(S.File.FullName, CompilationResult, EffectCnt, ToyCnt);
107  }
108 
109  LoadedScripts.ClearSelection();
110  LoadedScripts.Columns.Clear();
111  LoadedScripts.AutoGenerateColumns = true;
112  LoadedScripts.DataSource = DT;
113  LoadedScripts.Refresh();
114  LoadedScripts.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
115 
116  UpdateScriptTypes();
117  }
118 
119 
120 
121 
122  private void UpdateTypeXml()
123  {
124  string S = "";
125  if (ScriptTypes.SelectedRows.Count > 0)
126  {
127  string N = ScriptTypes.SelectedRows[0].Cells[0].Value.ToString();
128  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => (typeof(IEffect).IsAssignableFrom(p) || typeof(IToy).IsAssignableFrom(p)) && !p.IsAbstract));
129  if (Types.Contains(N))
130  {
131  Type T = Types[N];
132  object O = Activator.CreateInstance(T);
133 
134 
135  foreach (PropertyInfo PI in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
136  {
137  if (PI.CanWrite)
138  {
139  if (PI.PropertyType == typeof(string) && PI.Name == "Name")
140  {
141  if (typeof(IEffect).IsAssignableFrom(T))
142  {
143  PI.SetValue(O, "Effect Name", null);
144  }
145  else if (typeof(IToy).IsAssignableFrom(T))
146  {
147  PI.SetValue(O, "Toy Name", null);
148  }
149  else
150  {
151  PI.SetValue(O, "Name", null);
152  }
153  }
154  else if (PI.PropertyType.IsNumber())
155  {
156 
157  }
158  else if (PI.PropertyType == typeof(bool))
159  {
160  PI.SetValue(O, false, null);
161  }
162  else if (PI.PropertyType == typeof(string) && PI.Name.ToLower().Contains("output"))
163  {
164  PI.SetValue(O, "Name of a output", null);
165  }
166  else if (PI.PropertyType == typeof(string) && PI.Name.ToLower().Contains("toy"))
167  {
168  PI.SetValue(O, "Name of a toy", null);
169  }
170  else if (PI.PropertyType == typeof(string))
171  {
172  string V = (string)PI.GetValue(O, null);
173  if (V.IsNullOrWhiteSpace())
174  {
175  PI.SetValue(O, "{0} value".Build(PI.Name), null);
176  }
177  }
178  else if (PI.PropertyType == typeof(DateTime))
179  {
180  PI.SetValue(O, DateTime.MaxValue, null);
181  }
182  }
183  }
184 
185  try
186  {
187 
188 
189 
190 
191  XmlSerializerNamespaces EmptyNamepsaces = new XmlSerializerNamespaces(new[] { System.Xml.XmlQualifiedName.Empty });
192  var Serializer = new XmlSerializer(T);
193  var Settings = new System.Xml.XmlWriterSettings();
194  Settings.Indent = true;
195  Settings.OmitXmlDeclaration = true;
196 
197  using (var Stream = new StringWriter())
198  using (var Writer = System.Xml.XmlWriter.Create(Stream, Settings))
199  {
200  Serializer.Serialize(Writer,O, EmptyNamepsaces);
201  S = Stream.ToString();
202  }
203 
204  }
205  catch (Exception E)
206  {
207  S = "XML Serialization failed.\nException:\n{0}".Build(E.Message);
208  }
209 
210  }
211  else
212  {
213  S = "Type not found";
214  }
215 
216  }
217  TypeXml.Text = S;
218  }
219 
220 
221 
223  {
224  InitializeComponent();
225  this.Pinball = Pinball;
226  }
227 
228  private void LoadedScripts_SelectionChanged(object sender, EventArgs e)
229  {
230  UpdateScriptTypes();
231  }
232 
233  private void ScriptTypes_SelectionChanged(object sender, EventArgs e)
234  {
235  UpdateTypeXml();
236  }
237 
238  private void CopyToClipboard_Click(object sender, EventArgs e)
239  {
240  if (!TypeXml.Text.IsNullOrWhiteSpace())
241  {
242  Clipboard.SetText(TypeXml.Text);
243  }
244  }
245 
246 
247  }
248 }