com.alicizax.unity/Editor/HybridCLR/BuildDLLCommand.cs

108 lines
3.7 KiB
C#
Raw Normal View History

2025-02-07 16:04:12 +08:00
#if ENABLE_HYBRIDCLR
using HybridCLR.Editor;
using HybridCLR.Editor.Commands;
#endif
using AlicizaX.Editor;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class BuildDLLCommand
{
private const string EnableHybridClrScriptingDefineSymbol = "ENABLE_HYBRIDCLR";
2025-03-07 17:58:52 +08:00
public const string AssemblyTextAssetPath = "Bundles/DLL";
2025-02-07 16:04:12 +08:00
static BuildDLLCommand()
{
}
/// <summary>
/// 禁用HybridCLR宏定义。
/// </summary>
2025-03-07 17:58:52 +08:00
[MenuItem("HybridCLR/Tools/Define Symbols/Disable HybridCLR", false, 30)]
2025-02-07 16:04:12 +08:00
public static void Disable()
{
ScriptingDefineSymbols.RemoveScriptingDefineSymbol(EnableHybridClrScriptingDefineSymbol);
HybridCLR.Editor.SettingsUtil.Enable = false;
SyncAssemblyContent.RefreshAssembly();
}
/// <summary>
/// 开启HybridCLR宏定义。
/// </summary>
2025-03-07 17:58:52 +08:00
[MenuItem("HybridCLR/Tools/Tools/Define Symbols/Enable HybridCLR", false, 31)]
2025-02-07 16:04:12 +08:00
public static void Enable()
{
ScriptingDefineSymbols.RemoveScriptingDefineSymbol(EnableHybridClrScriptingDefineSymbol);
ScriptingDefineSymbols.AddScriptingDefineSymbol(EnableHybridClrScriptingDefineSymbol);
HybridCLR.Editor.SettingsUtil.Enable = true;
SyncAssemblyContent.RefreshAssembly();
}
#if ENABLE_HYBRIDCLR
2025-03-07 17:58:52 +08:00
[MenuItem("HybridCLR/Tools/Build/BuildAssets And CopyTo AssemblyTextAssetPath")]
2025-02-07 16:04:12 +08:00
public static void BuildAndCopyDlls()
{
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
CopyAOTHotUpdateDlls(target);
}
#endif
public static void BuildAndCopyDlls(BuildTarget target)
{
#if ENABLE_HYBRIDCLR
CompileDllCommand.CompileDll(target);
CopyAOTHotUpdateDlls(target);
#endif
}
public static void CopyAOTHotUpdateDlls(BuildTarget target)
{
CopyAOTAssembliesToAssetPath();
CopyHotUpdateAssembliesToAssetPath();
AssetDatabase.Refresh();
}
public static void CopyAOTAssembliesToAssetPath()
{
#if ENABLE_HYBRIDCLR
var target = EditorUserBuildSettings.activeBuildTarget;
string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
2025-03-07 17:58:52 +08:00
string aotAssembliesDstDir = Application.dataPath + "/" + AssemblyTextAssetPath;
2025-02-07 16:04:12 +08:00
2025-03-07 17:58:52 +08:00
foreach (var dll in AssemblyLoadData.Instance.AOTMetaAssemblies)
2025-02-07 16:04:12 +08:00
{
string srcDllPath = $"{aotAssembliesSrcDir}/{dll}";
if (!System.IO.File.Exists(srcDllPath))
{
2025-03-07 17:58:52 +08:00
Debug.LogError(
$"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。裁剪后的AOT dll在BuildPlayer时才能生成因此需要你先构建一次游戏App后再打包。");
2025-02-07 16:04:12 +08:00
continue;
}
string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.bytes";
System.IO.File.Copy(srcDllPath, dllBytesPath, true);
Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
}
#endif
}
public static void CopyHotUpdateAssembliesToAssetPath()
{
#if ENABLE_HYBRIDCLR
var target = EditorUserBuildSettings.activeBuildTarget;
string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
2025-03-07 17:58:52 +08:00
string hotfixAssembliesDstDir = Application.dataPath + "/" + AssemblyTextAssetPath;
2025-02-07 16:04:12 +08:00
foreach (var dll in SettingsUtil.HotUpdateAssemblyFilesExcludePreserved)
{
string dllPath = $"{hotfixDllSrcDir}/{dll}";
string dllBytesPath = $"{hotfixAssembliesDstDir}/{dll}.bytes";
System.IO.File.Copy(dllPath, dllBytesPath, true);
Debug.Log($"[CopyHotUpdateAssembliesToStreamingAssets] copy hotfix dll {dllPath} -> {dllBytesPath}");
}
#endif
}
}