diff --git a/src/Avalonia.Base/Platform/StandardAssetLoader.cs b/src/Avalonia.Base/Platform/StandardAssetLoader.cs
index 387f77f59b..118e57c7af 100644
--- a/src/Avalonia.Base/Platform/StandardAssetLoader.cs
+++ b/src/Avalonia.Base/Platform/StandardAssetLoader.cs
@@ -7,250 +7,249 @@ using System.Reflection;
using Avalonia.Platform.Internal;
using Avalonia.Utilities;
-namespace Avalonia.Platform
+namespace Avalonia.Platform;
+
+///
+/// Loads assets compiled into the application binary.
+///
+internal class StandardAssetLoader : IAssetLoader
{
+ private readonly IAssemblyDescriptorResolver _assemblyDescriptorResolver;
+ private AssemblyDescriptor? _defaultResmAssembly;
+
+ public StandardAssetLoader(IAssemblyDescriptorResolver resolver, Assembly? assembly = null)
+ {
+ if (assembly == null)
+ assembly = Assembly.GetEntryAssembly();
+ if (assembly != null)
+ _defaultResmAssembly = new AssemblyDescriptor(assembly);
+ _assemblyDescriptorResolver = resolver;
+ }
+
+ public StandardAssetLoader(Assembly? assembly = null) : this(new AssemblyDescriptorResolver(), assembly)
+ {
+
+ }
+
///
- /// Loads assets compiled into the application binary.
+ /// Sets the default assembly from which to load assets for which no assembly is specified.
///
- internal class StandardAssetLoader : IAssetLoader
+ /// The default assembly.
+ public void SetDefaultAssembly(Assembly assembly)
{
- private readonly IAssemblyDescriptorResolver _assemblyDescriptorResolver;
- private AssemblyDescriptor? _defaultResmAssembly;
+ _defaultResmAssembly = new AssemblyDescriptor(assembly);
+ }
- public StandardAssetLoader(IAssemblyDescriptorResolver resolver, Assembly? assembly = null)
- {
- if (assembly == null)
- assembly = Assembly.GetEntryAssembly();
- if (assembly != null)
- _defaultResmAssembly = new AssemblyDescriptor(assembly);
- _assemblyDescriptorResolver = resolver;
- }
+ ///
+ /// Checks if an asset with the specified URI exists.
+ ///
+ /// The URI.
+ ///
+ /// A base URI to use if is relative.
+ ///
+ /// True if the asset could be found; otherwise false.
+ public bool Exists(Uri uri, Uri? baseUri = null)
+ {
+ return TryGetAsset(uri, baseUri, out _);
+ }
- public StandardAssetLoader(Assembly? assembly = null) : this(new AssemblyDescriptorResolver(), assembly)
+ ///
+ /// Opens the asset with the requested URI.
+ ///
+ /// The URI.
+ ///
+ /// A base URI to use if is relative.
+ ///
+ /// A stream containing the asset contents.
+ ///
+ /// The asset could not be found.
+ ///
+ public Stream Open(Uri uri, Uri? baseUri = null) => OpenAndGetAssembly(uri, baseUri).Item1;
+
+ ///
+ /// Opens the asset with the requested URI and returns the asset stream and the
+ /// assembly containing the asset.
+ ///
+ /// The URI.
+ ///
+ /// A base URI to use if is relative.
+ ///
+ ///
+ /// The stream containing the resource contents together with the assembly.
+ ///
+ ///
+ /// The asset could not be found.
+ ///
+ public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri? baseUri = null)
+ {
+ if (TryGetAsset(uri, baseUri, out var assetDescriptor))
{
-
+ return (assetDescriptor.GetStream(), assetDescriptor.Assembly);
}
- ///
- /// Sets the default assembly from which to load assets for which no assembly is specified.
- ///
- /// The default assembly.
- public void SetDefaultAssembly(Assembly assembly)
+ throw new FileNotFoundException($"The resource {uri} could not be found.");
+ }
+
+ public Assembly? GetAssembly(Uri uri, Uri? baseUri)
+ {
+ if (!uri.IsAbsoluteUri && baseUri != null)
{
- _defaultResmAssembly = new AssemblyDescriptor(assembly);
+ uri = new Uri(baseUri, uri);
}
- ///
- /// Checks if an asset with the specified URI exists.
- ///
- /// The URI.
- ///
- /// A base URI to use if is relative.
- ///
- /// True if the asset could be found; otherwise false.
- public bool Exists(Uri uri, Uri? baseUri = null)
+ if (TryGetAssembly(uri, out var assemblyDescriptor))
{
- return TryGetAsset(uri, baseUri, out _);
+ return assemblyDescriptor.Assembly;
}
- ///
- /// Opens the asset with the requested URI.
- ///
- /// The URI.
- ///
- /// A base URI to use if is relative.
- ///
- /// A stream containing the asset contents.
- ///
- /// The asset could not be found.
- ///
- public Stream Open(Uri uri, Uri? baseUri = null) => OpenAndGetAssembly(uri, baseUri).Item1;
-
- ///
- /// Opens the asset with the requested URI and returns the asset stream and the
- /// assembly containing the asset.
- ///
- /// The URI.
- ///
- /// A base URI to use if is relative.
- ///
- ///
- /// The stream containing the resource contents together with the assembly.
- ///
- ///
- /// The asset could not be found.
- ///
- public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri? baseUri = null)
+ return null;
+ }
+
+ ///
+ /// Gets all assets of a folder and subfolders that match specified uri.
+ ///
+ /// The URI.
+ /// Base URI that is used if is relative.
+ /// All matching assets as a tuple of the absolute path to the asset and the assembly containing the asset
+ public IEnumerable GetAssets(Uri uri, Uri? baseUri)
+ {
+ if (uri.IsAbsoluteResm())
{
- if (TryGetAsset(uri, baseUri, out var assetDescriptor))
+ if (!TryGetAssembly(uri, out var assembly))
{
- return (assetDescriptor.GetStream(), assetDescriptor.Assembly);
+ assembly = _defaultResmAssembly;
}
- throw new FileNotFoundException($"The resource {uri} could not be found.");
+ return assembly?.Resources?
+ .Where(x => x.Key.Contains(uri.GetUnescapeAbsolutePath()))
+ .Select(x => new Uri($"resm:{x.Key}?assembly={assembly.Name}")) ??
+ Enumerable.Empty();
}
- public Assembly? GetAssembly(Uri uri, Uri? baseUri)
+ uri = uri.EnsureAbsolute(baseUri);
+
+ if (uri.IsAvares())
{
- if (!uri.IsAbsoluteUri && baseUri != null)
+ if (!TryGetResAsmAndPath(uri, out var assembly, out var path))
{
- uri = new Uri(baseUri, uri);
+ return Enumerable.Empty();
}
- if (TryGetAssembly(uri, out var assemblyDescriptor))
+ if (assembly?.AvaloniaResources == null)
{
- return assemblyDescriptor.Assembly;
+ return Enumerable.Empty();
}
- return null;
+ if (path.Length > 0 && path[path.Length - 1] != '/')
+ {
+ path += '/';
+ }
+
+ return assembly.AvaloniaResources
+ .Where(r => r.Key.StartsWith(path, StringComparison.Ordinal))
+ .Select(x => new Uri($"avares://{assembly.Name}{x.Key}"));
}
- ///
- /// Gets all assets of a folder and subfolders that match specified uri.
- ///
- /// The URI.
- /// Base URI that is used if is relative.
- /// All matching assets as a tuple of the absolute path to the asset and the assembly containing the asset
- public IEnumerable GetAssets(Uri uri, Uri? baseUri)
+ return Enumerable.Empty();
+ }
+
+ private bool TryGetAsset(Uri uri, Uri? baseUri, [NotNullWhen(true)] out IAssetDescriptor? assetDescriptor)
+ {
+ assetDescriptor = null;
+
+ if (uri.IsAbsoluteResm())
{
- if (uri.IsAbsoluteResm())
+ if (!TryGetAssembly(uri, out var assembly) && !TryGetAssembly(baseUri, out assembly))
{
- if (!TryGetAssembly(uri, out var assembly))
- {
- assembly = _defaultResmAssembly;
- }
-
- return assembly?.Resources?
- .Where(x => x.Key.Contains(uri.GetUnescapeAbsolutePath()))
- .Select(x => new Uri($"resm:{x.Key}?assembly={assembly.Name}")) ??
- Enumerable.Empty();
+ assembly = _defaultResmAssembly;
}
- uri = uri.EnsureAbsolute(baseUri);
-
- if (uri.IsAvares())
+ if (assembly?.Resources != null)
{
- if (!TryGetResAsmAndPath(uri, out var assembly, out var path))
- {
- return Enumerable.Empty();
- }
-
- if (assembly?.AvaloniaResources == null)
- {
- return Enumerable.Empty();
- }
+ var resourceKey = uri.AbsolutePath;
- if (path.Length > 0 && path[path.Length - 1] != '/')
+ if (assembly.Resources.TryGetValue(resourceKey, out assetDescriptor))
{
- path += '/';
+ return true;
}
-
- return assembly.AvaloniaResources
- .Where(r => r.Key.StartsWith(path, StringComparison.Ordinal))
- .Select(x => new Uri($"avares://{assembly.Name}{x.Key}"));
}
-
- return Enumerable.Empty();
}
- private bool TryGetAsset(Uri uri, Uri? baseUri, [NotNullWhen(true)] out IAssetDescriptor? assetDescriptor)
- {
- assetDescriptor = null;
+ uri = uri.EnsureAbsolute(baseUri);
- if (uri.IsAbsoluteResm())
+ if (uri.IsAvares())
+ {
+ if (TryGetResAsmAndPath(uri, out var assembly, out var path))
{
- if (!TryGetAssembly(uri, out var assembly) && !TryGetAssembly(baseUri, out assembly))
+ if (assembly.AvaloniaResources == null)
{
- assembly = _defaultResmAssembly;
+ return false;
}
- if (assembly?.Resources != null)
+ if (assembly.AvaloniaResources.TryGetValue(path, out assetDescriptor))
{
- var resourceKey = uri.AbsolutePath;
-
- if (assembly.Resources.TryGetValue(resourceKey, out assetDescriptor))
- {
- return true;
- }
+ return true;
}
}
+ }
- uri = uri.EnsureAbsolute(baseUri);
+ return false;
+ }
- if (uri.IsAvares())
- {
- if (TryGetResAsmAndPath(uri, out var assembly, out var path))
- {
- if (assembly.AvaloniaResources == null)
- {
- return false;
- }
-
- if (assembly.AvaloniaResources.TryGetValue(path, out assetDescriptor))
- {
- return true;
- }
- }
- }
+ private bool TryGetResAsmAndPath(Uri uri, [NotNullWhen(true)] out IAssemblyDescriptor? assembly, out string path)
+ {
+ path = uri.GetUnescapeAbsolutePath();
- return false;
+ if (TryLoadAssembly(uri.Authority, out assembly))
+ {
+ return true;
}
- private bool TryGetResAsmAndPath(Uri uri, [NotNullWhen(true)] out IAssemblyDescriptor? assembly, out string path)
+ return false;
+ }
+
+ private bool TryGetAssembly(Uri? uri, [NotNullWhen(true)] out IAssemblyDescriptor? assembly)
+ {
+ assembly = null;
+
+ if (uri != null)
{
- path = uri.GetUnescapeAbsolutePath();
+ if (!uri.IsAbsoluteUri)
+ {
+ return false;
+ }
- if (TryLoadAssembly(uri.Authority, out assembly))
+ if (uri.IsAvares() && TryGetResAsmAndPath(uri, out assembly, out _))
{
return true;
}
- return false;
- }
-
- private bool TryGetAssembly(Uri? uri, [NotNullWhen(true)] out IAssemblyDescriptor? assembly)
- {
- assembly = null;
-
- if (uri != null)
+ if (uri.IsResm())
{
- if (!uri.IsAbsoluteUri)
- {
- return false;
- }
+ var assemblyName = uri.GetAssemblyNameFromQuery();
- if (uri.IsAvares() && TryGetResAsmAndPath(uri, out assembly, out _))
+ if (assemblyName.Length > 0 && TryLoadAssembly(assemblyName, out assembly))
{
return true;
}
-
- if (uri.IsResm())
- {
- var assemblyName = uri.GetAssemblyNameFromQuery();
-
- if (assemblyName.Length > 0 && TryLoadAssembly(assemblyName, out assembly))
- {
- return true;
- }
- }
}
-
- return false;
}
- private bool TryLoadAssembly(string assemblyName, [NotNullWhen(true)] out IAssemblyDescriptor? assembly)
- {
- assembly = null;
+ return false;
+ }
- try
- {
- assembly = _assemblyDescriptorResolver.GetAssembly(assemblyName);
+ private bool TryLoadAssembly(string assemblyName, [NotNullWhen(true)] out IAssemblyDescriptor? assembly)
+ {
+ assembly = null;
- return true;
- }
- catch (Exception) { }
+ try
+ {
+ assembly = _assemblyDescriptorResolver.GetAssembly(assemblyName);
- return false;
+ return true;
}
+ catch (Exception) { }
+
+ return false;
}
}