Skip to content

Commit 86cf9d4

Browse files
committed
Extract parent class from {FFmpeg,RAIntegration}DownloaderForm
1 parent 129390f commit 86cf9d4

File tree

7 files changed

+222
-607
lines changed

7 files changed

+222
-607
lines changed
Lines changed: 19 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,34 @@
11
using System.IO;
2-
using System.Linq;
3-
using System.Net;
4-
using System.Threading;
5-
using System.Windows.Forms;
62

73
using BizHawk.Common;
84

95
namespace BizHawk.Client.EmuHawk
106
{
11-
/// <summary>
12-
/// Downloads FFmpeg
13-
/// </summary>
14-
public partial class FFmpegDownloaderForm : Form
7+
public sealed class FFmpegDownloaderForm : DownloaderForm
158
{
16-
public FFmpegDownloaderForm()
17-
{
18-
_path = FFmpegService.FFmpegPath;
19-
_url = FFmpegService.Url;
20-
21-
InitializeComponent();
22-
23-
txtLocation.Text = _path;
24-
txtUrl.Text = _url;
25-
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.)";
27-
}
28-
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;
36-
37-
private void ThreadProc()
38-
{
39-
Download();
40-
}
41-
42-
private void Download()
43-
{
44-
//the temp file is owned by this thread
45-
var fn = TempFileManager.GetTempFilename("ffmpeg_download", ".7z", false);
46-
47-
try
48-
{
49-
DirectoryInfo parentDir = new(Path.GetDirectoryName(_path)!);
50-
if (!parentDir.Exists) parentDir.Create();
51-
// check writable before bothering with the download
52-
if (File.Exists(_path)) File.Delete(_path);
53-
using var fs = File.Create(_path);
54-
using (var evt = new ManualResetEvent(false))
55-
{
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)
63-
{
64-
if (evt.WaitOne(10)) break;
65-
66-
//if the gui thread ordered an exit, cancel the download and wait for it to acknowledge
67-
if (_exiting)
68-
{
69-
client.CancelAsync();
70-
evt.WaitOne();
71-
break;
72-
}
73-
}
74-
}
9+
protected override string ComponentName
10+
=> "FFmpeg";
7511

76-
// throw new Exception("test of download failure");
12+
protected override string DownloadTemp { get; }
13+
= TempFileManager.GetTempFilename("ffmpeg_download", ".7z", delete: false);
7714

78-
//if we were ordered to exit, bail without wasting any more time
79-
if (_exiting) return;
80-
81-
//try acquiring file
82-
using (var hf = new HawkFile(fn))
83-
{
84-
using (var exe = OSTailoredCode.IsUnixHost ? hf.BindArchiveMember("ffmpeg") : hf.BindFirstOf(".exe"))
85-
{
86-
//last chance. exiting, don't dump the new ffmpeg file
87-
if (_exiting) return;
88-
exe!.GetStream().CopyTo(fs);
89-
fs.Dispose();
90-
if (OSTailoredCode.IsUnixHost)
91-
{
92-
OSTailoredCode.ConstructSubshell("chmod", $"+x {_path}", checkStdout: false).Start();
93-
Thread.Sleep(50); // Linux I/O flush idk
94-
}
95-
}
96-
}
97-
98-
//make sure it worked
99-
if (!FFmpegService.QueryServiceAvailable()) throw new Exception("download failed");
100-
101-
_succeeded = true;
102-
}
103-
catch (Exception e)
104-
{
105-
_failed = true;
106-
Util.DebugWriteLine($"FFmpeg download failed with:\n{e}");
107-
}
108-
finally
109-
{
110-
try
111-
{
112-
File.Delete(fn);
113-
}
114-
catch
115-
{
116-
// ignore
117-
}
118-
}
119-
}
120-
121-
private void btnDownload_Click(object sender, EventArgs e)
122-
{
123-
btnDownload.Text = "Downloading...";
124-
btnDownload.Enabled = false;
125-
_failed = false;
126-
_succeeded = false;
127-
_pct = 0;
128-
var t = new Thread(ThreadProc);
129-
t.Start();
130-
}
131-
132-
private void btnCancel_Click(object sender, EventArgs e)
133-
{
134-
Close();
135-
}
136-
137-
protected override void OnClosed(EventArgs e)
15+
public FFmpegDownloaderForm()
13816
{
139-
//inform the worker thread that it needs to try terminating without doing anything else
140-
//(it will linger on in background for a bit til it can service this)
141-
_exiting = true;
17+
Description = "BizHawk relies on a specific version of FFmpeg. No other version will do. The wrong version will be ignored. There is no way to override this behavior."
18+
+ "\n\nThe required version could not be found."
19+
+ (OSTailoredCode.IsUnixHost
20+
? "\n\n(Linux user: If installing manually, you can use a symlink.)"
21+
: "\n\nUse this dialog to download it automatically, or download it yourself from the URL below and place it in the specified location.");
22+
DownloadFrom = FFmpegService.Url;
23+
DownloadTo = FFmpegService.FFmpegPath;
14224
}
14325

144-
private void timer1_Tick(object sender, EventArgs e)
145-
{
146-
//if it's done, close the window. the user will be smart enough to reopen it
147-
if (_succeeded) Close();
148-
if (_failed)
149-
{
150-
_failed = false;
151-
_pct = 0;
152-
btnDownload.Text = "FAILED - Download Again";
153-
btnDownload.Enabled = true;
154-
}
155-
progressBar1.Value = _pct;
156-
}
26+
protected override Stream GetExtractionStream(HawkFile downloaded)
27+
=> (OSTailoredCode.IsUnixHost
28+
? downloaded.BindArchiveMember("ffmpeg")!
29+
: downloaded.BindFirstOf(".exe")).GetStream();
15730

158-
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
159-
{
160-
Util.OpenUrlExternal(_url);
161-
}
31+
protected override bool PostChmodCheck()
32+
=> FFmpegService.QueryServiceAvailable();
16233
}
16334
}

src/BizHawk.Client.EmuHawk/AVOut/FFmpegDownloaderForm.resx

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/BizHawk.Client.EmuHawk/AVOut/FFmpegDownloaderForm.Designer.cs renamed to src/BizHawk.Client.EmuHawk/DownloaderForm.Designer.cs

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)