CheckSettings.cs 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using HybridCLR.Editor.Settings;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using UnityEditor;
  10. using UnityEditor.Build;
  11. using UnityEditor.Build.Reporting;
  12. using UnityEngine;
  13. using static UnityEngine.GraphicsBuffer;
  14. namespace HybridCLR.Editor.BuildProcessors
  15. {
  16. internal class CheckSettings : IPreprocessBuildWithReport
  17. {
  18. public int callbackOrder => 0;
  19. public static bool DisableMethodBridgeDevelopmentFlagChecking { get; set; }
  20. public void OnPreprocessBuild(BuildReport report)
  21. {
  22. HybridCLRSettings globalSettings = SettingsUtil.HybridCLRSettings;
  23. if (!globalSettings.enable || globalSettings.useGlobalIl2cpp)
  24. {
  25. string oldIl2cppPath = Environment.GetEnvironmentVariable("UNITY_IL2CPP_PATH");
  26. if (!string.IsNullOrEmpty(oldIl2cppPath))
  27. {
  28. Environment.SetEnvironmentVariable("UNITY_IL2CPP_PATH", "");
  29. Debug.Log($"[CheckSettings] clean process environment variable: UNITY_IL2CPP_PATH, old vlaue:'{oldIl2cppPath}'");
  30. }
  31. }
  32. else
  33. {
  34. string curIl2cppPath = Environment.GetEnvironmentVariable("UNITY_IL2CPP_PATH");
  35. if (curIl2cppPath != SettingsUtil.LocalIl2CppDir)
  36. {
  37. Environment.SetEnvironmentVariable("UNITY_IL2CPP_PATH", SettingsUtil.LocalIl2CppDir);
  38. Debug.Log($"[CheckSettings] UNITY_IL2CPP_PATH old value:'{curIl2cppPath}', new value:'{SettingsUtil.LocalIl2CppDir}'");
  39. }
  40. }
  41. if (!globalSettings.enable)
  42. {
  43. return;
  44. }
  45. BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
  46. ScriptingImplementation curScriptingImplementation = PlayerSettings.GetScriptingBackend(buildTargetGroup);
  47. ScriptingImplementation targetScriptingImplementation = ScriptingImplementation.IL2CPP;
  48. if (curScriptingImplementation != targetScriptingImplementation)
  49. {
  50. Debug.LogError($"[CheckSettings] current ScriptingBackend:{curScriptingImplementation},have been switched to:{targetScriptingImplementation} automatically");
  51. PlayerSettings.SetScriptingBackend(buildTargetGroup, targetScriptingImplementation);
  52. }
  53. var installer = new Installer.InstallerController();
  54. if (!installer.HasInstalledHybridCLR())
  55. {
  56. throw new BuildFailedException($"You have not initialized HybridCLR, please install it via menu 'HybridCLR/Installer'");
  57. }
  58. if (installer.PackageVersion != installer.InstalledLibil2cppVersion)
  59. {
  60. throw new BuildFailedException($"You must run `HybridCLR/Installer` after upgrading package");
  61. }
  62. HybridCLRSettings gs = SettingsUtil.HybridCLRSettings;
  63. if (((gs.hotUpdateAssemblies?.Length + gs.hotUpdateAssemblyDefinitions?.Length) ?? 0) == 0)
  64. {
  65. Debug.LogWarning("[CheckSettings] No hot update modules configured in HybridCLRSettings");
  66. }
  67. if (!DisableMethodBridgeDevelopmentFlagChecking)
  68. {
  69. string methodBridgeFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp";
  70. var match = Regex.Match(File.ReadAllText(methodBridgeFile), @"// DEVELOPMENT=(\d)");
  71. if (match.Success)
  72. {
  73. int developmentFlagInMethodBridge = int.Parse(match.Groups[1].Value);
  74. int developmentFlagInEditorSettings = EditorUserBuildSettings.development ? 1 : 0;
  75. if (developmentFlagInMethodBridge != developmentFlagInEditorSettings)
  76. {
  77. Debug.LogError($"[CheckSettings] MethodBridge.cpp DEVELOPMENT flag:{developmentFlagInMethodBridge} is inconsistent with EditorUserBuildSettings.development:{developmentFlagInEditorSettings}. Please run 'HybridCLR/Generate/All' before building.");
  78. }
  79. }
  80. else
  81. {
  82. Debug.LogError("[CheckSettings] MethodBridge.cpp DEVELOPMENT flag not found. Please run 'HybridCLR/Generate/All' before building.");
  83. }
  84. }
  85. }
  86. }
  87. }