From 3de931a3afc1d0bfc610b4a557cfc417457bb394 Mon Sep 17 00:00:00 2001 From: walon Date: Fri, 24 May 2024 18:32:44 +0800 Subject: [PATCH] =?UTF-8?q?[opt]=20=E6=89=93=E5=8C=85=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E7=94=9F=E6=88=90=E6=A1=A5=E6=8E=A5=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=97=B6=E7=9A=84development=E9=80=89=E9=A1=B9=E4=B8=8E?= =?UTF-8?q?=E5=BD=93=E5=89=8Ddevelopment=E9=80=89=E9=A1=B9=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E3=80=82`Generate/All`=E4=B9=8B=E5=90=8E=E5=88=87?= =?UTF-8?q?=E6=8D=A2development=E9=80=89=E9=A1=B9=E5=86=8D=E6=89=93?= =?UTF-8?q?=E5=8C=85=EF=BC=8C=E5=B0=86=E4=BC=9A=E4=BA=A7=E7=94=9F=E4=B8=A5?= =?UTF-8?q?=E9=87=8D=E7=9A=84=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/BuildProcessors/CheckSettings.cs | 22 +++ .../Commands/MethodBridgeGeneratorCommand.cs | 1 + Editor/Commands/StripAOTDllCommand.cs | 147 ++++++++++-------- Editor/MethodBridge/Generator.cs | 9 ++ 4 files changed, 111 insertions(+), 68 deletions(-) diff --git a/Editor/BuildProcessors/CheckSettings.cs b/Editor/BuildProcessors/CheckSettings.cs index a5840bc..994683b 100644 --- a/Editor/BuildProcessors/CheckSettings.cs +++ b/Editor/BuildProcessors/CheckSettings.cs @@ -1,8 +1,10 @@ using HybridCLR.Editor.Settings; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using UnityEditor; using UnityEditor.Build; @@ -16,6 +18,8 @@ namespace HybridCLR.Editor.BuildProcessors { public int callbackOrder => 0; + public static bool DisableMethodBridgeDevelopmentFlagChecking { get; set; } + public void OnPreprocessBuild(BuildReport report) { HybridCLRSettings globalSettings = SettingsUtil.HybridCLRSettings; @@ -67,6 +71,24 @@ namespace HybridCLR.Editor.BuildProcessors Debug.LogWarning("[CheckSettings] No hot update modules configured in HybridCLRSettings"); } + if (!DisableMethodBridgeDevelopmentFlagChecking) + { + string methodBridgeFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp"; + var match = Regex.Match(File.ReadAllText(methodBridgeFile), @"// DEVELOPMENT=(\d)"); + if (match.Success) + { + int developmentFlagInMethodBridge = int.Parse(match.Groups[1].Value); + int developmentFlagInEditorSettings = EditorUserBuildSettings.development ? 1 : 0; + if (developmentFlagInMethodBridge != developmentFlagInEditorSettings) + { + Debug.LogError($"[CheckSettings] MethodBridge.cpp DEVELOPMENT flag:{developmentFlagInMethodBridge} is inconsistent with EditorUserBuildSettings.development:{developmentFlagInEditorSettings}. Please run 'HybridCLR/Generate/All' before building."); + } + } + else + { + Debug.LogError("[CheckSettings] MethodBridge.cpp DEVELOPMENT flag not found. Please run 'HybridCLR/Generate/All' before building."); + } + } } } } diff --git a/Editor/Commands/MethodBridgeGeneratorCommand.cs b/Editor/Commands/MethodBridgeGeneratorCommand.cs index fde1084..ab8f365 100644 --- a/Editor/Commands/MethodBridgeGeneratorCommand.cs +++ b/Editor/Commands/MethodBridgeGeneratorCommand.cs @@ -38,6 +38,7 @@ namespace HybridCLR.Editor.Commands TemplateCode = templateCode, OutputFile = outputFile, GenericMethods = analyzer.GenericMethods, + Development = EditorUserBuildSettings.development, }); g.PrepareMethods(); diff --git a/Editor/Commands/StripAOTDllCommand.cs b/Editor/Commands/StripAOTDllCommand.cs index 31ab3cd..46672eb 100644 --- a/Editor/Commands/StripAOTDllCommand.cs +++ b/Editor/Commands/StripAOTDllCommand.cs @@ -1,4 +1,5 @@ -using HybridCLR.Editor.Installer; +using HybridCLR.Editor.BuildProcessors; +using HybridCLR.Editor.Installer; using System; using System.Collections.Generic; using System.IO; @@ -85,99 +86,109 @@ namespace HybridCLR.Editor.Commands bool oldOpenHarmonyProj = EditorUserBuildSettings.exportAsOpenHarmonyProject; #endif bool oldBuildScriptsOnly = EditorUserBuildSettings.buildScriptsOnly; - EditorUserBuildSettings.buildScriptsOnly = true; - string location = GetLocationPathName(outputPath, target); string oldBuildLocation = EditorUserBuildSettings.GetBuildLocation(target); - EditorUserBuildSettings.SetBuildLocation(target, location); - - switch (target) + try { - case BuildTarget.StandaloneWindows: - case BuildTarget.StandaloneWindows64: + CheckSettings.DisableMethodBridgeDevelopmentFlagChecking = true; + EditorUserBuildSettings.buildScriptsOnly = true; + + string location = GetLocationPathName(outputPath, target); + EditorUserBuildSettings.SetBuildLocation(target, location); + + switch (target) { -#if UNITY_EDITOR_WIN - UnityEditor.WindowsStandalone.UserBuildSettings.createSolution = true; -#endif + case BuildTarget.StandaloneWindows: + case BuildTarget.StandaloneWindows64: + { + #if UNITY_EDITOR_WIN + UnityEditor.WindowsStandalone.UserBuildSettings.createSolution = true; + #endif + break; + } + case BuildTarget.StandaloneOSX: + { + #if UNITY_EDITOR_OSX + UnityEditor.OSXStandalone.UserBuildSettings.createXcodeProject = true; + #endif break; + } + #if TUANJIE_2022_3_OR_NEWER + case BuildTarget.HMIAndroid: + #endif + case BuildTarget.Android: + { + EditorUserBuildSettings.exportAsGoogleAndroidProject = true; + break; + } + #if TUANJIE_2022_3_OR_NEWER + case BuildTarget.OpenHarmony: + { + EditorUserBuildSettings.exportAsOpenHarmonyProject = true; + break; + } + #endif } - case BuildTarget.StandaloneOSX: + + Debug.Log($"GenerateStripedAOTDlls build option:{buildOptions}"); + + BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions() { -#if UNITY_EDITOR_OSX - UnityEditor.OSXStandalone.UserBuildSettings.createXcodeProject = true; -#endif - break; - } -#if TUANJIE_2022_3_OR_NEWER - case BuildTarget.HMIAndroid: -#endif - case BuildTarget.Android: + scenes = EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray(), + locationPathName = location, + options = buildOptions, + target = target, + targetGroup = BuildPipeline.GetBuildTargetGroup(target), + }; + + var report = BuildPipeline.BuildPlayer(buildPlayerOptions); + + + + if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded) { - EditorUserBuildSettings.exportAsGoogleAndroidProject = true; - break; + throw new Exception("GenerateStripedAOTDlls failed"); } -#if TUANJIE_2022_3_OR_NEWER - case BuildTarget.OpenHarmony: - { - EditorUserBuildSettings.exportAsOpenHarmonyProject = true; - break; - } -#endif } - - Debug.Log($"GenerateStripedAOTDlls build option:{buildOptions}"); - - BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions() + finally { - scenes = EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray(), - locationPathName = location, - options = buildOptions, - target = target, - targetGroup = BuildPipeline.GetBuildTargetGroup(target), - }; + CheckSettings.DisableMethodBridgeDevelopmentFlagChecking = false; + EditorUserBuildSettings.buildScriptsOnly = oldBuildScriptsOnly; + EditorUserBuildSettings.SetBuildLocation(target, oldBuildLocation); - var report = BuildPipeline.BuildPlayer(buildPlayerOptions); - - EditorUserBuildSettings.buildScriptsOnly = oldBuildScriptsOnly; - EditorUserBuildSettings.SetBuildLocation(target, oldBuildLocation); - - switch (target) - { - case BuildTarget.StandaloneWindows: - case BuildTarget.StandaloneWindows64: + switch (target) + { + case BuildTarget.StandaloneWindows: + case BuildTarget.StandaloneWindows64: { #if UNITY_EDITOR_WIN - UnityEditor.WindowsStandalone.UserBuildSettings.createSolution = oldCreateSolution; + UnityEditor.WindowsStandalone.UserBuildSettings.createSolution = oldCreateSolution; #endif break; } - case BuildTarget.StandaloneOSX: + case BuildTarget.StandaloneOSX: { #if UNITY_EDITOR_OSX - UnityEditor.OSXStandalone.UserBuildSettings.createXcodeProject = oldCreateSolution; + UnityEditor.OSXStandalone.UserBuildSettings.createXcodeProject = oldCreateSolution; #endif break; } #if TUANJIE_2022_3_OR_NEWER - case BuildTarget.HMIAndroid: + case BuildTarget.HMIAndroid: #endif - case BuildTarget.Android: - { - EditorUserBuildSettings.exportAsGoogleAndroidProject = oldExportAndroidProj; - break; - } + case BuildTarget.Android: + { + EditorUserBuildSettings.exportAsGoogleAndroidProject = oldExportAndroidProj; + break; + } #if TUANJIE_2022_3_OR_NEWER - case BuildTarget.OpenHarmony: - { - EditorUserBuildSettings.exportAsOpenHarmonyProject = oldOpenHarmonyProj; - break; - } + case BuildTarget.OpenHarmony: + { + EditorUserBuildSettings.exportAsOpenHarmonyProject = oldOpenHarmonyProj; + break; + } #endif - } - - if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded) - { - throw new Exception("GenerateStripedAOTDlls failed"); + } } Debug.Log($"GenerateStripedAOTDlls target:{target} path:{outputPath}"); } diff --git a/Editor/MethodBridge/Generator.cs b/Editor/MethodBridge/Generator.cs index 39451ad..a7f1954 100644 --- a/Editor/MethodBridge/Generator.cs +++ b/Editor/MethodBridge/Generator.cs @@ -25,6 +25,8 @@ namespace HybridCLR.Editor.MethodBridge public string OutputFile { get; set; } public IReadOnlyCollection GenericMethods { get; set; } + + public bool Development { get; set; } } private readonly List _genericMethods; @@ -33,6 +35,8 @@ namespace HybridCLR.Editor.MethodBridge private readonly string _outputFile; + private readonly bool _development; + private readonly TypeCreator _typeCreator; private readonly HashSet _managed2nativeMethodSet = new HashSet(); @@ -50,6 +54,7 @@ namespace HybridCLR.Editor.MethodBridge _templateCode = options.TemplateCode; _outputFile = options.OutputFile; _typeCreator = new TypeCreator(); + _development = options.Development; } private readonly Dictionary _sig2Types = new Dictionary(); @@ -391,6 +396,10 @@ namespace HybridCLR.Editor.MethodBridge List lines = new List(20_0000); + lines.Add("\n"); + lines.Add($"// DEVELOPMENT={(_development ? 1 : 0)}"); + lines.Add("\n"); + var classInfos = new List(); var classTypeSet = new HashSet(); foreach (var type in structTypes)