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