Skip to content

Commit b52bce6

Browse files
committed
Copy improvements between {FFmpeg,RAIntegration}DownloaderForm
1 parent 3ba9831 commit b52bce6

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
@@ -16,18 +16,24 @@ public partial class FFmpegDownloaderForm : Form
1616
{
1717
public FFmpegDownloaderForm()
1818
{
19+
_path = FFmpegService.FFmpegPath;
20+
_url = FFmpegService.Url;
21+
1922
InitializeComponent();
2023

21-
txtLocation.Text = FFmpegService.FFmpegPath;
22-
txtUrl.Text = FFmpegService.Url;
24+
txtLocation.Text = _path;
25+
txtUrl.Text = _url;
2326

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

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

3238
private void ThreadProc()
3339
{
@@ -41,50 +47,50 @@ private void Download()
4147

4248
try
4349
{
44-
DirectoryInfo parentDir = new(Path.GetDirectoryName(FFmpegService.FFmpegPath)!);
50+
DirectoryInfo parentDir = new(Path.GetDirectoryName(_path)!);
4551
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);
4755
using (var evt = new ManualResetEvent(false))
4856
{
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)
5064
{
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;
5566

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

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

7379
//if we were ordered to exit, bail without wasting any more time
74-
if (exiting) return;
80+
if (_exiting) return;
7581

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

96-
succeeded = true;
102+
_succeeded = true;
97103
}
98104
catch (Exception e)
99105
{
100-
failed = true;
106+
_failed = true;
101107
Util.DebugWriteLine($"FFmpeg download failed with:\n{e}");
102108
}
103109
finally
@@ -117,9 +123,9 @@ private void btnDownload_Click(object sender, EventArgs e)
117123
{
118124
btnDownload.Text = "Downloading...";
119125
btnDownload.Enabled = false;
120-
failed = false;
121-
succeeded = false;
122-
pct = 0;
126+
_failed = false;
127+
_succeeded = false;
128+
_pct = 0;
123129
var t = new Thread(ThreadProc);
124130
t.Start();
125131
}
@@ -133,26 +139,26 @@ protected override void OnClosed(EventArgs e)
133139
{
134140
//inform the worker thread that it needs to try terminating without doing anything else
135141
//(it will linger on in background for a bit til it can service this)
136-
exiting = true;
142+
_exiting = true;
137143
}
138144

139145
private void timer1_Tick(object sender, EventArgs e)
140146
{
141147
//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)
144150
{
145-
failed = false;
146-
pct = 0;
151+
_failed = false;
152+
_pct = 0;
147153
btnDownload.Text = "FAILED - Download Again";
148154
btnDownload.Enabled = true;
149155
}
150-
progressBar1.Value = pct;
156+
progressBar1.Value = _pct;
151157
}
152158

153159
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
154160
{
155-
Process.Start(FFmpegService.Url);
161+
Process.Start(_url);
156162
}
157163
}
158164
}

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

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

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

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

102102
_succeeded = true;
103103
}
104-
catch
104+
catch (Exception e)
105105
{
106106
_failed = true;
107+
Util.DebugWriteLine($"RAIntegration download failed with:\n{e}");
107108
}
108109
finally
109110
{

0 commit comments

Comments
 (0)