Skip to content

Commit 129390f

Browse files
committed
Copy improvements between {FFmpeg,RAIntegration}DownloaderForm
1 parent 9cd1b09 commit 129390f

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

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

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@ public partial class FFmpegDownloaderForm : Form
1515
{
1616
public FFmpegDownloaderForm()
1717
{
18+
_path = FFmpegService.FFmpegPath;
19+
_url = FFmpegService.Url;
20+
1821
InitializeComponent();
1922

20-
txtLocation.Text = FFmpegService.FFmpegPath;
21-
txtUrl.Text = FFmpegService.Url;
23+
txtLocation.Text = _path;
24+
txtUrl.Text = _url;
2225

2326
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.)";
2427
}
2528

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;
3036

3137
private void ThreadProc()
3238
{
@@ -40,50 +46,50 @@ private void Download()
4046

4147
try
4248
{
43-
DirectoryInfo parentDir = new(Path.GetDirectoryName(FFmpegService.FFmpegPath)!);
49+
DirectoryInfo parentDir = new(Path.GetDirectoryName(_path)!);
4450
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);
4654
using (var evt = new ManualResetEvent(false))
4755
{
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)
4963
{
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;
5465

55-
while (true)
66+
//if the gui thread ordered an exit, cancel the download and wait for it to acknowledge
67+
if (_exiting)
5668
{
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;
6672
}
6773
}
6874
}
6975

7076
// throw new Exception("test of download failure");
7177

7278
//if we were ordered to exit, bail without wasting any more time
73-
if (exiting) return;
79+
if (_exiting) return;
7480

7581
//try acquiring file
7682
using (var hf = new HawkFile(fn))
7783
{
7884
using (var exe = OSTailoredCode.IsUnixHost ? hf.BindArchiveMember("ffmpeg") : hf.BindFirstOf(".exe"))
7985
{
8086
//last chance. exiting, don't dump the new ffmpeg file
81-
if (exiting) return;
87+
if (_exiting) return;
8288
exe!.GetStream().CopyTo(fs);
8389
fs.Dispose();
8490
if (OSTailoredCode.IsUnixHost)
8591
{
86-
OSTailoredCode.ConstructSubshell("chmod", $"+x {FFmpegService.FFmpegPath}", checkStdout: false).Start();
92+
OSTailoredCode.ConstructSubshell("chmod", $"+x {_path}", checkStdout: false).Start();
8793
Thread.Sleep(50); // Linux I/O flush idk
8894
}
8995
}
@@ -92,11 +98,11 @@ private void Download()
9298
//make sure it worked
9399
if (!FFmpegService.QueryServiceAvailable()) throw new Exception("download failed");
94100

95-
succeeded = true;
101+
_succeeded = true;
96102
}
97103
catch (Exception e)
98104
{
99-
failed = true;
105+
_failed = true;
100106
Util.DebugWriteLine($"FFmpeg download failed with:\n{e}");
101107
}
102108
finally
@@ -116,9 +122,9 @@ private void btnDownload_Click(object sender, EventArgs e)
116122
{
117123
btnDownload.Text = "Downloading...";
118124
btnDownload.Enabled = false;
119-
failed = false;
120-
succeeded = false;
121-
pct = 0;
125+
_failed = false;
126+
_succeeded = false;
127+
_pct = 0;
122128
var t = new Thread(ThreadProc);
123129
t.Start();
124130
}
@@ -132,26 +138,26 @@ protected override void OnClosed(EventArgs e)
132138
{
133139
//inform the worker thread that it needs to try terminating without doing anything else
134140
//(it will linger on in background for a bit til it can service this)
135-
exiting = true;
141+
_exiting = true;
136142
}
137143

138144
private void timer1_Tick(object sender, EventArgs e)
139145
{
140146
//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)
143149
{
144-
failed = false;
145-
pct = 0;
150+
_failed = false;
151+
_pct = 0;
146152
btnDownload.Text = "FAILED - Download Again";
147153
btnDownload.Enabled = true;
148154
}
149-
progressBar1.Value = pct;
155+
progressBar1.Value = _pct;
150156
}
151157

152158
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
153159
{
154-
Util.OpenUrlExternal(FFmpegService.Url);
160+
Util.OpenUrlExternal(_url);
155161
}
156162
}
157163
}

src/BizHawk.Client.EmuHawk/RetroAchievements/RAIntegrationDownloaderForm.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private void Download()
5757

5858
try
5959
{
60+
DirectoryInfo parentDir = new(Path.GetDirectoryName(_path)!);
61+
if (!parentDir.Exists) parentDir.Create();
62+
// check writable before bothering with the download
63+
if (File.Exists(_path)) File.Delete(_path);
64+
using var fs = File.Create(_path);
6065
using (var evt = new ManualResetEvent(false))
6166
{
6267
using var client = new WebClient();
@@ -87,22 +92,18 @@ private void Download()
8792
//try acquiring file
8893
using (var dll = new HawkFile(fn))
8994
{
90-
var data = dll!.ReadAllBytes();
91-
9295
//last chance. exiting, don't dump the new RAIntegration file
9396
if (_exiting) return;
9497

95-
DirectoryInfo parentDir = new(Path.GetDirectoryName(_path)!);
96-
if (!parentDir.Exists) parentDir.Create();
97-
if (File.Exists(_path)) File.Delete(_path);
98-
File.WriteAllBytes(_path, data);
98+
dll.GetStream().CopyTo(fs);
9999
}
100100

101101
_succeeded = true;
102102
}
103-
catch
103+
catch (Exception e)
104104
{
105105
_failed = true;
106+
Util.DebugWriteLine($"RAIntegration download failed with:\n{e}");
106107
}
107108
finally
108109
{

0 commit comments

Comments
 (0)