Skip to content

Commit

Permalink
fix: Use UNO_BOOTSTRAP_WEBAPP_BASE_PATH to support path-based deep-li…
Browse files Browse the repository at this point in the history
…nking
  • Loading branch information
jeromelaban committed Jun 7, 2021
1 parent 58587d7 commit 890d5d5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
6 changes: 1 addition & 5 deletions src/AddIns/Uno.UI.Lottie/LottieVisualSourceBase.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Lottie
{
partial class LottieVisualSourceBase
{
private static readonly string? UNO_BOOTSTRAP_APP_BASE = global::System.Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_APP_BASE));

private AnimatedVisualPlayer? _initializedPlayer;
private Uri? _lastSource;
private Size _compositionSize = new Size(0, 0);
Expand Down Expand Up @@ -101,9 +99,7 @@ void OnJsonChanged(string updatedJson, string updatedCacheKey)
}
else
{
var documentPath = string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE)
? UriSource?.PathAndQuery
: UNO_BOOTSTRAP_APP_BASE + UriSource?.PathAndQuery;
var documentPath = Windows.Storage.Helpers.AssetsPathBuilder.BuildAssetUri(UriSource?.PathAndQuery);
_domLoaded = false;

js = new[]
Expand Down
5 changes: 3 additions & 2 deletions src/AddIns/Uno.UI.Lottie/Uno.UI.Lottie.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>

<Import Project="../../netcore-build.props"/>
<Import Project="../../targetframework-override.props"/>
<Import Project="../../netcore-build.props" />
<Import Project="../../targetframework-override.props" />

<PropertyGroup>
<NoWarn>$(NoWarn);NU1701</NoWarn>
Expand All @@ -28,6 +28,7 @@

<ItemGroup>
<None Remove="tsconfig.json" />
<Compile Include="..\..\Uno.UWP\Storage\Helpers\AssetsPathBuilder.wasm.cs" Link="AssetsPathBuilder.wasm.cs" />
<Content Include="tsconfig.json" />
</ItemGroup>

Expand Down
11 changes: 4 additions & 7 deletions src/Uno.UI/UI/Xaml/Media/Imaging/BitmapImage.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
using Uno.Extensions;
using Uno.Foundation;
using Windows.Graphics.Display;
using Windows.Storage.Helpers;
using Windows.Storage.Streams;
using Path = global::System.IO.Path;

namespace Windows.UI.Xaml.Media.Imaging
{
public sealed partial class BitmapImage : BitmapSource
{
private static readonly string UNO_BOOTSTRAP_APP_BASE = Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_APP_BASE));

internal ResolutionScale? ScaleOverride { get; set; }

internal string ContentType { get; set; } = "application/octet-stream";
Expand Down Expand Up @@ -72,7 +71,7 @@ internal static class AssetResolver

private static async Task<HashSet<string>> GetAssets()
{
var assetsUri = !string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE) ? $"{UNO_BOOTSTRAP_APP_BASE}/uno-assets.txt" : "uno-assets.txt";
var assetsUri = AssetsPathBuilder.BuildAssetUri("uno-assets.txt");

var assets = await WebAssemblyRuntime.InvokeAsync($"fetch('{assetsUri}').then(r => r.text())");

Expand Down Expand Up @@ -137,14 +136,12 @@ private static string GetScaledPath(string path, HashSet<string> assets, Resolut

if (assets.Contains(filePath))
{
return !string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE) ?
$"{UNO_BOOTSTRAP_APP_BASE}/{filePath}" :
filePath;
return AssetsPathBuilder.BuildAssetUri(filePath);
}
}
}

return !string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE) ? $"{UNO_BOOTSTRAP_APP_BASE}/{path}" : path;
return AssetsPathBuilder.BuildAssetUri(path);
}

