Example #1
0
        internal Assembly ResolveResourceAssembly(Uri resourceUrl, bool failOnMissingAssembly)
        {
            if (assemblies == null)
            {
                lock (SyncRoot) {
                    if (assemblies == null)
                    {
                        assemblies = new Dictionary <string, Assembly>();
                        AppDomain.CurrentDomain.AssemblyLoad += delegate { newAssembliesLoaded = true; };
                    }
                }
            }

            var assemblyName = ResourceUrl.GetEmbeddedResourceAssemblyName(resourceUrl);
            var assembly     = GetAssemblyByName(assemblyName);

            if (assembly == null)
            {
                if (newAssembliesLoaded)
                {
                    lock (SyncRoot) {
                        if (newAssembliesLoaded)
                        {
                            // add loaded assemblies to cache
                            newAssembliesLoaded = false;
                            foreach (var domainAssembly in AppDomain.CurrentDomain.GetAssemblies())
                            {
                                // replace if duplicated (can happen)
                                assemblies[domainAssembly.GetName().Name] = domainAssembly;
                            }
                        }
                    }
                }

                assembly = GetAssemblyByName(assemblyName);
                if (assembly == null)
                {
                    try {
                        // try load assembly from its name
                        var assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyName + ".dll");
                        assembly = AssemblyLoader.LoadAssembly(assemblyPath);
                    } catch (IOException) {
                        // ignore
                    }

                    if (assembly != null)
                    {
                        lock (SyncRoot) {
                            assemblies[assembly.GetName().Name] = assembly;
                        }
                    }
                }
            }

            if (failOnMissingAssembly && assembly == null)
            {
                throw new InvalidOperationException("Could not find assembly for: " + resourceUrl);
            }
            return(assembly);
        }
Example #2
0
        /// <param name="useSharedDomain">Shared domains means that the webview default domain will always be the same. When <see cref="useSharedDomain"/> is false a
        /// unique domain is used for every webview.</param>
        internal WebView(ResourceUrl initialAddress, bool useSharedDomain)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

#if DEBUG
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                throw new InvalidOperationException("Running debug version");
            }
#endif

            if (useSharedDomain)
            {
                CurrentDomainId = string.Empty;
            }
            else
            {
                CurrentDomainId = domainId.ToString();
                domainId++;
            }

            DefaultLocalUrl = new ResourceUrl(ResourceUrl.LocalScheme, "index.html").WithDomain(CurrentDomainId);

            Initialize(initialAddress?.WithDomain(CurrentDomainId));
        }
Example #3
0
        protected void LoadFrom(string source)
        {
            var userAssembly = GetUserCallingMethod().ReflectedType.Assembly;

            IsSecurityDisabled = true;
            Address            = new ResourceUrl(userAssembly, source).ToString();
        }
Example #4
0
        public void EnableHotReload(string baseLocation)
        {
            if (string.IsNullOrEmpty(baseLocation))
            {
                throw new InvalidOperationException("Hot reload does not work in release mode");
            }

            baseLocation = Path.GetDirectoryName(baseLocation);
            baseLocation = Path.GetFullPath(baseLocation + "\\..\\.."); // get up 2 levels (.../View/src -> .../)

            if (fileSystemWatcher != null)
            {
                fileSystemWatcher.Path = baseLocation;
                return;
            }

            fileSystemWatcher = new FileSystemWatcher(baseLocation);
            fileSystemWatcher.IncludeSubdirectories = true;
            fileSystemWatcher.NotifyFilter          = NotifyFilters.LastWrite;
            fileSystemWatcher.EnableRaisingEvents   = true;

            var filesChanged          = false;
            var fileExtensionsToWatch = new[] { ".js", ".css" };

            fileSystemWatcher.Changed += (sender, eventArgs) => {
                if (IsReady)
                {
                    // TODO visual studio reports a change in a file with a (strange) temporary name
                    //if (fileExtensionsToWatch.Any(e => eventArgs.Name.EndsWith(e))) {
                    filesChanged = true;
                    webView.Dispatcher.BeginInvoke((Action)(() => {
                        if (IsReady && !IsDisposing)
                        {
                            IsReady = false;
                            cacheInvalidationTimestamp = DateTime.UtcNow.Ticks.ToString();
                            webView.Reload(true);
                        }
                    }));
                    //}
                }
            };
            webView.BeforeResourceLoad += (WebView.ResourceHandler resourceHandler) => {
                if (filesChanged)
                {
                    var url  = new Uri(resourceHandler.Url);
                    var path = Path.Combine(ResourceUrl.GetEmbeddedResourcePath(url).Skip(1).ToArray()); // skip first part (namespace)
                    if (fileExtensionsToWatch.Any(e => path.EndsWith(e)))
                    {
                        path = Path.Combine(fileSystemWatcher.Path, path);
                        var file = new FileInfo(path);
                        if (file.Exists)
                        {
                            resourceHandler.RespondWith(path);
                        }
                    }
                }
            };
        }
