Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Un-hardcode field and method obfuscation patterns #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CommandGenerateIntermediary() {

@Override
public String getHelpString() {
return "<input-jar> <mapping-name> [-t|--target-namespace <namespace>] [-p|--obfuscation-pattern <regex pattern>]...";
return "<input-jar> <mapping-name> [-t|--target-namespace <namespace>] [-p|--obfuscation-class-pattern <regex pattern>]... [-f|--obfuscation-field-pattern <regex pattern>]... [-m|--obfuscation-method-pattern <regex pattern>]...";
}

@Override
Expand All @@ -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)) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CommandUpdateIntermediary() {

@Override
public String getHelpString() {
return "<old-jar> <new-jar> <old-mapping-file> <new-mapping-file> <match-file> [-t|--target-namespace <namespace>] [-p|--obfuscation-pattern <regex pattern>]";
return "<old-jar> <new-jar> <old-mapping-file> <new-mapping-file> <match-file> [-t|--target-namespace <namespace>] [-p|--obfuscation-class-pattern <regex pattern>]... [-f|--obfuscation-field-pattern <regex pattern>]... [-m|--obfuscation-method-pattern <regex pattern>]...";
}

@Override
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
46 changes: 33 additions & 13 deletions src/main/java/net/fabricmc/stitch/commands/GenState.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ class GenState {
private Scanner scanner = new Scanner(System.in);

private String targetNamespace = "net/minecraft/";
private final List<Pattern> obfuscatedPatterns = new ArrayList<Pattern>();
private final List<Pattern> obfuscatedClassPatterns = new ArrayList<>();
private final List<Pattern> obfuscatedFieldPatterns = new ArrayList<>();
private final List<Pattern> 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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down