return path;
Expand Down
5 changes: 2 additions & 3 deletions src/Uno.UI/UI/Xaml/Media/Imaging/SvgImageSource.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
using Uno.Extensions;
using Uno.Logging;
using static Windows.UI.Xaml.Media.Imaging.BitmapImage;
using Windows.Storage.Helpers;

namespace Windows.UI.Xaml.Media.Imaging
{
partial class SvgImageSource
{
private static readonly string UNO_BOOTSTRAP_APP_BASE = Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_APP_BASE));

internal string ContentType { get; set; } = "image/svg+xml";

partial void InitPartial()
Expand Down Expand Up @@ -52,7 +51,7 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH
}
else
{
var path = Path.Combine(UNO_BOOTSTRAP_APP_BASE, uri.OriginalString);
var path = AssetsPathBuilder.BuildAssetUri(uri.OriginalString);
image = new ImageData
{
Kind = ImageDataKind.Url,
Expand Down
8 changes: 3 additions & 5 deletions src/Uno.UWP/Storage/Helpers/AssetsManager.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ namespace Windows.Storage.Helpers
{
internal partial class AssetsManager
{
private static readonly string UNO_BOOTSTRAP_APP_BASE = Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_APP_BASE)) ?? "";

private static readonly Lazy<Task<HashSet<string>>> _assets = new Lazy<Task<HashSet<string>>>(() => GetAssets(CancellationToken.None));
private static readonly ConcurrentEntryManager _assetsGate = new ConcurrentEntryManager();

private static async Task<HashSet<string>> GetAssets(CancellationToken ct)
{
var assetsUri = !string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE) ? $"{UNO_BOOTSTRAP_APP_BASE}/uno-assets.txt" : "uno-assets.txt";
var assetsUri = AssetsPathBuilder.BuildAssetUri("uno-assets.txt");

var assets = await WebAssemblyRuntime.InvokeAsync($"Windows.Storage.AssetManager.DownloadAssetsManifest(\'{assetsUri}\')");

Expand All @@ -45,13 +43,13 @@ public static async Task<string> DownloadAsset(CancellationToken ct, string asse

if (assetSet.Contains(updatedPath))
{
var localPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, ".assetsCache", UNO_BOOTSTRAP_APP_BASE, updatedPath);
var localPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, ".assetsCache", AssetsPathBuilder.UNO_BOOTSTRAP_APP_BASE, updatedPath);

using var assetLock = await _assetsGate.LockForAsset(ct, updatedPath);

if (!File.Exists(localPath))
{
var assetUri = !string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE) ? $"{UNO_BOOTSTRAP_APP_BASE}/{updatedPath}" : updatedPath;
var assetUri = AssetsPathBuilder.BuildAssetUri(updatedPath);
var assetInfo = await WebAssemblyRuntime.InvokeAsync($"Windows.Storage.AssetManager.DownloadAsset(\'{assetUri}\')");

var parts = assetInfo.Split(';');
Expand Down
25 changes: 25 additions & 0 deletions src/Uno.UWP/Storage/Helpers/AssetsPathBuilder.wasm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;

namespace Windows.Storage.Helpers
{
/// <summary>
/// WebAssembly assets path builder
/// </summary>
internal static class AssetsPathBuilder
{
public static readonly string UNO_BOOTSTRAP_APP_BASE = Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_APP_BASE)) ?? "";
private static readonly string UNO_BOOTSTRAP_WEBAPP_BASE_PATH = Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_WEBAPP_BASE_PATH)) ?? "";

/// <summary>
/// Builds an actual asset path
/// </summary>
public static string? BuildAssetUri(string? contentRelativePath)
=> !string.IsNullOrEmpty(UNO_BOOTSTRAP_APP_BASE)
? $"{UNO_BOOTSTRAP_WEBAPP_BASE_PATH}{UNO_BOOTSTRAP_APP_BASE}/{contentRelativePath}"
: contentRelativePath;
}
}

0 comments on commit 890d5d5

Please sign in to comment.