hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
1.97k stars 172 forks source link

Draft: Automatic addition all registered classes to link.xml #493

Closed vangogih closed 9 months ago

vangogih commented 1 year ago

Hi @hadashiA,

I have mentioned (#363) that I want to add the auto-addition all registered classes in scopes to link.xml. Here is proof of concept. I have already tested generation for MonoBehaviours, ScriptableObject classes and classes with Serializable attribute.

How does it works: I made the same mechanism to turn on the feature as CodeGen. Via aditional asmdef VContainer.EnableLinkGenerator. You need to add that assebmly as dependency to your runtime asmdef to enable that feature. So enabling you can find in class VContainerLinkerProcessor. That class tries to find the assembly with VContainer and VContainer.EnableLinkGenerator dependency. And if they are exist It starts to analyze all types in picked asseblies.

The analyzer consists of:

  1. Finding all lifetime scopes and create their instances
  2. Finding all declared fields with SerializedField attribute or derived from Component or ScriptableObject.
  3. Initializing all have found fields via AddComponent, ScriptableObject.Create and Activator.CreateInstance
  4. Calling method Cofigure on LifetimeScope to register all dependencies which defined in lifetime scope
  5. Geting all registered types from builder

Next, I have wrote a custom link.xml builder class (VContainerXmlLinkBuilder) which based om unity's UnityEditor.Build.Pipeline.Utilities.LinkXmlGenerator class, but more powerful. It allows to preserve methods, fields and properties without whole class preserving (as LinkXmlGemerator does). I think that preserving all members by default is so frustrating feature.

That feature works with Vcontainer as backbox and doesn't require to change any parts of runtime.

I want to get your comment about that idea before continue. Feel free to give feedback 😄 If you approve that feature I will:

p.s. feature is draft, but you can test in the editor if you need.

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
vcontainer ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 28, 2023 at 0:41AM (UTC)
vangogih commented 1 year ago

@hadashiA ping 😀

hadashiA commented 10 months ago

@vangogih Sorry for the late reply. I've been busy with another OSS work.

Big thanks for your a lot of work. I think a lot of people would find the stripping measure interesting.

However, due to several points, I'm currently considering not including this in the main repo.

vangogih commented 9 months ago

@vangogih Sorry for the late reply. I've been busy with another OSS work.

Big thanks for your a lot of work. I think a lot of people would find the stripping measure interesting.

However, due to several points, I'm currently considering not including this in the main repo.

  • Currently, I think Roslyn analyzer is the leading tool for static analysis of source code.
  • Your algorithm is interesting, but maintaining both SoruceGenerator and this seems a bit much work.
  • VContaienr has SoruceGenerator. If we can derive the classes that are being used reliably, it might be possible to mark them [Perserve]. I prefer that approach.

@hadashiA Thank for your review and comment!

I think either the PR is quite outdated now. And we can implement same functionality other ways with SourceGenerator feature.

I'm going to close this PR, thanks again)