Skip to content

Commit

Permalink
refactor ResolveReaderType (#1553)
Browse files Browse the repository at this point in the history
* move resolve legacy type readers

* rename _contentMediaAssemblyName

* simplify resolvedReaderTypeName

* split assembly name literals

* use constants

* format strings

* _contentAudioAssemblyName

* format

* reorder
  • Loading branch information
nkast authored May 12, 2024
1 parent 34205ba commit 10ea14e
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions src/Xna.Framework.Content/Content/ContentTypeReaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public sealed class ContentTypeReaderManager

private static readonly string _contentAssemblyName;
private static readonly string _contentGraphicsAssemblyName;
private static readonly string _contentVideoAssemblyName;
private static readonly string _contentAudioAssemblyName;
private static readonly string _contentMediaAssemblyName;

private static readonly bool _isRunningOnNetCore;

Expand All @@ -32,7 +33,8 @@ static ContentTypeReaderManager()
_contentReadersCache = new Dictionary<Type, ContentTypeReader>(255);
_contentAssemblyName = ReflectionHelpers.GetAssembly(typeof(ContentTypeReaderManager)).FullName;
_contentGraphicsAssemblyName = "Xna.Framework.Graphics";
_contentVideoAssemblyName = "Xna.Framework.Media";
_contentAudioAssemblyName = "Xna.Framework.Audio";
_contentMediaAssemblyName = "Xna.Framework.Media";

_isRunningOnNetCore = ReflectionHelpers.GetAssembly(typeof(System.Object)).GetName().Name == "System.Private.CoreLib";

Expand Down Expand Up @@ -203,7 +205,6 @@ internal static Type ResolveReaderType(string readerTypeName)
// map net.framework (.net4) to core.net (.net5 or later)
if (readerTypeName.Contains(", mscorlib") && _isRunningOnNetCore)
{
resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = readerTypeName.Replace(", mscorlib", ", System.Private.CoreLib");
readerType = Type.GetType(resolvedReaderTypeName);

Check warning on line 209 in src/Xna.Framework.Content/Content/ContentTypeReaderManager.cs

View workflow job for this annotation

GitHub Actions / build

Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type.
if (readerType != null)
Expand All @@ -212,8 +213,7 @@ internal static Type ResolveReaderType(string readerTypeName)
// map core.net (.net5 or later) to net.framework (.net4)
if (readerTypeName.Contains(", System.Private.CoreLib") && !_isRunningOnNetCore)
{
resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", System.Private.CoreLib", ", mscorlib");
resolvedReaderTypeName = readerTypeName.Replace(", System.Private.CoreLib", ", mscorlib");
readerType = Type.GetType(resolvedReaderTypeName);

Check warning on line 217 in src/Xna.Framework.Content/Content/ContentTypeReaderManager.cs

View workflow job for this annotation

GitHub Actions / build

Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type.
if (readerType != null)
return readerType;
Expand All @@ -222,58 +222,50 @@ internal static Type ResolveReaderType(string readerTypeName)
// map XNA build-in TypeReaders
resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", Microsoft.Xna.Framework.Graphics", string.Format(", {0}", _contentGraphicsAssemblyName));
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", Microsoft.Xna.Framework.Video", string.Format(", {0}", _contentVideoAssemblyName));
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", Microsoft.Xna.Framework", string.Format(", {0}", _contentAssemblyName));
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", Microsoft.Xna.Framework.Video", string.Format(", {0}", _contentMediaAssemblyName));
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", Microsoft.Xna.Framework", string.Format(", {0}", _contentAssemblyName));
readerType = Type.GetType(resolvedReaderTypeName);

Check warning on line 227 in src/Xna.Framework.Content/Content/ContentTypeReaderManager.cs

View workflow job for this annotation

GitHub Actions / build

Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type.
if (readerType != null)
return readerType;

// map XNA & MonoGame build-in TypeReaders
resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", Microsoft.Xna.Framework", string.Format(", {0}", "Xna.Framework"));
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", "Xna.Framework"));
// map XNA build-in TypeReaders
resolvedReaderTypeName = readerTypeName.Replace(", Microsoft.Xna.Framework", string.Format(", {0}", "Xna.Framework"));
readerType = Type.GetType(resolvedReaderTypeName);

Check warning on line 233 in src/Xna.Framework.Content/Content/ContentTypeReaderManager.cs

View workflow job for this annotation

GitHub Actions / build

Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type.
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", "Xna.Framework.Audio"));
resolvedReaderTypeName = readerTypeName + string.Format(", {0}", "MonoGame.Framework");
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", "Xna.Framework.Media"));
resolvedReaderTypeName = readerTypeName + string.Format(", {0}", _contentGraphicsAssemblyName);
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = resolvedReaderTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", "Xna.Framework.Graphics"));
resolvedReaderTypeName = readerTypeName + string.Format(", {0}", _contentAudioAssemblyName);
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = readerTypeName + ", MonoGame.Framework";
resolvedReaderTypeName = readerTypeName + string.Format(", {0}", _contentMediaAssemblyName);
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = readerTypeName + ", Xna.Framework.Audio";
// map MonoGame build-in TypeReaders
resolvedReaderTypeName = readerTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", "Xna.Framework"));
readerType = Type.GetType(resolvedReaderTypeName);

Check warning on line 257 in src/Xna.Framework.Content/Content/ContentTypeReaderManager.cs

View workflow job for this annotation

GitHub Actions / build

Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type.
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = readerTypeName + ", Xna.Framework.Media";
resolvedReaderTypeName = readerTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", _contentGraphicsAssemblyName));
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;

resolvedReaderTypeName = readerTypeName;
resolvedReaderTypeName = readerTypeName + ", Xna.Framework.Graphics";
resolvedReaderTypeName = readerTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", _contentAudioAssemblyName));
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;
resolvedReaderTypeName = readerTypeName.Replace(", MonoGame.Framework", string.Format(", {0}", _contentMediaAssemblyName));
readerType = Type.GetType(resolvedReaderTypeName);
if (readerType != null)
return readerType;
Expand Down

0 comments on commit 10ea14e

Please sign in to comment.