Fix some issues with startup order and preinit mixins, closes #153

This commit is contained in:
Mumfrey committed 2017-01-23 16:14:12 +00:00
1 parent d2fe040cb7
commit ea4f8aea8e
5 files changed
+73 -4

No files matched your search

@@ -105,6 +105,9 @@ public class MixinPlatformManager {
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
// Do an early scan to ensure preinit mixins are discovered
this.scanClasspath();
}
public Collection<String> getPhaseProviderClasses() {
@@ -105,6 +105,31 @@ public final class Mixins {
Mixins.getConfigs().add(config);
}
/**
* Get the number of "unvisited" configurations available. This is the
* number of configurations which have been added since the last selection
* attempt.
*
* <p>If the transformer has already entered a phase but no mixins have yet
* been applied, it is safe to visit any additional configs which were
* registered in the mean time and may wish to apply to the current phase.
* This is particularly true during the PREINIT phase, which by necessity
* must start as soon as the first class is transformed after bootstrapping,
* but may not have any valid mixins until later in the actual preinit
* process due to the order in which things are discovered.
*
* @return unvisited config count
*/
public static int getUnvisitedCount() {
int count = 0;
for (Config config : Mixins.getConfigs()) {
if (!config.isVisited()) {
count++;
}
}
return count;
}
/**
* Get current pending configs set, only configs which have yet to be
* consumed are present in this set
@@ -58,6 +58,13 @@ public class Config {
return this.config;
}
/**
* Get whether config has been visited
*/
public boolean isVisited() {
return this.config.isVisited();
}
/**
* Get API-level config view
*/
@@ -268,6 +268,11 @@ class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
* Keep track of initialisation state
*/
private transient boolean prepared = false;
/**
* Track whether this mixin has been evaluated for selection yet
*/
private transient boolean visited = false;
/**
* Spawn via GSON, no public ctor for you
@@ -602,9 +607,15 @@ class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
// AMS - temp
public boolean select(MixinEnvironment environment) {
this.visited = true;
return this.env == environment;
}
// AMS - temp
boolean isVisited() {
return this.visited;
}
/**
* Get the number of mixins in this config, for debug logging
*
@@ -444,7 +444,12 @@ public class MixinTransformer extends TreeTransformer {
/**
* Handling an error state, do not process further mixins
*/
private boolean errorState = false;
private boolean errorState = false;
/**
* Number of classes transformed in the current phase
*/
private int transformedCount = 0;
/**
* ctor
@@ -530,9 +535,9 @@ public class MixinTransformer extends TreeTransformer {
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
if (this.currentEnvironment != environment && !locked) {
if (!locked) {
try {
this.select(environment);
this.checkSelect(environment);
} catch (Exception ex) {
this.lock.pop();
throw new MixinException(ex);
@@ -585,6 +590,7 @@ public class MixinTransformer extends TreeTransformer {
ClassNode targetClassNode = this.readClass(basicClass, true);
TargetClassContext context = new TargetClassContext(this.sessionId, transformedName, targetClassNode, mixins);
basicClass = this.applyMixins(context);
this.transformedCount++;
} catch (InvalidMixinException th) {
this.dumpClassOnFailure(transformedName, basicClass, environment);
this.handleMixinApplyError(transformedName, th, environment);
@@ -619,15 +625,32 @@ public class MixinTransformer extends TreeTransformer {
return targets;
}
private void checkSelect(MixinEnvironment environment) {
if (this.currentEnvironment != environment) {
this.select(environment);
return;
}
int unvisitedCount = Mixins.getUnvisitedCount();
if (unvisitedCount > 0 && this.transformedCount == 0) {
this.select(environment);
}
}
private void select(MixinEnvironment environment) {
this.verboseLoggingLevel = (environment.getOption(Option.DEBUG_VERBOSE)) ? Level.INFO : Level.DEBUG;
MixinTransformer.logger.log(this.verboseLoggingLevel, "Preparing mixins for {}", environment);
if (this.transformedCount > 0) {
MixinTransformer.logger.log(this.verboseLoggingLevel, "Ending {}, applied {} mixins", this.currentEnvironment, this.transformedCount);
}
String action = this.currentEnvironment == environment ? "Checking for additional" : "Preparing";
MixinTransformer.logger.log(this.verboseLoggingLevel, "{} mixins for {}", action, environment);
long startTime = System.currentTimeMillis();
this.selectConfigs(environment);
this.selectModules(environment);
int totalMixins = this.prepareConfigs(environment);
this.currentEnvironment = environment;
this.transformedCount = 0;
double elapsedTime = (System.currentTimeMillis() - startTime) * 0.001D;
if (elapsedTime > 0.25D) {