forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalProcess.cs
More file actions
294 lines (248 loc) · 8.69 KB
/
ExternalProcess.cs
File metadata and controls
294 lines (248 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#if UNITY_EDITOR
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Helper class for launching external processes inside of the unity editor.
/// </summary>
public class ExternalProcess : IDisposable
{
[DllImport("ExternalProcessAPI", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ExternalProcessAPI_CreateProcess([MarshalAs(UnmanagedType.LPStr)] string cmdline);
[DllImport("ExternalProcessAPI", CallingConvention = CallingConvention.Cdecl)]
private static extern bool ExternalProcessAPI_IsRunning(IntPtr handle);
[DllImport("ExternalProcessAPI", CallingConvention = CallingConvention.Cdecl)]
private static extern void ExternalProcessAPI_SendLine(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string line);
[DllImport("ExternalProcessAPI", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ExternalProcessAPI_GetLine(IntPtr handle);
[DllImport("ExternalProcessAPI", CallingConvention = CallingConvention.Cdecl)]
private static extern void ExternalProcessAPI_DestroyProcess(IntPtr handle);
[DllImport("ExternalProcessAPI", CallingConvention = CallingConvention.Cdecl)]
public static extern void ExternalProcessAPI_ConfirmOrBeginProcess([MarshalAs(UnmanagedType.LPStr)] string processName);
private IntPtr mHandle;
/*
* First some static utility functions, used by some other code as well.
* They are related to "external processes" so they appear here.
*/
private static string sAppDataPath;
public static void Launch(string appName)
{
// Full or relative paths only. Currently unused.
if (!appName.StartsWith(@"\"))
{
appName += @"\";
}
string appPath = AppDataPath + appName;
string appDir = Path.GetDirectoryName(appPath);
Process pr = new Process();
pr.StartInfo.FileName = appPath;
pr.StartInfo.WorkingDirectory = appDir;
pr.Start();
}
private static string AppDataPath
{
get
{
if (string.IsNullOrEmpty(sAppDataPath))
{
sAppDataPath = Application.dataPath.Replace("/", @"\");
}
return sAppDataPath;
}
}
public static bool FindAndLaunch(string appName)
{
return FindAndLaunch(appName, null);
}
public static bool FindAndLaunch(string appName, string args)
{
// Start at working directory, append appName (should read "appRelativePath"), see if it exists.
// If not go up to parent and try again till drive level reached.
string appPath = FindPathToExecutable(appName);
if (appPath == null)
{
return false;
}
string appDir = Path.GetDirectoryName(appPath);
Process pr = new Process();
pr.StartInfo.FileName = appPath;
pr.StartInfo.WorkingDirectory = appDir;
pr.StartInfo.Arguments = args;
return pr.Start();
}
public static string FindPathToExecutable(string appName)
{
// Start at working directory, append appName (should read "appRelativePath"), see if it exists.
// If not go up to parent and try again till drive level reached.
if (!appName.StartsWith(@"\"))
{
appName = @"\" + appName;
}
string searchDir = AppDataPath;
while (searchDir.Length > 3)
{
string appPath = searchDir + appName;
if (File.Exists(appPath))
{
return appPath;
}
searchDir = Path.GetDirectoryName(searchDir);
}
return null;
}
public static string MakeRelativePath(string path1, string path2)
{
// TBD- doesn't really belong in ExternalProcess.
path1 = path1.Replace('\\', '/');
path2 = path2.Replace('\\', '/');
path1 = path1.Replace("\"", "");
path2 = path2.Replace("\"", "");
Uri uri1 = new Uri(path1);
Uri uri2 = new Uri(path2);
Uri relativePath = uri1.MakeRelativeUri(uri2);
return relativePath.OriginalString;
}
/*
* The actual ExternalProcess class.
*/
public static ExternalProcess CreateExternalProcess(string appName)
{
return CreateExternalProcess(appName, null);
}
public static ExternalProcess CreateExternalProcess(string appName, string args)
{
// Seems like it would be safer and more informative to call this static method and test for null after.
try
{
return new ExternalProcess(appName, args);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError("Unable to start process " + appName + ", " + ex.Message + ".");
}
return null;
}
private ExternalProcess(string appName, string args)
{
appName = appName.Replace("/", @"\");
string appPath = appName;
if (!File.Exists(appPath))
{
appPath = FindPathToExecutable(appName);
}
if (appPath == null)
{
throw new ArgumentException("Unable to find app " + appPath);
}
// This may throw, calling code should catch the exception.
string launchString = args == null ? appPath : appPath + " " + args;
mHandle = ExternalProcessAPI_CreateProcess(launchString);
}
~ExternalProcess()
{
Dispose(false);
}
public bool IsRunning()
{
try
{
if (mHandle != IntPtr.Zero)
{
return ExternalProcessAPI_IsRunning(mHandle);
}
}
catch
{
Terminate();
}
return false;
}
public bool WaitForStart(float seconds)
{
return WaitFor(seconds, () => { return ExternalProcessAPI_IsRunning(mHandle); });
}
public bool WaitForShutdown(float seconds)
{
return WaitFor(seconds, () => { return !ExternalProcessAPI_IsRunning(mHandle); });
}
public bool WaitFor(float seconds, Func<bool> func)
{
if (seconds <= 0.0f)
seconds = 5.0f;
float end = Time.realtimeSinceStartup + seconds;
bool hasHappened = false;
while (Time.realtimeSinceStartup < end)
{
hasHappened = func();
if (hasHappened)
{
break;
}
Thread.Sleep(Math.Min(500, (int)(seconds * 1000)));
}
return hasHappened;
}
public void SendLine(string line)
{
try
{
if (mHandle != IntPtr.Zero)
{
ExternalProcessAPI_SendLine(mHandle, line);
}
}
catch
{
Terminate();
}
}
public string GetLine()
{
try
{
if (mHandle != IntPtr.Zero)
{
return Marshal.PtrToStringAnsi(ExternalProcessAPI_GetLine(mHandle));
}
}
catch
{
Terminate();
}
return null;
}
public void Terminate()
{
try
{
if (mHandle != IntPtr.Zero)
{
ExternalProcessAPI_DestroyProcess(mHandle);
}
}
catch
{
// TODO: Should we be catching something here?
}
mHandle = IntPtr.Zero;
}
// IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
Terminate();
}
}
}
#endif