@@ -15,18 +15,24 @@ public partial class FFmpegDownloaderForm : Form
15
15
{
16
16
public FFmpegDownloaderForm ( )
17
17
{
18
+ _path = FFmpegService . FFmpegPath ;
19
+ _url = FFmpegService . Url ;
20
+
18
21
InitializeComponent ( ) ;
19
22
20
- txtLocation . Text = FFmpegService . FFmpegPath ;
21
- txtUrl . Text = FFmpegService . Url ;
23
+ txtLocation . Text = _path ;
24
+ txtUrl . Text = _url ;
22
25
23
26
if ( OSTailoredCode . IsUnixHost ) textBox1 . Text = string . Join ( "\n " , textBox1 . Text . Split ( '\n ' ) . Take ( 3 ) ) + "\n \n (Linux user: If installing manually, you can use a symlink.)" ;
24
27
}
25
28
26
- private int pct = 0 ;
27
- private bool exiting = false ;
28
- private bool succeeded = false ;
29
- private bool failed = false ;
29
+ private readonly string _path ;
30
+ private readonly string _url ;
31
+
32
+ private int _pct = 0 ;
33
+ private bool _exiting = false ;
34
+ private bool _succeeded = false ;
35
+ private bool _failed = false ;
30
36
31
37
private void ThreadProc ( )
32
38
{
@@ -40,50 +46,50 @@ private void Download()
40
46
41
47
try
42
48
{
43
- DirectoryInfo parentDir = new ( Path . GetDirectoryName ( FFmpegService . FFmpegPath ) ! ) ;
49
+ DirectoryInfo parentDir = new ( Path . GetDirectoryName ( _path ) ! ) ;
44
50
if ( ! parentDir . Exists ) parentDir . Create ( ) ;
45
- using var fs = File . Create ( FFmpegService . FFmpegPath ) ; // check writable before bothering with the download
51
+ // check writable before bothering with the download
52
+ if ( File . Exists ( _path ) ) File . Delete ( _path ) ;
53
+ using var fs = File . Create ( _path ) ;
46
54
using ( var evt = new ManualResetEvent ( false ) )
47
55
{
48
- using ( var client = new WebClient ( ) )
56
+ using var client = new WebClient ( ) ;
57
+ ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
58
+ client . DownloadFileAsync ( new Uri ( _url ) , fn ) ;
59
+ client . DownloadProgressChanged += ( _ , progressArgs ) => _pct = progressArgs . ProgressPercentage ;
60
+ client . DownloadFileCompleted += ( _ , _ ) => evt . Set ( ) ; // we don't really need a status, we'll just try to unzip it when it's done
61
+
62
+ while ( true )
49
63
{
50
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
51
- client . DownloadFileAsync ( new Uri ( FFmpegService . Url ) , fn ) ;
52
- client . DownloadProgressChanged += ( _ , progressArgs ) => pct = progressArgs . ProgressPercentage ;
53
- client . DownloadFileCompleted += ( _ , _ ) => evt . Set ( ) ; // we don't really need a status, we'll just try to unzip it when it's done
64
+ if ( evt . WaitOne ( 10 ) ) break ;
54
65
55
- while ( true )
66
+ //if the gui thread ordered an exit, cancel the download and wait for it to acknowledge
67
+ if ( _exiting )
56
68
{
57
- if ( evt . WaitOne ( 10 ) ) break ;
58
-
59
- //if the gui thread ordered an exit, cancel the download and wait for it to acknowledge
60
- if ( exiting )
61
- {
62
- client . CancelAsync ( ) ;
63
- evt . WaitOne ( ) ;
64
- break ;
65
- }
69
+ client . CancelAsync ( ) ;
70
+ evt . WaitOne ( ) ;
71
+ break ;
66
72
}
67
73
}
68
74
}
69
75
70
76
// throw new Exception("test of download failure");
71
77
72
78
//if we were ordered to exit, bail without wasting any more time
73
- if ( exiting ) return ;
79
+ if ( _exiting ) return ;
74
80
75
81
//try acquiring file
76
82
using ( var hf = new HawkFile ( fn ) )
77
83
{
78
84
using ( var exe = OSTailoredCode . IsUnixHost ? hf . BindArchiveMember ( "ffmpeg" ) : hf . BindFirstOf ( ".exe" ) )
79
85
{
80
86
//last chance. exiting, don't dump the new ffmpeg file
81
- if ( exiting ) return ;
87
+ if ( _exiting ) return ;
82
88
exe ! . GetStream ( ) . CopyTo ( fs ) ;
83
89
fs . Dispose ( ) ;
84
90
if ( OSTailoredCode . IsUnixHost )
85
91
{
86
- OSTailoredCode . ConstructSubshell ( "chmod" , $ "+x { FFmpegService . FFmpegPath } ", checkStdout : false ) . Start ( ) ;
92
+ OSTailoredCode . ConstructSubshell ( "chmod" , $ "+x { _path } ", checkStdout : false ) . Start ( ) ;
87
93
Thread . Sleep ( 50 ) ; // Linux I/O flush idk
88
94
}
89
95
}
@@ -92,11 +98,11 @@ private void Download()
92
98
//make sure it worked
93
99
if ( ! FFmpegService . QueryServiceAvailable ( ) ) throw new Exception ( "download failed" ) ;
94
100
95
- succeeded = true ;
101
+ _succeeded = true ;
96
102
}
97
103
catch ( Exception e )
98
104
{
99
- failed = true ;
105
+ _failed = true ;
100
106
Util . DebugWriteLine ( $ "FFmpeg download failed with:\n { e } ") ;
101
107
}
102
108
finally
@@ -116,9 +122,9 @@ private void btnDownload_Click(object sender, EventArgs e)
116
122
{
117
123
btnDownload . Text = "Downloading..." ;
118
124
btnDownload . Enabled = false ;
119
- failed = false ;
120
- succeeded = false ;
121
- pct = 0 ;
125
+ _failed = false ;
126
+ _succeeded = false ;
127
+ _pct = 0 ;
122
128
var t = new Thread ( ThreadProc ) ;
123
129
t . Start ( ) ;
124
130
}
@@ -132,26 +138,26 @@ protected override void OnClosed(EventArgs e)
132
138
{
133
139
//inform the worker thread that it needs to try terminating without doing anything else
134
140
//(it will linger on in background for a bit til it can service this)
135
- exiting = true ;
141
+ _exiting = true ;
136
142
}
137
143
138
144
private void timer1_Tick ( object sender , EventArgs e )
139
145
{
140
146
//if it's done, close the window. the user will be smart enough to reopen it
141
- if ( succeeded ) Close ( ) ;
142
- if ( failed )
147
+ if ( _succeeded ) Close ( ) ;
148
+ if ( _failed )
143
149
{
144
- failed = false ;
145
- pct = 0 ;
150
+ _failed = false ;
151
+ _pct = 0 ;
146
152
btnDownload . Text = "FAILED - Download Again" ;
147
153
btnDownload . Enabled = true ;
148
154
}
149
- progressBar1 . Value = pct ;
155
+ progressBar1 . Value = _pct ;
150
156
}
151
157
152
158
private void linkLabel1_LinkClicked ( object sender , LinkLabelLinkClickedEventArgs e )
153
159
{
154
- Util . OpenUrlExternal ( FFmpegService . Url ) ;
160
+ Util . OpenUrlExternal ( _url ) ;
155
161
}
156
162
}
157
163
}
0 commit comments