mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-23 21:50:00 +00:00
Add export filter, allows exporting only interesting classes if required
This commit is contained in:
1 parent
e4c445b8a4
commit
97319a5049
3 files changed
+88
-18
No files matched your search
@@ -191,6 +191,21 @@ public class MixinEnvironment {
|
||||
*/
|
||||
DEBUG_EXPORT(Option.DEBUG_ALL, "export"),
|
||||
|
||||
/**
|
||||
* Export filter, if omitted allows all transformed classes to be
|
||||
* exported. If specified, acts as a filter for class names to export
|
||||
* and only matching classes will be exported. This is useful when using
|
||||
* Fernflower as exporting can be otherwise very slow. The following
|
||||
* wildcards are allowed:
|
||||
*
|
||||
* <dl>
|
||||
* <dt>*</dt><dd>Matches one or more characters except dot (.)</dd>
|
||||
* <dt>**</dt><dd>Matches any number of characters</dd>
|
||||
* <dt>?</dt><dd>Matches exactly one character</dd>
|
||||
* </dl>
|
||||
*/
|
||||
DEBUG_EXPORT_FILTER(Option.DEBUG_EXPORT, "filter"),
|
||||
|
||||
/**
|
||||
* Run the CheckClassAdapter on all classes after mixins are applied
|
||||
*/
|
||||
@@ -244,17 +259,21 @@ public class MixinEnvironment {
|
||||
this.property = (parent != null ? parent.property : Option.PREFIX) + "." + property;
|
||||
}
|
||||
|
||||
public Option getParent() {
|
||||
Option getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
public String getProperty() {
|
||||
String getProperty() {
|
||||
return this.property;
|
||||
}
|
||||
|
||||
protected boolean getValue() {
|
||||
boolean getBooleanValue() {
|
||||
return Booleans.parseBoolean(System.getProperty(this.property), false)
|
||||
|| (this.parent != null && this.parent.getValue());
|
||||
|| (this.parent != null && this.parent.getBooleanValue());
|
||||
}
|
||||
|
||||
String getStringValue() {
|
||||
return (this.parent == null || this.parent.getBooleanValue()) ? System.getProperty(this.property) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,21 +395,33 @@ public class MixinEnvironment {
|
||||
|
||||
this.options = new boolean[Option.values().length];
|
||||
for (Option option : Option.values()) {
|
||||
this.options[option.ordinal()] = option.getValue();
|
||||
this.options[option.ordinal()] = option.getBooleanValue();
|
||||
}
|
||||
|
||||
Side side = this.getSide();
|
||||
String codeSource = this.getCodeSource();
|
||||
this.logger.info("SpongePowered MIXIN Subsystem Version={} Source={}, Env={}", version, codeSource, side);
|
||||
|
||||
if (this.getOption(Option.DEBUG_VERBOSE) && this.phase == Phase.PREINIT) {
|
||||
PrettyPrinter printer = new PrettyPrinter(32);
|
||||
printer.add("SpongePowered MIXIN (Verbose debugging enabled)").centre().hr();
|
||||
printer.add("%25s : %s", "Code source", this.getClass().getProtectionDomain().getCodeSource().getLocation());
|
||||
printer.add("%25s : %s", "Code source", codeSource);
|
||||
printer.add("%25s : %s", "Internal Version", version).hr();
|
||||
for (Option option : Option.values()) {
|
||||
printer.add("%25s : %s%s", option.property, option.parent == null ? "" : " - ", this.getOption(option));
|
||||
}
|
||||
printer.hr().add("%25s : %s", "Detected Side", this.getSide());
|
||||
printer.hr().add("%25s : %s", "Detected Side", side);
|
||||
printer.print(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
private String getCodeSource() {
|
||||
try {
|
||||
return this.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
|
||||
} catch (Throwable th) {
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the phase for this environment
|
||||
@@ -529,6 +560,16 @@ public class MixinEnvironment {
|
||||
public void setOption(Option option, boolean value) {
|
||||
this.options[option.ordinal()] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the specified option from the current environment
|
||||
*
|
||||
* @param option Option to get
|
||||
* @return Option value
|
||||
*/
|
||||
public String getOptionValue(Option option) {
|
||||
return option.getStringValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke a mixin environment audit process
|
||||
|
||||
@@ -35,6 +35,7 @@ import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.minecraft.launchwrapper.IClassTransformer;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
@@ -266,8 +267,16 @@ public class MixinTransformer extends TreeTransformer {
|
||||
*/
|
||||
private boolean errorState = false;
|
||||
|
||||
private final IDecompiler decompiler;
|
||||
/**
|
||||
* Directory to export classes to when debug.export is enabled
|
||||
*/
|
||||
private final File classExportDir = new File(MixinTransformer.DEBUG_OUTPUT, "class");
|
||||
|
||||
/**
|
||||
* Runtime decompiler for exported classes
|
||||
*/
|
||||
private final IDecompiler decompiler;
|
||||
|
||||
/**
|
||||
* ctor
|
||||
*/
|
||||
@@ -285,12 +294,19 @@ public class MixinTransformer extends TreeTransformer {
|
||||
TreeInfo.setLock(this.lock);
|
||||
|
||||
this.decompiler = this.initDecompiler(new File(MixinTransformer.DEBUG_OUTPUT, "java"));
|
||||
|
||||
try {
|
||||
FileUtils.deleteDirectory(this.classExportDir);
|
||||
} catch (IOException ex) {
|
||||
this.logger.warn("Error cleaning class output directory: {}", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private IDecompiler initDecompiler(File outputPath) {
|
||||
if (!MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
this.logger.info("Attempting to load Fernflower decompiler");
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -386,7 +402,7 @@ public class MixinTransformer extends TreeTransformer {
|
||||
basicClass = this.applyMixins(transformedName, basicClass, mixins);
|
||||
} catch (InvalidMixinException th) {
|
||||
if (environment.getOption(Option.DUMP_TARGET_ON_FAILURE)) {
|
||||
MixinTransformer.dumpClass(transformedName.replace('.', '/') + ".target", basicClass);
|
||||
this.dumpClass(transformedName.replace('.', '/') + ".target", basicClass);
|
||||
}
|
||||
|
||||
this.handleMixinErrorState(transformedName, th);
|
||||
@@ -396,7 +412,7 @@ public class MixinTransformer extends TreeTransformer {
|
||||
return basicClass;
|
||||
} catch (Exception ex) {
|
||||
if (environment.getOption(Option.DUMP_TARGET_ON_FAILURE)) {
|
||||
MixinTransformer.dumpClass(transformedName.replace('.', '/') + ".target", basicClass);
|
||||
this.dumpClass(transformedName.replace('.', '/') + ".target", basicClass);
|
||||
}
|
||||
|
||||
throw new MixinTransformerError("An unexpected critical error was encountered", ex);
|
||||
@@ -639,23 +655,36 @@ public class MixinTransformer extends TreeTransformer {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
private String prepareFilter(String filter) {
|
||||
filter = "^\\Q" + filter.replace("**", "\201").replace("*", "\202").replace("?", "\203") + "\\E$";
|
||||
return filter.replace("\201", "\\E.*\\Q").replace("\202", "\\E[^\\.]+\\Q").replace("\203", "\\E.\\Q").replace("\\Q\\E", "");
|
||||
}
|
||||
|
||||
private boolean applyFilter(String filter, String subject) {
|
||||
return Pattern.compile(this.prepareFilter(filter), Pattern.CASE_INSENSITIVE).matcher(subject).matches();
|
||||
}
|
||||
|
||||
private byte[] writeClass(String transformedName, ClassNode targetClass) {
|
||||
// Collapse tree to bytes
|
||||
byte[] bytes = this.writeClass(targetClass);
|
||||
|
||||
// Export transformed class for debugging purposes
|
||||
if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) {
|
||||
File outputFile = MixinTransformer.dumpClass(transformedName.replace('.', '/'), bytes);
|
||||
if (this.decompiler != null) {
|
||||
this.decompiler.decompile(outputFile);
|
||||
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
|
||||
if (environment.getOption(Option.DEBUG_EXPORT)) {
|
||||
String filter = environment.getOptionValue(Option.DEBUG_EXPORT_FILTER);
|
||||
if (filter == null || this.applyFilter(filter, transformedName)) {
|
||||
File outputFile = this.dumpClass(transformedName.replace('.', '/'), bytes);
|
||||
if (this.decompiler != null) {
|
||||
this.decompiler.decompile(outputFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static File dumpClass(String fileName, byte[] bytes) {
|
||||
File outputFile = new File(MixinTransformer.DEBUG_OUTPUT, "class/" + fileName + ".class");
|
||||
private File dumpClass(String fileName, byte[] bytes) {
|
||||
File outputFile = new File(this.classExportDir, fileName + ".class");
|
||||
try {
|
||||
FileUtils.writeByteArrayToFile(outputFile, bytes);
|
||||
} catch (IOException ex) {
|
||||
|
||||
Reference in new issue
Block a user