WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
DirectOutput.cs
Go to the documentation of this file.
1 
2 
3 
7 using System.IO;
8 using System.Reflection;
9 using System;
10 namespace DirectOutput
11 {
15  public static class DirectOutputHandler
16  {
17 
22  public static string GetVersion()
23  {
24  return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
25  }
26 
41  public static void UpdateTableElement(string TableElementTypeChar, int Number, int Value)
42  {
43  char C;
44  try
45  {
46  C = TableElementTypeChar[0];
47  }
48  catch (Exception E)
49  {
50 
51  throw new Exception("Could not extract the first char of the TableElementTypeChar parameter", E);
52  }
53  try
54  {
55  Pinball.ReceiveData(C, Number, Value);
56  }
57  catch (Exception E)
58  {
59  if (Pinball == null)
60  {
61  throw new Exception("You must call Init before passing data to the DirectOutput framework");
62  }
63  else
64  {
65  throw new Exception("A exception occured when passing in data (TableElementTypeChar: {0}, Number: {1}, Value: {2})".Build(C, Number, Value), E);
66  }
67  }
68  }
69 
70 
82  public static void UpdateNamedTableElement(string TableElementName, int Value)
83  {
84  try
85  {
86  Pinball.ReceiveData(TableElementName, Value);
87  }
88  catch (Exception E)
89  {
90  if (Pinball == null)
91  {
92  throw new Exception("You must call Init before passing data to the DirectOutput framework");
93  }
94  else
95  {
96  throw new Exception("A exception occured when passing in data (TableElementName: {0}, Value: {1})".Build(TableElementName, Value), E);
97  }
98  }
99 
100  }
101 
102 
103 
104 
109  public static void Finish()
110  {
111  if (Pinball != null)
112  {
113  Pinball.Finish();
114  Pinball = null;
115  }
116 
117  }
118 
128  public static void Init(string HostingApplicationName, string TableFilename, string RomName)
129  {
130  if (Pinball == null)
131  {
132  //Check config dir for global config file
133 
134  string HostAppFilename = HostingApplicationName.Replace(".", "");
135 
136  foreach (char C in Path.GetInvalidFileNameChars())
137  {
138  HostAppFilename = HostAppFilename.Replace(C.ToString(), "");
139  }
140 
141  foreach (char C in Path.GetInvalidPathChars())
142  {
143  HostAppFilename = HostAppFilename.Replace(C.ToString(), "");
144  }
145 
146 
147  HostAppFilename = "GlobalConfig_{0}".Build(HostAppFilename);
148 
149  //Check config dir for global config file
150  FileInfo F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "config", "GlobalConfig_{0}.xml".Build(HostAppFilename)));
151  if (!F.Exists)
152  {
153  //Check if a shortcut to the config dir exists
154  FileInfo LnkFile = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "config", "GlobalConfig_{0}.lnk".Build(HostAppFilename)));
155  if (LnkFile.Exists)
156  {
157  string ConfigDirPath = ResolveShortcut(LnkFile);
158  if (Directory.Exists(ConfigDirPath))
159  {
160  F = new FileInfo(Path.Combine(ConfigDirPath, "GlobalConfig_{0}.xml".Build(HostAppFilename)));
161  }
162  }
163  if (!F.Exists)
164  {
165 
166  //Check default dir for global config file
167  F = new FileInfo("GlobalConfig_{0}.xml".Build(HostAppFilename));
168  if (!F.Exists)
169  {
170  //Check dll dir for global config file
171  F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "GlobalConfig_{0}.xml".Build(HostAppFilename)));
172  if (!F.Exists)
173  {
174  //if global config file does not exist, set filename to config directory.
175  F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "config", "GlobalConfig_{0}.xml".Build(HostAppFilename)));
176  if (!F.Directory.Exists)
177  {
178  //If the config dir does not exist set the dll dir for the config
179  F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "GlobalConfig_{0}.xml".Build(HostAppFilename)));
180  }
181  }
182  }
183  }
184  }
185 
186 
187  Pinball = new DirectOutput.Pinball();
188  Pinball.Setup((F.Exists ? F.FullName : ""), TableFilename, RomName);
189  Pinball.Init();
190  }
191  else
192  {
193  throw new Exception("Object has already been initialized. You must call Finish() before initializing again.");
194  }
195  }
196 
197 
202  public static void ShowFrontend()
203  {
204  if (Pinball != null)
205  {
206  try
207  {
209  }
210  catch (Exception E)
211  {
212  System.Windows.Forms.MessageBox.Show("Could not show DirectOutput frontend.\n The following exception occured:\n{0}".Build(E.Message), "DirectOutput");
213  }
214 
215  }
216  else
217  {
218  throw new Exception("Init has to be called before the frontend is opend.");
219  }
220  }
221 
222 
223 
224 
225  private static string ResolveShortcut(FileInfo ShortcutFile)
226  {
227  string TargetPath = "";
228  try
229  {
230  Type WScriptShell = Type.GetTypeFromProgID("WScript.Shell");
231  object Shell = Activator.CreateInstance(WScriptShell);
232  object Shortcut = WScriptShell.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, Shell, new object[] { ShortcutFile.FullName });
233  TargetPath = (string)Shortcut.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, Shortcut, null);
234  Shortcut = null;
235  Shell = null;
236  }
237  catch
238  {
239 
240  }
241 
242  try
243  {
244  if (Directory.Exists(TargetPath))
245  {
246  return TargetPath;
247  }
248  else if (File.Exists(TargetPath))
249  {
250  return TargetPath;
251  }
252  else
253  {
254  return "";
255  }
256 
257  }
258  catch
259  {
260  return "";
261  }
262  }
263 
264  #region Properties
265 
266 
273  public static bool IsInitialized
274  {
275  get
276  {
277  return Pinball != null;
278  }
279  }
280 
281 
282  private static Pinball _Pinball;
283 
290  public static Pinball Pinball
291  {
292  get { return _Pinball; }
293  private set { _Pinball = value; }
294  }
295 
296  #endregion
297 
298  }
299 }
void Setup(string GlobalConfigFilename="", string TableFilename="", string RomName="")
Configures the Pinball object. Loads the global config, table config and cabinet config ...
Definition: Pinball.cs:104
Pinball is the main object of the DirectOutput framework. It holds all objects required to process P...
Definition: Pinball.cs:23
static void Open(Pinball Pinball, Form Owner=null)
Definition: MainMenu.cs:58