mirror of
https://github.com/digital-jellyfish/Virtu.git
synced 2025-02-17 04:32:21 +00:00
Replaced event handlers with direct delegates where appropriate.
Cosmetic changes including using C# 4 named and optional parameters. --HG-- extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4046806
This commit is contained in:
parent
6700099803
commit
2ba3146299
@ -5,30 +5,10 @@ using System.Threading;
|
||||
|
||||
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<IntPtr, int> updater)
|
||||
{
|
||||
_sampleRate = sampleRate;
|
||||
_sampleChannels = sampleChannels;
|
||||
@ -36,6 +16,7 @@ namespace Jellyfish.Library
|
||||
_sampleSize = sampleSize;
|
||||
|
||||
_thread = new Thread(Run) { Name = "DirectSound" };
|
||||
_updater = updater;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@ -102,16 +83,7 @@ namespace Jellyfish.Library
|
||||
}
|
||||
}
|
||||
|
||||
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<IntPtr, int> updateBuffer)
|
||||
private void UpdateBuffer(int offset, int count, BufferLock flags, Action<IntPtr, int> updater)
|
||||
{
|
||||
RestoreBuffer();
|
||||
|
||||
@ -122,11 +94,11 @@ namespace Jellyfish.Library
|
||||
{
|
||||
if (buffer1 != IntPtr.Zero)
|
||||
{
|
||||
updateBuffer(buffer1, buffer1Size);
|
||||
updater(buffer1, buffer1Size);
|
||||
}
|
||||
if (buffer2 != IntPtr.Zero)
|
||||
{
|
||||
updateBuffer(buffer2, buffer2Size);
|
||||
updater(buffer2, buffer2Size);
|
||||
}
|
||||
}
|
||||
finally
|
||||
@ -158,15 +130,13 @@ namespace Jellyfish.Library
|
||||
|
||||
while (index < BlockCount)
|
||||
{
|
||||
UpdateBuffer((index + 1) % BlockCount); // update next block in circular buffer
|
||||
UpdateBuffer(((index + 1) % BlockCount) * _sampleSize, _sampleSize, BufferLock.None, _updater); // update next block in circular buffer
|
||||
index = WaitHandle.WaitAny(eventHandles);
|
||||
}
|
||||
|
||||
Uninitialize();
|
||||
}
|
||||
|
||||
public event EventHandler<DirectSoundUpdateEventArgs> Update;
|
||||
|
||||
private const int BlockCount = 2;
|
||||
|
||||
private int _sampleRate;
|
||||
@ -178,6 +148,7 @@ namespace Jellyfish.Library
|
||||
private IntPtr _window;
|
||||
private IDirectSound _device;
|
||||
private IDirectSoundBuffer _buffer;
|
||||
private Action<IntPtr, int> _updater;
|
||||
|
||||
private AutoResetEvent _position1Event = new AutoResetEvent(false);
|
||||
private AutoResetEvent _position2Event = new AutoResetEvent(false);
|
||||
|
@ -36,8 +36,7 @@ namespace Jellyfish.Library
|
||||
{
|
||||
}
|
||||
|
||||
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 @@ namespace Jellyfish.Library
|
||||
{
|
||||
}
|
||||
|
||||
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 @@ namespace Jellyfish.Library
|
||||
{
|
||||
}
|
||||
|
||||
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 @@ namespace Jellyfish.Library
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public void GetSecurityAttributes(bool inheritable, Action<SecurityAttributes> action)
|
||||
public void GetSecurityAttributes(Action<SecurityAttributes> action, bool inheritable = false)
|
||||
{
|
||||
GetSecurityAttributes(this, inheritable, action);
|
||||
GetSecurityAttributes(this, action, inheritable);
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public static void GetSecurityAttributes(ObjectSecurity security, bool inheritable, Action<SecurityAttributes> action)
|
||||
public static void GetSecurityAttributes(ObjectSecurity security, Action<SecurityAttributes> action, bool inheritable = false)
|
||||
{
|
||||
if (action == null)
|
||||
{
|
||||
|
@ -22,7 +22,7 @@ namespace Jellyfish.Library
|
||||
{
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
||||
public SafeGlobalAllocHandle() :
|
||||
base(true)
|
||||
base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
@ -33,12 +33,6 @@ namespace Jellyfish.Library
|
||||
SetHandle(existingHandle);
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public static SafeGlobalAllocHandle Allocate(int size)
|
||||
{
|
||||
return Allocate(0x0, size);
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public static SafeGlobalAllocHandle Allocate(byte[] value)
|
||||
{
|
||||
@ -53,15 +47,8 @@ namespace Jellyfish.Library
|
||||
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 @@ namespace Jellyfish.Library
|
||||
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 @@ namespace Jellyfish.Library
|
||||
{
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
||||
public SafeLocalAllocHandle() :
|
||||
base(true)
|
||||
base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
@ -102,12 +96,6 @@ namespace Jellyfish.Library
|
||||
SetHandle(existingHandle);
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public static SafeLocalAllocHandle Allocate(int size)
|
||||
{
|
||||
return Allocate(0x0, size);
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public static SafeLocalAllocHandle Allocate(byte[] value)
|
||||
{
|
||||
@ -122,15 +110,8 @@ namespace Jellyfish.Library
|
||||
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 @@ namespace Jellyfish.Library
|
||||
return alloc;
|
||||
}
|
||||
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
||||
[SecurityCritical]
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
return (NativeMethods.LocalFree(handle) == IntPtr.Zero);
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
[SuppressUnmanagedCodeSecurity]
|
||||
private static class NativeMethods
|
||||
|
@ -14,7 +14,7 @@ namespace Jellyfish.Library
|
||||
{
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
||||
public SafeFileHandle() :
|
||||
base(true)
|
||||
base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
@ -64,18 +64,18 @@ namespace Jellyfish.Library
|
||||
|
||||
[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;
|
||||
}
|
||||
|
@ -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 @@ namespace Jellyfish.Library
|
||||
|
||||
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 @@ namespace Jellyfish.Library
|
||||
// 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))
|
||||
|
@ -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<byte[], int> 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 @@ namespace Jellyfish.Library
|
||||
|
||||
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 @@ namespace Jellyfish.Library
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public event EventHandler<WaveMediaStreamSourceUpdateEventArgs> Update;
|
||||
|
||||
private byte[] _buffer;
|
||||
private int _bufferSize;
|
||||
private MemoryStream _bufferStream;
|
||||
private Action<byte[], int> _updater;
|
||||
private WaveFormat _waveFormat;
|
||||
private long _timestamp;
|
||||
private MediaStreamDescription _audioDescription;
|
||||
|
@ -49,6 +49,20 @@ namespace Jellyfish.Library
|
||||
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' };
|
||||
}
|
||||
}
|
||||
|
@ -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 @@ namespace Jellyfish.Library
|
||||
|
||||
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 @@ namespace Jellyfish.Library
|
||||
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);
|
||||
|
@ -8,13 +8,7 @@ namespace Jellyfish.Library
|
||||
public static class XmlSerializerHelpers
|
||||
{
|
||||
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
|
||||
public static T Deserialize<T>(Stream stream)
|
||||
{
|
||||
return Deserialize<T>(stream, null);
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
|
||||
public static T Deserialize<T>(Stream stream, string defaultNamespace)
|
||||
public static T Deserialize<T>(Stream stream, string defaultNamespace = null)
|
||||
{
|
||||
using (var reader = XmlReader.Create(stream))
|
||||
{
|
||||
@ -23,12 +17,7 @@ namespace Jellyfish.Library
|
||||
}
|
||||
}
|
||||
|
||||
public static void Serialize<T>(Stream stream, T instance)
|
||||
{
|
||||
Serialize<T>(stream, instance, null);
|
||||
}
|
||||
|
||||
public static void Serialize<T>(Stream stream, T instance, string defaultNamespace)
|
||||
public static void Serialize<T>(Stream stream, T instance, string defaultNamespace = null)
|
||||
{
|
||||
using (var writer = XmlWriter.Create(stream))
|
||||
{
|
||||
|
@ -33,8 +33,7 @@ namespace Jellyfish.Library
|
||||
{
|
||||
_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
|
||||
|
@ -31,6 +31,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Windows Phone' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -45,6 +46,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Xna.Framework">
|
||||
|
@ -31,6 +31,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Xbox 360' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -45,6 +46,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Xna.Framework">
|
||||
|
@ -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 @@ namespace Jellyfish.Virtu
|
||||
{
|
||||
_keyboardService = Machine.Services.GetService<KeyboardService>();
|
||||
_gamePortService = Machine.Services.GetService<GamePortService>();
|
||||
|
||||
_keyboardService.AsciiKeyDown += (sender, e) => Latch = e.AsciiKey;
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
|
||||
@ -121,10 +117,9 @@ namespace Jellyfish.Virtu
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
@ -55,7 +55,7 @@ namespace Jellyfish.Virtu.Services
|
||||
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;
|
||||
|
@ -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 @@ namespace Jellyfish.Virtu.Services
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnAsciiKeyDown(int asciiKey)
|
||||
{
|
||||
var handler = AsciiKeyDown;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, AsciiKeyEventArgs.Create(asciiKey));
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler<AsciiKeyEventArgs> AsciiKeyDown;
|
||||
|
||||
public bool IsAnyKeyDown { get; protected set; }
|
||||
public bool IsOpenAppleKeyDown { get; protected set; }
|
||||
public bool IsCloseAppleKeyDown { get; protected set; }
|
||||
|
@ -39,12 +39,7 @@ namespace Jellyfish.Virtu.Services
|
||||
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
|
||||
public T GetService<T>()
|
||||
{
|
||||
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 @@ namespace Jellyfish.Virtu.Services
|
||||
_serviceProviders.Remove(serviceType);
|
||||
}
|
||||
|
||||
object IServiceProvider.GetService(Type serviceType)
|
||||
{
|
||||
return _serviceProviders.ContainsKey(serviceType) ? _serviceProviders[serviceType] : null;
|
||||
}
|
||||
|
||||
private Dictionary<Type, MachineService> _serviceProviders = new Dictionary<Type, MachineService>();
|
||||
}
|
||||
}
|
||||
|
@ -14,12 +14,7 @@ namespace Jellyfish.Virtu.Services
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -20,15 +20,25 @@ namespace Jellyfish.Virtu.Services
|
||||
|
||||
_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 @@ namespace Jellyfish.Virtu.Services
|
||||
// });
|
||||
//}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ namespace Jellyfish.Virtu.Services
|
||||
int asciiKey = GetAsciiKey(e.Key, e.PlatformKeyCode);
|
||||
if (asciiKey >= 0)
|
||||
{
|
||||
OnAsciiKeyDown(asciiKey);
|
||||
Machine.Keyboard.Latch = asciiKey;
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
|
@ -18,9 +18,9 @@ namespace Jellyfish.Virtu.Services
|
||||
}
|
||||
|
||||
_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 @@ namespace Jellyfish.Virtu.Services
|
||||
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 @@ namespace Jellyfish.Virtu.Services
|
||||
// });
|
||||
//}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ namespace Jellyfish.Virtu.Services
|
||||
int asciiKey = GetAsciiKey(e.Key, e.KeyboardDevice);
|
||||
if (asciiKey >= 0)
|
||||
{
|
||||
OnAsciiKeyDown(asciiKey);
|
||||
Machine.Keyboard.Latch = asciiKey;
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
|
@ -37,6 +37,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Windows Phone' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -51,6 +52,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Xna.Framework">
|
||||
|
@ -33,6 +33,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Xbox 360' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -47,6 +48,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis>false</RunCodeAnalysis>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Xna.Framework">
|
||||
|
@ -15,6 +15,8 @@ namespace Jellyfish.Virtu
|
||||
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;
|
||||
|
@ -19,6 +19,7 @@ namespace Jellyfish.Virtu.Services
|
||||
_dynamicSoundEffect.BufferNeeded += OnDynamicSoundEffectBufferNeeded;
|
||||
_game.Exiting += (sender, e) => _dynamicSoundEffect.Stop();
|
||||
|
||||
_dynamicSoundEffect.SubmitBuffer(SampleZero);
|
||||
_dynamicSoundEffect.Play();
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ namespace Jellyfish.Virtu.Services
|
||||
int asciiKey = GetAsciiKey(key, gamePadControl);
|
||||
if (asciiKey >= 0)
|
||||
{
|
||||
OnAsciiKeyDown(asciiKey);
|
||||
Machine.Keyboard.Latch = asciiKey;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user