Skip to content

Commit

Permalink
Use IntermediaryType enum instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Jun 20, 2023
1 parent e1408d4 commit 51757d3
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions src/main/java/net/fabricmc/stitch/commands/GenState.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
Expand Down Expand Up @@ -69,7 +70,7 @@ class GenState {
private static final int officialIndex = -1;
private static final int intermediaryIndex = 0;
private final MemoryMappingTree mappingTree = new MemoryMappingTree();
private final Map<String, Integer> counters = new HashMap<>();
private final Map<IntermediaryType, Integer> counters = new HashMap<>();
private final Map<AbstractJarEntry, Integer> values = new IdentityHashMap<>();
private MappingFormat counterFileFormat = MappingFormat.TINY;
private MappingFormat mappingFileFormat = MappingFormat.TINY;
Expand All @@ -80,6 +81,18 @@ class GenState {
private Scanner scanner = new Scanner(System.in);
private String targetPackage = "";

public enum IntermediaryType {
CLASS("class"),
FIELD("field"),
METHOD("method");

public final String prefix;

IntermediaryType(String prefix) {
this.prefix = prefix;
}
}

GenState() throws IOException {
mappingTree.visitNamespaces(official, Arrays.asList(intermediary));

Expand Down Expand Up @@ -131,19 +144,19 @@ public void disableInteractive() {
interactive = false;
}

public String next(AbstractJarEntry entry, String name) {
return name + "_" + values.computeIfAbsent(entry, (e) -> {
int v = counters.getOrDefault(name, 1);
counters.put(name, v + 1);
public String next(AbstractJarEntry entry, IntermediaryType type) {
return type.prefix + "_" + values.computeIfAbsent(entry, (e) -> {
int v = counters.getOrDefault(type, 1);
counters.put(type, v + 1);
return v;
});
}

public void setCounter(String key, int value) {
public void setCounter(IntermediaryType key, int value) {
counters.put(key, value);
}

public Map<String, Integer> getCounters() {
public Map<IntermediaryType, Integer> getCounters() {
return Collections.unmodifiableMap(counters);
}

Expand Down Expand Up @@ -242,16 +255,16 @@ private String getFieldName(ClassStorage storage, JarClassEntry c, JarFieldEntry
}

if (existingName != null) {
if (existingName.contains("field_")) {
if (existingName.contains(IntermediaryType.FIELD.prefix + "_")) {
return existingName;
} else {
String newName = next(f, "field");
String newName = next(f, IntermediaryType.FIELD);
System.out.println(existingName + " is now " + newName);
return newName;
}
}

return next(f, "field");
return next(f, IntermediaryType.FIELD);
}

private final Map<JarMethodEntry, String> methodNames = new IdentityHashMap<>();
Expand Down Expand Up @@ -431,17 +444,17 @@ private String getMethodName(ClassStorage storageOld, ClassStorage storageNew, J
methodNames.put(mm, s);
}

if (s.contains("method_")) {
if (s.contains(IntermediaryType.METHOD.prefix + "_")) {
return s;
} else {
String newName = next(m, "method");
String newName = next(m, IntermediaryType.METHOD);
System.out.println(s + " is now " + newName);
return newName;
}
}
}

return next(m, "method");
return next(m, IntermediaryType.METHOD);
}

private MappingTree addClass(JarClassEntry c, ClassStorage storageOld, ClassStorage storage, String prefix) throws IOException {
Expand Down Expand Up @@ -485,12 +498,12 @@ private MappingTree addClass(JarClassEntry c, ClassStorage storageOld, ClassStor
}
}

if (cName != null && !cName.contains("class_")) {
System.out.println(cName + " is now " + (cName = next(c, "class")));
if (cName != null && !cName.contains(IntermediaryType.CLASS.prefix + "_")) {
System.out.println(cName + " is now " + (cName = next(c, IntermediaryType.CLASS)));
prefix = origPrefix;
anyIntermediaries = true;
} else if (cName == null) {
cName = next(c, "class");
cName = next(c, IntermediaryType.CLASS);
anyIntermediaries = true;
}
}
Expand Down Expand Up @@ -598,7 +611,14 @@ private void readCounterFileIfPresent() throws IOException {
while ((line = reader.readLine()) != null) {
if (line.startsWith("# INTERMEDIARY-COUNTER")) {
String[] parts = line.split(" ");
counters.put(parts[2], Integer.parseInt(parts[3]));
IntermediaryType type;

try {
type = IntermediaryType.valueOf(parts[2].toUpperCase(Locale.ROOT));
counters.put(type, Integer.parseInt(parts[3]));
} catch (IllegalArgumentException e) {
System.err.println("Encountered unknown intermediary type '" + parts[2] + "'");
}
}
}
}
Expand All @@ -607,13 +627,13 @@ private void readCounterFileIfPresent() throws IOException {

private void readCountersFromTree(MappingTree tree) {
String counter = tree.getMetadata("next-intermediary-class");
if (counter != null) counters.put("class", Integer.parseInt(counter));
if (counter != null) counters.put(IntermediaryType.CLASS, Integer.parseInt(counter));

counter = tree.getMetadata("next-intermediary-field");
if (counter != null) counters.put("field", Integer.parseInt(counter));
if (counter != null) counters.put(IntermediaryType.FIELD, Integer.parseInt(counter));

counter = tree.getMetadata("next-intermediary-method");
if (counter != null) counters.put("method", Integer.parseInt(counter));
if (counter != null) counters.put(IntermediaryType.METHOD, Integer.parseInt(counter));
}

private void writeCounters() throws IOException {
Expand All @@ -628,9 +648,9 @@ private void writeCounters() throws IOException {
mappingTree.visitNamespaces(official, Arrays.asList(intermediary));
}

mappingTree.visitMetadata("next-intermediary-class", counters.getOrDefault("class", 0).toString());
mappingTree.visitMetadata("next-intermediary-field", counters.getOrDefault("field", 0).toString());
mappingTree.visitMetadata("next-intermediary-method", counters.getOrDefault("method", 0).toString());
mappingTree.visitMetadata("next-intermediary-class", counters.getOrDefault(IntermediaryType.CLASS, 0).toString());
mappingTree.visitMetadata("next-intermediary-field", counters.getOrDefault(IntermediaryType.FIELD, 0).toString());
mappingTree.visitMetadata("next-intermediary-method", counters.getOrDefault(IntermediaryType.METHOD, 0).toString());

if (counterPath != null) {
MappingWriter writer = MappingWriter.create(counterPath, counterFileFormat);
Expand Down

0 comments on commit 51757d3

Please sign in to comment.