|
| 1 | +// Copyright © 2021 The CefSharp Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +namespace CefSharp |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// CefRuntime - Used to simplify loading of the CefSharp architecture specific resources. |
| 13 | + /// Typical use case would be when you are targeting AnyCPU |
| 14 | + /// </summary> |
| 15 | + public static class CefRuntime |
| 16 | + { |
| 17 | + private static ResolveEventHandler currentDomainAssemblyResolveHandler; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll |
| 21 | + /// needs to be loaded (x64/x86). |
| 22 | + /// This method subscribes to the <see cref="AppDomain.AssemblyResolve"/> event |
| 23 | + /// for <see cref="AppDomain.CurrentDomain"/> and loads the CefSharp.Core.Runtime.dll |
| 24 | + /// based on <see cref="Environment.Is64BitProcess"/>. |
| 25 | + /// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically |
| 26 | + /// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and |
| 27 | + /// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use. |
| 28 | + /// </summary> |
| 29 | + /// <param name="basePath"> |
| 30 | + /// The path containing the x64/x86 folders which contain the CefSharp/CEF resources. |
| 31 | + /// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path. |
| 32 | + /// (</param> |
| 33 | + public static void SubscribeAnyCpuAssemblyResolver(string basePath = null) |
| 34 | + { |
| 35 | + if(currentDomainAssemblyResolveHandler != null) |
| 36 | + { |
| 37 | + throw new Exception("UseAnyCpuAssemblyResolver has already been called, call "); |
| 38 | + } |
| 39 | + |
| 40 | + if(basePath == null) |
| 41 | + { |
| 42 | + basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; |
| 43 | + } |
| 44 | + |
| 45 | + currentDomainAssemblyResolveHandler = (sender, args) => |
| 46 | + { |
| 47 | + if (args.Name.StartsWith("CefSharp.Core.Runtime")) |
| 48 | + { |
| 49 | + string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll"; |
| 50 | + string archSpecificPath = Path.Combine(basePath, |
| 51 | + Environment.Is64BitProcess ? "x64" : "x86", |
| 52 | + assemblyName); |
| 53 | + |
| 54 | + return File.Exists(archSpecificPath) |
| 55 | + ? System.Reflection.Assembly.LoadFile(archSpecificPath) |
| 56 | + : null; |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + }; |
| 61 | + |
| 62 | + AppDomain.CurrentDomain.AssemblyResolve += currentDomainAssemblyResolveHandler; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Unsubscribe from the <see cref="AppDomain.AssemblyResolve"/> event |
| 67 | + /// for <see cref="AppDomain.CurrentDomain"/> that was added in <see cref="UseAnyCpuAssemblyResolver"/> |
| 68 | + /// </summary> |
| 69 | + public static void UnsubscribeAnyCpuAssemblyResolver() |
| 70 | + { |
| 71 | + AppDomain.CurrentDomain.AssemblyResolve -= currentDomainAssemblyResolveHandler; |
| 72 | + |
| 73 | + currentDomainAssemblyResolveHandler = null; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll |
| 78 | + /// needs to be loaded (x64/x86). |
| 79 | + /// This method calls <see cref="Assembly.LoadFile(string)"/> to immediately load CefSharp.Core.Runtime.dll |
| 80 | + /// based on <see cref="Environment.Is64BitProcess"/>. |
| 81 | + /// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically |
| 82 | + /// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and |
| 83 | + /// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use. |
| 84 | + /// </summary> |
| 85 | + /// <param name="basePath"> |
| 86 | + /// The path containing the x64/x86 folders which contain the CefSharp/CEF resources. |
| 87 | + /// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path. |
| 88 | + /// (</param> |
| 89 | + public static void LoadCefSharpCoreRuntimeAnyCpu(string basePath = null) |
| 90 | + { |
| 91 | + const string assemblyName = "CefSharp.Core.Runtime.dll"; |
| 92 | + |
| 93 | + if (basePath == null) |
| 94 | + { |
| 95 | + basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; |
| 96 | + } |
| 97 | + |
| 98 | + var env = Environment.Is64BitProcess ? "x64" : "x86"; |
| 99 | + string archSpecificPath = Path.Combine(basePath, |
| 100 | + env, |
| 101 | + assemblyName); |
| 102 | + |
| 103 | + if (File.Exists(archSpecificPath)) |
| 104 | + { |
| 105 | + Assembly.LoadFile(archSpecificPath); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + throw new FileNotFoundException("Unable to load for arch " + env, archSpecificPath); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments