From 080f5e1f23ed25e0011c7d6471b3e37fada85c30 Mon Sep 17 00:00:00 2001 From: Jeremy Swartwood Date: Tue, 19 Nov 2024 20:02:30 -0900 Subject: [PATCH] Add compile time exclusion (#964) * Add compile time exclusion * Accept suggested changes --- .../Content/ContentReaderExtensions.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/source/MonoGame.Extended/Content/ContentReaderExtensions.cs b/source/MonoGame.Extended/Content/ContentReaderExtensions.cs index 8becfe0d..64c54e19 100644 --- a/source/MonoGame.Extended/Content/ContentReaderExtensions.cs +++ b/source/MonoGame.Extended/Content/ContentReaderExtensions.cs @@ -9,12 +9,21 @@ namespace MonoGame.Extended.Content { public static class ContentReaderExtensions { - private static readonly FieldInfo _contentReaderGraphicsDeviceFieldInfo = typeof(ContentReader).GetTypeInfo().GetDeclaredField("graphicsDevice"); + +#if FNA + public static GraphicsDevice GetGraphicsDevice(this ContentReader contentReader) + { + var serviceProvider = contentReader.ContentManager.ServiceProvider; + var graphicsDeviceService = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService; + + if (graphicsDeviceService == null) + { + throw new InvalidOperationException("No Graphics Device Service"); + } - public static GraphicsDevice GetGraphicsDevice(this ContentReader contentReader) - { - return (GraphicsDevice)_contentReaderGraphicsDeviceFieldInfo.GetValue(contentReader); - } + return graphicsDeviceService.GraphicsDevice; + } +#endif public static string RemoveExtension(string path) {