diff --git a/ShiftOS.Objects/Save.cs b/ShiftOS.Objects/Save.cs index 0fef7b3..cbd77e2 100644 --- a/ShiftOS.Objects/Save.cs +++ b/ShiftOS.Objects/Save.cs @@ -34,9 +34,9 @@ namespace ShiftOS.Objects //Better to store this stuff server-side so we can do some neat stuff with hacking... public class Save { - - public int MusicVolume { get; set; } - public int SfxVolume { get; set; } + public bool MusicEnabled = true; + public bool SoundEnabled = true; + public int MusicVolume = 100; [Obsolete("This save variable is no longer used in Beta 2.4 and above of ShiftOS. Please use ShiftOS.Engine.SaveSystem.CurrentUser.Username to access the current user's username.")] public string Username { get; set; } diff --git a/ShiftOS.WinForms/AudioManager.cs b/ShiftOS.WinForms/AudioManager.cs index ec12614..afa0d78 100644 --- a/ShiftOS.WinForms/AudioManager.cs +++ b/ShiftOS.WinForms/AudioManager.cs @@ -88,23 +88,46 @@ namespace ShiftOS.WinForms }; while (shuttingDown == false) { - str = new MemoryStream(GetRandomSong()); - mp3 = new NAudio.Wave.Mp3FileReader(str); - o = new NAudio.Wave.WaveOut(); - o.Init(mp3); - bool c = false; - o.Play(); - o.PlaybackStopped += (s, a) => + if (Engine.SaveSystem.CurrentSave != null) { - c = true; - }; - while (!c) - { - try + if (Engine.SaveSystem.CurrentSave.MusicEnabled) { - o.Volume = (float)Engine.SaveSystem.CurrentSave.MusicVolume / 100; + str = new MemoryStream(GetRandomSong()); + mp3 = new NAudio.Wave.Mp3FileReader(str); + o = new NAudio.Wave.WaveOut(); + o.Init(mp3); + bool c = false; + o.Play(); + o.PlaybackStopped += (s, a) => + { + c = true; + }; + + while (!c) + { + if (Engine.SaveSystem.CurrentSave.MusicEnabled) + { + try + { + o.Volume = (float)Engine.SaveSystem.CurrentSave.MusicVolume / 100; + } + catch { } + } + else + { + o.Stop(); + c = true; + } + Thread.Sleep(10); + } } - catch { } + else + { + Thread.Sleep(10); + } + } + else + { Thread.Sleep(10); } } diff --git a/ShiftOS_TheReturn/AudioManager.cs b/ShiftOS_TheReturn/AudioManager.cs index 553a1d9..0a1a210 100644 --- a/ShiftOS_TheReturn/AudioManager.cs +++ b/ShiftOS_TheReturn/AudioManager.cs @@ -83,21 +83,28 @@ namespace ShiftOS.Engine /// The file to play. public static void Play(string file) { - try + bool play = true; + float volume = 1f; + if (SaveSystem.CurrentSave != null) { - _reader = new AudioFileReader(file); - _out = new WaveOut(); - _out.Init(_reader); - _out.Play(); - if (SaveSystem.CurrentSave == null) - _out.Volume = 1.0f; - else - _out.Volume = (float)SaveSystem.CurrentSave.MusicVolume / 100; - _out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); }; + play = (SaveSystem.CurrentSave.SoundEnabled); + volume = (float)SaveSystem.CurrentSave.MusicVolume / 100f; } - catch(Exception ex) + if (play) { - Console.WriteLine("Audio error: " + ex.Message); + try + { + _reader = new AudioFileReader(file); + _out = new WaveOut(); + _out.Init(_reader); + _out.Volume = volume; + _out.Play(); + _out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); }; + } + catch (Exception ex) + { + Console.WriteLine("Audio error: " + ex.Message); + } } } @@ -107,15 +114,27 @@ namespace ShiftOS.Engine /// The stream to read from. public static void PlayStream(Stream str) { - var bytes = new byte[str.Length]; - str.Read(bytes, 0, bytes.Length); - ShiftOS.Engine.AudioManager.Stop(); - if (File.Exists("snd.wav")) - File.Delete("snd.wav"); - File.WriteAllBytes("snd.wav", bytes); - - ShiftOS.Engine.AudioManager.Play("snd.wav"); - + try + { + bool play = true; + float volume = 1f; + if (SaveSystem.CurrentSave != null) + { + play = (SaveSystem.CurrentSave.SoundEnabled); + volume = (float)SaveSystem.CurrentSave.MusicVolume / 100f; + } + if (play) + { + ShiftOS.Engine.AudioManager.Stop(); + _out = new WaveOut(); + var mp3 = new WaveFileReader(str); + _out.Init(mp3); + _out.Volume = volume; + _out.Play(); + _out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); }; + } + } + catch { } } public static event Action PlayCompleted; diff --git a/ShiftOS_TheReturn/Commands.cs b/ShiftOS_TheReturn/Commands.cs index ec89539..e379a50 100644 --- a/ShiftOS_TheReturn/Commands.cs +++ b/ShiftOS_TheReturn/Commands.cs @@ -383,26 +383,48 @@ namespace ShiftOS.Engine [Namespace("sos")] public static class ShiftOSCommands { - [Command("setsfxvolume", description = "Set the volume of various sound effects to a value between 1 and 100.")] + + [Command("setsfxenabled", description = "Set whether or not sound effects are enabled in the system.")] [RequiresArgument("value")] - public static bool SetSfxVolume(Dictionary args) + public static bool SetSfxEnabled(Dictionary args) { - int value = int.Parse(args["value"].ToString()); - if (value >= 0 && value <= 100) + try { - SaveSystem.CurrentSave.SfxVolume = value; + bool value = Convert.ToBoolean(args["value"].ToString()); + SaveSystem.CurrentSave.SoundEnabled = value; SaveSystem.SaveGame(); } - else + catch { - Console.WriteLine("Volume must be between 0 and 100!"); + Console.WriteLine("Error: Value must be either true or false."); } return true; } - [Command("setmusicvolume", description ="Set the music volume to a value between 1 and 100.")] + + + [Command("setmusicenabled", description = "Set whether or not music is enabled in the system.")] [RequiresArgument("value")] - public static bool SetMusicVolume(Dictionary args) + public static bool SetMusicEnabled(Dictionary args) + { + try + { + bool value = Convert.ToBoolean(args["value"].ToString()); + SaveSystem.CurrentSave.MusicEnabled = value; + SaveSystem.SaveGame(); + } + catch + { + Console.WriteLine("Error: Value must be either true or false."); + } + return true; + } + + + + [Command("setsfxvolume", description ="Set the system sound volume to a value between 1 and 100.")] + [RequiresArgument("value")] + public static bool SetSfxVolume(Dictionary args) { int value = int.Parse(args["value"].ToString()); if(value >= 0 && value <= 100)