From 767cc9059d7bd3a5c6c670251993cde189ff6d75 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Thu, 15 Dec 2022 23:45:29 -0800 Subject: [PATCH] Improve handling of RA http requests, add some handling in case RA sound files are missing. Normally this shouldn't be needed as docs specify if the wav file fails to load it plays the default beep sound, except actually it just throws in practice??? The 2.9 rcs apparently have the "overlay" folder missing, so the sound files aren't present. I'm assuming there's some issue with build scripts there for releases... --- .../RetroAchievements/RCheevos.Http.cs | 12 +++++------- .../RetroAchievements/RCheevos.Login.cs | 4 ++-- .../RetroAchievements/RCheevos.Sound.cs | 14 ++++++++++++++ .../RetroAchievements/RCheevos.cs | 12 ++++++------ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Http.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Http.cs index 089704b4f9..84138d4286 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Http.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Http.cs @@ -1,28 +1,25 @@ using System; using System.Net.Http; -using System.Text; using System.Threading.Tasks; namespace BizHawk.Client.EmuHawk { public partial class RCheevos { - private static readonly HttpClient _http = new(); + private static readonly HttpClient _http = new() { DefaultRequestHeaders = { ConnectionClose = true } }; private static async Task HttpGet(string url) { - _http.DefaultRequestHeaders.ConnectionClose = false; var response = await _http.GetAsync(url).ConfigureAwait(false); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); } - return null; + return new byte[1]; } private static async Task HttpPost(string url, string post) { - _http.DefaultRequestHeaders.ConnectionClose = true; HttpResponseMessage response; try { @@ -30,11 +27,12 @@ namespace BizHawk.Client.EmuHawk } catch (Exception e) { - return Encoding.UTF8.GetBytes(e.ToString()); // bleh + Console.WriteLine(e); + return new byte[1]; } if (!response.IsSuccessStatusCode) { - return null; + return new byte[1]; } return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); } diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Login.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Login.cs index 5c59de9fdd..31bcc47658 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Login.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Login.cs @@ -70,7 +70,7 @@ namespace BizHawk.Client.EmuHawk config.RAUsername = Username; config.RAToken = ApiToken; InitLoginDone.Set(); - if (EnableSoundEffects) _loginSound.Play(); + if (EnableSoundEffects) _loginSound.PlayNoExceptions(); return; } @@ -83,7 +83,7 @@ namespace BizHawk.Client.EmuHawk if (LoggedIn && EnableSoundEffects) { - _loginSound.Play(); + _loginSound.PlayNoExceptions(); } } diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Sound.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Sound.cs index 8c09fb407d..b6fd226035 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Sound.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Sound.cs @@ -18,4 +18,18 @@ namespace BizHawk.Client.EmuHawk private bool EnableSoundEffects { get; set; } } + + public static class SoundPlayerExtensions + { + public static void PlayNoExceptions(this SoundPlayer sound) + { + try + { + sound.Play(); + } + catch + { + } + } + } } \ No newline at end of file diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs index 9d2e605432..ea7569bbe4 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs @@ -436,7 +436,7 @@ namespace BizHawk.Client.EmuHawk var prefix = HardcoreMode ? "[HARDCORE] " : ""; _mainForm.AddOnScreenMessage($"{prefix}Achievement Unlocked!"); _mainForm.AddOnScreenMessage(cheevo.Description); - if (EnableSoundEffects) _unlockSound.Play(); + if (EnableSoundEffects) _unlockSound.PlayNoExceptions(); if (cheevo.IsOfficial) { @@ -457,7 +457,7 @@ namespace BizHawk.Client.EmuHawk var prefix = HardcoreMode ? "[HARDCORE] " : ""; _mainForm.AddOnScreenMessage($"{prefix}Achievement Primed!"); _mainForm.AddOnScreenMessage(cheevo.Description); - if (EnableSoundEffects) _infoSound.Play(); + if (EnableSoundEffects) _infoSound.PlayNoExceptions(); } break; @@ -476,7 +476,7 @@ namespace BizHawk.Client.EmuHawk CurrentLboard = lboard; _mainForm.AddOnScreenMessage($"Leaderboard Attempt Started!"); _mainForm.AddOnScreenMessage(lboard.Description); - if (EnableSoundEffects) _lboardStartSound.Play(); + if (EnableSoundEffects) _lboardStartSound.PlayNoExceptions(); } } @@ -498,7 +498,7 @@ namespace BizHawk.Client.EmuHawk _mainForm.AddOnScreenMessage($"Leaderboard Attempt Failed! ({lboard.Score})"); _mainForm.AddOnScreenMessage(lboard.Description); - if (EnableSoundEffects) _lboardFailedSound.Play(); + if (EnableSoundEffects) _lboardFailedSound.PlayNoExceptions(); } lboard.SetScore(0); @@ -536,7 +536,7 @@ namespace BizHawk.Client.EmuHawk _mainForm.AddOnScreenMessage($"Leaderboard Attempt Complete! ({lboard.Score})"); _mainForm.AddOnScreenMessage(lboard.Description); - if (EnableSoundEffects) _unlockSound.Play(); + if (EnableSoundEffects) _unlockSound.PlayNoExceptions(); } } @@ -563,7 +563,7 @@ namespace BizHawk.Client.EmuHawk var prefix = HardcoreMode ? "[HARDCORE] " : ""; _mainForm.AddOnScreenMessage($"{prefix}Achievement Unprimed!"); _mainForm.AddOnScreenMessage(cheevo.Description); - if (EnableSoundEffects) _infoSound.Play(); + if (EnableSoundEffects) _infoSound.PlayNoExceptions(); } break;