Skip to content

Commit

Permalink
Merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Mar 10, 2024
2 parents 6e5d676 + da3d2b9 commit dd8eb1b
Show file tree
Hide file tree
Showing 34 changed files with 824 additions and 372 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ javafx {
dependencies {
api "org.ow2.asm:asm:${asm_version}"
api "org.ow2.asm:asm-tree:${asm_version}"
api "org.slf4j:slf4j-api:${slf4j_version}"
api "net.fabricmc:mapping-io:${mappingio_version}"
implementation "org.ow2.asm:asm-commons:${asm_version}"
implementation "org.ow2.asm:asm-util:${asm_version}"
implementation "com.github.javaparser:javaparser-core:${javaparser_version}"
implementation "net.fabricmc:cfr:${fabric_cfr_version}"
implementation "org.vineflower:vineflower:${vineflower_version}"
implementation "org.bitbucket.mstrobel:procyon-compilertools:${procyon_version}"
runtimeOnly "org.tinylog:tinylog-impl:${tinylog_version}"
runtimeOnly "org.tinylog:slf4j-tinylog:${tinylog_version}"

// JavaFX for all platforms (needed for cross-platform fat jar)
runtimeOnly "org.openjfx:javafx-base:${javafx_version}:win"
Expand Down Expand Up @@ -92,6 +95,10 @@ jar {
}
}

jar {
processResources.exclude('tinylog-dev.properties')
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ mappingio_version = 0.5.0
javaparser_version = 3.25.6
javafx_version = 21.0.1
checkstyle_version = 10.12.5
slf4j_version = 2.0.12
tinylog_version = 2.7.0
4 changes: 2 additions & 2 deletions src/main/java/matcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

public class Main {
public static void main(String[] args) {
Config.init();
PluginLoader.run();
Config.init(args);
PluginLoader.run(args);
Application.launch(Gui.class, args);
}
}
37 changes: 24 additions & 13 deletions src/main/java/matcher/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import matcher.classifier.ClassClassifier;
import matcher.classifier.ClassifierLevel;
import matcher.classifier.FieldClassifier;
Expand Down Expand Up @@ -103,8 +106,15 @@ public void initFromMatches(List<Path> inputDirs,
List<Path> classPathA = resolvePaths(inputDirs, cpFilesA);
List<Path> classPathB = resolvePaths(inputDirs, cpFilesB);

ProjectConfig config = new ProjectConfig(pathsA, pathsB, classPathA, classPathB, sharedClassPath, false,
nonObfuscatedClassPatternA, nonObfuscatedClassPatternB, nonObfuscatedMemberPatternA, nonObfuscatedMemberPatternB);
ProjectConfig config = new ProjectConfig.Builder(pathsA, pathsB)
.classPathA(new ArrayList<>(classPathA))
.classPathB(new ArrayList<>(classPathB))
.sharedClassPath(new ArrayList<>(sharedClassPath))
.nonObfuscatedClassPatternA(nonObfuscatedClassPatternA)
.nonObfuscatedClassPatternB(nonObfuscatedClassPatternB)
.nonObfuscatedMemberPatternA(nonObfuscatedMemberPatternA)
.nonObfuscatedMemberPatternB(nonObfuscatedMemberPatternB)
.build();
if (!config.isValid()) throw new IOException("invalid config");
Config.setProjectConfig(config);
Config.saveAsLast();
Expand Down Expand Up @@ -166,7 +176,7 @@ public void match(ClassInstance a, ClassInstance b) {
if (a.getArrayDimensions() != b.getArrayDimensions()) throw new IllegalArgumentException("the classes don't have the same amount of array dimensions");
if (a.getMatch() == b) return;

System.out.println("match class "+a+" -> "+b+(a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Matching class {} -> {}{}", a, b, (a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));

if (a.getMatch() != null) {
a.getMatch().setMatch(null);
Expand Down Expand Up @@ -293,7 +303,7 @@ public void match(MethodInstance a, MethodInstance b) {
if (a.getCls().getMatch() != b.getCls()) throw new IllegalArgumentException("the methods don't belong to the same class");
if (a.getMatch() == b) return;

System.out.println("match method "+a+" -> "+b+(a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Matching method {} -> {}{}", a, b, (a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));

Set<MethodInstance> membersA = a.getAllHierarchyMembers();
Set<MethodInstance> membersB = b.getAllHierarchyMembers();
Expand Down Expand Up @@ -362,7 +372,7 @@ public void match(FieldInstance a, FieldInstance b) {
if (a.getCls().getMatch() != b.getCls()) throw new IllegalArgumentException("the methods don't belong to the same class");
if (a.getMatch() == b) return;

System.out.println("match field "+a+" -> "+b+(a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Matching field {} -> {}{}", a, b, (a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));

if (a.getMatch() != null) a.getMatch().setMatch(null);
if (b.getMatch() != null) b.getMatch().setMatch(null);
Expand All @@ -380,7 +390,7 @@ public void match(MethodVarInstance a, MethodVarInstance b) {
if (a.isArg() != b.isArg()) throw new IllegalArgumentException("the method vars are not of the same kind");
if (a.getMatch() == b) return;

System.out.println("match method arg "+a+" -> "+b+(a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Matching method arg {} -> {}{}", a, b, (a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));

if (a.getMatch() != null) a.getMatch().setMatch(null);
if (b.getMatch() != null) b.getMatch().setMatch(null);
Expand All @@ -395,7 +405,7 @@ public void unmatch(ClassInstance cls) {
if (cls == null) throw new NullPointerException("null class");
if (cls.getMatch() == null) return;

System.out.println("unmatch class "+cls+" (was "+cls.getMatch()+")"+(cls.hasMappedName() ? " ("+cls.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Unmatching class {} (was {}){}", cls, cls.getMatch(), (cls.hasMappedName() ? " ("+cls.getName(NameType.MAPPED_PLAIN)+")" : ""));

cls.getMatch().setMatch(null);
cls.setMatch(null);
Expand All @@ -417,7 +427,7 @@ public void unmatch(MemberInstance<?> m) {
if (m == null) throw new NullPointerException("null member");
if (m.getMatch() == null) return;

System.out.println("unmatch member "+m+" (was "+m.getMatch()+")"+(m.hasMappedName() ? " ("+m.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Unmatching member {} (was {}){}", m, m.getMatch(), (m.hasMappedName() ? " ("+m.getName(NameType.MAPPED_PLAIN)+")" : ""));

if (m instanceof MethodInstance) {
for (MethodVarInstance arg : ((MethodInstance) m).getArgs()) {
Expand Down Expand Up @@ -445,7 +455,7 @@ public void unmatch(MethodVarInstance a) {
if (a == null) throw new NullPointerException("null method var");
if (a.getMatch() == null) return;

System.out.println("unmatch method var "+a+" (was "+a.getMatch()+")"+(a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));
LOGGER.debug("Unmatching method var {} (was {}){}", a, a.getMatch(), (a.hasMappedName() ? " ("+a.getName(NameType.MAPPED_PLAIN)+")" : ""));

a.getMatch().setMatch(null);
a.setMatch(null);
Expand Down Expand Up @@ -524,7 +534,7 @@ public boolean autoMatchClasses(ClassifierLevel level, double absThreshold, doub
match(entry.getKey(), entry.getValue());
}

System.out.println("Auto matched "+matches.size()+" classes ("+(classes.size() - matches.size())+" unmatched, "+env.getClassesA().size()+" total)");
LOGGER.info("Auto matched {} classes ({} unmatched, {} total)", matches.size(), (classes.size() - matches.size()), env.getClassesA().size());

return !matches.isEmpty();
}
Expand Down Expand Up @@ -570,7 +580,7 @@ public boolean autoMatchMethods(ClassifierLevel level, double absThreshold, doub
match(entry.getKey(), entry.getValue());
}

System.out.println("Auto matched "+matches.size()+" methods ("+totalUnmatched.get()+" unmatched)");
LOGGER.info("Auto matched {} methods ({} unmatched)", matches.size(), totalUnmatched.get());

return !matches.isEmpty();
}
Expand All @@ -591,7 +601,7 @@ public boolean autoMatchFields(ClassifierLevel level, double absThreshold, doubl
match(entry.getKey(), entry.getValue());
}

System.out.println("Auto matched "+matches.size()+" fields ("+totalUnmatched.get()+" unmatched)");
LOGGER.info("Auto matched {} fields ({} unmatched)", matches.size(), totalUnmatched.get());

return !matches.isEmpty();
}
Expand Down Expand Up @@ -706,7 +716,7 @@ private boolean autoMatchMethodVars(boolean isArg, Function<MethodInstance, Meth
match(entry.getKey(), entry.getValue());
}

System.out.println("Auto matched "+matches.size()+" method "+(isArg ? "arg" : "var")+"s ("+totalUnmatched.get()+" unmatched)");
LOGGER.info("Auto matched {} method {}s ({} unmatched)", matches.size(), (isArg ? "arg" : "var"), totalUnmatched.get());

return !matches.isEmpty();
}
Expand Down Expand Up @@ -836,6 +846,7 @@ public static class MatchingStatus {
}

public static final ExecutorService threadPool = Executors.newWorkStealingPool();
public static final Logger LOGGER = LoggerFactory.getLogger("Matcher");

private final ClassEnvironment env;
private final ClassifierLevel autoMatchLevel = ClassifierLevel.Extra;
Expand Down
57 changes: 40 additions & 17 deletions src/main/java/matcher/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,57 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class PluginLoader {
public static void run() {
Path pluginFolder = Paths.get("plugins");
URL[] urls = new URL[0];

if (Files.isDirectory(pluginFolder)) {
try (Stream<Path> stream = Files.list(pluginFolder)) {
urls = stream
.filter(p -> p.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
.map(p -> {
try {
return p.toUri().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList()).toArray(urls);
public static void run(String[] args) {
List<Path> pluginPaths = new ArrayList<>();
pluginPaths.add(Paths.get("plugins"));

for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--additional-plugins":
while (i+1 < args.length && !args[i+1].startsWith("--")) {
pluginPaths.add(Path.of(args[++i]));
}

break;
}
}

List<URL> urls = new ArrayList<>();

for (Path path : pluginPaths) {
try {
if (Files.isDirectory(path)) {
Stream<Path> stream = Files.list(path);
urls.addAll(stream
.filter(p -> p.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
.map(p -> {
try {
return p.toUri().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList()));
stream.close();
} else if (path.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
urls.add(path.toUri().toURL());
} else {
System.err.println("No plugin(s) found at " + path.toFile().getCanonicalPath());
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

URLClassLoader cl = new URLClassLoader(urls);
URLClassLoader cl = new URLClassLoader(urls.toArray(new URL[0]));

ServiceLoader<Plugin> pluginLoader = ServiceLoader.load(Plugin.class, cl);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/matcher/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ public static Handle getTargetHandle(Handle bsm, Object[] bsmArgs) {
} else if (isIrrelevantBsm(bsm)) {
return null;
} else {
System.out.printf("unknown invokedynamic bsm: %s/%s%s (tag=%d iif=%b)%n", bsm.getOwner(), bsm.getName(), bsm.getDesc(), bsm.getTag(), bsm.isInterface());
Matcher.LOGGER.warn("Unknown invokedynamic bsm: {}/{}{} (tag={} iif={})",
bsm.getOwner(), bsm.getName(), bsm.getDesc(), bsm.getTag(), bsm.isInterface());

return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/matcher/classifier/ClassClassifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public double getScore(ClassInstance clsA, ClassInstance clsB, ClassEnvironment
@Override
public double getScore(ClassInstance clsA, ClassInstance clsB, ClassEnvironment env) {
/*if (clsA.getName().equals("agl") && clsB.getName().equals("aht")) {
System.out.println();
Matcher.LOGGER.info();
}*/

final double absThreshold = 0.8;
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/matcher/classifier/ClassifierUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceMethodVisitor;

import matcher.Matcher;
import matcher.Util;
import matcher.classifier.MatchingCache.CacheToken;
import matcher.type.ClassEnvironment;
Expand Down Expand Up @@ -377,10 +378,11 @@ private static <T> int compareInsns(AbstractInsnNode insnA, AbstractInsnNode ins
implB.getOwner(), implB.getName(), implB.getDesc(), Util.isCallToInterface(implB),
env) ? COMPARED_SIMILAR : COMPARED_DISTINCT;
default:
System.out.println("unexpected impl tag: "+implA.getTag());
Matcher.LOGGER.warn("Unexpected impl tag: {}", implA.getTag());
}
} else if (!Util.isIrrelevantBsm(a.bsm)) {
System.out.printf("unknown invokedynamic bsm: %s/%s%s (tag=%d iif=%b)%n", a.bsm.getOwner(), a.bsm.getName(), a.bsm.getDesc(), a.bsm.getTag(), a.bsm.isInterface());
Matcher.LOGGER.warn("Unknown invokedynamic bsm: {}/{}{} (tag={} iif={})",
a.bsm.getOwner(), a.bsm.getName(), a.bsm.getDesc(), a.bsm.getTag(), a.bsm.isInterface());
}

// TODO: implement
Expand Down Expand Up @@ -622,10 +624,10 @@ private static <T, U> int[] mapLists(T listA, T listB, ListElementRetriever<T, U

/*for (int j = 0; j <= sizeB; j++) {
for (int i = 0; i <= sizeA; i++) {
System.out.printf("%2d ", v[i + j * size]);
Matcher.LOGGER.debug("%2d ", v[i + j * size]);
}
System.out.println();
Matcher.LOGGER.debug("");
}*/

int i = sizeA;
Expand All @@ -641,10 +643,10 @@ private static <T, U> int[] mapLists(T listA, T listB, ListElementRetriever<T, U
if (keepCost <= delCost && keepCost <= insCost) {
if (c - keepCost >= COMPARED_DISTINCT) {
assert c - keepCost == COMPARED_DISTINCT;
//System.out.printf("%d/%d rep %s -> %s%n", i-1, j-1, toString(elementRetriever.apply(listA, i - 1)), toString(elementRetriever.apply(listB, j - 1)));
//Matcher.LOGGER.debug("{}/{} rep {} -> {}", i-1, j-1, toString(elementRetriever.apply(listA, i - 1)), toString(elementRetriever.apply(listB, j - 1)));
ret[i - 1] = -1;
} else {
//System.out.printf("%d/%d eq %s - %s%n", i-1, j-1, toString(elementRetriever.apply(listA, i - 1)), toString(elementRetriever.apply(listB, j - 1)));
//Matcher.LOGGER.debug("{}/{} eq {} - {}", i-1, j-1, toString(elementRetriever.apply(listA, i - 1)), toString(elementRetriever.apply(listB, j - 1)));
ret[i - 1] = j - 1;

/*U e = elementRetriever.apply(listA, i - 1);
Expand All @@ -658,11 +660,11 @@ private static <T, U> int[] mapLists(T listA, T listB, ListElementRetriever<T, U
i--;
j--;
} else if (delCost < insCost) {
//System.out.printf("%d/%d del %s%n", i-1, j-1, toString(elementRetriever.apply(listA, i - 1)));
//Matcher.LOGGER.debug("{}/{} del {}", i-1, j-1, toString(elementRetriever.apply(listA, i - 1)));
ret[i - 1] = -1;
i--;
} else {
//System.out.printf("%d/%d ins %s%n", i-1, j-1, toString(elementRetriever.apply(listB, j - 1)));
//Matcher.LOGGER.debug("{}/{} ins {}", i-1, j-1, toString(elementRetriever.apply(listB, j - 1)));
j--;
}
}
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/matcher/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import java.util.prefs.Preferences;

public class Config {
public static void init() {
public static void init(String[] args) {
Preferences prefs = Preferences.userRoot(); // in ~/.java/.userPrefs

try {
if (prefs.nodeExists(userPrefFolder)) {
prefs = prefs.node(userPrefFolder);

if (prefs.nodeExists(lastProjectSetupKey)) setProjectConfig(new ProjectConfig(prefs.node(lastProjectSetupKey)));
if (prefs.nodeExists(lastProjectSetupKey)) setProjectConfig(new ProjectConfig.Builder(prefs.node(lastProjectSetupKey)).build());
setInputDirs(loadList(prefs, lastInputDirsKey, Config::deserializePath));
setVerifyInputFiles(prefs.getBoolean(lastVerifyInputFilesKey, true));
setUidConfig(new UidConfig(prefs));
Expand All @@ -26,6 +26,22 @@ public static void init() {
} catch (BackingStoreException e) {
// ignored
}

for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--theme":
String themeId = args[++i];
Theme theme = Theme.getById(themeId);

if (theme == null) {
System.err.println("Startup arg '--theme' couldn't be applied, as there exists no theme with ID " + themeId + "!");
} else {
setTheme(theme);
}

break;
}
}
}

private Config() { }
Expand Down Expand Up @@ -78,11 +94,10 @@ public static boolean setUidConfig(UidConfig config) {
public static void setTheme(Theme value) {
if (value != null) {
theme = value;
saveTheme();
}
}

private static void saveTheme() {
public static void saveTheme() {
Preferences root = Preferences.userRoot().node(userPrefFolder);

try {
Expand Down Expand Up @@ -140,7 +155,7 @@ static Path deserializePath(String path) {
private static final String lastVerifyInputFilesKey = "last-verify-input-files";
private static final String themeKey = "theme";

private static ProjectConfig projectConfig = new ProjectConfig();
private static ProjectConfig projectConfig = ProjectConfig.EMPTY;
private static final List<Path> inputDirs = new ArrayList<>();
private static boolean verifyInputFiles = true;
private static UidConfig uidConfig = new UidConfig();
Expand Down
Loading

0 comments on commit dd8eb1b

Please sign in to comment.