Skip to content

Commit

Permalink
Run all preinject operations before starting any actual injections
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed May 18, 2024
1 parent a2751aa commit 1f34ee9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ protected void prepareInjections(MixinTargetContext mixin) {
}
}

/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
* #applyPreInjections(
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
*/
@Override
protected void applyPreInjections(MixinTargetContext mixin) {
if (Feature.INJECTORS_IN_INTERFACE_MIXINS.isEnabled()) {
super.applyPreInjections(mixin);
return;
}
}

/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
* #applyInjections(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,22 @@ enum ApplicatorPass {
/**
* Enumerate injectors and scan for injection points
*/
PREINJECT,
INJECT_PREPARE,

/**
* Apply accessors and invokers
*/
ACCESSOR,

/**
* Apply preinjection steps on injectors from previous pass
*/
INJECT_PREINJECT,

/**
* Apply injectors from previous pass
*/
INJECT
INJECT_APPLY

}

Expand Down Expand Up @@ -224,7 +229,7 @@ final void apply(SortedSet<MixinInfo> mixins) {
IActivity applyActivity = this.activities.begin("Mixin");

Set<Integer> orders = MixinApplicatorStandard.ORDERS_NONE;
if (pass == ApplicatorPass.INJECT) {
if (pass == ApplicatorPass.INJECT_APPLY) {
orders = new TreeSet<Integer>();
for (MixinTargetContext context : mixinContexts) {
context.getInjectorOrders(orders);
Expand Down Expand Up @@ -303,7 +308,7 @@ protected final void applyMixin(MixinTargetContext mixin, ApplicatorPass pass, i
this.applyInitialisers(mixin);
break;

case PREINJECT:
case INJECT_PREPARE:
activity.next("Prepare Injections");
this.prepareInjections(mixin);
break;
Expand All @@ -313,7 +318,12 @@ protected final void applyMixin(MixinTargetContext mixin, ApplicatorPass pass, i
this.applyAccessors(mixin);
break;

case INJECT:
case INJECT_PREINJECT:
activity.next("Apply Injections");
this.applyPreInjections(mixin);
break;

case INJECT_APPLY:
activity.next("Apply Injections");
this.applyInjections(mixin, injectorOrder);
break;
Expand Down Expand Up @@ -718,6 +728,17 @@ protected void prepareInjections(MixinTargetContext mixin) {
mixin.prepareInjections();
}

/**
* Run preinject application on all injectors discovered in the previous
* pass
*
* @param mixin Mixin being applied
* @param injectorOrder injector order for this pass
*/
protected void applyPreInjections(MixinTargetContext mixin) {
mixin.applyPreInjections();
}

/**
* Apply all injectors discovered in the previous pass
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,29 @@ void getInjectorOrders(Set<Integer> orders) {
orders.add(injectInfo.getOrder());
}
}

/**
* Run the preinject step for all discovered injectors
*/
void applyPreInjections() {
this.activities.clear();

try {
IActivity applyActivity = this.activities.begin("PreInject");
IActivity preInjectActivity = this.activities.begin("?");
for (InjectionInfo injectInfo : this.injectors) {
preInjectActivity.next(injectInfo.toString());
injectInfo.preInject();
}
applyActivity.end();
} catch (InvalidMixinException ex) {
ex.prepend(this.activities);
throw ex;
} catch (Exception ex) {
throw new InvalidMixinException(this, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst transforming the mixin class:", ex,
this.activities);
}
}

/**
* Apply injectors discovered in the {@link #prepareInjections()} pass
Expand All @@ -1427,14 +1450,7 @@ void applyInjections(int injectorOrder) {
}

try {
IActivity applyActivity = this.activities.begin("PreInject");
IActivity preInjectActivity = this.activities.begin("?");
for (InjectionInfo injectInfo : injectors) {
preInjectActivity.next(injectInfo.toString());
injectInfo.preInject();
}

applyActivity.next("Inject");
IActivity applyActivity = this.activities.begin("Inject");
IActivity injectActivity = this.activities.begin("?");
for (InjectionInfo injectInfo : injectors) {
injectActivity.next(injectInfo.toString());
Expand Down

0 comments on commit 1f34ee9

Please sign in to comment.