33// Licensed under the MIT License. See LICENSE in the project root for license information.
44//
55
6- using UnityEngine ;
7- using System . Net ;
86using System ;
9- using System . IO ;
107using System . Collections . Generic ;
8+ using System . Diagnostics ;
9+ using System . IO ;
10+ using System . Net ;
11+ using System . Text ;
12+ using System . Threading ;
13+ using UnityEngine ;
14+ using Debug = UnityEngine . Debug ;
1115
1216namespace HoloToolkit . Unity
1317{
@@ -21,12 +25,12 @@ public class BuildDeployPortal
2125 public const int TimeoutMS = ( int ) ( TimeOut * 1000.0f ) ;
2226 public const float MaxWaitTime = 20.0f ;
2327
24- public static readonly string kAPI_ProcessQuery = @"http://{0}/api/resourcemanager/processes" ;
25- public static readonly string kAPI_PackagesQuery = @"http://{0}/api/appx/packagemanager/packages" ;
26- public static readonly string kAPI_InstallQuery = @"http://{0}/api/app/packagemanager/package" ;
27- public static readonly string kAPI_InstallStatusQuery = @"http://{0}/api/app/packagemanager/state" ;
28- public static readonly string kAPI_AppQuery = @"http://{0}/api/taskmanager/app" ;
29- public static readonly string kAPI_FileQuery = @"http://{0}/api/filesystem/apps/file" ;
28+ public static readonly string API_ProcessQuery = @"http://{0}/api/resourcemanager/processes" ;
29+ public static readonly string API_PackagesQuery = @"http://{0}/api/appx/packagemanager/packages" ;
30+ public static readonly string API_InstallQuery = @"http://{0}/api/app/packagemanager/package" ;
31+ public static readonly string API_InstallStatusQuery = @"http://{0}/api/app/packagemanager/state" ;
32+ public static readonly string API_AppQuery = @"http://{0}/api/taskmanager/app" ;
33+ public static readonly string API_FileQuery = @"http://{0}/api/filesystem/apps/file" ;
3034
3135 // Enums
3236 public enum AppInstallStatus
@@ -79,11 +83,13 @@ public class ProcessDesc
7983 public int VirtualSize ;
8084 public int WorkingSetSize ;
8185 }
86+
8287 [ Serializable ]
8388 public class ProcessList
8489 {
8590 public ProcessDesc [ ] Processes ;
8691 }
92+
8793 [ Serializable ]
8894 public class InstallStatus
8995 {
@@ -92,18 +98,24 @@ public class InstallStatus
9298 public string Reason ;
9399 public bool Success ;
94100 }
101+
95102 [ Serializable ]
96103 public class Response
97104 {
98105 public string Reason ;
99106 }
107+
100108 private class TimeoutWebClient : WebClient
101109 {
102110 protected override WebRequest GetWebRequest ( Uri uri )
103111 {
104112 WebRequest lWebRequest = base . GetWebRequest ( uri ) ;
105- lWebRequest . Timeout = BuildDeployPortal . TimeoutMS ;
106- ( ( HttpWebRequest ) lWebRequest ) . ReadWriteTimeout = BuildDeployPortal . TimeoutMS ;
113+
114+ if ( lWebRequest == null ) { return null ; }
115+
116+ lWebRequest . Timeout = TimeoutMS ;
117+ ( ( HttpWebRequest ) lWebRequest ) . ReadWriteTimeout = TimeoutMS ;
118+
107119 return lWebRequest ;
108120 }
109121 }
@@ -120,19 +132,21 @@ public static bool IsAppRunning(string appName, ConnectInfo connectInfo)
120132 using ( var client = new TimeoutWebClient ( ) )
121133 {
122134 client . Credentials = new NetworkCredential ( connectInfo . User , connectInfo . Password ) ;
123- string query = string . Format ( kAPI_ProcessQuery , connectInfo . IP ) ;
124- string procListJSON = client . DownloadString ( query ) ;
135+ string query = string . Format ( API_ProcessQuery , connectInfo . IP ) ;
136+ string downloadString = client . DownloadString ( query ) ;
125137
126- ProcessList procList = JsonUtility . FromJson < ProcessList > ( procListJSON ) ;
127- for ( int i = 0 ; i < procList . Processes . Length ; ++ i )
138+ var processList = JsonUtility . FromJson < ProcessList > ( downloadString ) ;
139+ for ( int i = 0 ; i < processList . Processes . Length ; ++ i )
128140 {
129- string procName = procList . Processes [ i ] . ImageName ;
130- if ( procName . Contains ( appName ) )
141+ string processName = processList . Processes [ i ] . ImageName ;
142+
143+ if ( processName . Contains ( appName ) )
131144 {
132145 return true ;
133146 }
134147 }
135148 }
149+
136150 return false ;
137151 }
138152
@@ -141,19 +155,23 @@ public static AppInstallStatus GetInstallStatus(ConnectInfo connectInfo)
141155 using ( var client = new TimeoutWebClient ( ) )
142156 {
143157 client . Credentials = new NetworkCredential ( connectInfo . User , connectInfo . Password ) ;
144- string query = string . Format ( kAPI_InstallStatusQuery , connectInfo . IP ) ;
158+ string query = string . Format ( API_InstallStatusQuery , connectInfo . IP ) ;
145159 string statusJSON = client . DownloadString ( query ) ;
146- InstallStatus status = JsonUtility . FromJson < InstallStatus > ( statusJSON ) ;
160+ var status = JsonUtility . FromJson < InstallStatus > ( statusJSON ) ;
147161
148162 if ( status == null )
149163 {
150164 return AppInstallStatus . Installing ;
151165 }
152- else if ( status . Success == false )
166+
167+ Debug . LogFormat ( "Install Status: {0}|{1}|{2}|{3}" , status . Code , status . CodeText , status . Reason , status . Success ) ;
168+
169+ if ( status . Success == false )
153170 {
154171 Debug . LogError ( status . Reason + "(" + status . CodeText + ")" ) ;
155172 return AppInstallStatus . InstallFail ;
156173 }
174+
157175 return AppInstallStatus . InstallSuccess ;
158176 }
159177 }
@@ -163,10 +181,10 @@ public static AppDetails QueryAppDetails(string packageFamilyName, ConnectInfo c
163181 using ( var client = new TimeoutWebClient ( ) )
164182 {
165183 client . Credentials = new NetworkCredential ( connectInfo . User , connectInfo . Password ) ;
166- string query = string . Format ( kAPI_PackagesQuery , connectInfo . IP ) ;
184+ string query = string . Format ( API_PackagesQuery , connectInfo . IP ) ;
167185 string appListJSON = client . DownloadString ( query ) ;
168186
169- AppList appList = JsonUtility . FromJson < AppList > ( appListJSON ) ;
187+ var appList = JsonUtility . FromJson < AppList > ( appListJSON ) ;
170188 for ( int i = 0 ; i < appList . InstalledPackages . Length ; ++ i )
171189 {
172190 string thisAppName = appList . InstalledPackages [ i ] . PackageFamilyName ;
@@ -176,6 +194,7 @@ public static AppDetails QueryAppDetails(string packageFamilyName, ConnectInfo c
176194 }
177195 }
178196 }
197+
179198 return null ;
180199 }
181200
@@ -190,11 +209,11 @@ public static bool InstallApp(string appFullPath, ConnectInfo connectInfo, bool
190209 string depPath = Path . GetDirectoryName ( appFullPath ) + @"\Dependencies\x86\" ;
191210
192211 // Post it using the REST API
193- WWWForm form = new WWWForm ( ) ;
212+ var form = new WWWForm ( ) ;
194213
195214 // APPX file
196- FileStream stream = new FileStream ( appFullPath , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
197- BinaryReader reader = new BinaryReader ( stream ) ;
215+ var stream = new FileStream ( appFullPath , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
216+ var reader = new BinaryReader ( stream ) ;
198217 form . AddBinaryData ( fileName , reader . ReadBytes ( ( int ) reader . BaseStream . Length ) , fileName ) ;
199218 stream . Close ( ) ;
200219
@@ -226,18 +245,19 @@ public static bool InstallApp(string appFullPath, ConnectInfo connectInfo, bool
226245 }
227246
228247 // Query
229- string query = string . Format ( kAPI_InstallQuery , connectInfo . IP ) ;
248+ string query = string . Format ( API_InstallQuery , connectInfo . IP ) ;
230249 query += "?package=" + WWW . EscapeURL ( fileName ) ;
231- WWW www = new WWW ( query , form . data , headers ) ;
250+
251+ var www = new WWW ( query , form . data , headers ) ;
232252 DateTime queryStartTime = DateTime . Now ;
233- while ( ! www . isDone &&
234- ( ( DateTime . Now - queryStartTime ) . TotalSeconds < TimeOut ) )
253+
254+ while ( ! www . isDone && ( DateTime . Now - queryStartTime ) . TotalSeconds < TimeOut )
235255 {
236- System . Threading . Thread . Sleep ( 10 ) ;
256+ Thread . Sleep ( 10 ) ;
237257 }
238258
239259 // Give it a short time before checking
240- System . Threading . Thread . Sleep ( 250 ) ;
260+ Thread . Sleep ( 250 ) ;
241261
242262 // Report
243263 if ( www . isDone )
@@ -258,26 +278,25 @@ public static bool InstallApp(string appFullPath, ConnectInfo connectInfo, bool
258278
259279 // Wait for done (if requested)
260280 DateTime waitStartTime = DateTime . Now ;
261- while ( waitForDone &&
262- ( ( DateTime . Now - waitStartTime ) . TotalSeconds < MaxWaitTime ) )
281+ while ( waitForDone && ( DateTime . Now - waitStartTime ) . TotalSeconds < MaxWaitTime )
263282 {
264283 AppInstallStatus status = GetInstallStatus ( connectInfo ) ;
265284 if ( status == AppInstallStatus . InstallSuccess )
266285 {
267286 Debug . Log ( "Install Successful!" ) ;
268287 break ;
269288 }
270- else if ( status == AppInstallStatus . InstallFail )
289+ if ( status == AppInstallStatus . InstallFail )
271290 {
272291 Debug . LogError ( "Install Failed!" ) ;
273292 break ;
274293 }
275294
276295 // Wait a bit and we'll ask again
277- System . Threading . Thread . Sleep ( 1000 ) ;
296+ Thread . Sleep ( 1000 ) ;
278297 }
279298 }
280- catch ( System . Exception ex )
299+ catch ( Exception ex )
281300 {
282301 Debug . LogError ( ex . ToString ( ) ) ;
283302 return false ;
@@ -299,21 +318,21 @@ public static bool UninstallApp(string packageFamilyName, ConnectInfo connectInf
299318 }
300319
301320 // Setup the command
302- string query = string . Format ( kAPI_InstallQuery , connectInfo . IP ) ;
321+ string query = string . Format ( API_InstallQuery , connectInfo . IP ) ;
303322 query += "?package=" + WWW . EscapeURL ( appDetails . PackageFullName ) ;
304323
305324 // Use HttpWebRequest for a delete query
306- HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( query ) ;
325+ var request = ( HttpWebRequest ) WebRequest . Create ( query ) ;
307326 request . Timeout = TimeoutMS ;
308327 request . Credentials = new NetworkCredential ( connectInfo . User , connectInfo . Password ) ;
309328 request . Method = "DELETE" ;
310- using ( HttpWebResponse httpResponse = ( HttpWebResponse ) request . GetResponse ( ) )
329+ using ( var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) )
311330 {
312331 Debug . Log ( "Response = " + httpResponse . StatusDescription ) ;
313332 httpResponse . Close ( ) ;
314333 }
315334 }
316- catch ( System . Exception ex )
335+ catch ( Exception ex )
317336 {
318337 Debug . LogError ( ex . ToString ( ) ) ;
319338 return false ;
@@ -328,23 +347,23 @@ public static bool LaunchApp(string packageFamilyName, ConnectInfo connectInfo)
328347 AppDetails appDetails = QueryAppDetails ( packageFamilyName , connectInfo ) ;
329348 if ( appDetails == null )
330349 {
331- Debug . LogError ( "Appliation not found" ) ;
350+ Debug . LogError ( "Application not found" ) ;
332351 return false ;
333352 }
334353
335354 // Setup the command
336- string query = string . Format ( kAPI_AppQuery , connectInfo . IP ) ;
355+ string query = string . Format ( API_AppQuery , connectInfo . IP ) ;
337356 query += "?appid=" + WWW . EscapeURL ( EncodeTo64 ( appDetails . PackageRelativeId ) ) ;
338- query += "&package=" + WWW . EscapeURL ( EncodeTo64 ( appDetails . PackageFamilyName ) ) ;
357+ query += "&package=" + WWW . EscapeURL ( appDetails . PackageFullName ) ;
339358
340359 // Use HttpWebRequest
341- HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( query ) ;
360+ var request = ( HttpWebRequest ) WebRequest . Create ( query ) ;
342361 request . Timeout = TimeoutMS ;
343362 request . Credentials = new NetworkCredential ( connectInfo . User , connectInfo . Password ) ;
344363 request . Method = "POST" ;
345364
346365 // Query
347- using ( HttpWebResponse httpResponse = ( HttpWebResponse ) request . GetResponse ( ) )
366+ using ( var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) )
348367 {
349368 Debug . Log ( "Response = " + httpResponse . StatusDescription ) ;
350369 httpResponse . Close ( ) ;
@@ -361,26 +380,26 @@ public static bool KillApp(string packageFamilyName, ConnectInfo connectInfo)
361380 AppDetails appDetails = QueryAppDetails ( packageFamilyName , connectInfo ) ;
362381 if ( appDetails == null )
363382 {
364- Debug . LogError ( "Appliation not found" ) ;
383+ Debug . LogError ( "Application not found" ) ;
365384 return false ;
366385 }
367386
368387 // Setup the command
369- string query = string . Format ( kAPI_AppQuery , connectInfo . IP ) ;
388+ string query = string . Format ( API_AppQuery , connectInfo . IP ) ;
370389 query += "?package=" + WWW . EscapeURL ( EncodeTo64 ( appDetails . PackageFullName ) ) ;
371390
372391 // And send it across
373- HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( query ) ;
392+ var request = ( HttpWebRequest ) WebRequest . Create ( query ) ;
374393 request . Timeout = TimeoutMS ;
375394 request . Credentials = new NetworkCredential ( connectInfo . User , connectInfo . Password ) ;
376395 request . Method = "DELETE" ;
377- using ( HttpWebResponse httpResponse = ( HttpWebResponse ) request . GetResponse ( ) )
396+ using ( var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) )
378397 {
379398 Debug . Log ( "Response = " + httpResponse . StatusDescription ) ;
380399 httpResponse . Close ( ) ;
381400 }
382401 }
383- catch ( System . Exception ex )
402+ catch ( Exception ex )
384403 {
385404 Debug . LogError ( ex . ToString ( ) ) ;
386405 return false ;
@@ -408,15 +427,15 @@ public static bool DeviceLogFile_View(string packageFamilyName, ConnectInfo conn
408427 }
409428
410429 // Download the file
411- string query = string . Format ( kAPI_FileQuery , connectInfo . IP ) ;
430+ string query = string . Format ( API_FileQuery , connectInfo . IP ) ;
412431 query += "?knownfolderid=LocalAppData" ;
413432 query += "&filename=UnityPlayer.log" ;
414433 query += "&packagefullname=" + appDetails . PackageFullName ;
415434 query += "&path=%5C%5CTempState" ;
416435 client . DownloadFile ( query , logFile ) ;
417436
418437 // Open it up in default text editor
419- System . Diagnostics . Process . Start ( logFile ) ;
438+ Process . Start ( logFile ) ;
420439 }
421440 catch ( Exception ex )
422441 {
@@ -429,16 +448,17 @@ public static bool DeviceLogFile_View(string packageFamilyName, ConnectInfo conn
429448 }
430449
431450 // Helpers
432- static string EncodeTo64 ( string toEncode )
451+ private static string EncodeTo64 ( string toEncode )
433452 {
434- byte [ ] toEncodeAsBytes = System . Text . ASCIIEncoding . ASCII . GetBytes ( toEncode ) ;
435- string returnValue = System . Convert . ToBase64String ( toEncodeAsBytes ) ;
453+ byte [ ] toEncodeAsBytes = Encoding . ASCII . GetBytes ( toEncode ) ;
454+ string returnValue = Convert . ToBase64String ( toEncodeAsBytes ) ;
436455 return returnValue ;
437456 }
438- static string DecodeFrom64 ( string encodedData )
457+
458+ private static string DecodeFrom64 ( string encodedData )
439459 {
440- byte [ ] encodedDataAsBytes = System . Convert . FromBase64String ( encodedData ) ;
441- string returnValue = System . Text . ASCIIEncoding . ASCII . GetString ( encodedDataAsBytes ) ;
460+ byte [ ] encodedDataAsBytes = Convert . FromBase64String ( encodedData ) ;
461+ string returnValue = Encoding . ASCII . GetString ( encodedDataAsBytes ) ;
442462 return returnValue ;
443463 }
444464 }
0 commit comments