1+ using ComputerUtils . Android . Encryption ;
2+ using ComputerUtils . Android . Logging ;
3+ using ComputerUtils . Android . VarUtils ;
4+ using QuestAppVersionSwitcher . ClientModels ;
5+ using QuestAppVersionSwitcher . Core ;
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . IO ;
9+ using System . Net ;
10+ using System . Text . Json ;
11+
12+ namespace QuestAppVersionSwitcher
13+ {
14+ public class DownloadManager : DownloadProgress
15+ {
16+ public delegate void DownloadFinished ( DownloadManager manager ) ;
17+ public event DownloadFinished DownloadFinishedEvent ;
18+ public string tmpPath = "" ;
19+
20+ public void StartDownload ( long binaryid , string password , string version , string app )
21+ {
22+ string decodedToken = PasswordEncryption . Decrypt ( CoreService . coreVars . token , password ) ;
23+ WebClient downloader = new WebClient ( ) ;
24+ tmpPath = CoreService . coreVars . QAVDTmpDowngradeDir + DateTime . Now . Ticks + ".apk" ;
25+ List < long > lastBytesPerSec = new List < long > ( ) ;
26+ DateTime lastUpdate = DateTime . Now ;
27+ bool locked = false ;
28+ long BytesToRecieve = 0 ;
29+ long lastBytes = 0 ;
30+ this . name = app + " " + version ;
31+ this . backupName = this . name + " Downgraded" ;
32+ foreach ( char r in QAVSWebserver . ReservedChars )
33+ {
34+ this . backupName = this . backupName . Replace ( r , '_' ) ;
35+ }
36+ downloader . DownloadProgressChanged += ( o , e ) =>
37+ {
38+ if ( locked ) return ;
39+
40+ locked = true ;
41+ double secondsPassed = ( DateTime . Now - lastUpdate ) . TotalSeconds ;
42+ if ( secondsPassed >= 0.5 )
43+ {
44+ BytesToRecieve = e . TotalBytesToReceive ;
45+ string current = SizeConverter . ByteSizeToString ( e . BytesReceived ) ;
46+ string total = SizeConverter . ByteSizeToString ( BytesToRecieve ) ;
47+ long bytesPerSec = ( long ) Math . Round ( ( e . BytesReceived - lastBytes ) / secondsPassed ) ;
48+ lastBytesPerSec . Add ( bytesPerSec ) ;
49+ if ( lastBytesPerSec . Count > 5 ) lastBytesPerSec . RemoveAt ( 0 ) ;
50+ lastBytes = e . BytesReceived ;
51+ long avg = 0 ;
52+ foreach ( long l in lastBytesPerSec ) avg += l ;
53+ avg = avg / lastBytesPerSec . Count ;
54+ this . done = e . BytesReceived ;
55+ this . total = BytesToRecieve ;
56+ this . speed = bytesPerSec ;
57+ this . eTASeconds = ( e . TotalBytesToReceive - e . BytesReceived ) / avg ;
58+ this . doneString = SizeConverter . ByteSizeToString ( this . done ) ;
59+ this . totalString = SizeConverter . ByteSizeToString ( this . total ) ;
60+ this . speedString = SizeConverter . ByteSizeToString ( this . speed , 0 ) + "/s" ;
61+ this . eTAString = SizeConverter . SecondsToBetterString ( this . eTASeconds ) ;
62+ this . percentage = this . done / ( double ) this . total ;
63+ this . percentageString = String . Format ( "{0:0.#}" , this . percentage * 100 ) + "%" ;
64+ lastUpdate = DateTime . Now ;
65+ }
66+ locked = false ;
67+ } ;
68+ downloader . DownloadFileCompleted += ( o , e ) =>
69+ {
70+ if ( e . Error != null )
71+ {
72+ Logger . Log ( e . Error . ToString ( ) , LoggingType . Error ) ;
73+ if ( File . Exists ( tmpPath ) ) File . Delete ( tmpPath ) ;
74+ SetEmpty ( ) ;
75+ this . backupName = "Unknown Error: Have you entered your token in the Tools & Options section? Doing this is needed. Otherwise you don't own the game you are trying to download." ;
76+ this . textColor = "#EE0000" ;
77+ }
78+ else
79+ {
80+ DownloadFinishedEvent ( this ) ;
81+ this . backupName = "Done: restore " + backupName + " to downgrade your game any time" ;
82+ this . textColor = "#30e34b" ;
83+ }
84+ } ;
85+ downloader . DownloadFileAsync ( new Uri ( "https://securecdn.oculus.com/binaries/download/?id=" + binaryid + "&access_token=" + decodedToken ) , tmpPath ) ;
86+ }
87+
88+ public void SetEmpty ( bool alsoSize = true )
89+ {
90+
91+ this . speed = 0 ;
92+ this . eTASeconds = 0 ;
93+ if ( alsoSize )
94+ {
95+ this . done = 0 ;
96+ this . total = 0 ;
97+ this . doneString = "" ;
98+ this . totalString = "" ;
99+ this . percentage = 0 ;
100+ this . percentageString = "" ;
101+ }
102+ this . speedString = "" ;
103+ this . eTAString = "" ;
104+ }
105+ }
106+ }
0 commit comments