feat: make Unity integration optional

This commit is contained in:
cneicy committed 2026-10-03 23:36:43 +08:00
1 parent a92c9d2cea
commit 240d24ba7a
27 files changed
+916 -69

No files matched your search

+65 -8
View File
@@ -1,7 +1,10 @@
#nullable enable
using System;
using System.Collections.Generic;
using ShrinkShared.CodeGen;
using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Unity.CompilationPipeline.Common.Diagnostics;
using Unity.CompilationPipeline.Common.ILPostProcessing;
@@ -11,12 +14,66 @@ namespace ShrinkEventBus.CodeGen
{
public override ILPostProcessor GetInstance() => this;
// ShrinkRegistryILPostProcessor owns the complete pass, including event bindings.
public override bool WillProcess(ICompiledAssembly compiledAssembly) => false;
public override bool WillProcess(ICompiledAssembly compiledAssembly)
{
#if SHRINK_EVENTBUS_HAS_SHARED_CODEGEN
// The shared processor owns event weaving when its package is installed.
return false;
#else
return compiledAssembly.Name != "ShrinkEventBus.Runtime" &&
compiledAssembly.References.Any(path =>
Path.GetFileNameWithoutExtension(path) == "ShrinkEventBus.Runtime");
#endif
}
public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) =>
WillProcess(compiledAssembly)
? UnityShrinkCodeGenAdapter.Process(compiledAssembly, "ShrinkEventBus.CodeGen")
: new ILPostProcessResult(compiledAssembly.InMemoryAssembly, new List<DiagnosticMessage>());
public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
{
var diagnostics = new List<DiagnosticMessage>();
#if !SHRINK_EVENTBUS_HAS_SHARED_CODEGEN
if (!WillProcess(compiledAssembly))
return new ILPostProcessResult(compiledAssembly.InMemoryAssembly, diagnostics);
try
{
using var resolver = new PostProcessorAssemblyResolver(compiledAssembly);
using var pe = new MemoryStream(compiledAssembly.InMemoryAssembly.PeData);
var pdbData = compiledAssembly.InMemoryAssembly.PdbData;
var hasSymbols = pdbData != null && pdbData.Length > 0;
using var pdb = hasSymbols ? new MemoryStream(pdbData!) : null;
using var assembly = AssemblyDefinition.ReadAssembly(pe, new ReaderParameters
{
AssemblyResolver = resolver,
ReadingMode = ReadingMode.Immediate,
ReflectionImporterProvider = new PostProcessorReflectionImporterProvider(),
ReadSymbols = hasSymbols,
SymbolStream = pdb,
SymbolReaderProvider = hasSymbols ? new PortablePdbReaderProvider() : null
});
resolver.AddAssemblyDefinitionBeingOperatedOn(assembly);
if (assembly.Name.HasPublicKey)
throw new InvalidOperationException("Signed assemblies cannot be woven by the standalone EventBus processor.");
if (!EventBusAssemblyWeaver.Weave(assembly.MainModule))
return new ILPostProcessResult(compiledAssembly.InMemoryAssembly, diagnostics);
using var outputPe = new MemoryStream();
using var outputPdb = new MemoryStream();
assembly.Write(outputPe, new WriterParameters
{
WriteSymbols = hasSymbols,
SymbolStream = hasSymbols ? outputPdb : null,
SymbolWriterProvider = hasSymbols ? new PortablePdbWriterProvider() : null
});
return new ILPostProcessResult(new InMemoryAssembly(outputPe.ToArray(),
hasSymbols ? outputPdb.ToArray() : Array.Empty<byte>()), diagnostics);
}
catch (Exception exception)
{
diagnostics.Add(new DiagnosticMessage
{
DiagnosticType = DiagnosticType.Error,
MessageData = $"[ShrinkEventBus.CodeGen] {exception.Message}"
});
}
#endif
return new ILPostProcessResult(compiledAssembly.InMemoryAssembly, diagnostics);
}
}
}