[opt] 打包时检查生成桥接函数时的development选项与当前development选项一致。`Generate/All`之后切换development选项再打包,将会产生严重的崩溃

main
walon 2024-05-24 18:32:44 +08:00
parent b96d6fc10d
commit 3de931a3af
4 changed files with 111 additions and 68 deletions

View File

@ -1,8 +1,10 @@
using HybridCLR.Editor.Settings; using HybridCLR.Editor.Settings;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEditor; using UnityEditor;
using UnityEditor.Build; using UnityEditor.Build;
@ -16,6 +18,8 @@ namespace HybridCLR.Editor.BuildProcessors
{ {
public int callbackOrder => 0; public int callbackOrder => 0;
public static bool DisableMethodBridgeDevelopmentFlagChecking { get; set; }
public void OnPreprocessBuild(BuildReport report) public void OnPreprocessBuild(BuildReport report)
{ {
HybridCLRSettings globalSettings = SettingsUtil.HybridCLRSettings; HybridCLRSettings globalSettings = SettingsUtil.HybridCLRSettings;
@ -67,6 +71,24 @@ namespace HybridCLR.Editor.BuildProcessors
Debug.LogWarning("[CheckSettings] No hot update modules configured in HybridCLRSettings"); 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.");
}
}
} }
} }
} }

View File

@ -38,6 +38,7 @@ namespace HybridCLR.Editor.Commands
TemplateCode = templateCode, TemplateCode = templateCode,
OutputFile = outputFile, OutputFile = outputFile,
GenericMethods = analyzer.GenericMethods, GenericMethods = analyzer.GenericMethods,
Development = EditorUserBuildSettings.development,
}); });
g.PrepareMethods(); g.PrepareMethods();

View File

@ -1,4 +1,5 @@
using HybridCLR.Editor.Installer; using HybridCLR.Editor.BuildProcessors;
using HybridCLR.Editor.Installer;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -85,10 +86,14 @@ namespace HybridCLR.Editor.Commands
bool oldOpenHarmonyProj = EditorUserBuildSettings.exportAsOpenHarmonyProject; bool oldOpenHarmonyProj = EditorUserBuildSettings.exportAsOpenHarmonyProject;
#endif #endif
bool oldBuildScriptsOnly = EditorUserBuildSettings.buildScriptsOnly; bool oldBuildScriptsOnly = EditorUserBuildSettings.buildScriptsOnly;
string oldBuildLocation = EditorUserBuildSettings.GetBuildLocation(target);
try
{
CheckSettings.DisableMethodBridgeDevelopmentFlagChecking = true;
EditorUserBuildSettings.buildScriptsOnly = true; EditorUserBuildSettings.buildScriptsOnly = true;
string location = GetLocationPathName(outputPath, target); string location = GetLocationPathName(outputPath, target);
string oldBuildLocation = EditorUserBuildSettings.GetBuildLocation(target);
EditorUserBuildSettings.SetBuildLocation(target, location); EditorUserBuildSettings.SetBuildLocation(target, location);
switch (target) switch (target)
@ -96,33 +101,33 @@ namespace HybridCLR.Editor.Commands
case BuildTarget.StandaloneWindows: case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64: case BuildTarget.StandaloneWindows64:
{ {
#if UNITY_EDITOR_WIN #if UNITY_EDITOR_WIN
UnityEditor.WindowsStandalone.UserBuildSettings.createSolution = true; UnityEditor.WindowsStandalone.UserBuildSettings.createSolution = true;
#endif #endif
break; break;
} }
case BuildTarget.StandaloneOSX: case BuildTarget.StandaloneOSX:
{ {
#if UNITY_EDITOR_OSX #if UNITY_EDITOR_OSX
UnityEditor.OSXStandalone.UserBuildSettings.createXcodeProject = true; UnityEditor.OSXStandalone.UserBuildSettings.createXcodeProject = true;
#endif #endif
break; break;
} }
#if TUANJIE_2022_3_OR_NEWER #if TUANJIE_2022_3_OR_NEWER
case BuildTarget.HMIAndroid: case BuildTarget.HMIAndroid:
#endif #endif
case BuildTarget.Android: case BuildTarget.Android:
{ {
EditorUserBuildSettings.exportAsGoogleAndroidProject = true; EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
break; break;
} }
#if TUANJIE_2022_3_OR_NEWER #if TUANJIE_2022_3_OR_NEWER
case BuildTarget.OpenHarmony: case BuildTarget.OpenHarmony:
{ {
EditorUserBuildSettings.exportAsOpenHarmonyProject = true; EditorUserBuildSettings.exportAsOpenHarmonyProject = true;
break; break;
} }
#endif #endif
} }
Debug.Log($"GenerateStripedAOTDlls build option:{buildOptions}"); Debug.Log($"GenerateStripedAOTDlls build option:{buildOptions}");
@ -138,6 +143,16 @@ namespace HybridCLR.Editor.Commands
var report = BuildPipeline.BuildPlayer(buildPlayerOptions); var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
{
throw new Exception("GenerateStripedAOTDlls failed");
}
}
finally
{
CheckSettings.DisableMethodBridgeDevelopmentFlagChecking = false;
EditorUserBuildSettings.buildScriptsOnly = oldBuildScriptsOnly; EditorUserBuildSettings.buildScriptsOnly = oldBuildScriptsOnly;
EditorUserBuildSettings.SetBuildLocation(target, oldBuildLocation); EditorUserBuildSettings.SetBuildLocation(target, oldBuildLocation);
@ -174,10 +189,6 @@ namespace HybridCLR.Editor.Commands
} }
#endif #endif
} }
if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
{
throw new Exception("GenerateStripedAOTDlls failed");
} }
Debug.Log($"GenerateStripedAOTDlls target:{target} path:{outputPath}"); Debug.Log($"GenerateStripedAOTDlls target:{target} path:{outputPath}");
} }

