LlamaLad7 / MixinExtras

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

[Suggestion] @WrapMethod #65

Closed KingContaria closed 1 month ago

KingContaria commented 6 months ago

Hi,

a new @WrapMethod injector, which would work similar to WrapOperation, except wrapping the entire method instead of just a single call.

I am currently working on a project which requires me to do some synchronization, however it seems all the ways i can think of to accomplish my goal have some sort of downside. Specifically, I am trying to synchronize this entire method on the this.surfaceBuilder object.

public void buildSurface(Random random, Chunk chunk, int x, int z, int worldHeight, double noise, BlockState defaultBlock, BlockState defaultFluid, int seaLevel, long seed) {
    this.surfaceBuilder.initSeed(seed);
    this.surfaceBuilder.generate(random, chunk, this, x, z, worldHeight, noise, defaultBlock, defaultFluid, seaLevel, seed);
}

I can't use two WrapOperation's because i rely on it being a single synchronization block and i can't use a mixin config plugin to synchronize the method because i want it to synchronize specifically on the surfacebuilder object AND additionally i only want to apply the synchronization conditionally. Another solution I've considered is using some form of lock and simply injecting at head and tail, however it seems not ideal to have the lock release not be in a finally block.

Using a new @WrapMethod injector i could simply wrap the entire method with my synchronization logic. It could also allow for adding try-catch or -finally blocks.

The only thing similar in the closed issues i could find was https://github.com/LlamaLad7/MixinExtras/issues/8 suggesting try-catch-finally injectors which are now covered by wrapoperation, however you also talked about potentially wrapping the entire method or slices of methods. While this wouldn't cover only wrapping a certain slice, it would allow catching errors in the entire method, not just on a single operation.

LlamaLad7 commented 6 months ago

This is planned but good to have an issue tracking it.

TropheusJ commented 4 months ago

I also need this, pretty much need to wrap a whole method in a try-with-resources.

Need to wrap a parameter, and then close the wrapper before return.