From 536170a94675b07e462cc46cdb6493586bdd6dcc Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Mon, 26 Oct 2020 20:07:59 +0000 Subject: [PATCH] Allowing injecting in constructor's (#30 #40) * Allowing injecting in constructor It is bad to assume that everyone does business logic outside of constructor. We don't want redirect conflict hell * This should work Signed-off-by: liach * Add notes for easy portin in the future Signed-off-by: liach * Restore build number * Adopt new versioning scheme Signed-off-by: liach Co-authored-by: liach <7806504+liach@users.noreply.github.com> Co-authored-by: liach --- .../asm/mixin/injection/InjectionPoint.java | 13 +++++++++++++ .../mixin/injection/callback/CallbackInjector.java | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 5d13cda3..62878768 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -289,6 +289,19 @@ public abstract class InjectionPoint { * @return restriction level */ public RestrictTargetLevel getTargetRestriction(IInjectionPointContext context) { + return RestrictTargetLevel.CONSTRUCTORS_AFTER_DELEGATE; // Fabric change: allow inject in constructors + } + + /** + * Returns the target restriction level for this injection point's cancellation for + * {@literal @Inject} annotations. This level defines whether an injection point + * can declare {@code cancellable = true}. + * + * @param context injection-specific context + * @return restriction level + */ + // Fabric addition: prevent cancellation of inject in constructors + public RestrictTargetLevel getCancellationRestriction(IInjectionPointContext context) { return RestrictTargetLevel.METHODS_ONLY; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index d36ed952..98be09bc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -397,6 +397,16 @@ public class CallbackInjector extends Injector { throw new InvalidInjectionException(this.info, String.format("%s selector %s", ip, ex.getMessage())); } + // Fabric start: prevent cancellation in constructor injections + if (this.cancellable) { + try { + this.checkTargetForNode(target, injectionNode, ip.getCancellationRestriction(this.info)); + } catch (InvalidInjectionException ex) { + throw new InvalidInjectionException(this.info, String.format("%s selector (cancellable = true) %s", ip, ex.getMessage())); + } + } + // Fabric end + String id = ip.getId(); if (Strings.isNullOrEmpty(id)) { continue;