public static void LoadNative() { if (NativeLoaded) { return; } if (webResources == null) { StandaloneWebResources standaloneWebResources = new StandaloneWebResources(Application.dataPath + "/Resources/browser_assets"); standaloneWebResources.LoadIndex(); webResources = standaloneWebResources; } int debugPort = (Debug.isDebugBuild ? 9849 : 0); FileLocations.CEFDirs dirs = FileLocations.Dirs; string fullName = Directory.GetParent(Application.dataPath).FullName; string environmentVariable = Environment.GetEnvironmentVariable("PATH"); environmentVariable = environmentVariable + ";" + fullName; Environment.SetEnvironmentVariable("PATH", environmentVariable); StandaloneShutdown.Create(); try { zfb_destroyAllBrowsers(); zfb_setDebugFunc(LogCallback); zfb_localRequestFuncs(HeaderCallback, DataCallback); zfb_setCallbacksEnabled(enabled: true); ZFBInitialSettings zFBInitialSettings = default(ZFBInitialSettings); zFBInitialSettings.cefPath = dirs.resourcesPath; zFBInitialSettings.localePath = dirs.localesPath; zFBInitialSettings.subprocessFile = dirs.subprocessFile; zFBInitialSettings.userAgent = UserAgent.GetUserAgent(); zFBInitialSettings.logFile = dirs.logFile; zFBInitialSettings.debugPort = debugPort; zFBInitialSettings.multiThreadedMessageLoop = 1; ZFBInitialSettings settings = zFBInitialSettings; foreach (string commandLineSwitch in commandLineSwitches) { zfb_addCLISwitch(commandLineSwitch); } if (!zfb_init(settings)) { throw new Exception("Failed to initialize browser system."); } NativeLoaded = true; } finally { } AppDomain.CurrentDomain.DomainUnload += delegate { zfb_destroyAllBrowsers(); zfb_setCallbacksEnabled(enabled: false); }; }
public static void LoadNative() { if (NativeLoaded) { return; } if (webResources == null) { //if the user hasn't given us a WebResources to use, use the default #if UNITY_EDITOR webResources = new EditorWebResources(); #else var swr = new StandaloneWebResources(Application.dataPath + "/" + StandaloneWebResources.DefaultPath); swr.LoadIndex(); webResources = swr; #endif } //For Editor/debug builds, we'll open a port you can just http:// to inspect pages. //Don't do this for real builds, though. It makes it really, really easy for the end user to call //random JS in the page, potentially affecting or bypassing game logic. var debugPort = Debug.isDebugBuild ? DebugPort : 0; var dirs = FileLocations.Dirs; #if UNITY_EDITOR_OSX FixProcessPermissions(dirs); #endif // Debug.Log("Browser dirs " + dirs.cefPath + " " + dirs.localesPath); // //We need a certain CWD on some platforms so we can load the DLL (now) and Loading our DLL implies loading a ton of other things, plus init() needs to // //load various other files // //So, change the cwd while setting up (this is more an issue in builds than the editor) // var loadDir = (string)null; // if (!Application.isEditor) { // //We need the CWD to be the folder of the .exe in standalone builds // loadDir = Directory.GetParent(Application.dataPath).FullName; // } #if UNITY_STANDALONE_WIN //make sure the child processes can be started (their dependent .dlls are next to the main .exe, not in the Plugins folder) var loadDir = Directory.GetParent(Application.dataPath).FullName; var path = Environment.GetEnvironmentVariable("PATH"); path += ";" + loadDir; Environment.SetEnvironmentVariable("PATH", path); #endif #if UNITY_EDITOR_LINUX Environment.SetEnvironmentVariable("ZF_CEF_FORCE_EXE_DIR", Directory.GetParent(dirs.subprocessFile).FullName); #endif #if HAND_LOAD_SYMBOLS HandLoadSymbols(dirs.binariesPath); #endif #if !UNITY_EDITOR StandaloneShutdown.Create(); #endif // foreach (DictionaryEntry env in Environment.GetEnvironmentVariables()) { // Debug.Log("Env var " + env.Key + "=" + env.Value); // } // // #if true || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX // //Add library load paths so we can load our bits // var libDir = dirs.binariesPath; // //// var path = Environment.GetEnvironmentVariable("DYLD_FRAMEWORK_PATH"); //// if (!string.IsNullOrEmpty(path)) path += ":" + libDir; //// else path = libDir; //// Environment.SetEnvironmentVariable("DYLD_FRAMEWORK_PATH", path); // // var path = Environment.GetEnvironmentVariable("DYLD_LIBRARY_PATH"); //Debug.Log("Start DYLD_LIBRARY_PATH as " + path); //// if (!string.IsNullOrEmpty(path)) path += ":" + libDir; //// else path = libDir; //// Environment.SetEnvironmentVariable("DYLD_LIBRARY_PATH", path); //// //// Debug.Log("Set DYLD_LIBRARY_PATH to " + path); // // #endif // var oldCWD = Directory.GetCurrentDirectory(); try { // if (loadDir != null) Directory.SetCurrentDirectory(loadDir); // Debug.Log("Current dir " + Directory.GetCurrentDirectory()); //There never should be any, but just in case, destroy any existing browsers on a re-init zfb_destroyAllBrowsers(); //Caution: Careful with these functions you pass to native. The Unity Editor will //reload assemblies, leaving the function pointers dangling. If any native calls try to use them //before we load back up and re-register them... *boom*. //To prevent this, we call zfb_setCallbacksEnabled to disable callbacks before we get unloaded. zfb_setDebugFunc(LogCallback); zfb_localRequestFuncs(HeaderCallback, DataCallback); zfb_setCallbacksEnabled(true); var settings = new ZFBInitialSettings() { cefPath = dirs.resourcesPath, localePath = dirs.localesPath, subprocessFile = dirs.subprocessFile, userAgent = UserAgent.GetUserAgent(), logFile = dirs.logFile, debugPort = debugPort, /* * This is complicated. * Depending on how this is set, you get to deal with a whole slew of bugs. * * Enabled: * DCHECK on exit if extensions enabled * Multithreaded support for various API funcs * * Disabled: * Have to tick the backend, which tends to munch more CPU than we'd like * Backend tick eats input events * Backend tick eats crashes Unity on exit betimes */ #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN //CEF doesn't have the multithreaded loop implemented on 'cept Windows multiThreadedMessageLoop = 1, #else multiThreadedMessageLoop = 0, #endif }; // Debug.Log("Start CEF on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId); var initRes = zfb_init(settings); if (!initRes) { throw new Exception("Failed to initialize browser system."); } NativeLoaded = true; //System.Diagnostics.Process.Start(dirs.subprocessFile); } finally { // Directory.SetCurrentDirectory(oldCWD); } AppDomain.CurrentDomain.DomainUnload += (sender, args) => { zfb_destroyAllBrowsers(); zfb_setCallbacksEnabled(false); }; }
public static void LoadNative() { if (NativeLoaded) { return; } Profiler.BeginSample("BrowserNative.LoadNative"); if (webResources == null) { //if the user hasn't given us a WebResources to use, use the default #if UNITY_EDITOR webResources = new EditorWebResources(); #else var swr = new StandaloneWebResources(Application.dataPath + "/" + StandaloneWebResources.DefaultPath); swr.LoadIndex(); webResources = swr; #endif } //For Editor/debug builds, we'll open a port you can just http:// to inspect pages. //Don't do this for real builds, though. It makes it really, really easy for the end user to call //random JS in the page, potentially affecting or bypassing game logic. var debugPort = Debug.isDebugBuild ? DebugPort : 0; var dirs = FileLocations.Dirs; #if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX FixProcessPermissions(dirs); #endif #if UNITY_STANDALONE_WIN && !UNITY_EDITOR //make sure the child processes can be started (their dependent .dlls are next to the main .exe, not in the Plugins folder) var loadDir = Directory.GetParent(Application.dataPath).FullName; var path = Environment.GetEnvironmentVariable("PATH"); path += ";" + loadDir; Environment.SetEnvironmentVariable("PATH", path); #elif UNITY_EDITOR_WIN //help it find our .dlls var path = Environment.GetEnvironmentVariable("PATH"); path += ";" + dirs.binariesPath; Environment.SetEnvironmentVariable("PATH", path); #endif LoadSymbols(); StandaloneShutdown.Create(); //There never should be any, but just in case, destroy any existing browsers on a re-init zfb_destroyAllBrowsers(); //Caution: Careful with these functions you pass to native. The Unity Editor will //reload assemblies, leaving the function pointers dangling. If any native calls try to use them //before we load back up and re-register them we can crash. //To prevent this, we call zfb_setCallbacksEnabled to disable callbacks before we get unloaded. zfb_setDebugFunc(LogCallback); zfb_setLocalRequestHandler(NewRequestCallback); zfb_setCallbacksEnabled(true); var settings = new ZFBInitialSettings() { cefPath = dirs.resourcesPath, localePath = dirs.localesPath, subprocessFile = dirs.subprocessFile, userAgent = UserAgent.GetUserAgent(), logFile = dirs.logFile, profilePath = _profilePath, debugPort = debugPort, multiThreadedMessageLoop = 0, //this argument is pretty much defunct, the slave just blocks on CefRunMessageLoop on all platforms }; foreach (var arg in commandLineSwitches) { zfb_addCLISwitch(arg); } var initRes = zfb_init(settings); if (!initRes) { throw new Exception("Failed to initialize browser system."); } NativeLoaded = true; Profiler.EndSample(); AppDomain.CurrentDomain.DomainUnload += (sender, args) => { isAppDomainUnloading = true; //Shutdown should happen StandaloneShutdown, but in some cases, like the Unity Editor //reloading assemblies, we don't get OnApplicationQuit because we didn't "quit", even though //everything gets shut down. //Make sure the backend shuts down, in this case, or it will crash when we try to start it again. UnloadNative(); }; }