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...
This commit is contained in:
CasualPokePlayer 2022-12-15 23:45:29 -08:00
parent f3ee3e7956
commit 767cc9059d
4 changed files with 27 additions and 15 deletions

View File

@ -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<byte[]> 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<byte[]> 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);
}

View File

@ -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();
}
}

View File

@ -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
{
}
}
}
}

View File

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