DirectOuput Framework R2
DirectOutput framework R2 for virtual pinball cabinets
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
TableElementConditionEffect.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using DirectOutput.Table;
6 using Ciloci.Flee;
7 using System.Xml.Serialization;
8 
9 namespace DirectOutput.FX.ConditionFX
10 {
15  {
16  private string _Condition;
17 
24  public string Condition
25  {
26  get { return _Condition; }
27  set { _Condition = value; }
28  }
29 
30  [XmlIgnore]
31  IGenericExpression<bool> ConditionExpression = null;
32 
33 
34  public List<string> GetVariables()
35  {
36  List<string> Variables = new List<string>();
37 
38  string C = Condition;
39  if (C.IsNullOrWhiteSpace()) return Variables;
40 
41  try
42  {
43 
44  int VariableStart = -1;
45  for (int i = 0; i < C.Length - 1; i++)
46  {
47  if (Enum.IsDefined(typeof(TableElementTypeEnum), (int)C[i]))
48  {
49  //Found a possible variable start letter
50  if (VariableStart >= 0)
51  {
52  //We're already inside a variable. A second letter is not allowed here.
53  VariableStart = -1;
54  }
55  else
56  {
57  //Register the start pos of the variable
58  VariableStart = i;
59  }
60  }
61  else if (VariableStart >= 0)
62  {
63  if (C.Substring(i, 1).IsInteger())
64  {
65  //Still inside the variable
66  }
67  else if (C.Substring(i, 1) == "-" && (i - VariableStart) == 1)
68  {
69  //Variable has a negative number
70  }
71  else if ((i - VariableStart) > 1 && C.Substring(VariableStart + 1, i - VariableStart - 1).IsInteger())
72  {
73  //Outside the variable and variable is ok
74  if (!Variables.Contains(C.Substring(VariableStart, i - VariableStart).ToUpper()))
75  {
76  Variables.Add(C.Substring(VariableStart, i - VariableStart).ToUpper());
77  }
78  VariableStart = -1;
79  }
80  else
81  {
82  //outside the variable and no valid variable spec found
83  VariableStart = -1;
84  }
85 
86 
87  }
88  else
89  {
90  //This is not variable content
91  VariableStart = -1;
92  }
93  }
94  }
95  catch (Exception E)
96  {
97  Log.Exception("A exception occured while trying to extract variable names from the condition {0}.".Build(Condition), E);
98  }
99 
100  return Variables;
101  }
102 
103 
104  private void InitCondition()
105  {
106  List<string> Variables = GetVariables();
107  string C = Condition;
108  ConditionExpression = null;
109 
110  if (C.IsNullOrWhiteSpace())
111  {
112  Log.Warning("No condition has been set for {0} named {1}.".Build(this.GetType().Name, Name));
113  return;
114  };
115 
116 
117  ExpressionContext Context = new ExpressionContext();
118  Context.Options.ParseCulture = System.Globalization.CultureInfo.InvariantCulture;
119  try
120  {
121  Context.Imports.AddType(typeof(Math));
122 
123  foreach (string V in Variables)
124  {
125  int P = 0;
126  while (P<C.Length && P>=0)
127  {
128  P = C.IndexOf(V, P + 1, StringComparison.OrdinalIgnoreCase);
129  if (P < 0) break;
130  if (!C.Substring(P, V.Length + 1).Equals(V + ".", StringComparison.OrdinalIgnoreCase))
131  {
132  C = C.Substring(0, P) + "{0}.Value".Build(V) + C.Substring(P + V.Length);
133  }
134  }
135 // C = C.Replace(V, "{0}.Value".Build(V), StringComparison.OrdinalIgnoreCase);
136 
137 
138  if (Table.TableElements.Contains((TableElementTypeEnum)V[0], V.Substring(1).ToInteger()))
139  {
140  Table.TableElements.UpdateState((TableElementTypeEnum)V[0], V.Substring(1).ToInteger(), 0);
141  }
142 
143 
144  TableElement TE = Table.TableElements[(TableElementTypeEnum)V[0], V.Substring(1).ToInteger()];
145 
146  Context.Variables[V] = TE;
147  }
148  }
149  catch (Exception E)
150  {
151  Log.Exception("A exception has occured while setting up the variables for condition {0} of effect {1}.".Build(Condition, Name), E);
152  return;
153 
154  }
155 
156  try
157  {
158  ConditionExpression = Context.CompileGeneric<bool>(C);
159 
160  }
161  catch (Exception E)
162  {
163  Log.Exception("A exception has occured while compiling the condition {0} (internaly translated to {2}) of effect {1}.".Build(Condition, Name, C), E);
164  return;
165  }
166 
167 
168 
169 
170  }
171 
172 
173 
178  public override void Trigger(TableElementData TableElementData)
179  {
180  if (ConditionExpression != null)
181  {
182  try
183  {
184  if (ConditionExpression.Evaluate())
185  {
186  TableElementData.Value = 255;
187  }
188  else
189  {
190  TableElementData.Value = 0;
191  }
192  TriggerTargetEffect(TableElementData);
193  }
194  catch (Exception E)
195  {
196 
197  Log.Exception("A exception occured when evaluating the expression {0} of effect {1}. Effect will be deactivated.".Build(ConditionExpression.Text, Name), E);
198  ConditionExpression = null;
199 
200  }
201  }
202  }
203 
204 
205 
206 
207 
213  public override void Init(Table.Table Table)
214  {
215  base.Init(Table);
216  InitCondition();
217  }
218 
219 
224  public override void Finish()
225  {
226  ConditionExpression = null;
227  base.Finish();
228  }
229 
230 
231  }
232 }