OVRADBTool.cs 7.3 KB

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