Example #5
0
        public void EnableHotReload(string baseLocation)
        {
            baseLocation = Path.GetDirectoryName(baseLocation);
            baseLocation = Path.GetFullPath(baseLocation + "\\..\\.."); // get up 2 levels (.../View/src -> .../)

            if (fileSystemWatcher != null)
            {
                fileSystemWatcher.Path = baseLocation;
                return;
            }

            fileSystemWatcher = new FileSystemWatcher(baseLocation);
            fileSystemWatcher.IncludeSubdirectories = true;
            fileSystemWatcher.NotifyFilter          = NotifyFilters.LastWrite;
            fileSystemWatcher.EnableRaisingEvents   = true;

            var filesChanged          = false;
            var fileExtensionsToWatch = new[] { ".js", ".css" };

            fileSystemWatcher.Changed += (sender, eventArgs) => {
                if (IsReady)
                {
                    if (fileExtensionsToWatch.Any(e => eventArgs.Name.EndsWith(e)))
                    {
                        filesChanged = true;
                        webView.Dispatcher.BeginInvoke((Action)(() => {
                            if (IsReady)
                            {
                                IsReady = false;
                                webView.Reload(true);
                            }
                        }));
                    }
                }
            };
            webView.BeforeResourceLoad += (WebView.ResourceHandler resourceHandler) => {
                if (filesChanged)
                {
                    var url  = new Uri(resourceHandler.Url);
                    var path = Path.Combine(ResourceUrl.GetEmbeddedResourcePath(url).Skip(1).ToArray()); // skip first part (namespace)
                    if (fileExtensionsToWatch.Any(e => path.EndsWith(e)))
                    {
                        path = Path.Combine(fileSystemWatcher.Path, path);
                        var file = new FileInfo(path);
                        if (file.Exists)
                        {
                            resourceHandler.RespondWith(path);
                        }
                    }
                }
            };
        }
        protected virtual void LoadEmbeddedResource(ResourceHandler resourceHandler, Uri url)
        {
            var resourceAssembly = ResolveResourceAssembly(url);
            var resourcePath     = ResourceUrl.GetEmbeddedResourcePath(url);

            var extension = Path.GetExtension(resourcePath.Last()).ToLower();

            var resourceStream = TryGetResourceWithFullPath(resourceAssembly, resourcePath);

            if (resourceStream != null)
            {
                resourceHandler.RespondWith(resourceStream, extension);
            }
        }
Example #7
0
        internal static Stream TryGetResource(Uri url, bool failOnMissingAssembly, out string extension)
        {
            var resourceAssembly = cache.ResolveResourceAssembly(url, failOnMissingAssembly);

            if (resourceAssembly == null)
            {
                extension = string.Empty;
                return(null);
            }
            var resourcePath = ResourceUrl.GetEmbeddedResourcePath(url);

            extension = Path.GetExtension(resourcePath.Last()).ToLower();
            var resourceStream = TryGetResourceWithFullPath(resourceAssembly, resourcePath);

            return(resourceStream);
        }
        protected Assembly ResolveResourceAssembly(Uri resourceUrl)
        {
            if (assemblies == null)
            {
                assemblies = new Dictionary <string, Assembly>();
                AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded;
            }

            var assemblyName = ResourceUrl.GetEmbeddedResourceAssemblyName(resourceUrl);
            var assembly     = GetAssemblyByName(assemblyName);

            if (assembly == null)
            {
                if (newAssembliesLoaded)
                {
                    // add loaded assemblies to cache
                    newAssembliesLoaded = false;
                    foreach (var domainAssembly in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        // replace if duplicated (can happen)
                        assemblies[domainAssembly.GetName().Name] = domainAssembly;
                    }
                }

                assembly = GetAssemblyByName(assemblyName);
                if (assembly == null)
                {
                    // try load assembly from its name
                    assembly = AppDomain.CurrentDomain.Load(new AssemblyName(assemblyName));
                    if (assembly != null)
                    {
                        assemblies[assembly.GetName().Name] = assembly;
                    }
                }
            }

            if (assembly != null)
            {
                return(assembly);
            }

            throw new InvalidOperationException("Could not find assembly for: " + resourceUrl);
        }
Example #9
0
 public void LoadResource(ResourceUrl resourceUrl, string frameName = MainFrameName)
 {
     LoadUrl(resourceUrl.WithDomain(CurrentDomainId), frameName);
 }
Example #10
0
 public void LoadResource(ResourceUrl resourceUrl)
 {
     Address = resourceUrl.WithDomain(CurrentDomainId);
 }