LlamaLad7 / MixinExtras

Companion library to SpongePowered Mixin with many custom injectors for a more expressive experience.
MIT License
304 stars 17 forks source link

[Suggestion] find a way to add a constructor. #23

Closed thecatcore closed 1 year ago

thecatcore commented 1 year ago

It’s possible to add a constructor to a mixined class by using ASM and visiting the method in a MixinPluginConfig postApply. But you have to write the byte code yourself, I would like to find an easier way to do it.

LlamaLad7 commented 1 year ago

Do you specifically have a use-case for which allocating a fresh instance and calling a mixed-in initializer method doesn't work?

LlamaLad7 commented 1 year ago

It's also worth noting that it's not actually possible to add a constructor because they're a source-only concept. Bytecode has initializer methods (<init>) which contain the code from the relevant constructor, but also copy-pasted versions of every field initializer and non-static initialization block. The copy-pasted bits are impossible to extract without reliable line number information, which Minecraft obviously does not have, so you'd have to remake all the initializers yourself, which is kind of annoying and at that point I don't really see the benefit over the approach I mentioned above. Still curious to see your use-case though, maybe there's something I'm missing.

thecatcore commented 1 year ago

Well, I'm working on a project called Fabricated-(Legacy-)Forge, which ports Forge into a fabric mod on old mc versions and I''ve successfully ported patches to mixins in 99% case. But some patches add new constructor to some classes, so rn I'm copying the bytecode from the patched forge class and writing it using ASM.

thecatcore commented 1 year ago

See: https://github.com/arthurbambou/Fabricated-Legacy-Forge/blob/1.3.2-forge/src/main/java/fr/catcore/fabricatedforge/mixin/FabricatedForgeMixinPlugin.java