diff --git a/Library/DirectSound.cs b/Library/DirectSound.cs index 8cd9766..60ce476 100644 --- a/Library/DirectSound.cs +++ b/Library/DirectSound.cs @@ -5,30 +5,10 @@ namespace Jellyfish.Library { - public sealed class DirectSoundUpdateEventArgs : EventArgs - { - private DirectSoundUpdateEventArgs() - { - } - - public static DirectSoundUpdateEventArgs Create(IntPtr buffer, int bufferSize) - { - _instance.Buffer = buffer; - _instance.BufferSize = bufferSize; - - return _instance; // use singleton; avoids garbage - } - - public IntPtr Buffer { get; private set; } - public int BufferSize { get; private set; } - - private static readonly DirectSoundUpdateEventArgs _instance = new DirectSoundUpdateEventArgs(); - } - public sealed partial class DirectSound : IDisposable { [SecurityCritical] - public DirectSound(int sampleRate, int sampleChannels, int sampleBits, int sampleSize) + public DirectSound(int sampleRate, int sampleChannels, int sampleBits, int sampleSize, Action updater) { _sampleRate = sampleRate; _sampleChannels = sampleChannels; @@ -36,6 +16,7 @@ public DirectSound(int sampleRate, int sampleChannels, int sampleBits, int sampl _sampleSize = sampleSize; _thread = new Thread(Run) { Name = "DirectSound" }; + _updater = updater; } public void Dispose() @@ -102,16 +83,7 @@ private void RestoreBuffer() } } - private void UpdateBuffer(int block) - { - var handler = Update; - if (handler != null) - { - UpdateBuffer(block * _sampleSize, _sampleSize, BufferLock.None, (buffer, bufferSize) => handler(this, DirectSoundUpdateEventArgs.Create(buffer, bufferSize))); - } - } - - private void UpdateBuffer(int offset, int count, BufferLock flags, Action updateBuffer) + private void UpdateBuffer(int offset, int count, BufferLock flags, Action updater) { RestoreBuffer(); @@ -122,11 +94,11 @@ private void UpdateBuffer(int offset, int count, BufferLock flags, Action Update; - private const int BlockCount = 2; private int _sampleRate; @@ -178,6 +148,7 @@ private void Run() // com mta thread private IntPtr _window; private IDirectSound _device; private IDirectSoundBuffer _buffer; + private Action _updater; private AutoResetEvent _position1Event = new AutoResetEvent(false); private AutoResetEvent _position2Event = new AutoResetEvent(false); diff --git a/Library/GeneralSecurity.cs b/Library/GeneralSecurity.cs index 43914f5..7b0aacd 100644 --- a/Library/GeneralSecurity.cs +++ b/Library/GeneralSecurity.cs @@ -36,8 +36,7 @@ public GeneralAccessRule(IdentityReference identity, int rights, InheritanceFlag { } - public GeneralAccessRule(IdentityReference identity, int rights, bool isInherited, InheritanceFlags inheritance, PropagationFlags propagation, - AccessControlType type) : + public GeneralAccessRule(IdentityReference identity, int rights, bool isInherited, InheritanceFlags inheritance, PropagationFlags propagation, AccessControlType type) : base(identity, rights, isInherited, inheritance, propagation, type) { } @@ -57,8 +56,7 @@ public GeneralAuditRule(IdentityReference identity, int rights, InheritanceFlags { } - public GeneralAuditRule(IdentityReference identity, int rights, bool isInherited, InheritanceFlags inheritance, PropagationFlags propagation, - AuditFlags audit) : + public GeneralAuditRule(IdentityReference identity, int rights, bool isInherited, InheritanceFlags inheritance, PropagationFlags propagation, AuditFlags audit) : base(identity, rights, isInherited, inheritance, propagation, audit) { } @@ -73,34 +71,22 @@ public GeneralSecurity(bool isContainer, ResourceType resourceType) : { } - public GeneralSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle) : - base(isContainer, resourceType, handle, AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner) - { - } - - public GeneralSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections) : + public GeneralSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner) : base(isContainer, resourceType, handle, includeSections) { } - public GeneralSecurity(bool isContainer, ResourceType resourceType, string name) : - base(isContainer, resourceType, name, AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner) - { - } - - public GeneralSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections) : + public GeneralSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner) : base(isContainer, resourceType, name, includeSections) { } - public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, - PropagationFlags propagationFlags, AccessControlType type) + public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type) { return new GeneralAccessRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, type); } - public override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, - PropagationFlags propagationFlags, AuditFlags flags) + public override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags) { return new GeneralAuditRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, flags); } @@ -118,13 +104,13 @@ public void AddAuditRule(GeneralAuditRule rule) } [SecurityCritical] - public void GetSecurityAttributes(bool inheritable, Action action) + public void GetSecurityAttributes(Action action, bool inheritable = false) { - GetSecurityAttributes(this, inheritable, action); + GetSecurityAttributes(this, action, inheritable); } [SecurityCritical] - public static void GetSecurityAttributes(ObjectSecurity security, bool inheritable, Action action) + public static void GetSecurityAttributes(ObjectSecurity security, Action action, bool inheritable = false) { if (action == null) { diff --git a/Library/SafeAllocHandle.cs b/Library/SafeAllocHandle.cs index 2d176fb..ff6a800 100644 --- a/Library/SafeAllocHandle.cs +++ b/Library/SafeAllocHandle.cs @@ -22,7 +22,7 @@ public sealed class SafeGlobalAllocHandle : SafeAllocHandle { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public SafeGlobalAllocHandle() : - base(true) + base(ownsHandle: true) { } @@ -33,12 +33,6 @@ public SafeGlobalAllocHandle(IntPtr existingHandle, bool ownsHandle) : SetHandle(existingHandle); } - [SecurityCritical] - public static SafeGlobalAllocHandle Allocate(int size) - { - return Allocate(0x0, size); - } - [SecurityCritical] public static SafeGlobalAllocHandle Allocate(byte[] value) { @@ -53,15 +47,8 @@ public static SafeGlobalAllocHandle Allocate(byte[] value) return alloc; } - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [SecurityCritical] - protected override bool ReleaseHandle() - { - return (NativeMethods.GlobalFree(handle) == IntPtr.Zero); - } - - [SecurityCritical] - private static SafeGlobalAllocHandle Allocate(uint flags, int size) + public static SafeGlobalAllocHandle Allocate(int size, uint flags = 0x0) { var alloc = NativeMethods.GlobalAlloc(flags, (IntPtr)size); if (alloc.IsInvalid) @@ -72,6 +59,13 @@ private static SafeGlobalAllocHandle Allocate(uint flags, int size) return alloc; } + [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] + [SecurityCritical] + protected override bool ReleaseHandle() + { + return (NativeMethods.GlobalFree(handle) == IntPtr.Zero); + } + [SecurityCritical] [SuppressUnmanagedCodeSecurity] private static class NativeMethods @@ -91,7 +85,7 @@ public sealed class SafeLocalAllocHandle : SafeAllocHandle { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public SafeLocalAllocHandle() : - base(true) + base(ownsHandle: true) { } @@ -102,12 +96,6 @@ public SafeLocalAllocHandle(IntPtr existingHandle, bool ownsHandle) : SetHandle(existingHandle); } - [SecurityCritical] - public static SafeLocalAllocHandle Allocate(int size) - { - return Allocate(0x0, size); - } - [SecurityCritical] public static SafeLocalAllocHandle Allocate(byte[] value) { @@ -122,15 +110,8 @@ public static SafeLocalAllocHandle Allocate(byte[] value) return alloc; } - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [SecurityCritical] - protected override bool ReleaseHandle() - { - return (NativeMethods.LocalFree(handle) == IntPtr.Zero); - } - - [SecurityCritical] - private static SafeLocalAllocHandle Allocate(uint flags, int size) + public static SafeLocalAllocHandle Allocate(int size, uint flags = 0x0) { var alloc = NativeMethods.LocalAlloc(flags, (IntPtr)size); if (alloc.IsInvalid) @@ -141,6 +122,13 @@ private static SafeLocalAllocHandle Allocate(uint flags, int size) return alloc; } + [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] + [SecurityCritical] + protected override bool ReleaseHandle() + { + return (NativeMethods.LocalFree(handle) == IntPtr.Zero); + } + [SecurityCritical] [SuppressUnmanagedCodeSecurity] private static class NativeMethods diff --git a/Library/SafeFileHandle.cs b/Library/SafeFileHandle.cs index a7b2436..b133ce7 100644 --- a/Library/SafeFileHandle.cs +++ b/Library/SafeFileHandle.cs @@ -14,7 +14,7 @@ public sealed class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public SafeFileHandle() : - base(true) + base(ownsHandle: true) { } @@ -64,18 +64,18 @@ public void SetAccessControl(GeneralSecurity fileSecurity) [SecurityCritical] private static SafeFileHandle CreateFile(string fileName, uint fileAccess, uint fileShare, uint fileMode, uint fileOptions, GeneralSecurity fileSecurity, - bool inheritable) + bool inheritable = false) { var file = new SafeFileHandle(); - GeneralSecurity.GetSecurityAttributes(fileSecurity, inheritable, securityAttributes => + GeneralSecurity.GetSecurityAttributes(fileSecurity, securityAttributes => { file = NativeMethods.CreateFile(fileName, fileAccess, fileShare, securityAttributes, fileMode, fileOptions, IntPtr.Zero); if (file.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } - }); + }, inheritable); return file; } diff --git a/Library/Silverlight/ApplicationBase.cs b/Library/Silverlight/ApplicationBase.cs index 7860f75..fa35c17 100644 --- a/Library/Silverlight/ApplicationBase.cs +++ b/Library/Silverlight/ApplicationBase.cs @@ -6,12 +6,7 @@ namespace Jellyfish.Library { public class ApplicationBase : Application { - public ApplicationBase() : - this(null) - { - } - - public ApplicationBase(string name) + public ApplicationBase(string name = null) { Name = name; @@ -21,7 +16,7 @@ public ApplicationBase(string name) private void OnApplicationUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { - MessageBox.Show(GetExceptionMessage(e.ExceptionObject), GetExceptionCaption("Application Exception", false), MessageBoxButton.OK); + MessageBox.Show(GetExceptionMessage(e.ExceptionObject), GetExceptionCaption("Application Exception"), MessageBoxButton.OK); e.Handled = true; } @@ -30,7 +25,7 @@ private void OnApplicationUnhandledException(object sender, ApplicationUnhandled // MessageBox.Show(GetExceptionMessage(e.ExceptionObject as Exception), GetExceptionCaption("AppDomain Exception", e.IsTerminating), MessageBoxButton.OK); //} - private string GetExceptionCaption(string title, bool isTerminating) + private string GetExceptionCaption(string title, bool isTerminating = false) { var caption = new StringBuilder(); if (!string.IsNullOrEmpty(Name)) diff --git a/Library/Silverlight/WaveMediaStreamSource.cs b/Library/Silverlight/WaveMediaStreamSource.cs index d4e1bd4..fac27ba 100644 --- a/Library/Silverlight/WaveMediaStreamSource.cs +++ b/Library/Silverlight/WaveMediaStreamSource.cs @@ -1,41 +1,20 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.IO; using System.Windows.Media; namespace Jellyfish.Library { - public sealed class WaveMediaStreamSourceUpdateEventArgs : EventArgs - { - private WaveMediaStreamSourceUpdateEventArgs() - { - } - - public static WaveMediaStreamSourceUpdateEventArgs Create(byte[] buffer, int bufferSize) - { - _instance.Buffer = buffer; - _instance.BufferSize = bufferSize; - - return _instance; // use singleton; avoids garbage - } - - [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] - public byte[] Buffer { get; private set; } - public int BufferSize { get; private set; } - - private static readonly WaveMediaStreamSourceUpdateEventArgs _instance = new WaveMediaStreamSourceUpdateEventArgs(); - } - public sealed class WaveMediaStreamSource : MediaStreamSource, IDisposable { - public WaveMediaStreamSource(int sampleRate, int sampleChannels, int sampleBits, int sampleSize, int sampleLatency) + public WaveMediaStreamSource(int sampleRate, int sampleChannels, int sampleBits, int sampleSize, int sampleLatency, Action updater) { _bufferSize = sampleSize; _buffer = new byte[_bufferSize]; _bufferStream = new MemoryStream(_buffer); _waveFormat = new WaveFormat(sampleRate, sampleChannels, sampleBits); AudioBufferLength = sampleLatency; // ms; avoids audio delay + _updater = updater; } public void Dispose() @@ -55,11 +34,7 @@ protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagn protected override void GetSampleAsync(MediaStreamType mediaStreamType) { - var handler = Update; - if (handler != null) - { - handler(this, WaveMediaStreamSourceUpdateEventArgs.Create(_buffer, _bufferSize)); - } + _updater(_buffer, _bufferSize); var sample = new MediaStreamSample(_audioDescription, _bufferStream, 0, _bufferSize, _timestamp, _emptySampleDict); _timestamp += _bufferSize * 10000000L / _waveFormat.AverageBytesPerSec; // 100 ns @@ -89,11 +64,10 @@ protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStrea throw new NotImplementedException(); } - public event EventHandler Update; - private byte[] _buffer; private int _bufferSize; private MemoryStream _bufferStream; + private Action _updater; private WaveFormat _waveFormat; private long _timestamp; private MediaStreamDescription _audioDescription; diff --git a/Library/StringBuilderExtensions.cs b/Library/StringBuilderExtensions.cs index 744400d..6dfb299 100644 --- a/Library/StringBuilderExtensions.cs +++ b/Library/StringBuilderExtensions.cs @@ -49,6 +49,20 @@ public static StringBuilder AppendWithoutGarbage(this StringBuilder builder, int return builder; } +#if WINDOWS_PHONE || XBOX + public static StringBuilder Clear(this StringBuilder builder) + { + if (builder == null) + { + throw new ArgumentNullException("builder"); + } + + builder.Length = 0; + + return builder; + } +#endif + private static readonly char[] Digits = new char[] { '9', '8', '7', '6', '5', '4', '3', '2', '1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; } } diff --git a/Library/Wpf/ApplicationBase.cs b/Library/Wpf/ApplicationBase.cs index 5b3635f..dc335b4 100644 --- a/Library/Wpf/ApplicationBase.cs +++ b/Library/Wpf/ApplicationBase.cs @@ -10,13 +10,7 @@ namespace Jellyfish.Library public class ApplicationBase : Application { [SecurityCritical] - public ApplicationBase() : - this(null) - { - } - - [SecurityCritical] - public ApplicationBase(string name) + public ApplicationBase(string name = null) { Name = name; @@ -26,7 +20,7 @@ public ApplicationBase(string name) private void OnApplicationDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { - MessageBox.Show(GetExceptionMessage(e.Exception), GetExceptionCaption("Application Dispatcher Exception", true)); + MessageBox.Show(GetExceptionMessage(e.Exception), GetExceptionCaption("Application Dispatcher Exception", isTerminating: true)); e.Handled = true; Shutdown(); } @@ -36,7 +30,7 @@ private void OnAppDomainUnhandledException(object sender, UnhandledExceptionEven MessageBox.Show(GetExceptionMessage(e.ExceptionObject as Exception), GetExceptionCaption("AppDomain Exception", e.IsTerminating)); } - private string GetExceptionCaption(string title, bool isTerminating) + private string GetExceptionCaption(string title, bool isTerminating = false) { var caption = new StringBuilder(); caption.AppendFormat("[{0}] ", Process.GetCurrentProcess().Id); diff --git a/Library/XmlSerializerHelpers.cs b/Library/XmlSerializerHelpers.cs index 8f98886..7f68fab 100644 --- a/Library/XmlSerializerHelpers.cs +++ b/Library/XmlSerializerHelpers.cs @@ -8,13 +8,7 @@ namespace Jellyfish.Library public static class XmlSerializerHelpers { [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")] - public static T Deserialize(Stream stream) - { - return Deserialize(stream, null); - } - - [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")] - public static T Deserialize(Stream stream, string defaultNamespace) + public static T Deserialize(Stream stream, string defaultNamespace = null) { using (var reader = XmlReader.Create(stream)) { @@ -23,12 +17,7 @@ public static T Deserialize(Stream stream, string defaultNamespace) } } - public static void Serialize(Stream stream, T instance) - { - Serialize(stream, instance, null); - } - - public static void Serialize(Stream stream, T instance, string defaultNamespace) + public static void Serialize(Stream stream, T instance, string defaultNamespace = null) { using (var writer = XmlWriter.Create(stream)) { diff --git a/Library/Xna/FrameRateCounter.cs b/Library/Xna/FrameRateCounter.cs index 219ac4b..2aa7a7b 100644 --- a/Library/Xna/FrameRateCounter.cs +++ b/Library/Xna/FrameRateCounter.cs @@ -33,8 +33,7 @@ public override void Draw(GameTime gameTime) { _frameCount++; - _frameRateBuilder.Length = 0; - _frameRateBuilder.AppendWithoutGarbage(_frameRate).Append(" fps"); + _frameRateBuilder.Clear().AppendWithoutGarbage(_frameRate).Append(" fps"); _spriteBatch.Begin(); //_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position - Vector2.UnitX, Color.Black); // rough outline diff --git a/Library/Xna/Jellyfish.Library.Xna.Phone.csproj b/Library/Xna/Jellyfish.Library.Xna.Phone.csproj index 71d8c10..2556585 100644 --- a/Library/Xna/Jellyfish.Library.Xna.Phone.csproj +++ b/Library/Xna/Jellyfish.Library.Xna.Phone.csproj @@ -31,6 +31,7 @@ true AllRules.ruleset false + default pdbonly @@ -45,6 +46,7 @@ true AllRules.ruleset false + default diff --git a/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj b/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj index 0455017..fcdd66c 100644 --- a/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj +++ b/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj @@ -31,6 +31,7 @@ true AllRules.ruleset false + default pdbonly @@ -45,6 +46,7 @@ true AllRules.ruleset false + default diff --git a/Virtu/Keyboard.cs b/Virtu/Keyboard.cs index 670f677..8d47a47 100644 --- a/Virtu/Keyboard.cs +++ b/Virtu/Keyboard.cs @@ -1,7 +1,5 @@ -using System; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using Jellyfish.Virtu.Services; -using Jellyfish.Virtu.Settings; namespace Jellyfish.Virtu { @@ -16,8 +14,6 @@ public override void Initialize() { _keyboardService = Machine.Services.GetService(); _gamePortService = Machine.Services.GetService(); - - _keyboardService.AsciiKeyDown += (sender, e) => Latch = e.AsciiKey; } [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] @@ -121,10 +117,9 @@ public void ResetStrobe() } public bool IsAnyKeyDown { get { return _keyboardService.IsAnyKeyDown; } } + public int Latch { get { return _latch; } set { _latch = value; Strobe = true; } } public bool Strobe { get; private set; } - private int Latch { get { return _latch; } set { _latch = value; Strobe = true; } } - private KeyboardService _keyboardService; private GamePortService _gamePortService; diff --git a/Virtu/Services/AudioService.cs b/Virtu/Services/AudioService.cs index e4796f1..f41acda 100644 --- a/Virtu/Services/AudioService.cs +++ b/Virtu/Services/AudioService.cs @@ -55,7 +55,7 @@ protected void Update(int bufferSize, Action updateBuffer) // audio public const int SampleLatency = 40; // ms public const int SampleSize = (SampleRate * SampleLatency / 1000) * SampleChannels * (SampleBits / 8); - private static readonly byte[] SampleZero = new byte[SampleSize]; + protected static readonly byte[] SampleZero = new byte[SampleSize]; private byte[] _buffer = new byte[SampleSize]; private int _index; diff --git a/Virtu/Services/KeyboardService.cs b/Virtu/Services/KeyboardService.cs index 40d2e3b..557c5ce 100644 --- a/Virtu/Services/KeyboardService.cs +++ b/Virtu/Services/KeyboardService.cs @@ -1,26 +1,7 @@ using System; -using System.Threading; namespace Jellyfish.Virtu.Services { - public sealed class AsciiKeyEventArgs : EventArgs - { - private AsciiKeyEventArgs() - { - } - - public static AsciiKeyEventArgs Create(int asciiKey) - { - _instance.AsciiKey = asciiKey; - - return _instance; // use singleton; avoids garbage - } - - public int AsciiKey { get; private set; } - - private static readonly AsciiKeyEventArgs _instance = new AsciiKeyEventArgs(); - } - public abstract class KeyboardService : MachineService { protected KeyboardService(Machine machine) : @@ -48,17 +29,6 @@ public virtual void Update() // main thread } } - protected void OnAsciiKeyDown(int asciiKey) - { - var handler = AsciiKeyDown; - if (handler != null) - { - handler(this, AsciiKeyEventArgs.Create(asciiKey)); - } - } - - public event EventHandler AsciiKeyDown; - public bool IsAnyKeyDown { get; protected set; } public bool IsOpenAppleKeyDown { get; protected set; } public bool IsCloseAppleKeyDown { get; protected set; } diff --git a/Virtu/Services/MachineServices.cs b/Virtu/Services/MachineServices.cs index 60dac9c..0b3627e 100644 --- a/Virtu/Services/MachineServices.cs +++ b/Virtu/Services/MachineServices.cs @@ -39,12 +39,7 @@ public void ForEach(Action action) [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")] public T GetService() { - return (T)GetService(typeof(T)); - } - - public object GetService(Type serviceType) - { - return _serviceProviders.ContainsKey(serviceType) ? _serviceProviders[serviceType] : null; + return (T)((IServiceProvider)this).GetService(typeof(T)); } public void RemoveService(Type serviceType) @@ -52,6 +47,11 @@ public void RemoveService(Type serviceType) _serviceProviders.Remove(serviceType); } + object IServiceProvider.GetService(Type serviceType) + { + return _serviceProviders.ContainsKey(serviceType) ? _serviceProviders[serviceType] : null; + } + private Dictionary _serviceProviders = new Dictionary(); } } diff --git a/Virtu/Services/StorageService.cs b/Virtu/Services/StorageService.cs index caa1a12..fb837a4 100644 --- a/Virtu/Services/StorageService.cs +++ b/Virtu/Services/StorageService.cs @@ -14,12 +14,7 @@ protected StorageService(Machine machine) : { } - public static Stream GetResourceStream(string resourceName) - { - return GetResourceStream(resourceName, 0); - } - - public static Stream GetResourceStream(string resourceName, int resourceSize) + public static Stream GetResourceStream(string resourceName, int resourceSize = 0) { var resourceManager = new ResourceManager("Jellyfish.Virtu.g", Assembly.GetExecutingAssembly()) { IgnoreCase = true }; var resourceStream = (Stream)resourceManager.GetObject(resourceName); diff --git a/Virtu/Silverlight/Services/SilverlightAudioService.cs b/Virtu/Silverlight/Services/SilverlightAudioService.cs index b52f5be..440a0b9 100644 --- a/Virtu/Silverlight/Services/SilverlightAudioService.cs +++ b/Virtu/Silverlight/Services/SilverlightAudioService.cs @@ -20,15 +20,25 @@ public SilverlightAudioService(Machine machine, UserControl page, MediaElement m _page = page; _media = media; + _mediaSource = new WaveMediaStreamSource(SampleRate, SampleChannels, SampleBits, SampleSize, SampleLatency, OnMediaSourceUpdate); _page.Loaded += (sender, e) => { _media.SetSource(_mediaSource); _media.Play(); }; - _mediaSource.Update += OnMediaSourceUpdate; #if !WINDOWS_PHONE _page.Unloaded += (sender, e) => _media.Stop(); #endif } - private void OnMediaSourceUpdate(object sender, WaveMediaStreamSourceUpdateEventArgs e) // audio thread + protected override void Dispose(bool disposing) + { + if (disposing) + { + _mediaSource.Dispose(); + } + + base.Dispose(disposing); + } + + private void OnMediaSourceUpdate(byte[] buffer, int bufferSize) // audio thread { //if (_count++ % (1000 / SampleLatency) == 0) //{ @@ -38,12 +48,12 @@ private void OnMediaSourceUpdate(object sender, WaveMediaStreamSourceUpdateEvent // }); //} - Update(e.BufferSize, (source, count) => Buffer.BlockCopy(source, 0, e.Buffer, 0, count)); + Update(bufferSize, (source, count) => Buffer.BlockCopy(source, 0, buffer, 0, count)); } private UserControl _page; private MediaElement _media; - private WaveMediaStreamSource _mediaSource = new WaveMediaStreamSource(SampleRate, SampleChannels, SampleBits, SampleSize, SampleLatency); + private WaveMediaStreamSource _mediaSource; //private int _count; } } diff --git a/Virtu/Silverlight/Services/SilverlightKeyboardService.cs b/Virtu/Silverlight/Services/SilverlightKeyboardService.cs index f3959d6..57923d8 100644 --- a/Virtu/Silverlight/Services/SilverlightKeyboardService.cs +++ b/Virtu/Silverlight/Services/SilverlightKeyboardService.cs @@ -71,7 +71,7 @@ private void OnPageKeyDown(object sender, KeyEventArgs e) int asciiKey = GetAsciiKey(e.Key, e.PlatformKeyCode); if (asciiKey >= 0) { - OnAsciiKeyDown(asciiKey); + Machine.Keyboard.Latch = asciiKey; e.Handled = true; } diff --git a/Virtu/Wpf/Services/WpfAudioService.cs b/Virtu/Wpf/Services/WpfAudioService.cs index 578871a..abcb9a7 100644 --- a/Virtu/Wpf/Services/WpfAudioService.cs +++ b/Virtu/Wpf/Services/WpfAudioService.cs @@ -18,9 +18,9 @@ public WpfAudioService(Machine machine, Window window) : } _window = window; + _directSound = new DirectSound(SampleRate, SampleChannels, SampleBits, SampleSize, OnDirectSoundUpdate); _window.SourceInitialized += (sender, e) => _directSound.Start(_window.GetHandle()); - _directSound.Update += OnDirectSoundUpdate; _window.Closed += (sender, e) => _directSound.Stop(); } @@ -34,7 +34,7 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - private void OnDirectSoundUpdate(object sender, DirectSoundUpdateEventArgs e) // audio thread + private void OnDirectSoundUpdate(IntPtr buffer, int bufferSize) // audio thread { //if (_count++ % (1000 / SampleLatency) == 0) //{ @@ -44,11 +44,11 @@ private void OnDirectSoundUpdate(object sender, DirectSoundUpdateEventArgs e) // // }); //} - Update(e.BufferSize, (source, count) => Marshal.Copy(source, 0, e.Buffer, count)); + Update(bufferSize, (source, count) => Marshal.Copy(source, 0, buffer, count)); } private Window _window; - private DirectSound _directSound = new DirectSound(SampleRate, SampleChannels, SampleBits, SampleSize); + private DirectSound _directSound; //private int _count; } } diff --git a/Virtu/Wpf/Services/WpfKeyboardService.cs b/Virtu/Wpf/Services/WpfKeyboardService.cs index b80c20c..018a9ec 100644 --- a/Virtu/Wpf/Services/WpfKeyboardService.cs +++ b/Virtu/Wpf/Services/WpfKeyboardService.cs @@ -71,7 +71,7 @@ private void OnWindowKeyDown(object sender, KeyEventArgs e) int asciiKey = GetAsciiKey(e.Key, e.KeyboardDevice); if (asciiKey >= 0) { - OnAsciiKeyDown(asciiKey); + Machine.Keyboard.Latch = asciiKey; e.Handled = true; } diff --git a/Virtu/Xna/Jellyfish.Virtu.Xna.Phone.csproj b/Virtu/Xna/Jellyfish.Virtu.Xna.Phone.csproj index 71c7ea9..326506e 100644 --- a/Virtu/Xna/Jellyfish.Virtu.Xna.Phone.csproj +++ b/Virtu/Xna/Jellyfish.Virtu.Xna.Phone.csproj @@ -37,6 +37,7 @@ true AllRules.ruleset false + default pdbonly @@ -51,6 +52,7 @@ true AllRules.ruleset false + default diff --git a/Virtu/Xna/Jellyfish.Virtu.Xna.Xbox.csproj b/Virtu/Xna/Jellyfish.Virtu.Xna.Xbox.csproj index e30eb69..bfc6540 100644 --- a/Virtu/Xna/Jellyfish.Virtu.Xna.Xbox.csproj +++ b/Virtu/Xna/Jellyfish.Virtu.Xna.Xbox.csproj @@ -33,6 +33,7 @@ true AllRules.ruleset false + default pdbonly @@ -47,6 +48,7 @@ true AllRules.ruleset false + default diff --git a/Virtu/Xna/MainGame.cs b/Virtu/Xna/MainGame.cs index 03c69a1..dcab58f 100644 --- a/Virtu/Xna/MainGame.cs +++ b/Virtu/Xna/MainGame.cs @@ -15,6 +15,8 @@ public MainGame() : GraphicsDeviceManager.PreferredBackBufferWidth = 480; // TODO remove; works around known ctp issue GraphicsDeviceManager.PreferredBackBufferHeight = 800; #endif + IsMouseVisible = true; + var frameRateCounter = new FrameRateCounter(this); // no initializers; avoids CA2000 Components.Add(frameRateCounter); frameRateCounter.DrawOrder = 1; diff --git a/Virtu/Xna/Services/XnaAudioService.cs b/Virtu/Xna/Services/XnaAudioService.cs index 9966b6f..9ed122c 100644 --- a/Virtu/Xna/Services/XnaAudioService.cs +++ b/Virtu/Xna/Services/XnaAudioService.cs @@ -19,6 +19,7 @@ public XnaAudioService(Machine machine, GameBase game) : _dynamicSoundEffect.BufferNeeded += OnDynamicSoundEffectBufferNeeded; _game.Exiting += (sender, e) => _dynamicSoundEffect.Stop(); + _dynamicSoundEffect.SubmitBuffer(SampleZero); _dynamicSoundEffect.Play(); } diff --git a/Virtu/Xna/Services/XnaKeyboardService.cs b/Virtu/Xna/Services/XnaKeyboardService.cs index 42d70fa..ebc1452 100644 --- a/Virtu/Xna/Services/XnaKeyboardService.cs +++ b/Virtu/Xna/Services/XnaKeyboardService.cs @@ -86,7 +86,7 @@ private void OnKeyDown(Keys key, bool gamePadControl) int asciiKey = GetAsciiKey(key, gamePadControl); if (asciiKey >= 0) { - OnAsciiKeyDown(asciiKey); + Machine.Keyboard.Latch = asciiKey; } }