-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiOSOverUsbSupport.cs
387 lines (302 loc) · 9.69 KB
/
iOSOverUsbSupport.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using Mono.Debugging.Client;
using Mono.Debugging.Soft;
using MonoDevelop.Core;
namespace MonoDevelop.Debugger.Soft.Unity
{
class iOSUsbConnector: IUnityDbgConnector
{
readonly string udid;
readonly ushort port = 12000;
public SoftDebuggerStartInfo SetupConnection()
{
Usbmuxd.StartIosProxy(port, 56000, udid);
var args = new SoftDebuggerConnectArgs(udid, IPAddress.Loopback, port);
return new SoftDebuggerStartInfo(args);
}
public void OnDisconnect()
{
Usbmuxd.StopIosProxy(port);
}
public iOSUsbConnector(string udid)
{
this.udid = udid;
}
}
static class Usbmuxd
{
// Note: This struct is used in .Net for interop. so do not change it, or know what
// you are doing!
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct iOSDevice
{
public int productId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
public string udid;
};
const string nativeDllOsx = "UnityEditor.iOS.Extensions.Native.dylib";
const string nativeDllWin32 = "x86\\UnityEditor.iOS.Extensions.Native.dll";
const string nativeDllWin64 = "x86_64\\UnityEditor.iOS.Extensions.Native.dll";
static IDllLoader loader;
static IntPtr dllHandle;
public delegate bool StartIosProxyDelegate(ushort localPort, ushort devicePort, [MarshalAs(UnmanagedType.LPStr)] string deviceId);
public delegate void StopIosProxyDelegate(ushort localPort);
public delegate void StartUsbmuxdListenThreadDelegate();
public delegate void StopUsbmuxdListenThreadDelegate();
public delegate uint UsbmuxdGetDeviceCountDelegate();
public delegate bool UsbmuxdGetDeviceDelegate(uint index, out iOSDevice device);
public static StartIosProxyDelegate StartIosProxy;
public static StopIosProxyDelegate StopIosProxy;
public static StartUsbmuxdListenThreadDelegate StartUsbmuxdListenThread;
public static StopUsbmuxdListenThreadDelegate StopUsbmuxdListenThread;
public static UsbmuxdGetDeviceCountDelegate UsbmuxdGetDeviceCount;
public static UsbmuxdGetDeviceDelegate UsbmuxdGetDevice;
public static bool Supported { get { return loader != null; } }
public static bool IsDllLoaded { get { return dllHandle != IntPtr.Zero; } }
static void LoadDll(string nativeDll)
{
if (dllHandle == IntPtr.Zero)
{
dllHandle = loader.LoadLibrary(nativeDll);
if (dllHandle != IntPtr.Zero)
Console.WriteLine("Loaded: " + nativeDll);
else
Console.WriteLine("Couldn't load: " + nativeDll);
}
}
static TType LoadFunction<TType>(string name) where TType: class
{
if (dllHandle == IntPtr.Zero)
throw new Exception("iOS native extension dll was not loaded");
IntPtr addr = loader.GetProcAddress(dllHandle, name);
return Marshal.GetDelegateForFunctionPointer(addr, typeof(TType)) as TType;
}
static void InitFunctions()
{
StartUsbmuxdListenThread = LoadFunction<StartUsbmuxdListenThreadDelegate>("StartUsbmuxdListenThread");
StopUsbmuxdListenThread = LoadFunction<StopUsbmuxdListenThreadDelegate>("StopUsbmuxdListenThread");
UsbmuxdGetDeviceCount = LoadFunction<UsbmuxdGetDeviceCountDelegate>("UsbmuxdGetDeviceCount");
UsbmuxdGetDevice = LoadFunction<UsbmuxdGetDeviceDelegate>("UsbmuxdGetDevice");
StartIosProxy = LoadFunction<StartIosProxyDelegate>("StartIosProxy");
StopIosProxy = LoadFunction<StopIosProxyDelegate>("StopIosProxy");
}
public static void Setup(string dllPath)
{
if (Platform.IsMac)
dllPath = Path.Combine(dllPath, nativeDllOsx);
else if (Platform.IsWindows && Environment.Is64BitProcess)
dllPath = Path.Combine(dllPath, nativeDllWin64);
else if (Platform.IsWindows)
dllPath = Path.Combine(dllPath, nativeDllWin32);
LoadDll(dllPath);
InitFunctions();
}
static Usbmuxd() {
if (Platform.IsMac)
loader = new PosixDllLoader();
else if (Platform.IsWindows)
loader = new WindowsDllLoader();
else
throw new NotSupportedException("Platform not supported");
}
}
interface IDllLoader {
IntPtr LoadLibrary(string fileName);
void FreeLibrary(IntPtr handle);
IntPtr GetProcAddress(IntPtr dllHandle, string name);
}
class PosixDllLoader: IDllLoader
{
const int RTLD_LAZY = 1;
const int RTLD_NOW = 2;
[DllImport("libdl")]
private static extern IntPtr dlopen(String fileName, int flags);
[DllImport("libdl")]
private static extern IntPtr dlsym(IntPtr handle, String symbol);
[DllImport("libdl")]
private static extern int dlclose(IntPtr handle);
[DllImport("libdl")]
private static extern IntPtr dlerror();
public IntPtr LoadLibrary(string fileName)
{
if (!File.Exists (fileName))
throw new FileNotFoundException (fileName);
// clear previous errors if any
dlerror();
var res = dlopen(fileName, RTLD_NOW);
var err = dlerror();
if (res == IntPtr.Zero) {
throw new Exception("dlopen: " + Marshal.PtrToStringAnsi(err));
}
return res;
}
public void FreeLibrary(IntPtr handle)
{
dlclose(handle);
}
public IntPtr GetProcAddress(IntPtr dllHandle, string name)
{
// clear previous errors if any
dlerror();
var res = dlsym(dllHandle, name);
var errPtr = dlerror();
if (errPtr != IntPtr.Zero) {
throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errPtr));
}
return res;
}
}
public class WindowsDllLoader: IDllLoader {
void IDllLoader.FreeLibrary(IntPtr handle) {
FreeLibrary(handle);
}
IntPtr IDllLoader.GetProcAddress(IntPtr dllHandle, string name) {
return GetProcAddress(dllHandle, name);
}
IntPtr IDllLoader.LoadLibrary(string fileName) {
if (!File.Exists (fileName))
throw new FileNotFoundException (fileName);
return LoadLibrary(fileName);
}
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string fileName);
[DllImport("kernel32.dll")]
private static extern int FreeLibrary(IntPtr handle);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress (IntPtr handle, string procedureName);
}
public struct iOSDeviceDescription
{
readonly public int vendorId;
readonly public int productId;
readonly public string type;
readonly public string model;
public iOSDeviceDescription(int vendorId, int productId, string type, string model)
{
this.vendorId = vendorId;
this.productId = productId;
this.type = type;
this.model = model;
}
}
static class iOSDevices
{
static iOSDeviceDescription[] descriptions;
static string oldUnityPath;
public static iOSDeviceDescription[] LoadDescriptions(string path)
{
var devices = new List<iOSDeviceDescription>();
string[] lines = File.ReadAllLines(path);
foreach (var line in lines)
{
string[] cols = line.Split(';');
// Skip comment
if ((cols.Length > 0) && (cols[0].Length > 0) && (cols[0][0] == '#'))
continue;
if (cols.Length < 4)
continue;
int vendorId;
int productId;
string type = cols[2];
string model = cols[3];
if (!int.TryParse(cols[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out vendorId))
continue;
if (!int.TryParse(cols[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out productId))
continue;
devices.Add(new iOSDeviceDescription(vendorId, productId, type, model));
}
return devices.ToArray();
}
public static bool FindDescription(int vendorId, int productId, out iOSDeviceDescription device)
{
foreach (var d in descriptions)
{
if ((d.productId == productId) && (d.vendorId == vendorId))
{
device = d;
return true;
}
}
device = default(iOSDeviceDescription);
return false;
}
static void SetupDescriptions(string path)
{
path = Path.Combine(path, "Data", "iosdevices.csv");
try {
descriptions = LoadDescriptions(path);
} catch (Exception e) {
LoggingService.LogWarning("Failed to load: " + path, e);
descriptions = new iOSDeviceDescription[0];
}
}
static bool SetupDll(string path)
{
try {
Usbmuxd.Setup(path);
if (Usbmuxd.IsDllLoaded)
Usbmuxd.StartUsbmuxdListenThread();
return true;
} catch (Exception e) {
LoggingService.LogWarning("Error while initializing usbmuxd", e);
return false;
}
}
static bool Setup()
{
string path = Path.Combine(Util.UnityEditorDataFolder, "PlaybackEngines", "iOSSupport");
// Don't try to load again if it already failed once
if (oldUnityPath == path)
return false;
else
oldUnityPath = path;
SetupDescriptions(path);
return SetupDll(path);
}
public static bool Supported
{
get {
return Usbmuxd.Supported;
}
}
public static bool Initialized
{
get
{
return Usbmuxd.IsDllLoaded || Setup();
}
}
public static void GetUSBDevices(ConnectorRegistry connectors, List<ProcessInfo> processes)
{
if (!Initialized)
return;
try {
uint count = Usbmuxd.UsbmuxdGetDeviceCount();
for (uint i = 0; i < count; i++) {
var device = new Usbmuxd.iOSDevice();
if (Usbmuxd.UsbmuxdGetDevice(i, out device) && !string.IsNullOrEmpty(device.udid)) {
var name = GetNameForDevice(device);
var processId = connectors.GetProcessIdForUniqueId(device.udid);
processes.Add(new ProcessInfo(processId, "Unity iOS USB: " + name));
connectors.Connectors[processId] = new iOSUsbConnector(device.udid);
}
}
} catch (Exception e) {
LoggingService.LogError("Error while getting USB devices", e);
}
}
static string GetNameForDevice(Usbmuxd.iOSDevice device)
{
iOSDeviceDescription description;
if (FindDescription(0x05AC, device.productId, out description))
return description.type + " (" + description.model + ")";
else
return "Unknown iOS device";
}
}
}