From 8c6c464ae1600bda30324d2f85ea219e379173a3 Mon Sep 17 00:00:00 2001 From: cneicy Date: Mon, 9 Mar 2026 19:07:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20IL=E6=89=AB=E6=8F=8F=E5=BF=98=E8=AE=B0?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E7=89=B9=E6=80=A7=E5=A4=B4=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeGen/Editor/EventBusILPostProcessor.cs | 41 +++++++++++++++++++++-- package.json | 2 +- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CodeGen/Editor/EventBusILPostProcessor.cs b/CodeGen/Editor/EventBusILPostProcessor.cs index 92b1d56..0043818 100644 --- a/CodeGen/Editor/EventBusILPostProcessor.cs +++ b/CodeGen/Editor/EventBusILPostProcessor.cs @@ -32,7 +32,31 @@ namespace ShrinkEventBus.CodeGen var assemblyDefinition = AssemblyDefinitionFor(compiledAssembly); var mainModule = assemblyDefinition.MainModule; - ImportReferences(mainModule); + bool needWeaving = false; + foreach (var type in mainModule.Types) + { + if (HasAttribute(type, "EventBusSubscriberAttribute") && InheritsFromMonoBehaviour(type)) + { + needWeaving = true; + break; + } + } + + if (!needWeaving) return null; + + try + { + ImportReferences(mainModule); + } + catch (System.Exception ex) + { + _diagnostics.Add(new DiagnosticMessage + { + DiagnosticType = DiagnosticType.Error, + MessageData = $"[ShrinkEventBus] ILPostProcessor Error: {ex.Message}" + }); + return GetResult(assemblyDefinition, _diagnostics); + } foreach (var type in mainModule.Types) { @@ -172,7 +196,16 @@ namespace ShrinkEventBus.CodeGen private void ImportReferences(ModuleDefinition module) { var runtimeModule = FindRuntimeModule(module); - var eventBusType = runtimeModule.GetAllTypes().First(t => t.Name == "EventBus"); + if (runtimeModule == null) + { + throw new System.Exception("Cannot find ShrinkEventBus.Runtime module in assembly references."); + } + + var eventBusType = runtimeModule.GetAllTypes().FirstOrDefault(t => t.Name == "EventBus"); + if (eventBusType == null) + { + throw new System.Exception("Cannot find EventBus class in ShrinkEventBus.Runtime."); + } _autoRegisterMethodRef = module.ImportReference( eventBusType.Methods.First(m => m.Name == "AutoRegister")); @@ -190,9 +223,11 @@ namespace ShrinkEventBus.CodeGen private void SearchRecursive(AssemblyDefinition asm, ref ModuleDefinition found, HashSet visited) { + if (asm == null) return; + foreach (var mod in asm.Modules) { - if (mod.Name == "ShrinkEventBus.Runtime.dll") + if (mod.Name == "ShrinkEventBus.Runtime.dll" || mod.Name == "ShrinkEventBus.Runtime") { found = mod; return; diff --git a/package.json b/package.json index 9fc3273..7b4f3cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.cneicy.shrink-eventbus", - "version": "1.1.3", + "version": "1.1.4", "displayName": "ShrinkEventBus", "description": "高性能、类型安全的 Unity 事件总线,支持优先级调度与自动注册。", "unity": "2022.3",