예제 #1
0
        private static async Task <HashSet <string> > GetAssets(CancellationToken ct)
        {
            var assetsUri = AssetsPathBuilder.BuildAssetUri("uno-assets.txt");

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

            return(new HashSet <string>(Regex.Split(assets, "\r\n|\r|\n"), StringComparer.OrdinalIgnoreCase));
        }
예제 #2
0
        public static async Task <string> DownloadAsset(CancellationToken ct, string assetPath)
        {
            var updatedPath = assetPath.TrimStart("/");
            var assetSet    = await _assets.Value;

            if (assetSet.Contains(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  = AssetsPathBuilder.BuildAssetUri(updatedPath);
                    var assetInfo = await WebAssemblyRuntime.InvokeAsync($"Windows.Storage.AssetManager.DownloadAsset(\'{assetUri}\')");

                    var parts = assetInfo.Split(';');
                    if (parts.Length == 2)
                    {
                        var ptr    = (IntPtr)int.Parse(parts[0], CultureInfo.InvariantCulture);
                        var length = int.Parse(parts[1], CultureInfo.InvariantCulture);

                        try
                        {
                            var buffer = new byte[length];
                            Marshal.Copy(ptr, buffer, 0, length);

                            if (Path.GetDirectoryName(localPath) is { } path)
                            {
                                Directory.CreateDirectory(path);
                            }

                            File.WriteAllBytes(localPath, buffer);
                        }
                        finally
                        {
                            Marshal.FreeHGlobal(ptr);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException($"Invalid Windows.Storage.AssetManager.DownloadAsset return value");
                    }
                }

                return(localPath);
            }
            else
            {
                throw new FileNotFoundException($"The file [{assetPath}] cannot be found");
            }
        }