2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
9 using DirectOutput.Scripting;
10 using DirectOutput.FX;
11 using DirectOutput.Cab.Toys;
13 using System.Xml.Serialization;
14 using System.Reflection;
16 namespace DirectOutput.Frontend
24 get {
return _Pinball; }
28 UpdateLoadedScripts();
33 private void UpdateScriptTypes()
35 DataTable DT =
new DataTable();
36 DT.Columns.Add(
"Type", typeof(
string));
37 DT.Columns.Add(
"Interface", typeof(
string));
39 if (LoadedScripts.SelectedRows.Count > 0)
41 string N = LoadedScripts.SelectedRows[0].Cells[0].Value.ToString();
49 foreach (Type T
in S.
Assembly.GetTypes().Where(t => typeof(
IEffect).IsAssignableFrom(t) && !t.IsAbstract))
51 DT.Rows.Add(T.Name,
"IEffect");
54 foreach (Type T
in S.
Assembly.GetTypes().Where(t => typeof(
IToy).IsAssignableFrom(t) && !t.IsAbstract))
56 DT.Rows.Add(T.Name,
"IToy");
65 ScriptTypes.ClearSelection();
66 ScriptTypes.Columns.Clear();
67 ScriptTypes.AutoGenerateColumns =
true;
68 ScriptTypes.DataSource = DT;
69 ScriptTypes.Refresh();
70 ScriptTypes.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
76 private void UpdateLoadedScripts()
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));
84 foreach (
Script S
in Pinball.Scripts)
88 string CompilationResult =
"";
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";
103 CompilationResult =
"Not compiled";
106 DT.Rows.Add(S.
File.FullName, CompilationResult, EffectCnt, ToyCnt);
109 LoadedScripts.ClearSelection();
110 LoadedScripts.Columns.Clear();
111 LoadedScripts.AutoGenerateColumns =
true;
112 LoadedScripts.DataSource = DT;
113 LoadedScripts.Refresh();
114 LoadedScripts.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
122 private void UpdateTypeXml()
125 if (ScriptTypes.SelectedRows.Count > 0)
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))
132 object O = Activator.CreateInstance(T);
135 foreach (PropertyInfo PI
in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
139 if (PI.PropertyType == typeof(
string) && PI.Name ==
"Name")
141 if (typeof(
IEffect).IsAssignableFrom(T))
143 PI.SetValue(O,
"Effect Name", null);
145 else if (typeof(
IToy).IsAssignableFrom(T))
147 PI.SetValue(O,
"Toy Name", null);
151 PI.SetValue(O,
"Name", null);
154 else if (PI.PropertyType.IsNumber())
158 else if (PI.PropertyType == typeof(
bool))
160 PI.SetValue(O,
false, null);
162 else if (PI.PropertyType == typeof(
string) && PI.Name.ToLower().Contains(
"output"))
164 PI.SetValue(O,
"Name of a output", null);
166 else if (PI.PropertyType == typeof(
string) && PI.Name.ToLower().Contains(
"toy"))
168 PI.SetValue(O,
"Name of a toy", null);
170 else if (PI.PropertyType == typeof(
string))
172 string V = (string)PI.GetValue(O, null);
173 if (V.IsNullOrWhiteSpace())
175 PI.SetValue(O,
"{0} value".Build(PI.Name), null);
178 else if (PI.PropertyType == typeof(DateTime))
180 PI.SetValue(O, DateTime.MaxValue, null);
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;
197 using (var Stream =
new StringWriter())
198 using (var Writer = System.Xml.XmlWriter.Create(Stream, Settings))
200 Serializer.Serialize(Writer,O, EmptyNamepsaces);
201 S = Stream.ToString();
207 S =
"XML Serialization failed.\nException:\n{0}".Build(E.Message);
213 S =
"Type not found";
224 InitializeComponent();
225 this.Pinball = Pinball;
228 private void LoadedScripts_SelectionChanged(
object sender, EventArgs e)
233 private void ScriptTypes_SelectionChanged(
object sender, EventArgs e)
238 private void CopyToClipboard_Click(
object sender, EventArgs e)
240 if (!TypeXml.Text.IsNullOrWhiteSpace())
242 Clipboard.SetText(TypeXml.Text);