diff --git a/src/Avalonia.Base/Platform/IAssetLoader.cs b/src/Avalonia.Base/Platform/IAssetLoader.cs index ba30af60bf..dda2cbc2d5 100644 --- a/src/Avalonia.Base/Platform/IAssetLoader.cs +++ b/src/Avalonia.Base/Platform/IAssetLoader.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Collections.Generic; using System.IO; using System.Reflection; @@ -18,8 +19,8 @@ namespace Avalonia.Platform /// AssetLoader needs a refactor cause right now it lives in 3+ platforms which /// can all be loaded on Windows. /// - /// - void SetDefaultAssembly(Assembly asm); + /// + void SetDefaultAssembly(Assembly assembly); /// /// Checks if an asset with the specified URI exists. @@ -32,32 +33,39 @@ namespace Avalonia.Platform bool Exists(Uri uri, Uri baseUri = null); /// - /// Opens the resource with the requested URI. + /// Opens the asset with the requested URI. /// /// The URI. /// /// A base URI to use if is relative. /// - /// A stream containing the resource contents. + /// A stream containing the asset contents. /// - /// The resource was not found. + /// The asset could not be found. /// Stream Open(Uri uri, Uri baseUri = null); /// - /// Opens the resource with the requested URI and returns the resource string and the - /// assembly containing the resource. + /// 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 stream containing the asset contents together with the assembly. /// /// - /// The resource was not found. + /// The asset could not be found. /// - Tuple OpenAndGetAssembly(Uri uri, Uri baseUri = null); + (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null); + + /// + /// Gets all assets of a folder and subfolders that match specified uri. + /// + /// The URI. + /// All matching assets as a tuple of the absolute path to the asset and the assembly containing the asset + IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri); } } diff --git a/src/Shared/PlatformSupport/AssetLoader.cs b/src/Shared/PlatformSupport/AssetLoader.cs index fa11edb57b..b4d17b22a1 100644 --- a/src/Shared/PlatformSupport/AssetLoader.cs +++ b/src/Shared/PlatformSupport/AssetLoader.cs @@ -57,21 +57,21 @@ namespace Avalonia.Shared.PlatformSupport } /// - /// Opens the resource with the requested URI. + /// Opens the asset with the requested URI. /// /// The URI. /// /// A base URI to use if is relative. /// - /// A stream containing the resource contents. + /// A stream containing the asset contents. /// - /// The resource was not found. + /// The asset could not be found. /// public Stream Open(Uri uri, Uri baseUri = null) => OpenAndGetAssembly(uri, baseUri).Item1; - + /// - /// Opens the resource with the requested URI and returns the resource string and the - /// assembly containing the resource. + /// Opens the asset with the requested URI and returns the asset stream and the + /// assembly containing the asset. /// /// The URI. /// @@ -81,9 +81,9 @@ namespace Avalonia.Shared.PlatformSupport /// The stream containing the resource contents together with the assembly. /// /// - /// The resource was not found. + /// The asset could not be found. /// - public Tuple OpenAndGetAssembly(Uri uri, Uri baseUri = null) + public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null) { var asset = GetAsset(uri, baseUri); @@ -92,7 +92,21 @@ namespace Avalonia.Shared.PlatformSupport throw new FileNotFoundException($"The resource {uri} could not be found."); } - return Tuple.Create(asset.GetStream(), asset.Assembly); + return (asset.GetStream(), asset.Assembly); + } + + /// + /// Gets all assets of a folder and subfolders that match specified uri. + /// + /// The URI. + /// All matching assets as a tuple of the absolute path to the asset and the assembly containing the asset + public IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri) + { + var assembly = GetAssembly(uri); + + return assembly?.Resources.Where(x => x.Key.Contains(uri.AbsolutePath)) + .Select(x => (x.Key, x.Value.Assembly)) ?? + Enumerable.Empty<(string AbsolutePath, Assembly Assembly)>(); } private IAssetDescriptor GetAsset(Uri uri, Uri baseUri) @@ -219,4 +233,4 @@ namespace Avalonia.Shared.PlatformSupport public string Name { get; } } } -} +} \ No newline at end of file diff --git a/tests/Avalonia.UnitTests/MockAssetLoader.cs b/tests/Avalonia.UnitTests/MockAssetLoader.cs index d6b70aee16..11d66128a7 100644 --- a/tests/Avalonia.UnitTests/MockAssetLoader.cs +++ b/tests/Avalonia.UnitTests/MockAssetLoader.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; -using System.Threading.Tasks; using Avalonia.Platform; namespace Avalonia.UnitTests @@ -27,10 +26,16 @@ namespace Avalonia.UnitTests { return new MemoryStream(Encoding.UTF8.GetBytes(_assets[uri])); } - - public Tuple OpenAndGetAssembly(Uri uri, Uri baseUri = null) + + public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null) + { + return (Open(uri, baseUri), (Assembly)null); + } + + public IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri) { - return Tuple.Create(Open(uri, baseUri), (Assembly)null); + return _assets.Keys.Where(x => x.AbsolutePath.Contains(uri.AbsolutePath)) + .Select(x => (x.AbsolutePath, Assembly.GetEntryAssembly())); } public void SetDefaultAssembly(Assembly asm)