@ -20,20 +20,20 @@ namespace Avalonia.Shared.PlatformSupport
private static readonly Dictionary < string , AssemblyDescriptor > AssemblyNameCache
private static readonly Dictionary < string , AssemblyDescriptor > AssemblyNameCache
= new Dictionary < string , AssemblyDescriptor > ( ) ;
= new Dictionary < string , AssemblyDescriptor > ( ) ;
private AssemblyDescriptor _d efaultAssembly ;
private AssemblyDescriptor _d efaultResm Assembly ;
/// <summary>
/// <summary>
/// Initializes a new instance of the <see cref="AssetLoader"/> class.
/// Initializes a new instance of the <see cref="AssetLoader"/> class.
/// </summary>
/// </summary>
/// <param name="assembly">
/// <param name="assembly">
/// The default assembly from which to load assets for which no assembly is specified.
/// The default assembly from which to load resm: assets for which no assembly is specified.
/// </param>
/// </param>
public AssetLoader ( Assembly assembly = null )
public AssetLoader ( Assembly assembly = null )
{
{
if ( assembly = = null )
if ( assembly = = null )
assembly = Assembly . GetEntryAssembly ( ) ;
assembly = Assembly . GetEntryAssembly ( ) ;
if ( assembly ! = null )
if ( assembly ! = null )
_d efaultAssembly = new AssemblyDescriptor ( assembly ) ;
_d efaultResm Assembly = new AssemblyDescriptor ( assembly ) ;
}
}
/// <summary>
/// <summary>
@ -42,7 +42,7 @@ namespace Avalonia.Shared.PlatformSupport
/// <param name="assembly">The default assembly.</param>
/// <param name="assembly">The default assembly.</param>
public void SetDefaultAssembly ( Assembly assembly )
public void SetDefaultAssembly ( Assembly assembly )
{
{
_d efaultAssembly = new AssemblyDescriptor ( assembly ) ;
_d efaultResm Assembly = new AssemblyDescriptor ( assembly ) ;
}
}
/// <summary>
/// <summary>
@ -115,7 +115,7 @@ namespace Avalonia.Shared.PlatformSupport
}
}
uri = EnsureAbsolute ( uri , baseUri ) ;
uri = EnsureAbsolute ( uri , baseUri ) ;
if ( uri . Scheme = = "res" )
if ( uri . Scheme = = "ava res" )
{
{
var ( asm , path ) = GetResAsmAndPath ( uri ) ;
var ( asm , path ) = GetResAsmAndPath ( uri ) ;
if ( asm = = null )
if ( asm = = null )
@ -129,7 +129,7 @@ namespace Avalonia.Shared.PlatformSupport
return Enumerable . Empty < Uri > ( ) ;
return Enumerable . Empty < Uri > ( ) ;
path = path . TrimEnd ( '/' ) + '/' ;
path = path . TrimEnd ( '/' ) + '/' ;
return asm . AvaloniaResources . Where ( r = > r . Key . StartsWith ( path ) )
return asm . AvaloniaResources . Where ( r = > r . Key . StartsWith ( path ) )
. Select ( x = > new Uri ( $"res:asm: {asm.Name}{x.Key}" ) ) ;
. Select ( x = > new Uri ( $"avares:// {asm.Name}{x.Key}" ) ) ;
}
}
return Enumerable . Empty < Uri > ( ) ;
return Enumerable . Empty < Uri > ( ) ;
@ -153,7 +153,7 @@ namespace Avalonia.Shared.PlatformSupport
{
{
if ( uri . IsAbsoluteUri & & uri . Scheme = = "resm" )
if ( uri . IsAbsoluteUri & & uri . Scheme = = "resm" )
{
{
var asm = GetAssembly ( uri ) ? ? GetAssembly ( baseUri ) ? ? _d efaultAssembly ;
var asm = GetAssembly ( uri ) ? ? GetAssembly ( baseUri ) ? ? _d efaultResm Assembly ;
if ( asm = = null )
if ( asm = = null )
{
{
@ -171,13 +171,9 @@ namespace Avalonia.Shared.PlatformSupport
uri = EnsureAbsolute ( uri , baseUri ) ;
uri = EnsureAbsolute ( uri , baseUri ) ;
if ( uri . Scheme = = "res" )
if ( uri . Scheme = = "ava res" )
{
{
var ( asm , path ) = GetResAsmAndPath ( uri ) ;
var ( asm , path ) = GetResAsmAndPath ( uri ) ;
if ( asm = = null )
throw new ArgumentException (
"No default assembly, entry assembly or explicit assembly specified; " +
"don't know where to look up for the resource, try specifying assembly explicitly." ) ;
if ( asm . AvaloniaResources = = null )
if ( asm . AvaloniaResources = = null )
return null ;
return null ;
asm . AvaloniaResources . TryGetValue ( path , out var desc ) ;
asm . AvaloniaResources . TryGetValue ( path , out var desc ) ;
@ -189,18 +185,8 @@ namespace Avalonia.Shared.PlatformSupport
private ( AssemblyDescriptor asm , string path ) GetResAsmAndPath ( Uri uri )
private ( AssemblyDescriptor asm , string path ) GetResAsmAndPath ( Uri uri )
{
{
string path = null , asmPart = null ;
var asm = GetAssembly ( uri . Authority ) ;
if ( ! uri . AbsolutePath . StartsWith ( "asm:" ) )
return ( asm , uri . AbsolutePath ) ;
path = uri . AbsolutePath ;
else
{
var sp = uri . AbsolutePath . Split ( new [ ] { '/' } , 2 ) ;
asmPart = sp [ 0 ] . Substring ( 4 ) ;
path = '/' + sp [ 1 ] ;
}
var asm = ( asmPart = = null ? null : GetAssembly ( asmPart ) ) ? ? _d efaultAssembly ;
return ( asm , path ) ;
}
}
private AssemblyDescriptor GetAssembly ( Uri uri )
private AssemblyDescriptor GetAssembly ( Uri uri )
@ -209,7 +195,7 @@ namespace Avalonia.Shared.PlatformSupport
{
{
if ( ! uri . IsAbsoluteUri )
if ( ! uri . IsAbsoluteUri )
return null ;
return null ;
if ( uri . Scheme = = "res" )
if ( uri . Scheme = = "ava res" )
return GetResAsmAndPath ( uri ) . asm ;
return GetResAsmAndPath ( uri ) . asm ;
if ( uri . Scheme = = "resm" )
if ( uri . Scheme = = "resm" )
@ -230,9 +216,7 @@ namespace Avalonia.Shared.PlatformSupport
private AssemblyDescriptor GetAssembly ( string name )
private AssemblyDescriptor GetAssembly ( string name )
{
{
if ( name = = null )
if ( name = = null )
{
throw new ArgumentNullException ( nameof ( name ) ) ;
return _d efaultAssembly ;
}
AssemblyDescriptor rv ;
AssemblyDescriptor rv ;
if ( ! AssemblyNameCache . TryGetValue ( name , out rv ) )
if ( ! AssemblyNameCache . TryGetValue ( name , out rv ) )
@ -247,9 +231,7 @@ namespace Avalonia.Shared.PlatformSupport
{
{
// iOS does not support loading assemblies dynamically!
// iOS does not support loading assemblies dynamically!
//
//
#if NETCOREAPP1_0
#if __IOS__
AssemblyNameCache [ name ] = rv = new AssemblyDescriptor ( Assembly . Load ( new AssemblyName ( name ) ) ) ;
#elif __IOS__
throw new InvalidOperationException (
throw new InvalidOperationException (
$"Assembly {name} needs to be referenced and explicitly loaded before loading resources" ) ;
$"Assembly {name} needs to be referenced and explicitly loaded before loading resources" ) ;
#else
#else
@ -400,5 +382,13 @@ namespace Avalonia.Shared.PlatformSupport
public Dictionary < string , IAssetDescriptor > AvaloniaResources { get ; }
public Dictionary < string , IAssetDescriptor > AvaloniaResources { get ; }
public string Name { get ; }
public string Name { get ; }
}
}
public static void RegisterResUriParsers ( )
{
UriParser . Register ( new GenericUriParser ( GenericUriParserOptions . GenericAuthority |
GenericUriParserOptions . NoUserInfo |
GenericUriParserOptions . NoPort | GenericUriParserOptions . NoQuery |
GenericUriParserOptions . NoFragment ) , "avares" , - 1 ) ;
}
}
}
}
}