View File

@ -25,6 +25,8 @@ namespace HybridCLR.Editor.MethodBridge
public string OutputFile { get; set; } public string OutputFile { get; set; }
public IReadOnlyCollection<GenericMethod> GenericMethods { get; set; } public IReadOnlyCollection<GenericMethod> GenericMethods { get; set; }
public bool Development { get; set; }
} }
private readonly List<GenericMethod> _genericMethods; private readonly List<GenericMethod> _genericMethods;
@ -33,6 +35,8 @@ namespace HybridCLR.Editor.MethodBridge
private readonly string _outputFile; private readonly string _outputFile;
private readonly bool _development;
private readonly TypeCreator _typeCreator; private readonly TypeCreator _typeCreator;
private readonly HashSet<MethodDesc> _managed2nativeMethodSet = new HashSet<MethodDesc>(); private readonly HashSet<MethodDesc> _managed2nativeMethodSet = new HashSet<MethodDesc>();
@ -50,6 +54,7 @@ namespace HybridCLR.Editor.MethodBridge
_templateCode = options.TemplateCode; _templateCode = options.TemplateCode;
_outputFile = options.OutputFile; _outputFile = options.OutputFile;
_typeCreator = new TypeCreator(); _typeCreator = new TypeCreator();
_development = options.Development;
} }
private readonly Dictionary<string, TypeInfo> _sig2Types = new Dictionary<string, TypeInfo>(); private readonly Dictionary<string, TypeInfo> _sig2Types = new Dictionary<string, TypeInfo>();
@ -391,6 +396,10 @@ namespace HybridCLR.Editor.MethodBridge
List<string> lines = new List<string>(20_0000); List<string> lines = new List<string>(20_0000);
lines.Add("\n");
lines.Add($"// DEVELOPMENT={(_development ? 1 : 0)}");
lines.Add("\n");
var classInfos = new List<ClassInfo>(); var classInfos = new List<ClassInfo>();
var classTypeSet = new HashSet<TypeInfo>(); var classTypeSet = new HashSet<TypeInfo>();
foreach (var type in structTypes) foreach (var type in structTypes)