From 0c5aa1a69ccdf9b1ba593ef1dca21aa2a7c4d752 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com>
Date: Fri, 5 Sep 2025 19:46:17 +0800
Subject: [PATCH] modify
---
Editor/AlicizaX.EditorExtension.Editor.asmdef | 14 +-
Editor/HybridCLR.meta | 8 +
Editor/HybridCLR/BuildDLLCommand.cs | 154 ++++++++++++++++++
Editor/HybridCLR/BuildDLLCommand.cs.meta | 3 +
4 files changed, 175 insertions(+), 4 deletions(-)
create mode 100644 Editor/HybridCLR.meta
create mode 100644 Editor/HybridCLR/BuildDLLCommand.cs
create mode 100644 Editor/HybridCLR/BuildDLLCommand.cs.meta
diff --git a/Editor/AlicizaX.EditorExtension.Editor.asmdef b/Editor/AlicizaX.EditorExtension.Editor.asmdef
index 1a36a90..eabc31b 100644
--- a/Editor/AlicizaX.EditorExtension.Editor.asmdef
+++ b/Editor/AlicizaX.EditorExtension.Editor.asmdef
@@ -6,10 +6,10 @@
"GUID:75b6f2078d190f14dbda4a5b747d709c",
"GUID:e34a5702dd353724aa315fb8011f08c3",
"GUID:4d1926c9df5b052469a1c63448b7609a",
- "GUID:be2f20a77f3232f44b9711ef43234aac",
- "GUID:a19b414bea3b97240a91aeab9a8eab36",
"GUID:acfef7cabed3b0a42b25edb1cd4fa259",
- "GUID:2765e68924a08a94ea0ea66b31c0168f"
+ "GUID:2765e68924a08a94ea0ea66b31c0168f",
+ "GUID:1619e00706139ce488ff80c0daeea8e7",
+ "GUID:2373f786d14518f44b0f475db77ba4de"
],
"includePlatforms": [
"Editor"
@@ -20,6 +20,12 @@
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
- "versionDefines": [],
+ "versionDefines": [
+ {
+ "name": "com.code-philosophy.hybridclr",
+ "expression": "",
+ "define": "HYBIRDCLR_SUPPORT"
+ }
+ ],
"noEngineReferences": false
}
\ No newline at end of file
diff --git a/Editor/HybridCLR.meta b/Editor/HybridCLR.meta
new file mode 100644
index 0000000..ac61be0
--- /dev/null
+++ b/Editor/HybridCLR.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: b1e3904628b58d742976a1f0512c441e
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Editor/HybridCLR/BuildDLLCommand.cs b/Editor/HybridCLR/BuildDLLCommand.cs
new file mode 100644
index 0000000..90f7fd6
--- /dev/null
+++ b/Editor/HybridCLR/BuildDLLCommand.cs
@@ -0,0 +1,154 @@
+#if HYBIRDCLR_SUPPORT
+
+#if ENABLE_HYBRIDCLR
+using System;
+using System.Collections.Generic;
+using System.IO;
+using HybridCLR.Editor;
+using HybridCLR.Editor.Commands;
+#endif
+
+using System.Reflection;
+using AlicizaX;
+using AlicizaX.Editor;
+using UnityEditor;
+using UnityEngine;
+
+[InitializeOnLoad]
+public static class BuildDLLCommand
+{
+ private const string EnableHybridClrScriptingDefineSymbol = "ENABLE_HYBRIDCLR";
+ public static string AssemblyTextAssetPath = Application.dataPath + "/" + "Bundles/DLL";
+
+ public static void SetCopyPath(string path)
+ {
+ AssemblyTextAssetPath = path;
+ }
+
+ ///
+ /// 禁用HybridCLR宏定义。
+ ///
+ [MenuItem("HybridCLR/Tools/Define Symbols/Disable HybridCLR", false, 30)]
+ public static void Disable()
+ {
+ ScriptingDefineSymbols.RemoveScriptingDefineSymbol(EnableHybridClrScriptingDefineSymbol);
+ HybridCLR.Editor.SettingsUtil.Enable = false;
+ }
+
+ ///
+ /// 开启HybridCLR宏定义。
+ ///
+ [MenuItem("HybridCLR/Tools/Define Symbols/Enable HybridCLR", false, 31)]
+ public static void Enable()
+ {
+ ScriptingDefineSymbols.RemoveScriptingDefineSymbol(EnableHybridClrScriptingDefineSymbol);
+ ScriptingDefineSymbols.AddScriptingDefineSymbol(EnableHybridClrScriptingDefineSymbol);
+ HybridCLR.Editor.SettingsUtil.Enable = true;
+ }
+
+#if ENABLE_HYBRIDCLR &&HYBIRDCLR_SUPPORT
+ [MenuItem("HybridCLR/Tools/BuildAssets And CopyTo AssemblyTextAssetPath")]
+ public static void BuildAndCopyDlls()
+ {
+ BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
+ CompileDllCommand.CompileDll(target);
+ CopyAOTHotUpdateDlls(target);
+ }
+
+
+ public static void GenerateHybridCLRSome()
+ {
+ PrebuildCommand.GenerateAll();
+ }
+
+ public static void BuildAndCopyDlls(BuildTarget target)
+ {
+ CompileDllCommand.CompileDll(target);
+ CopyAOTHotUpdateDlls(target);
+ }
+
+ public static void CopyAOTHotUpdateDlls(BuildTarget target)
+ {
+ CopyAOTAssembliesToAssetPath();
+ CopyHotUpdateAssembliesToAssetPath();
+ AssetDatabase.Refresh();
+ }
+
+ public static void CopyAOTAssembliesToAssetPath()
+ {
+ IReadOnlyList patchedAotAssembliesPath = GetStaticFieldValue();
+ if (patchedAotAssembliesPath == null)
+ {
+ Debug.Log("请先生成AOT补充脚本");
+ return;
+ }
+
+ var target = EditorUserBuildSettings.activeBuildTarget;
+ string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
+ string aotAssembliesDstDir = AssemblyTextAssetPath;
+
+ foreach (var dll in patchedAotAssembliesPath)
+ {
+ string srcDllPath = $"{aotAssembliesSrcDir}/{dll}";
+ if (!System.IO.File.Exists(srcDllPath))
+ {
+ Debug.LogError(
+ $"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
+ continue;
+ }
+
+ string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.bytes";
+ System.IO.File.Copy(srcDllPath, dllBytesPath, true);
+ Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
+ }
+ }
+
+ private static IReadOnlyList GetStaticFieldValue()
+ {
+ var gs = SettingsUtil.HybridCLRSettings;
+ string scriptPath = $"{Application.dataPath}/{gs.outputAOTGenericReferenceFile}";
+ if (!File.Exists(scriptPath))
+ {
+ return null;
+ }
+ FileInfo info = new FileInfo(scriptPath);
+ string scriptTypeName = info.Name.Replace(".cs", "");
+
+ Type targetType = Utility.Assembly.GetType(scriptTypeName);
+ if (targetType == null)
+ {
+ Debug.LogError($"can not find script type: {scriptTypeName}");
+ return null;
+ }
+
+ // 反射获取静态字段
+ FieldInfo field = targetType.GetField("PatchedAOTAssemblyList",
+ BindingFlags.Public | BindingFlags.Static);
+
+ if (field == null)
+ {
+ Debug.LogError($"Field 'PatchedAOTAssemblyList' not found in {targetType.Name}");
+ return null;
+ }
+
+ // 获取字段值并打印
+ return field.GetValue(null) as IReadOnlyList;
+ }
+
+ public static void CopyHotUpdateAssembliesToAssetPath()
+ {
+ var target = EditorUserBuildSettings.activeBuildTarget;
+
+ string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
+ string hotfixAssembliesDstDir = AssemblyTextAssetPath;
+ 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
+}
+#endif
diff --git a/Editor/HybridCLR/BuildDLLCommand.cs.meta b/Editor/HybridCLR/BuildDLLCommand.cs.meta
new file mode 100644
index 0000000..5a82fd4
--- /dev/null
+++ b/Editor/HybridCLR/BuildDLLCommand.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 0761954ba63648b8b73f8ebb736bb9eb
+timeCreated: 1737524294
\ No newline at end of file