From 1d3bc6ea2450953db2e2a22bc3ab81d8b1d510a4 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 24 Aug 2020 11:49:31 +0100 Subject: [PATCH] Un-hardcode field and method obfuscation patterns --- .../commands/CommandGenerateIntermediary.java | 33 ++++++++++--- .../commands/CommandUpdateIntermediary.java | 33 ++++++++++--- .../fabricmc/stitch/commands/GenState.java | 46 +++++++++++++------ 3 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/fabricmc/stitch/commands/CommandGenerateIntermediary.java b/src/main/java/net/fabricmc/stitch/commands/CommandGenerateIntermediary.java index e1e6dd4..ba5db61 100644 --- a/src/main/java/net/fabricmc/stitch/commands/CommandGenerateIntermediary.java +++ b/src/main/java/net/fabricmc/stitch/commands/CommandGenerateIntermediary.java @@ -29,7 +29,7 @@ public CommandGenerateIntermediary() { @Override public String getHelpString() { - return " [-t|--target-namespace ] [-p|--obfuscation-pattern ]..."; + return " [-t|--target-namespace ] [-p|--obfuscation-class-pattern ]... [-f|--obfuscation-field-pattern ]... [-m|--obfuscation-method-pattern ]..."; } @Override @@ -49,7 +49,9 @@ public void run(String[] args) throws Exception { } GenState state = new GenState(); - boolean clearedPatterns = false; + boolean clearedClassPatterns = false; + boolean clearedFieldPatterns = false; + boolean clearedMethodPatterns = false; for (int i = 2; i < args.length; i++) { switch (args[i].toLowerCase(Locale.ROOT)) { @@ -60,11 +62,30 @@ public void run(String[] args) throws Exception { break; case "-p": case "--obfuscation-pattern": - if (!clearedPatterns) - state.clearObfuscatedPatterns(); - clearedPatterns = true; + case "--obfuscated-class-pattern": + if (!clearedClassPatterns) + state.clearObfuscatedClassPatterns(); + clearedClassPatterns = true; - state.addObfuscatedPattern(args[i + 1]); + state.addObfuscatedClassPattern(args[i + 1]); + i++; + break; + case "-f": + case "--obfuscation-field-pattern": + if (!clearedFieldPatterns) + state.clearObfuscatedFieldPatterns(); + clearedFieldPatterns = true; + + state.addObfuscatedFieldPattern(args[i + 1]); + i++; + break; + case "-m": + case "--obfuscation-method-pattern": + if (!clearedMethodPatterns) + state.clearObfuscatedMethodPatterns(); + clearedMethodPatterns = true; + + state.addObfuscatedMethodPattern(args[i + 1]); i++; break; } diff --git a/src/main/java/net/fabricmc/stitch/commands/CommandUpdateIntermediary.java b/src/main/java/net/fabricmc/stitch/commands/CommandUpdateIntermediary.java index 59a1265..a9908f3 100644 --- a/src/main/java/net/fabricmc/stitch/commands/CommandUpdateIntermediary.java +++ b/src/main/java/net/fabricmc/stitch/commands/CommandUpdateIntermediary.java @@ -30,7 +30,7 @@ public CommandUpdateIntermediary() { @Override public String getHelpString() { - return " [-t|--target-namespace ] [-p|--obfuscation-pattern ]"; + return " [-t|--target-namespace ] [-p|--obfuscation-class-pattern ]... [-f|--obfuscation-field-pattern ]... [-m|--obfuscation-method-pattern ]..."; } @Override @@ -59,7 +59,9 @@ public void run(String[] args) throws Exception { } GenState state = new GenState(); - boolean clearedPatterns = false; + boolean clearedClassPatterns = false; + boolean clearedFieldPatterns = false; + boolean clearedMethodPatterns = false; for (int i = 5; i < args.length; i++) { switch (args[i].toLowerCase(Locale.ROOT)) { @@ -70,11 +72,30 @@ public void run(String[] args) throws Exception { break; case "-p": case "--obfuscation-pattern": - if (!clearedPatterns) - state.clearObfuscatedPatterns(); - clearedPatterns = true; + case "--obfuscated-class-pattern": + if (!clearedClassPatterns) + state.clearObfuscatedClassPatterns(); + clearedClassPatterns = true; - state.addObfuscatedPattern(args[i + 1]); + state.addObfuscatedClassPattern(args[i + 1]); + i++; + break; + case "-f": + case "--obfuscation-field-pattern": + if (!clearedFieldPatterns) + state.clearObfuscatedFieldPatterns(); + clearedFieldPatterns = true; + + state.addObfuscatedFieldPattern(args[i + 1]); + i++; + break; + case "-m": + case "--obfuscation-method-pattern": + if (!clearedMethodPatterns) + state.clearObfuscatedMethodPatterns(); + clearedMethodPatterns = true; + + state.addObfuscatedMethodPattern(args[i + 1]); i++; break; } diff --git a/src/main/java/net/fabricmc/stitch/commands/GenState.java b/src/main/java/net/fabricmc/stitch/commands/GenState.java index beab5d8..49294f3 100644 --- a/src/main/java/net/fabricmc/stitch/commands/GenState.java +++ b/src/main/java/net/fabricmc/stitch/commands/GenState.java @@ -45,10 +45,14 @@ class GenState { private Scanner scanner = new Scanner(System.in); private String targetNamespace = "net/minecraft/"; - private final List obfuscatedPatterns = new ArrayList(); + private final List obfuscatedClassPatterns = new ArrayList<>(); + private final List obfuscatedFieldPatterns = new ArrayList<>(); + private final List obfuscatedMethodPatterns = new ArrayList<>(); public GenState() { - this.obfuscatedPatterns.add(Pattern.compile("^[^/]*$")); // Default ofbfuscation. Minecraft classes without a package are obfuscated. + this.obfuscatedClassPatterns.add(Pattern.compile("^[^/]*$")); // Default ofbfuscation. Minecraft classes without a package are obfuscated. + this.obfuscatedFieldPatterns.add(Pattern.compile("^.{0,2}_?$")); + this.obfuscatedMethodPatterns.add(Pattern.compile("^[^<].?_?$")); } public void setWriteAll(boolean writeAll) { @@ -74,12 +78,28 @@ public void setTargetNamespace(final String namespace) { this.targetNamespace = namespace; } - public void clearObfuscatedPatterns() { - this.obfuscatedPatterns.clear(); + public void clearObfuscatedClassPatterns() { + this.obfuscatedClassPatterns.clear(); } - public void addObfuscatedPattern(String regex) throws PatternSyntaxException { - this.obfuscatedPatterns.add(Pattern.compile(regex)); + public void addObfuscatedClassPattern(String regex) throws PatternSyntaxException { + this.obfuscatedClassPatterns.add(Pattern.compile(regex)); + } + + public void clearObfuscatedFieldPatterns() { + this.obfuscatedFieldPatterns.clear(); + } + + public void addObfuscatedFieldPattern(String regex) throws PatternSyntaxException { + this.obfuscatedFieldPatterns.add(Pattern.compile(regex)); + } + + public void clearObfuscatedMethodPatterns() { + this.obfuscatedMethodPatterns.clear(); + } + + public void addObfuscatedMethodPattern(String regex) throws PatternSyntaxException { + this.obfuscatedMethodPatterns.add(Pattern.compile(regex)); } public void setCounter(String key, int value) { @@ -120,20 +140,20 @@ public static boolean isMappedClass(ClassStorage storage, JarClassEntry c) { return !c.isAnonymous(); } - public static boolean isMappedField(ClassStorage storage, JarClassEntry c, JarFieldEntry f) { + public boolean isMappedField(ClassStorage storage, JarClassEntry c, JarFieldEntry f) { return isUnmappedFieldName(f.getName()); } - public static boolean isUnmappedFieldName(String name) { - return name.length() <= 2 || (name.length() == 3 && name.charAt(2) == '_'); + public boolean isUnmappedFieldName(String name) { + return this.obfuscatedFieldPatterns.stream().anyMatch(p -> p.matcher(name).matches()); } - public static boolean isMappedMethod(ClassStorage storage, JarClassEntry c, JarMethodEntry m) { + public boolean isMappedMethod(ClassStorage storage, JarClassEntry c, JarMethodEntry m) { return isUnmappedMethodName(m.getName()) && m.isSource(storage, c); } - public static boolean isUnmappedMethodName(String name) { - return (name.length() <= 2 || (name.length() == 3 && name.charAt(2) == '_')) && name.charAt(0) != '<'; + public boolean isUnmappedMethodName(String name) { + return this.obfuscatedMethodPatterns.stream().anyMatch(p -> p.matcher(name).matches()); } @Nullable @@ -353,7 +373,7 @@ private void addClass(BufferedWriter writer, JarClassEntry c, ClassStorage stora String cname = ""; String prefixSaved = translatedPrefix; - if(!this.obfuscatedPatterns.stream().anyMatch(p -> p.matcher(className).matches())) { + if(!this.obfuscatedClassPatterns.stream().anyMatch(p -> p.matcher(className).matches())) { translatedPrefix = c.getFullyQualifiedName(); } else { if (!isMappedClass(storage, c)) {