2020-06-27 23:36:53 -05:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2019-08-20 19:50:28 -07:00
|
|
|
|
using System.Media;
|
|
|
|
|
using PKHeX.Core;
|
2019-10-03 18:23:40 -07:00
|
|
|
|
using PKHeX.Drawing;
|
2019-08-20 19:50:28 -07:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
2019-10-03 19:09:02 -07:00
|
|
|
|
public sealed class CryPlayer
|
2019-08-20 19:50:28 -07:00
|
|
|
|
{
|
2020-12-21 23:37:07 -08:00
|
|
|
|
private readonly SoundPlayer Sounds = new();
|
2019-08-20 19:50:28 -07:00
|
|
|
|
|
2021-04-11 18:09:54 -07:00
|
|
|
|
public void PlayCry(ISpeciesForm pk, int format)
|
2019-08-20 19:50:28 -07:00
|
|
|
|
{
|
|
|
|
|
if (pk.Species == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-04-11 18:09:54 -07:00
|
|
|
|
string path = GetCryPath(pk, Main.CryPath, format);
|
2019-08-20 19:50:28 -07:00
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Sounds.SoundLocation = path;
|
|
|
|
|
try { Sounds.Play(); }
|
2020-09-18 22:11:13 -07:00
|
|
|
|
#pragma warning disable CA1031 // Do not catch general exception types
|
2020-06-27 23:36:53 -05:00
|
|
|
|
catch { Debug.WriteLine("Failed to play sound."); }
|
2020-09-18 22:11:13 -07:00
|
|
|
|
#pragma warning restore CA1031 // Do not catch general exception types
|
2019-08-20 19:50:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop() => Sounds.Stop();
|
|
|
|
|
|
2021-04-11 18:09:54 -07:00
|
|
|
|
private static string GetCryPath(ISpeciesForm pk, string cryFolder, int format)
|
2019-08-20 19:50:28 -07:00
|
|
|
|
{
|
2021-04-11 18:09:54 -07:00
|
|
|
|
var name = GetCryFileName(pk, format);
|
2019-08-20 19:50:28 -07:00
|
|
|
|
var path = Path.Combine(cryFolder, $"{name}.wav");
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
path = Path.Combine(cryFolder, $"{pk.Species}.wav");
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2019-10-03 18:35:33 -07:00
|
|
|
|
|
2021-04-11 18:09:54 -07:00
|
|
|
|
private static string GetCryFileName(ISpeciesForm pk, int format)
|
2019-10-03 18:35:33 -07:00
|
|
|
|
{
|
2020-12-10 20:42:30 -08:00
|
|
|
|
if (pk.Species == (int)Species.Urshifu && pk.Form == 1) // same sprite for both forms, but different cries
|
2020-06-19 18:51:15 -05:00
|
|
|
|
return "892-1";
|
|
|
|
|
|
2019-10-03 18:35:33 -07:00
|
|
|
|
// don't grab sprite of pkm, no gender specific cries
|
2021-04-11 18:09:54 -07:00
|
|
|
|
var res = SpriteName.GetResourceStringSprite(pk.Species, pk.Form, 0, 0, format);
|
2021-05-14 15:30:55 -07:00
|
|
|
|
|
|
|
|
|
// people like - instead of _ file names ;)
|
|
|
|
|
return res.Replace('_', '-')[1..]; // skip leading underscore
|
2019-10-03 18:35:33 -07:00
|
|
|
|
}
|
2019-08-20 19:50:28 -07:00
|
|
|
|
}
|
2021-05-14 15:30:55 -07:00
|
|
|
|
}
|