BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Editor / OVRADBTool.cs

OVRADBTool.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRADBTool.cs 7.2 KiB Download Raw
1/************************************************************************************
2
3Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
4
5Licensed under the Oculus SDK License Version 3.4.1 (the "License");
6you may not use the Oculus SDK except in compliance with the License,
7which is provided at the time of installation or download, or which
8otherwise accompanies this software in either electronic or hard copy form.
9
10You may obtain a copy of the License at
11
12https://developer.oculus.com/licenses/sdk-3.4.1
13
14Unless required by applicable law or agreed to in writing, the Oculus SDK
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19
20************************************************************************************/
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Diagnostics;
26using System.IO;
27using System.Text;
28using System.Threading;
29using UnityEngine;
30
31using Debug = UnityEngine.Debug;
32
33public class OVRADBTool
34{
35 public bool isReady;
36
37 public string androidSdkRoot;
38 public string androidPlatformToolsPath;
39 public string adbPath;
40
41 public OVRADBTool(string androidSdkRoot)
42 {
43 if (!String.IsNullOrEmpty(androidSdkRoot))
44 {
45 this.androidSdkRoot = androidSdkRoot;
46 }
47 else
48 {
49 this.androidSdkRoot = String.Empty;
50 }
51
52 if (this.androidSdkRoot.EndsWith("\\") || this.androidSdkRoot.EndsWith("/"))
53 {
54 this.androidSdkRoot = this.androidSdkRoot.Remove(this.androidSdkRoot.Length - 1);
55 }
56 androidPlatformToolsPath = Path.Combine(this.androidSdkRoot, "platform-tools");
57 adbPath = Path.Combine(androidPlatformToolsPath, "adb.exe");
58 isReady = File.Exists(adbPath);
59 }
60
61 public static bool IsAndroidSdkRootValid(string androidSdkRoot)
62 {
63 OVRADBTool tool = new OVRADBTool(androidSdkRoot);
64 return tool.isReady;
65 }
66
67 public delegate void WaitingProcessToExitCallback();
68
69 public int StartServer(WaitingProcessToExitCallback waitingProcessToExitCallback)
70 {
71 string outputString;
72 string errorString;
73
74 int exitCode = RunCommand(new string[] { "start-server" }, waitingProcessToExitCallback, out outputString, out errorString);
75 return exitCode;
76 }
77
78 public int KillServer(WaitingProcessToExitCallback waitingProcessToExitCallback)
79 {
80 string outputString;
81 string errorString;
82
83 int exitCode = RunCommand(new string[] { "kill-server" }, waitingProcessToExitCallback, out outputString, out errorString);
84 return exitCode;
85 }
86
87 public int ForwardPort(int port, WaitingProcessToExitCallback waitingProcessToExitCallback)
88 {
89 string outputString;
90 string errorString;
91
92 string portString = string.Format("tcp:{0}", port);
93
94 int exitCode = RunCommand(new string[] { "forward", portString, portString }, waitingProcessToExitCallback, out outputString, out errorString);
95 return exitCode;
96 }
97
98 public int ReleasePort(int port, WaitingProcessToExitCallback waitingProcessToExitCallback)
99 {
100 string outputString;
101 string errorString;
102
103 string portString = string.Format("tcp:{0}", port);
104
105 int exitCode = RunCommand(new string[] { "forward", "--remove", portString }, waitingProcessToExitCallback, out outputString, out errorString);
106 return exitCode;
107 }
108
109 public List<string> GetDevices()
110 {
111 string outputString;
112 string errorString;
113
114 RunCommand(new string[] { "devices" }, null, out outputString, out errorString);
115 string[] devices = outputString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
116
117 List<string> deviceList = new List<string>(devices);
118 deviceList.RemoveAt(0);
119
120 for(int i = 0; i < deviceList.Count; i++)
121 {
122 string deviceName = deviceList[i];
123 int index = deviceName.IndexOf('\t');
124 deviceList[i] = deviceName.Substring(0, index);
125 }
126
127 return deviceList;
128 }
129
130 private StringBuilder outputStringBuilder = null;
131 private StringBuilder errorStringBuilder = null;
132
133 public int RunCommand(string[] arguments, WaitingProcessToExitCallback waitingProcessToExitCallback, out string outputString, out string errorString)
134 {
135 int exitCode = -1;
136
137 if (!isReady)
138 {
139 Debug.LogWarning("OVRADBTool not ready");
140 outputString = string.Empty;
141 errorString = "OVRADBTool not ready";
142 return exitCode;
143 }
144
145 string args = string.Join(" ", arguments);
146
147 ProcessStartInfo startInfo = new ProcessStartInfo(adbPath, args);
148 startInfo.WorkingDirectory = androidSdkRoot;
149 startInfo.CreateNoWindow = true;
150 startInfo.UseShellExecute = false;
151 startInfo.WindowStyle = ProcessWindowStyle.Hidden;
152 startInfo.RedirectStandardOutput = true;
153 startInfo.RedirectStandardError = true;
154
155 outputStringBuilder = new StringBuilder("");
156 errorStringBuilder = new StringBuilder("");
157
158 Process process = Process.Start(startInfo);
159 process.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceivedHandler);
160 process.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataReceivedHandler);
161
162 process.BeginOutputReadLine();
163 process.BeginErrorReadLine();
164
165 try
166 {
167 do
168 {
169 if (waitingProcessToExitCallback != null)
170 {
171 waitingProcessToExitCallback();
172 }
173 } while (!process.WaitForExit(100));
174
175 process.WaitForExit();
176 }
177 catch (Exception e)
178 {
179 Debug.LogWarningFormat("[OVRADBTool.RunCommand] exception {0}", e.Message);
180 }
181
182 exitCode = process.ExitCode;
183
184 process.Close();
185
186 outputString = outputStringBuilder.ToString();
187 errorString = errorStringBuilder.ToString();
188
189 outputStringBuilder = null;
190 errorStringBuilder = null;
191
192 if (!string.IsNullOrEmpty(errorString))
193 {
194 if (errorString.Contains("Warning"))
195 {
196 UnityEngine.Debug.LogWarning("OVRADBTool " + errorString);
197 }
198 else
199 {
200 UnityEngine.Debug.LogError("OVRADBTool " + errorString);
201 }
202 }
203
204 return exitCode;
205 }
206
207 public Process RunCommandAsync(string[] arguments, DataReceivedEventHandler outputDataRecievedHandler)
208 {
209 if (!isReady)
210 {
211 Debug.LogWarning("OVRADBTool not ready");
212 return null;
213 }
214
215 string args = string.Join(" ", arguments);
216
217 ProcessStartInfo startInfo = new ProcessStartInfo(adbPath, args);
218 startInfo.WorkingDirectory = androidSdkRoot;
219 startInfo.CreateNoWindow = true;
220 startInfo.UseShellExecute = false;
221 startInfo.WindowStyle = ProcessWindowStyle.Hidden;
222 startInfo.RedirectStandardOutput = true;
223 startInfo.RedirectStandardError = true;
224
225 Process process = Process.Start(startInfo);
226 if (outputDataRecievedHandler != null)
227 {
228 process.OutputDataReceived += new DataReceivedEventHandler(outputDataRecievedHandler);
229 }
230
231 process.BeginOutputReadLine();
232 process.BeginErrorReadLine();
233
234 return process;
235 }
236
237 private void OutputDataReceivedHandler(object sendingProcess, DataReceivedEventArgs args)
238 {
239 // Collect the sort command output.
240 if (!string.IsNullOrEmpty(args.Data))
241 {
242 // Add the text to the collected output.
243 outputStringBuilder.Append(args.Data);
244 outputStringBuilder.Append(Environment.NewLine);
245 }
246 }
247
248 private void ErrorDataReceivedHandler(object sendingProcess, DataReceivedEventArgs args)
249 {
250 // Collect the sort command output.
251 if (!string.IsNullOrEmpty(args.Data))
252 {
253 // Add the text to the collected output.
254 errorStringBuilder.Append(args.Data);
255 errorStringBuilder.Append(Environment.NewLine);
256 }
257 }
258}