DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
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 DataReceive(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 
75  public static void Finish()
76  {
77  if (Pinball != null)
78  {
79  Pinball.Finish();
80  Pinball = null;
81  }
82 
83  }
84 
94  public static void Init(string HostingApplicationName, string TableFilename, string RomName)
95  {
96  if (Pinball == null)
97  {
98  //Check config dir for global config file
99 
100  string HostAppFilename = HostingApplicationName.Replace(".", "");
101 
102  foreach (char C in Path.GetInvalidFileNameChars())
103  {
104  HostAppFilename = HostAppFilename.Replace(C.ToString(), "");
105  }
106 
107  foreach (char C in Path.GetInvalidPathChars())
108  {
109  HostAppFilename = HostAppFilename.Replace(C.ToString(), "");
110  }
111 
112 
113  HostAppFilename = "GlobalConfig_{0}".Build(HostAppFilename);
114 
115  //Check config dir for global config file
116  FileInfo F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "config", "GlobalConfig_{0}.xml".Build(HostAppFilename)));
117  if (!F.Exists)
118  {
119  //Check if a shortcut to the config dir exists
120  FileInfo LnkFile = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "config", "GlobalConfig_{0}.lnk".Build(HostAppFilename)));
121  if (LnkFile.Exists)
122  {
123  string ConfigDirPath = ResolveShortcut(LnkFile);
124  if (Directory.Exists(ConfigDirPath))
125  {
126  F = new FileInfo(Path.Combine(ConfigDirPath, "GlobalConfig_{0}.xml".Build(HostAppFilename)));
127  }
128  }
129  if (!F.Exists)
130  {
131 
132  //Check default dir for global config file
133  F = new FileInfo("GlobalConfig_{0}.xml".Build(HostAppFilename));
134  if (!F.Exists)
135  {
136  //Check dll dir for global config file
137  F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "GlobalConfig_{0}.xml".Build(HostAppFilename)));
138  if (!F.Exists)
139  {
140  //if global config file does not exist, set filename to config directory.
141  F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "config", "GlobalConfig_{0}.xml".Build(HostAppFilename)));
142  if (!F.Directory.Exists)
143  {
144  //If the config dir does not exist set the dll dir for the config
145  F = new FileInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "GlobalConfig_{0}.xml".Build(HostAppFilename)));
146  }
147  }
148  }
149  }
150  }
151 
152 
153  Pinball = new DirectOutput.Pinball();
154  Pinball.Init((F.Exists ? F.FullName : ""), TableFilename, RomName);
155  }
156  else
157  {
158  throw new Exception("Object has already been initialized. You must call Finish() before initializing again.");
159  }
160  }
161 
162 
167  public static void ShowFrontend()
168  {
169  if (Pinball != null)
170  {
171  try
172  {
173  DirectOutput.Frontend.MainMenu.Open(Pinball);
174  }
175  catch (Exception E)
176  {
177  System.Windows.Forms.MessageBox.Show("Could not show DirectOutput frontend.\n The following exception occured:\n{0}".Build(E.Message), "DirectOutput");
178  }
179 
180  }
181  else
182  {
183  throw new Exception("Init has to be called before the frontend is opend.");
184  }
185  }
186 
187 
188 
189 
190  private static string ResolveShortcut(FileInfo ShortcutFile)
191  {
192  string TargetPath = "";
193  try
194  {
195  Type WScriptShell = Type.GetTypeFromProgID("WScript.Shell");
196  object Shell = Activator.CreateInstance(WScriptShell);
197  object Shortcut = WScriptShell.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, Shell, new object[] { ShortcutFile.FullName });
198  TargetPath = (string)Shortcut.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, Shortcut, null);
199  Shortcut = null;
200  Shell = null;
201  }
202  catch
203  {
204 
205  }
206 
207  try
208  {
209  if (Directory.Exists(TargetPath))
210  {
211  return TargetPath;
212  }
213  else if (File.Exists(TargetPath))
214  {
215  return TargetPath;
216  }
217  else
218  {
219  return "";
220  }
221 
222  }
223  catch
224  {
225  return "";
226  }
227  }
228 
229  #region Properties
230 
231 
232 
233  private static Pinball _Pinball;
234 
241  public static Pinball Pinball
242  {
243  get { return _Pinball; }
244  private set { _Pinball = value; }
245  }
246 
247  #endregion
248 
249  }
250 }