using System; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; namespace BizHawk.Emulation.Common.IEmulatorExtensions { public static class Extensions { public static CoreAttributes Attributes(this IEmulator core) { return (CoreAttributes)Attribute.GetCustomAttribute(core.GetType(), typeof(CoreAttributes)); } // todo: most of the special cases involving the NullEmulator should probably go away public static bool IsNull(this IEmulator core) { return core == null || core is NullEmulator; } public static bool HasVideoProvider(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IVideoProvider AsVideoProvider(this IEmulator core) { return core.ServiceProvider.GetService(); } /// /// Returns the core's VideoProvider, or a suitable dummy provider /// public static IVideoProvider AsVideoProviderOrDefault(this IEmulator core) { return core.ServiceProvider.GetService() ?? NullVideo.Instance; } public static bool HasSoundProvider(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static ISoundProvider AsSoundProvider(this IEmulator core) { return core.ServiceProvider.GetService(); } private static readonly ConditionalWeakTable CachedNullSoundProviders = new ConditionalWeakTable(); /// /// returns the core's SoundProvider, or a suitable dummy provider /// public static ISoundProvider AsSoundProviderOrDefault(this IEmulator core) { return core.ServiceProvider.GetService() ?? CachedNullSoundProviders.GetValue(core, e => new NullSound(e.CoreComm.VsyncNum, e.CoreComm.VsyncDen)); } public static bool HasMemoryDomains(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IMemoryDomains AsMemoryDomains(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool HasSaveRam(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static ISaveRam AsSaveRam(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool HasSavestates(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IStatable AsStatable(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool CanPollInput(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IInputPollable AsInputPollable(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool InputCallbacksAvailable(this IEmulator core) { if (core == null) { return false; } // TODO: this is a pretty ugly way to handle this var pollable = core.ServiceProvider.GetService(); if (pollable != null) { try { var callbacks = pollable.InputCallbacks; return true; } catch (NotImplementedException) { return false; } } return false; } public static bool HasDriveLight(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IDriveLight AsDriveLight(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool CanDebug(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IDebuggable AsDebuggable(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool CpuTraceAvailable(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static ITraceable AsTracer(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool MemoryCallbacksAvailable(this IEmulator core) { if (core == null) { return false; } // TODO: this is a pretty ugly way to handle this var debuggable = (IDebuggable)core.ServiceProvider.GetService(); if (debuggable != null) { try { var callbacks = debuggable.MemoryCallbacks; return true; } catch (NotImplementedException) { return false; } } return false; } public static bool MemoryCallbacksAvailable(this IDebuggable core) { if (core == null) { return false; } try { var callbacks = core.MemoryCallbacks; return true; } catch (NotImplementedException) { return false; } } public static bool CanDisassemble(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IDisassemblable AsDissassembler(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool CanPoke(this MemoryDomain d) { if (!d.Writable) { return false; } // once upon a time, we did a try { poke(peek) } here, but that was before Writable was added. the poke(peek) is not acceptable. If there are further problems, make sure Writable is correct. return true; } public static bool HasRegions(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static IRegionable AsRegionable(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool CanCDLog(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static ICodeDataLogger AsCodeDataLogger(this IEmulator core) { return core.ServiceProvider.GetService(); } public static ILinkable AsLinkable(this IEmulator core) { return core.ServiceProvider.GetService(); } public static bool UsesLinkCable(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static bool CanGenerateGameDBEntries(this IEmulator core) { if (core == null) { return false; } return core.ServiceProvider.HasService(); } public static ICreateGameDBEntries AsGameDBEntryGenerator(this IEmulator core) { return core.ServiceProvider.GetService(); } // TODO: a better place for these public static bool IsImplemented(this MethodInfo info) { // If a method is marked as Not implemented, it is not implemented no matter what the body is if (info.GetCustomAttributes(false).Any(a => a is FeatureNotImplemented)) { return false; } // adelikat: we can't rely on this anymore // Some methods throw an exception by design, such as ISoundProvider.GetSamplesAsync() // If async is not provided by the implementation this method will throw an exception // We need to figure out a reliable way to check specifically for a NotImplementedException, then maybe this method will be more useful // If a method is not marked but all it does is throw an exception, consider it not implemented //return !info.ThrowsError(); return true; } public static bool IsImplemented(this PropertyInfo info) { return !info.GetCustomAttributes(false).Any(a => a is FeatureNotImplemented); } } }