Skip to content

Commit

Permalink
add interactive conflict resolution to updateIntermediary
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Dec 15, 2018
1 parent 955d8e0 commit 41ea7d4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
77 changes: 46 additions & 31 deletions src/main/java/net/fabricmc/stitch/commands/GenState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import net.fabricmc.stitch.representation.*;
import net.fabricmc.stitch.util.MatcherUtil;
import net.fabricmc.stitch.util.StitchUtil;
import net.fabricmc.tinyremapper.TinyUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.Opcodes;
Expand All @@ -30,7 +31,12 @@ class GenState {
private final Map<Entry, Integer> values = new IdentityHashMap<>();
private GenMap oldToIntermediary, newToOld;
private GenMap newToIntermediary;
private boolean rewriteMode = false;
private boolean interactive = true;
private Scanner scanner = new Scanner(System.in);

public void disableInteractive() {
interactive = false;
}

public String next(Entry entry, String name) {
return name + "_" + values.computeIfAbsent(entry, (e) -> {
Expand Down Expand Up @@ -114,6 +120,14 @@ private String getFieldName(ClassStorage storage, ClassEntry c, FieldEntry f) {

private final Map<MethodEntry, String> methodNames = new IdentityHashMap<>();

private String getNamesListEntry(ClassEntry classEntry) {
StringBuilder builder = new StringBuilder(classEntry.getFullyQualifiedName());
if (classEntry.isInterface()) {
builder.append("(itf)");
}
return builder.toString();
}

@Nullable
private String getMethodName(ClassStorage storageOld, ClassStorage storageNew, ClassEntry c, MethodEntry m) {
if (!isMappedMethod(storageNew, c, m)) {
Expand All @@ -126,14 +140,14 @@ private String getMethodName(ClassStorage storageOld, ClassStorage storageNew, C

if (newToOld != null || newToIntermediary != null) {
List<ClassEntry> ccList = m.getMatchingEntries(storageNew, c);
Set<String> names = new HashSet<>();
Map<String, Set<String>> names = new HashMap<>();

for (ClassEntry cc : ccList) {
GenMap.DescEntry findEntry = null;
if (newToIntermediary != null) {
findEntry = newToIntermediary.getMethod(cc.getFullyQualifiedName(), m.getName(), m.getDescriptor());
if (findEntry != null) {
names.add(findEntry.getName());
names.computeIfAbsent(findEntry.getName(), (s) -> new TreeSet<>()).add(getNamesListEntry(cc));
}
}

Expand All @@ -143,7 +157,7 @@ private String getMethodName(ClassStorage storageOld, ClassStorage storageNew, C
GenMap.DescEntry newToOldEntry = findEntry;
findEntry = oldToIntermediary.getMethod(newToOldEntry);
if (findEntry != null) {
names.add(findEntry.getName());
names.computeIfAbsent(findEntry.getName(), (s) -> new TreeSet<>()).add(getNamesListEntry(cc));
} else {
// more involved...
ClassEntry oldBase = storageOld.getClass(newToOldEntry.getOwner(), false);
Expand All @@ -154,7 +168,7 @@ private String getMethodName(ClassStorage storageOld, ClassStorage storageNew, C
for (ClassEntry ccc : cccList) {
findEntry = oldToIntermediary.getMethod(ccc.getFullyQualifiedName(), oldM.getName(), oldM.getDescriptor());
if (findEntry != null) {
names.add(findEntry.getName());
names.computeIfAbsent(findEntry.getName(), (s) -> new TreeSet<>()).add(getNamesListEntry(ccc));
}
}
}
Expand All @@ -164,33 +178,36 @@ private String getMethodName(ClassStorage storageOld, ClassStorage storageNew, C
}

if (names.size() > 1) {
if (rewriteMode) {
int lowestNum = Integer.MAX_VALUE;
for (String s : names) {
if (!s.startsWith("method_")) {
throw new RuntimeException("Could not rewrite method: " + s);
}

int v = new Integer(s.split("_")[1]);
if (v < lowestNum) {
lowestNum = v;
}
}
return "method_" + lowestNum;
}

StringBuilder builder = new StringBuilder("Conflict: ");
int i = 0;
for (String s : names) {
if ((i++) > 0) {
builder.append(", ");
}
builder.append(s);
System.out.println("Conflict detected - matched same target name!");
List<String> nameList = new ArrayList<>(names.keySet());

for (int i = 0; i < nameList.size(); i++) {
String s = nameList.get(i);
System.out.println((i+1) + ") " + s + " <- " + StitchUtil.join(", ", names.get(s)));
}

throw new RuntimeException(builder.toString());
if (!interactive) {
throw new RuntimeException("Conflict detected!");
}

while (true) {
String cmd = scanner.nextLine();
int i;
try {
i = Integer.parseInt(cmd);
} catch (NumberFormatException e) {
e.printStackTrace();
continue;
}

if (i >= 1 && i <= nameList.size()) {
methodNames.put(m, nameList.get(i - 1));
System.out.println("OK!");
break;
}
}
} else if (names.size() == 1) {
String s = names.iterator().next();
String s = names.keySet().iterator().next();
methodNames.put(m, s);
return s;
}
Expand Down Expand Up @@ -291,8 +308,6 @@ public void prepareRewrite(File oldMappings) throws IOException {
TinyUtils.read(reader, "official", "intermediary", oldToIntermediary::addClass, oldToIntermediary::addField, oldToIntermediary::addMethod);
}
}

rewriteMode = true;
}

public void prepareUpdate(File oldMappings, File matches) throws IOException {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/fabricmc/stitch/util/StitchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ private StitchUtil() {

}

public static String join(String joiner, Collection<String> c) {
StringBuilder builder = new StringBuilder();
int i = 0;
for (String s : c) {
if ((i++) > 0) {
builder.append(joiner);
}

builder.append(s);
}
return builder.toString();
}

public static <T> Set<T> newIdentityHashSet() {
return Collections.newSetFromMap(new IdentityHashMap<>());
}
Expand Down

0 comments on commit 41ea7d4

Please sign in to comment.