From fc043e02196ca9d157b397da17d668245b36c264 Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Tue, 17 Mar 2020 13:45:29 +0100 Subject: [PATCH] Improved LinuxSoundManager.cs --- testexetrisathlon/OSCheck.cs | 24 ++++++++++ testexetrisathlon/Program.cs | 47 ++++++++----------- testexetrisathlon/SoundManagement/Beeper.cs | 9 ++-- .../SoundManagement/LinuxSoundManager.cs | 27 +++++------ .../SoundManagement/ProcessLoop.cs | 33 +++++++++++++ .../SoundManagement/WindowsSoundManager.cs | 2 +- 6 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 testexetrisathlon/OSCheck.cs create mode 100644 testexetrisathlon/SoundManagement/ProcessLoop.cs diff --git a/testexetrisathlon/OSCheck.cs b/testexetrisathlon/OSCheck.cs new file mode 100644 index 0000000..c86761e --- /dev/null +++ b/testexetrisathlon/OSCheck.cs @@ -0,0 +1,24 @@ +using System; +using System.Linq; + +namespace testexetrisathlon +{ + internal static class OSCheck + { + private static bool _checkedWindows; + private static bool _isWindows; + + public static bool IsWindows + { + get + { + if (_checkedWindows) return _isWindows; + _isWindows = + new[] {PlatformID.Win32S, PlatformID.Win32Windows, PlatformID.Win32NT, PlatformID.WinCE} + .Contains(Environment.OSVersion.Platform); + _checkedWindows = true; + return _isWindows; + } + } + } +} \ No newline at end of file diff --git a/testexetrisathlon/Program.cs b/testexetrisathlon/Program.cs index b7d1ed6..1f0d99f 100644 --- a/testexetrisathlon/Program.cs +++ b/testexetrisathlon/Program.cs @@ -1,11 +1,8 @@ -//#define WINDOWS - -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; using testexetrisathlon.SoundManagement; using static System.Console; @@ -59,20 +56,6 @@ namespace testexetrisathlon #if DEBUG private static void Main() { - soundManager = new -#if WINDOW - WindowsSoundManager -#else - LinuxSoundManager -#endif - (); - soundManager.Init(new Dictionary - { - {"Intro", "testexetrisathlon.Intro.wav"}, - {"InGame1", "testexetrisathlon.InGame1.wav"}, - {"InGame2", "testexetrisathlon.InGame2.wav"}, - {"GameOver", "testexetrisathlon.GameOver.wav"} - }); Debug = true; #else private static void Main(string[] args) @@ -81,11 +64,20 @@ namespace testexetrisathlon #endif BackgroundColor = ConsoleColor.Red; ForegroundColor = ConsoleColor.Yellow; -#if WINDOWS - SetWindowSize(42, 29); - if (Debug) - SetWindowSize(50, 40); -#endif + soundManager = OSCheck.IsWindows ? (ISoundManager) new WindowsSoundManager() : new LinuxSoundManager(); + soundManager.Init(new Dictionary + { + {"Intro", "testexetrisathlon.Intro.wav"}, + {"InGame1", "testexetrisathlon.InGame1.wav"}, + {"InGame2", "testexetrisathlon.InGame2.wav"}, + {"GameOver", "testexetrisathlon.GameOver.wav"} + }); + if (OSCheck.IsWindows) + { + SetWindowSize(42, 29); + if (Debug) + SetWindowSize(50, 40); + } SetCursorPosition(0, 0); Clear(); bool playing = true; @@ -220,10 +212,11 @@ namespace testexetrisathlon { Clear(); DrawSymbol(); -#if !WINDOWS - SetCursorPosition(2, 19); - Write("Volume is not supported in this build!"); -#endif + if (!OSCheck.IsWindows) + { + SetCursorPosition(2, 19); + Write("Volume is not supported in this build!"); + } bool barActive = true; int currentSetting = 0; while (barActive) diff --git a/testexetrisathlon/SoundManagement/Beeper.cs b/testexetrisathlon/SoundManagement/Beeper.cs index b83d1e5..ba5804f 100644 --- a/testexetrisathlon/SoundManagement/Beeper.cs +++ b/testexetrisathlon/SoundManagement/Beeper.cs @@ -6,11 +6,10 @@ namespace testexetrisathlon.SoundManagement { public static void Beep(int frequency, int duration) { -#if WINDOWS - Console.Beep(frequency, duration); -#else - Console.Write("\a"); -#endif + if (OSCheck.IsWindows) + Console.Beep(frequency, duration); + else + Console.Write("\a"); } } } \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/LinuxSoundManager.cs b/testexetrisathlon/SoundManagement/LinuxSoundManager.cs index a8e26fb..9ac1fec 100644 --- a/testexetrisathlon/SoundManagement/LinuxSoundManager.cs +++ b/testexetrisathlon/SoundManagement/LinuxSoundManager.cs @@ -8,12 +8,14 @@ namespace testexetrisathlon.SoundManagement public class LinuxSoundManager : ISoundManager { private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); - private Dictionary _files; + private ProcessLoop? _alsaProc; private string _current; - private Process? alsa; + private Dictionary _files; + public void Dispose() { foreach (string file in _files.Values) File.Delete(file); + if (_alsaProc != null) _alsaProc.IsRunning = false; } public void Init(Dictionary manifestResources) @@ -35,27 +37,20 @@ namespace testexetrisathlon.SoundManagement public void SetCurrent(string id) { if (_current == id) return; - alsa?.Kill(); + if (_alsaProc != null) _alsaProc.IsRunning = false; _current = id; //TODO fix actually killing, remove orphan processes - alsa = new Process + _alsaProc = new ProcessLoop(new ProcessStartInfo { - StartInfo = new ProcessStartInfo - { - FileName = "/bin/sh", - Arguments = $"-c \"while [ true ]; do aplay -q {_files[id].Replace("\"", "\\\"")}; done\"", - RedirectStandardOutput = false, - RedirectStandardInput = false, - UseShellExecute = false, - CreateNoWindow = true, - } - }; - alsa.Start(); + FileName = "aplay", + Arguments = $"-q {_files[id].Replace("\"", "\\\"")}", + CreateNoWindow = true + }); + _alsaProc.CreateLoopThread().Start(); } public void SetVolume(int percent) { - } } } \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/ProcessLoop.cs b/testexetrisathlon/SoundManagement/ProcessLoop.cs new file mode 100644 index 0000000..1a03af5 --- /dev/null +++ b/testexetrisathlon/SoundManagement/ProcessLoop.cs @@ -0,0 +1,33 @@ +using System.Diagnostics; +using System.Threading; + +namespace testexetrisathlon.SoundManagement +{ + internal class ProcessLoop + { + private readonly ProcessStartInfo _info; + private Process _currentProc; + private bool _isRunning = true; + + public ProcessLoop(ProcessStartInfo info) => _info = info; + + public bool IsRunning + { + set + { + if (_isRunning && !value) + _currentProc.Kill(); + _isRunning = value; + } + } + + public Thread CreateLoopThread() => new Thread(() => + { + while (_isRunning) + { + _currentProc = Process.Start(_info); + _currentProc.WaitForExit(); + } + }); + } +} \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/WindowsSoundManager.cs b/testexetrisathlon/SoundManagement/WindowsSoundManager.cs index 0136d2b..92de78a 100644 --- a/testexetrisathlon/SoundManagement/WindowsSoundManager.cs +++ b/testexetrisathlon/SoundManagement/WindowsSoundManager.cs @@ -8,10 +8,10 @@ namespace testexetrisathlon.SoundManagement public sealed class WindowsSoundManager : ISoundManager { private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); - private WaveOutEvent _output; private WaveStream _current; private string _currentId; private Dictionary _loadedSounds; + private WaveOutEvent _output; public void Init(Dictionary manifestResources) {