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 + "\\..\\.."); if (this.fileSystemWatcher != null) { this.fileSystemWatcher.Path = baseLocation; return; } this.fileSystemWatcher = new FileSystemWatcher(baseLocation); this.fileSystemWatcher.IncludeSubdirectories = true; this.fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite; this.fileSystemWatcher.EnableRaisingEvents = true; bool filesChanged = false; string[] fileExtensionsToWatch = new string[] { ".js", ".css" }; this.fileSystemWatcher.Changed += delegate(object sender, FileSystemEventArgs eventArgs) { if (this.IsReady) { filesChanged = true; this.webView.BeginInvoke(new Action(() => { if (this.IsReady) { this.IsReady = false; this.cacheInvalidationTimestamp = DateTime.UtcNow.Ticks.ToString(); this.webView.Reload(true); } })); } }; this.webView.BeforeResourceLoad += delegate(CefSharpMe.ResourceHandler resourceHandler) { if (filesChanged) { Uri resourceUrl = new Uri(resourceHandler.Url); string path = Path.Combine(ResourceUrl.GetEmbeddedResourcePath(resourceUrl).Skip(1).ToArray <string>()); if (fileExtensionsToWatch.Any((string e) => path.EndsWith(e))) { path = Path.Combine(this.fileSystemWatcher.Path, path); if (new FileInfo(path).Exists) { resourceHandler.RespondWith(path); } } } }; }
/// <summary> /// 加载嵌入的资源 /// </summary> /// <param name="resourceHandler"></param> /// <param name="url"></param> internal virtual void LoadEmbeddedResource(CefSharpMe.ResourceHandler resourceHandler, Uri url) { Assembly assembly = this.ResolveResourceAssembly(url); string[] embeddedResourcePath = ResourceUrl.GetEmbeddedResourcePath(url); string extension = Path.GetExtension(embeddedResourcePath.Last <string>()).ToLower(); Stream stream = this.TryGetResourceWithFullPath(assembly, embeddedResourcePath); if (stream != null) { resourceHandler.RespondWith(stream, extension); } }
internal static string[] GetEmbeddedResourcePath(Uri resourceUrl) { if (ResourceUrl.ContainsAssemblyLocation(resourceUrl)) { int num = resourceUrl.AbsolutePath.IndexOf(";"); return(resourceUrl.AbsolutePath.Substring(num + 1).Split(new string[] { "/" }, StringSplitOptions.None)); } return((from p in resourceUrl.Segments.Skip(1) select p.Replace("/", "")).ToArray <string>()); }
public ResourceUrl(Assembly assembly, params string[] path) : this(path) { string name = assembly.GetName().Name; if (this.url.StartsWith("/")) { this.url = "assembly:" + name + ";" + this.url.Substring(1); } else { this.url = name + "/" + this.url; } this.url = ResourceUrl.BuildUrl("embedded", this.url); }
internal static string GetEmbeddedResourceAssemblyName(Uri resourceUrl) { if (ResourceUrl.ContainsAssemblyLocation(resourceUrl)) { string text = resourceUrl.AbsolutePath.Substring("/assembly:".Length); int length = Math.Max(0, text.IndexOf(";")); return(text.Substring(0, length)); } if (resourceUrl.Segments.Length <= 1) { return(string.Empty); } string text2 = resourceUrl.Segments[1]; if (!text2.EndsWith("/")) { return(text2); } return(text2.Substring(0, text2.Length - "/".Length)); }
/// <summary> /// 定位资源的程序集 /// </summary> /// <param name="resourceUrl"></param> /// <returns></returns> protected Assembly ResolveResourceAssembly(Uri resourceUrl) { if (this.assemblies == null) { this.assemblies = new Dictionary <string, Assembly>(); AppDomain.CurrentDomain.AssemblyLoad += this.OnAssemblyLoaded; } string embeddedResourceAssemblyName = ResourceUrl.GetEmbeddedResourceAssemblyName(resourceUrl); Assembly assembly = this.GetAssemblyByName(embeddedResourceAssemblyName); if (assembly == null) { if (this.newAssembliesLoaded) { this.newAssembliesLoaded = false; foreach (Assembly assembly2 in AppDomain.CurrentDomain.GetAssemblies()) { this.assemblies[assembly2.GetName().Name] = assembly2; } } assembly = this.GetAssemblyByName(embeddedResourceAssemblyName); if (assembly == null) { assembly = AppDomain.CurrentDomain.Load(new AssemblyName(embeddedResourceAssemblyName)); if (assembly != null) { this.assemblies[assembly.GetName().Name] = assembly; } } } if (assembly != null) { return(assembly); } throw new InvalidOperationException("Could not find assembly for: " + resourceUrl); }
public void LoadResource(ResourceUrl resourceUrl) { this.Address = resourceUrl.WithDomain(this.CurrentDomainId); }
public SimpleViewModule(string moduleName, ResourceUrl url) { this.moduleName = moduleName; this.source = url.ToString(); }
private static string BuildUrl(string scheme, string path) { return(scheme + Uri.SchemeDelimiter + ResourceUrl.CombinePath("webview{0}", path)); }
internal ResourceUrl(string scheme, string path) { this.url = ResourceUrl.BuildUrl(scheme, path); }