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
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.Setup((F.Exists ? F.FullName : ""), TableFilename, RomName);
155  Pinball.Init();
156  }
157  else
158  {
159  throw new Exception("Object has already been initialized. You must call Finish() before initializing again.");
160  }
161  }
162 
163 
168  public static void ShowFrontend()
169  {
170  if (Pinball != null)
171  {
172  try
173  {
174  DirectOutput.Frontend.MainMenu.Open(Pinball);
175  }
176  catch (Exception E)
177  {
178  System.Windows.Forms.MessageBox.Show("Could not show DirectOutput frontend.\n The following exception occured:\n{0}".Build(E.Message), "DirectOutput");
179  }
180 
181  }
182  else
183  {
184  throw new Exception("Init has to be called before the frontend is opend.");
185  }
186  }
187 
188 
189 
190 
191  private static string ResolveShortcut(FileInfo ShortcutFile)
192  {
193  string TargetPath = "";
194  try
195  {
196  Type WScriptShell = Type.GetTypeFromProgID("WScript.Shell");
197  object Shell = Activator.CreateInstance(WScriptShell);
198  object Shortcut = WScriptShell.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, Shell, new object[] { ShortcutFile.FullName });
199  TargetPath = (string)Shortcut.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, Shortcut, null);
200  Shortcut = null;
201  Shell = null;
202  }
203  catch
204  {
205 
206  }
207 
208  try
209  {
210  if (Directory.Exists(TargetPath))
211  {
212  return TargetPath;
213  }
214  else if (File.Exists(TargetPath))
215  {
216  return TargetPath;
217  }
218  else
219  {
220  return "";
221  }
222 
223  }
224  catch
225  {
226  return "";
227  }
228  }
229 
230  #region Properties
231 
232 
233 
234  private static Pinball _Pinball;
235 
242  public static Pinball Pinball
243  {
244  get { return _Pinball; }
245  private set { _Pinball = value; }
246  }
247 
248  #endregion
249 
250  }
251 }