|
| 1 | +/************************************************************************************ |
| 2 | +
|
| 3 | +Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 4 | +
|
| 5 | +Licensed under the Oculus SDK License Version 3.4.1 (the "License"); |
| 6 | +you may not use the Oculus SDK except in compliance with the License, |
| 7 | +which is provided at the time of installation or download, or which |
| 8 | +otherwise accompanies this software in either electronic or hard copy form. |
| 9 | +
|
| 10 | +You may obtain a copy of the License at |
| 11 | +
|
| 12 | +https://developer.oculus.com/licenses/sdk-3.4.1 |
| 13 | +
|
| 14 | +Unless required by applicable law or agreed to in writing, the Oculus SDK |
| 15 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +See the License for the specific language governing permissions and |
| 18 | +limitations under the License. |
| 19 | +
|
| 20 | +************************************************************************************/ |
| 21 | + |
| 22 | +using System; |
| 23 | +using System.Collections; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.Diagnostics; |
| 26 | +using System.IO; |
| 27 | +using System.Text; |
| 28 | +using System.Threading; |
| 29 | +using UnityEngine; |
| 30 | + |
| 31 | +using Debug = UnityEngine.Debug; |
| 32 | + |
| 33 | +public 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 | + private StringBuilder outputStringBuilder = null; |
| 110 | + private StringBuilder errorStringBuilder = null; |
| 111 | + |
| 112 | + public int RunCommand(string[] arguments, WaitingProcessToExitCallback waitingProcessToExitCallback, out string outputString, out string errorString) |
| 113 | + { |
| 114 | + int exitCode = -1; |
| 115 | + |
| 116 | + if (!isReady) |
| 117 | + { |
| 118 | + Debug.LogWarning("OVRADBTool not ready"); |
| 119 | + outputString = string.Empty; |
| 120 | + errorString = "OVRADBTool not ready"; |
| 121 | + return exitCode; |
| 122 | + } |
| 123 | + |
| 124 | + string args = string.Join(" ", arguments); |
| 125 | + |
| 126 | + ProcessStartInfo startInfo = new ProcessStartInfo(adbPath, args); |
| 127 | + startInfo.WorkingDirectory = androidSdkRoot; |
| 128 | + startInfo.CreateNoWindow = true; |
| 129 | + startInfo.UseShellExecute = false; |
| 130 | + startInfo.WindowStyle = ProcessWindowStyle.Hidden; |
| 131 | + startInfo.RedirectStandardOutput = true; |
| 132 | + startInfo.RedirectStandardError = true; |
| 133 | + |
| 134 | + outputStringBuilder = new StringBuilder(""); |
| 135 | + errorStringBuilder = new StringBuilder(""); |
| 136 | + |
| 137 | + Process process = Process.Start(startInfo); |
| 138 | + process.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceivedHandler); |
| 139 | + |
| 140 | + process.BeginOutputReadLine(); |
| 141 | + process.BeginErrorReadLine(); |
| 142 | + |
| 143 | + try |
| 144 | + { |
| 145 | + do |
| 146 | + { |
| 147 | + if (waitingProcessToExitCallback != null) |
| 148 | + { |
| 149 | + waitingProcessToExitCallback(); |
| 150 | + } |
| 151 | + } while (!process.WaitForExit(100)); |
| 152 | + |
| 153 | + process.WaitForExit(); |
| 154 | + } |
| 155 | + catch (Exception e) |
| 156 | + { |
| 157 | + Debug.LogWarningFormat("[OVRADBTool.RunCommand] exception {0}", e.Message); |
| 158 | + } |
| 159 | + |
| 160 | + exitCode = process.ExitCode; |
| 161 | + |
| 162 | + process.Close(); |
| 163 | + |
| 164 | + outputString = outputStringBuilder.ToString(); |
| 165 | + errorString = errorStringBuilder.ToString(); |
| 166 | + |
| 167 | + outputStringBuilder = null; |
| 168 | + errorStringBuilder = null; |
| 169 | + |
| 170 | + return exitCode; |
| 171 | + } |
| 172 | + |
| 173 | + private void OutputDataReceivedHandler(object sendingProcess, DataReceivedEventArgs args) |
| 174 | + { |
| 175 | + // Collect the sort command output. |
| 176 | + if (!string.IsNullOrEmpty(args.Data)) |
| 177 | + { |
| 178 | + // Add the text to the collected output. |
| 179 | + outputStringBuilder.Append(args.Data); |
| 180 | + outputStringBuilder.Append(Environment.NewLine); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private void ErrorDataReceivedHandler(object sendingProcess, DataReceivedEventArgs args) |
| 185 | + { |
| 186 | + // Collect the sort command output. |
| 187 | + if (!string.IsNullOrEmpty(args.Data)) |
| 188 | + { |
| 189 | + // Add the text to the collected output. |
| 190 | + errorStringBuilder.Append(args.Data); |
| 191 | + errorStringBuilder.Append(Environment.NewLine); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments