From 03058694906e5aa0c55aeecbbf5a821a080a9937 Mon Sep 17 00:00:00 2001 From: xenolightning Date: Fri, 16 Sep 2016 17:18:37 +1200 Subject: [PATCH] Minor improvements accross a range of components --- .../CoreAudioController.cs | 58 +++--- .../CoreAudioDevice.cs | 1 + .../Internal/AudioEndpointVolumeCallback.cs | 75 ++++---- .../Internal/Interfaces/WaveFormat.cs | 2 - .../Internal/MultimediaDeviceCollection.cs | 6 +- .../Internal/SystemEventNotifcationClient.cs | 169 ++++++++++-------- .../FilteredObservableTests.cs | 2 + .../Stubs/DeviceStub.cs | 2 +- AudioSwitcher.AudioApi/BitDepth.cs | 1 + .../Capabilities/ISpeakerConfiguration.cs | 6 +- AudioSwitcher.AudioApi/Device.cs | 6 + .../Observables/AsyncBroadcaster.cs | 7 +- .../Observables/Broadcaster.cs | 7 +- .../Observables/BroadcasterBase.cs | 11 +- .../Observables/FilteredBroadcaster.cs | 3 +- .../Properties/AssemblyInfo.cs | 2 + AudioSwitcher.Scripting.Lua/LuaEngine.cs | 3 - .../LuaExecutionContext.cs | 16 +- .../Properties/AssemblyInfo.cs | 1 - AudioSwitcher.Scripting/ExecutionResult.cs | 1 + 20 files changed, 197 insertions(+), 182 deletions(-) diff --git a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs index b9a89dc..3775160 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioController.cs @@ -25,17 +25,24 @@ public CoreAudioController() { // ReSharper disable once SuspiciousTypeConversion.Global _innerEnumerator = ComObjectFactory.GetDeviceEnumerator(); + _systemEvents = new SystemEventNotifcationClient(_innerEnumerator); if (_innerEnumerator == null) return; - _systemEvents = new SystemEventNotifcationClient(_innerEnumerator); - _systemEvents.DeviceAdded.Subscribe(x => OnDeviceAdded(x.DeviceId)); _systemEvents.DeviceRemoved.Subscribe(x => OnDeviceRemoved(x.DeviceId)); - }); - RefreshSystemDevices(); + _deviceCache = new HashSet(); + IMultimediaDeviceCollection collection; + _innerEnumerator.EnumAudioEndpoints(EDataFlow.All, EDeviceState.All, out collection); + + using (var coll = new MultimediaDeviceCollection(collection)) + { + foreach (var mDev in coll) + CacheDevice(mDev); + } + }); } internal SystemEventNotifcationClient SystemEvents => _systemEvents; @@ -63,25 +70,20 @@ private void OnDeviceRemoved(string deviceId) protected override void Dispose(bool disposing) { - ComThread.BeginInvoke(() => + if (_systemEvents != null) { - if (_systemEvents != null) - { - _systemEvents.Dispose(); - _systemEvents = null; - } + _systemEvents.Dispose(); + _systemEvents = null; + } - _innerEnumerator = null; - }) - .ContinueWith(x => + foreach (var device in _deviceCache) { - foreach (var device in _deviceCache) - { - device.Dispose(); - } - _deviceCache?.Clear(); - _lock?.Dispose(); - }); + device.Dispose(); + } + + _deviceCache?.Clear(); + _lock?.Dispose(); + _innerEnumerator = null; base.Dispose(disposing); @@ -120,22 +122,6 @@ private CoreAudioDevice GetDevice(string realId) } } - private void RefreshSystemDevices() - { - ComThread.Invoke(() => - { - _deviceCache = new HashSet(); - IMultimediaDeviceCollection collection; - _innerEnumerator.EnumAudioEndpoints(EDataFlow.All, EDeviceState.All, out collection); - - using (var coll = new MultimediaDeviceCollection(collection)) - { - foreach (var mDev in coll) - CacheDevice(mDev); - } - }); - } - private CoreAudioDevice GetOrAddDeviceFromRealId(string deviceId) { //This pre-check here may prevent more com objects from being created diff --git a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs index df67f92..64d227a 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/CoreAudioDevice.cs @@ -224,6 +224,7 @@ protected override void Dispose(bool disposing) { if (!_isDisposed) { + _properties?.Dispose(); _peakValueTimerSubscription?.Dispose(); ComThread.BeginInvoke(() => diff --git a/AudioSwitcher.AudioApi.CoreAudio/Internal/AudioEndpointVolumeCallback.cs b/AudioSwitcher.AudioApi.CoreAudio/Internal/AudioEndpointVolumeCallback.cs index f7f0a04..2453dd1 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/Internal/AudioEndpointVolumeCallback.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/Internal/AudioEndpointVolumeCallback.cs @@ -46,43 +46,50 @@ internal AudioEndpointVolumeCallback(AudioEndpointVolume handler) public int OnNotify(IntPtr notifyData) { - //Since AUDIO_VOLUME_NOTIFICATION_DATA is dynamic in length based on the - //number of audio channels available we cannot just call PtrToStructure - //to get all data, thats why it is split up into two steps, first the static - //data is marshalled into the data structure, then with some IntPtr math the - //remaining floats are read from memory. - // - var data = - (AudioVolumeNotificationDataStruct) - Marshal.PtrToStructure(notifyData, typeof (AudioVolumeNotificationDataStruct)); - - //Determine offset in structure of the first float - var offset = Marshal.OffsetOf(typeof (AudioVolumeNotificationDataStruct), "ChannelVolume"); - //Determine offset in memory of the first float - var firstFloatPtr = (IntPtr) ((long) notifyData + (long) offset); - - //Something weird happened, better to ignore it and move on - if (data.Channels > 100) - return 0; - - var voldata = new float[data.Channels]; - - //Read all floats from memory. - for (var i = 0; i < data.Channels; i++) + try { - voldata[i] = (float) Marshal.PtrToStructure(firstFloatPtr, typeof (float)); + //Since AUDIO_VOLUME_NOTIFICATION_DATA is dynamic in length based on the + //number of audio channels available we cannot just call PtrToStructure + //to get all data, thats why it is split up into two steps, first the static + //data is marshalled into the data structure, then with some IntPtr math the + //remaining floats are read from memory. + // + var data = + (AudioVolumeNotificationDataStruct) + Marshal.PtrToStructure(notifyData, typeof(AudioVolumeNotificationDataStruct)); + + //Determine offset in structure of the first float + var offset = Marshal.OffsetOf(typeof(AudioVolumeNotificationDataStruct), "ChannelVolume"); + //Determine offset in memory of the first float + var firstFloatPtr = (IntPtr) ((long) notifyData + (long) offset); + + //Something weird happened, better to ignore it and move on + if (data.Channels > 100) + return 0; + + var voldata = new float[data.Channels]; + + //Read all floats from memory. + for (var i = 0; i < data.Channels; i++) + { + voldata[i] = (float) Marshal.PtrToStructure(firstFloatPtr, typeof(float)); + } + + //Create combined structure and Fire Event in parent class. + var notificationData = new AudioVolumeNotificationData(data.EventContext, data.Muted, + data.MasterVolume, voldata); + + var p = _handler.Target as AudioEndpointVolume; + + if (_handler.IsAlive) + p?.FireNotification(notificationData); + } + catch(Exception ex) + { + return ex.HResult; } - //Create combined structure and Fire Event in parent class. - var notificationData = new AudioVolumeNotificationData(data.EventContext, data.Muted, - data.MasterVolume, voldata); - - var p = _handler.Target as AudioEndpointVolume; - - if (_handler.IsAlive) - p?.FireNotification(notificationData); - - return 0; + return 0; //success } } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi.CoreAudio/Internal/Interfaces/WaveFormat.cs b/AudioSwitcher.AudioApi.CoreAudio/Internal/Interfaces/WaveFormat.cs index 5229d1e..ce96f6e 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/Internal/Interfaces/WaveFormat.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/Internal/Interfaces/WaveFormat.cs @@ -47,8 +47,6 @@ protected WaveFormat(SampleRate rate, BitDepth bits, SpeakerConfiguration channe { throw new ArgumentOutOfRangeException(nameof(channelMask), "Channels must be 1 or greater"); } - // minimum 16 bytes, sometimes 18 for PCM - waveFormatTag = WaveFormatEncoding.Pcm; sampleRate = (int)rate; bitsPerSample = (short)bits; diff --git a/AudioSwitcher.AudioApi.CoreAudio/Internal/MultimediaDeviceCollection.cs b/AudioSwitcher.AudioApi.CoreAudio/Internal/MultimediaDeviceCollection.cs index f1c3338..d62d1e0 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/Internal/MultimediaDeviceCollection.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/Internal/MultimediaDeviceCollection.cs @@ -54,11 +54,7 @@ internal MultimediaDeviceCollection(IMultimediaDeviceCollection parent) public void Dispose() { - if (_multimediaDeviceCollection != null) - { - Marshal.FinalReleaseComObject(_multimediaDeviceCollection); - _multimediaDeviceCollection = null; - } + _multimediaDeviceCollection = null; } public IEnumerator GetEnumerator() diff --git a/AudioSwitcher.AudioApi.CoreAudio/Internal/SystemEventNotifcationClient.cs b/AudioSwitcher.AudioApi.CoreAudio/Internal/SystemEventNotifcationClient.cs index 0c3e88d..a26137a 100644 --- a/AudioSwitcher.AudioApi.CoreAudio/Internal/SystemEventNotifcationClient.cs +++ b/AudioSwitcher.AudioApi.CoreAudio/Internal/SystemEventNotifcationClient.cs @@ -1,47 +1,19 @@ using System; using AudioSwitcher.AudioApi.CoreAudio.Interfaces; +using AudioSwitcher.AudioApi.CoreAudio.Threading; using AudioSwitcher.AudioApi.Observables; namespace AudioSwitcher.AudioApi.CoreAudio { - internal sealed class SystemEventNotifcationClient : IMultimediaNotificationClient, IDisposable + internal sealed class SystemEventNotifcationClient : IDisposable { - private readonly IMultimediaDeviceEnumerator _enumerator; - - internal class DeviceStateChangedArgs - { - public string DeviceId { get; set; } - public EDeviceState State { get; set; } - } - - internal class DeviceAddedArgs - { - public string DeviceId { get; set; } - } - - internal class DeviceRemovedArgs - { - public string DeviceId { get; set; } - } - - internal class DefaultChangedArgs - { - public string DeviceId { get; set; } - public EDataFlow DataFlow { get; set; } - public ERole DeviceRole { get; set; } - } - - internal class PropertyChangedArgs - { - public string DeviceId { get; set; } - public PropertyKey PropertyKey { get; set; } - } private readonly Broadcaster _deviceStateChanged; private readonly Broadcaster _deviceAdded; private readonly Broadcaster _deviceRemoved; private readonly Broadcaster _defaultDeviceChanged; private readonly Broadcaster _propertyChanged; + private ComMultimediaNotificationClient _innerClient; private bool _isDisposed; public IObservable DeviceStateChanged => _deviceStateChanged.AsObservable(); @@ -56,76 +28,127 @@ internal class PropertyChangedArgs public SystemEventNotifcationClient(IMultimediaDeviceEnumerator enumerator) { - _enumerator = enumerator; - _deviceStateChanged = new Broadcaster(); _deviceAdded = new Broadcaster(); _deviceRemoved = new Broadcaster(); _defaultDeviceChanged = new Broadcaster(); _propertyChanged = new Broadcaster(); - enumerator.RegisterEndpointNotificationCallback(this); + _innerClient = new ComMultimediaNotificationClient(this, enumerator); } - void IMultimediaNotificationClient.OnDeviceStateChanged(string deviceId, EDeviceState newState) + public void Dispose() { - _deviceStateChanged.OnNext(new DeviceStateChangedArgs - { - DeviceId = deviceId, - State = newState - }); + if (_isDisposed) + return; + + _innerClient.Dispose(); + _deviceStateChanged.Dispose(); + _deviceAdded.Dispose(); + _deviceRemoved.Dispose(); + _defaultDeviceChanged.Dispose(); + _propertyChanged.Dispose(); + + _innerClient = null; + + _isDisposed = true; } - void IMultimediaNotificationClient.OnDeviceAdded(string deviceId) + internal class DeviceStateChangedArgs { - _deviceAdded.OnNext(new DeviceAddedArgs - { - DeviceId = deviceId - }); + public string DeviceId { get; set; } + public EDeviceState State { get; set; } } - void IMultimediaNotificationClient.OnDeviceRemoved(string deviceId) + internal class DeviceAddedArgs { - _deviceRemoved.OnNext(new DeviceRemovedArgs - { - DeviceId = deviceId - }); + public string DeviceId { get; set; } } - void IMultimediaNotificationClient.OnDefaultDeviceChanged(EDataFlow dataFlow, ERole deviceRole, string defaultDeviceId) + internal class DeviceRemovedArgs { - _defaultDeviceChanged.OnNext(new DefaultChangedArgs - { - DeviceId = defaultDeviceId, - DataFlow = dataFlow, - DeviceRole = deviceRole - }); + public string DeviceId { get; set; } + } + internal class DefaultChangedArgs + { + public string DeviceId { get; set; } + public EDataFlow DataFlow { get; set; } + public ERole DeviceRole { get; set; } } - void IMultimediaNotificationClient.OnPropertyValueChanged(string deviceId, PropertyKey propertyKey) + internal class PropertyChangedArgs { - _propertyChanged.OnNext(new PropertyChangedArgs - { - DeviceId = deviceId, - PropertyKey = propertyKey - }); + public string DeviceId { get; set; } + public PropertyKey PropertyKey { get; set; } } - public void Dispose() + private sealed class ComMultimediaNotificationClient : IMultimediaNotificationClient, IDisposable { - if (_isDisposed) - return; + private readonly SystemEventNotifcationClient _client; + private readonly IMultimediaDeviceEnumerator _enumerator; - _enumerator.UnregisterEndpointNotificationCallback(this); + public ComMultimediaNotificationClient(SystemEventNotifcationClient client, IMultimediaDeviceEnumerator enumerator) + { + ComThread.Assert(); - _deviceStateChanged.Dispose(); - _deviceAdded.Dispose(); - _deviceRemoved.Dispose(); - _defaultDeviceChanged.Dispose(); - _propertyChanged.Dispose(); + _client = client; + _enumerator = enumerator; - _isDisposed = true; + enumerator.RegisterEndpointNotificationCallback(this); + } + + void IMultimediaNotificationClient.OnDeviceStateChanged(string deviceId, EDeviceState newState) + { + _client._deviceStateChanged.OnNext(new DeviceStateChangedArgs + { + DeviceId = deviceId, + State = newState + }); + } + + void IMultimediaNotificationClient.OnDeviceAdded(string deviceId) + { + _client._deviceAdded.OnNext(new DeviceAddedArgs + { + DeviceId = deviceId + }); + } + + void IMultimediaNotificationClient.OnDeviceRemoved(string deviceId) + { + _client._deviceRemoved.OnNext(new DeviceRemovedArgs + { + DeviceId = deviceId + }); + } + + void IMultimediaNotificationClient.OnDefaultDeviceChanged(EDataFlow dataFlow, ERole deviceRole, string defaultDeviceId) + { + _client._defaultDeviceChanged.OnNext(new DefaultChangedArgs + { + DeviceId = defaultDeviceId, + DataFlow = dataFlow, + DeviceRole = deviceRole + }); + } + + void IMultimediaNotificationClient.OnPropertyValueChanged(string deviceId, PropertyKey propertyKey) + { + _client._propertyChanged.OnNext(new PropertyChangedArgs + { + DeviceId = deviceId, + PropertyKey = propertyKey + }); + } + + public void Dispose() + { + ComThread.BeginInvoke(() => + { + _enumerator.UnregisterEndpointNotificationCallback(this); + }); + } } } } diff --git a/AudioSwitcher.AudioApi.Tests/FilteredObservableTests.cs b/AudioSwitcher.AudioApi.Tests/FilteredObservableTests.cs index de12af3..009fc0b 100644 --- a/AudioSwitcher.AudioApi.Tests/FilteredObservableTests.cs +++ b/AudioSwitcher.AudioApi.Tests/FilteredObservableTests.cs @@ -104,6 +104,8 @@ public void Filtered_Broadcaster_Is_Disposed() { var b = new Broadcaster().When(x => x == 2) as FilteredBroadcaster; + Assert.NotNull(b); + b.Dispose(); Assert.True(b.IsDisposed); diff --git a/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs b/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs index b7cfb2b..a195a90 100644 --- a/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs +++ b/AudioSwitcher.AudioApi.Tests/Stubs/DeviceStub.cs @@ -8,7 +8,7 @@ public DeviceStub() : base(null) internal void Dispose() { - Dispose(true); + base.Dispose(true); } public void FirePropertyChanged(string name) diff --git a/AudioSwitcher.AudioApi/BitDepth.cs b/AudioSwitcher.AudioApi/BitDepth.cs index 8fb35e4..99599e6 100644 --- a/AudioSwitcher.AudioApi/BitDepth.cs +++ b/AudioSwitcher.AudioApi/BitDepth.cs @@ -2,6 +2,7 @@ { public enum BitDepth : short { + B8 = 8, B16 = 16, B24 = 24, B32 = 32 diff --git a/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs b/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs index 7d5abea..9b8cb39 100644 --- a/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs +++ b/AudioSwitcher.AudioApi/Capabilities/ISpeakerConfiguration.cs @@ -2,10 +2,10 @@ { public interface ISpeakerConfiguration : IDeviceCapability { - SpeakerConfiguration SpeakerConfiguration { get; } + bool IsSupported(SpeakerConfiguration configuration); - void IsSupported(SpeakerConfiguration configuration); + SpeakerConfiguration Get(); - void SetSpeakerConfiguration(SpeakerConfiguration configuration); + void Set(SpeakerConfiguration configuration); } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi/Device.cs b/AudioSwitcher.AudioApi/Device.cs index 027062c..3bce17f 100644 --- a/AudioSwitcher.AudioApi/Device.cs +++ b/AudioSwitcher.AudioApi/Device.cs @@ -160,5 +160,11 @@ protected virtual void Dispose(bool disposing) _propertyChanged.Dispose(); _peakValueChanged.Dispose(); } + + ~Device() + { + Dispose(false); + } + } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi/Observables/AsyncBroadcaster.cs b/AudioSwitcher.AudioApi/Observables/AsyncBroadcaster.cs index 21d4eba..18f8320 100644 --- a/AudioSwitcher.AudioApi/Observables/AsyncBroadcaster.cs +++ b/AudioSwitcher.AudioApi/Observables/AsyncBroadcaster.cs @@ -10,7 +10,6 @@ public sealed class AsyncBroadcaster : BroadcasterBase private readonly object _observerLock = new object(); private readonly HashSet> _observers; private bool _isComplete; - private bool _isDisposed; public override bool HasObservers { @@ -23,8 +22,6 @@ public override bool HasObservers } } - public override bool IsDisposed => _isDisposed; - public override bool IsComplete => _isComplete; public AsyncBroadcaster() @@ -105,7 +102,7 @@ public override IDisposable Subscribe(IObserver observer) }); } - public override void Dispose() + protected override void Dispose(bool disposing) { OnCompleted(); @@ -113,8 +110,6 @@ public override void Dispose() { _observers.Clear(); } - - _isDisposed = true; } } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi/Observables/Broadcaster.cs b/AudioSwitcher.AudioApi/Observables/Broadcaster.cs index f2f6bd0..bf3849b 100644 --- a/AudioSwitcher.AudioApi/Observables/Broadcaster.cs +++ b/AudioSwitcher.AudioApi/Observables/Broadcaster.cs @@ -9,7 +9,6 @@ public class Broadcaster : BroadcasterBase private readonly object _observerLock = new object(); private readonly HashSet> _observers; private bool _isComplete; - private bool _isDisposed; public override bool HasObservers { @@ -22,8 +21,6 @@ public override bool HasObservers } } - public override bool IsDisposed => _isDisposed; - public override bool IsComplete => _isComplete; public Broadcaster() @@ -139,7 +136,7 @@ protected virtual void ObserverDisposal(IObserver observer) } } - public override void Dispose() + protected override void Dispose(bool disposing) { OnCompleted(); @@ -147,8 +144,6 @@ public override void Dispose() { _observers.Clear(); } - - _isDisposed = true; } } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi/Observables/BroadcasterBase.cs b/AudioSwitcher.AudioApi/Observables/BroadcasterBase.cs index 170a5f8..a37b072 100644 --- a/AudioSwitcher.AudioApi/Observables/BroadcasterBase.cs +++ b/AudioSwitcher.AudioApi/Observables/BroadcasterBase.cs @@ -6,7 +6,7 @@ public abstract class BroadcasterBase : IBroadcaster, IDisposable { public abstract bool HasObservers { get; } - public abstract bool IsDisposed { get; } + public bool IsDisposed { get; private set; } public abstract bool IsComplete { get; } @@ -18,6 +18,13 @@ public abstract class BroadcasterBase : IBroadcaster, IDisposable public abstract IDisposable Subscribe(IObserver observer); - public abstract void Dispose(); + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + IsDisposed = true; + } + + protected abstract void Dispose(bool disposing); } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi/Observables/FilteredBroadcaster.cs b/AudioSwitcher.AudioApi/Observables/FilteredBroadcaster.cs index 7b5e4c4..e6804fc 100644 --- a/AudioSwitcher.AudioApi/Observables/FilteredBroadcaster.cs +++ b/AudioSwitcher.AudioApi/Observables/FilteredBroadcaster.cs @@ -19,10 +19,9 @@ public override void OnNext(T value) base.OnNext(value); } - public override void Dispose() + protected override void Dispose(bool disposing) { _observerSubscription.Dispose(); - base.Dispose(); } } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi/Properties/AssemblyInfo.cs b/AudioSwitcher.AudioApi/Properties/AssemblyInfo.cs index 283cfb6..010c866 100644 --- a/AudioSwitcher.AudioApi/Properties/AssemblyInfo.cs +++ b/AudioSwitcher.AudioApi/Properties/AssemblyInfo.cs @@ -1,5 +1,6 @@ using System.Reflection; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information @@ -11,6 +12,7 @@ [assembly: AssemblyProduct("AudioSwitcher.AudioApi")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] +[assembly: ComVisible(false)] #if DEBUG diff --git a/AudioSwitcher.Scripting.Lua/LuaEngine.cs b/AudioSwitcher.Scripting.Lua/LuaEngine.cs index dc20fce..67d7c2e 100644 --- a/AudioSwitcher.Scripting.Lua/LuaEngine.cs +++ b/AudioSwitcher.Scripting.Lua/LuaEngine.cs @@ -1,6 +1,4 @@ using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; using MoonSharp.Interpreter; @@ -10,7 +8,6 @@ public sealed class LuaEngine : ScriptEngineBase { public LuaEngine() - : base() { UserData.RegisterAssembly(); } diff --git a/AudioSwitcher.Scripting.Lua/LuaExecutionContext.cs b/AudioSwitcher.Scripting.Lua/LuaExecutionContext.cs index 1b8a2c6..ce3c937 100644 --- a/AudioSwitcher.Scripting.Lua/LuaExecutionContext.cs +++ b/AudioSwitcher.Scripting.Lua/LuaExecutionContext.cs @@ -1,17 +1,17 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Threading; using System.Threading.Tasks; -using MoonSharp.Interpreter; namespace AudioSwitcher.Scripting.Lua { public sealed class LuaExecutionContext : IExecutionContext { - private bool _isDebug; + private readonly bool _isDebug; private CancellationToken _cancellationToken; - private Dictionary _libraries; + private readonly Dictionary _libraries; public LuaExecutionContext(bool isDebug, CancellationToken cancellationToken) : this(isDebug, Enumerable.Empty(), cancellationToken) @@ -47,13 +47,13 @@ public void Dispose() public bool IsDebug { - get; + get + { + return _isDebug; + } } - public IDictionary Libraries - { - get; - } + public IDictionary Libraries => new ReadOnlyDictionary(_libraries); public void SetOutput(IScriptOutput output) { diff --git a/AudioSwitcher.Scripting.Lua/Properties/AssemblyInfo.cs b/AudioSwitcher.Scripting.Lua/Properties/AssemblyInfo.cs index 895233b..19b7945 100644 --- a/AudioSwitcher.Scripting.Lua/Properties/AssemblyInfo.cs +++ b/AudioSwitcher.Scripting.Lua/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/AudioSwitcher.Scripting/ExecutionResult.cs b/AudioSwitcher.Scripting/ExecutionResult.cs index 2a70b12..d672ccc 100644 --- a/AudioSwitcher.Scripting/ExecutionResult.cs +++ b/AudioSwitcher.Scripting/ExecutionResult.cs @@ -38,6 +38,7 @@ public T Value } + [Serializable] public class ExecutionException : Exception { public int LineNumber