WindowsBuildScript.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2022 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_STANDALONE_WIN
  15. #pragma warning disable CS0618
  16. using System;
  17. using System.IO;
  18. using System.Linq;
  19. using UnityEditor;
  20. using UnityEditor.Build;
  21. using UnityEditor.Callbacks;
  22. using UnityEngine;
  23. using UnityEngine.Rendering;
  24. using Vuplex.WebView.Internal;
  25. namespace Vuplex.WebView.Editor {
  26. /// <summary>
  27. /// Windows build script that copies the Chromium plugin executable's files to the
  28. /// required location in the built application folder.
  29. /// </summary>
  30. public class WindowsBuildScript : IPreprocessBuild {
  31. public int callbackOrder { get { return 0; } }
  32. public void OnPreprocessBuild(BuildTarget buildTarget, string buildPath) {
  33. if (buildTarget != BuildTarget.StandaloneWindows) {
  34. return;
  35. }
  36. #if !VUPLEX_DISABLE_GRAPHICS_API_WARNING
  37. var selectedGraphicsApi = PlayerSettings.GetGraphicsAPIs(buildTarget)[0];
  38. var error = VXUtils.GetGraphicsApiErrorMessage(selectedGraphicsApi, new GraphicsDeviceType[] { GraphicsDeviceType.Direct3D11 });
  39. if (error != null) {
  40. throw new BuildFailedException(error);
  41. }
  42. #endif
  43. }
  44. [PostProcessBuild(700)]
  45. public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject) {
  46. if (!(target == BuildTarget.StandaloneWindows || target == BuildTarget.StandaloneWindows64)) {
  47. return;
  48. }
  49. var buildPluginDirectoryPath = _getBuiltPluginDirectoryPath(target, pathToBuiltProject);
  50. var sourceChromiumDirectory = EditorUtils.FindDirectory(Path.Combine(new string[] { Application.dataPath, "Vuplex", "WebView", "Standalone", "Windows", "Plugins", CHROMIUM_DIRECTORY_NAME }));
  51. var destinationChromiumDirectory = Path.Combine(buildPluginDirectoryPath, CHROMIUM_DIRECTORY_NAME);
  52. EditorUtils.CopyAndReplaceDirectory(sourceChromiumDirectory, destinationChromiumDirectory);
  53. // Don't include the developer's Chromium log in the built app.
  54. var chromiumLogFilePath = Path.Combine(destinationChromiumDirectory, "log-chromium.txt~");
  55. if (File.Exists(chromiumLogFilePath)) {
  56. File.Delete(chromiumLogFilePath);
  57. }
  58. }
  59. const string DLL_FILE_NAME = "VuplexWebViewWindows.dll";
  60. const string CHROMIUM_DIRECTORY_NAME = "VuplexWebViewChromium";
  61. static string _getBuiltPluginDirectoryPath(BuildTarget target, string pathToBuiltProject) {
  62. var productName = Path.GetFileNameWithoutExtension(pathToBuiltProject);
  63. var buildDirectoryPath = _getParentDirectoryOfFile(pathToBuiltProject, '/');
  64. var architecture = target == BuildTarget.StandaloneWindows64 ? "x86_64" : "x86";
  65. var expectedPluginPath = Path.Combine(buildDirectoryPath, productName + "_Data", "Plugins", architecture, DLL_FILE_NAME);
  66. var actualPluginPath = EditorUtils.FindFile(expectedPluginPath, buildDirectoryPath);
  67. return _getParentDirectoryOfFile(actualPluginPath, Path.DirectorySeparatorChar);
  68. }
  69. static string _getParentDirectoryOfFile(string filePath, char pathSeparator) {
  70. var pathComponents = filePath.Split(new char[] { pathSeparator }).ToList();
  71. return String.Join(Path.DirectorySeparatorChar.ToString(), pathComponents.GetRange(0, pathComponents.Count - 1).ToArray());
  72. }
  73. }
  74. }
  75. #endif