diff options
author | zaaarf <zaaarf@proton.me> | 2023-09-01 12:09:26 +0200 |
---|---|---|
committer | zaaarf <zaaarf@proton.me> | 2023-09-01 12:09:26 +0200 |
commit | fa6c73faf0ebdaac59a309f7c4bc114a3df99a24 (patch) | |
tree | 2fa03aa5e2be18e44725057e8288ef4007621d8f /src/main/java/ftbsc/lll/mapper/impl | |
parent | ea030e54b999aba4d8c3409c1507047f0a8aa8c5 (diff) |
feat: rewrote the mapper api to make more sense
Diffstat (limited to 'src/main/java/ftbsc/lll/mapper/impl')
4 files changed, 79 insertions, 81 deletions
diff --git a/src/main/java/ftbsc/lll/mapper/impl/MultiMapper.java b/src/main/java/ftbsc/lll/mapper/impl/MultiMapper.java index 7eeb0d5..1ff1459 100644 --- a/src/main/java/ftbsc/lll/mapper/impl/MultiMapper.java +++ b/src/main/java/ftbsc/lll/mapper/impl/MultiMapper.java @@ -2,9 +2,9 @@ package ftbsc.lll.mapper.impl; import com.google.auto.service.AutoService; import ftbsc.lll.exceptions.MalformedMappingsException; -import ftbsc.lll.mapper.AbstractMapper; -import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.mapper.IMappingFormat; import ftbsc.lll.mapper.MapperProvider; +import ftbsc.lll.mapper.tools.Mapper; import ftbsc.lll.mapper.tools.MappingUtils; import ftbsc.lll.mapper.tools.data.ClassData; @@ -15,21 +15,22 @@ import java.util.List; * Special mapper type that actually resolves to an ordered * sequence of mappers applied one after the other. */ -@AutoService(IMapper.class) -public class MultiMapper extends AbstractMapper { +@AutoService(IMappingFormat.class) +public class MultiMapper implements IMappingFormat { @Override public boolean claim(List<String> lines) { return lines.get(0).equals("lll multimapper"); } @Override - public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { - List<IMapper> mapperList = new ArrayList<>(); + public Mapper getMapper(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + Mapper result = new Mapper(); + List<Mapper> mapperList = new ArrayList<>(); + for(int i = 1; i < lines.size(); i++) { List<String> data = MapperProvider.fetchFromLocalOrRemote(lines.get(i)); - IMapper mapper = MapperProvider.getMapper(data); - mapper.populate(data, ignoreErrors); - mapperList.add(mapper); + IMappingFormat format = MapperProvider.getMapper(data); + mapperList.add(format.getMapper(data, ignoreErrors)); } mapperList.get(0).getRawMappings().forEach((name, data) -> { @@ -41,7 +42,7 @@ public class MultiMapper extends AbstractMapper { data.getMethods().forEach((signature, methodData) -> { for(int i = 1; i < mapperList.size(); i++) { - IMapper mapper = mapperList.get(i); + Mapper mapper = mapperList.get(i); methodData = mapper.getMethodData(methodData.parentClass.nameMapped, methodData.nameMapped, MappingUtils.mapMethodDescriptor(methodData.signature.descriptor, mapper, false)); } @@ -54,12 +55,9 @@ public class MultiMapper extends AbstractMapper { sumData.addField(fieldName, fieldData.nameMapped, fieldData.descriptor); }); - this.mappings.put(sumData.name, sumData); + result.getRawMappings().put(sumData.name, sumData); }); - } - @Override - protected AbstractMapper newInstance() { - return new MultiMapper(); + return result; } } diff --git a/src/main/java/ftbsc/lll/mapper/impl/SRGMapper.java b/src/main/java/ftbsc/lll/mapper/impl/SRGMapper.java index e47ca38..c92dba7 100644 --- a/src/main/java/ftbsc/lll/mapper/impl/SRGMapper.java +++ b/src/main/java/ftbsc/lll/mapper/impl/SRGMapper.java @@ -2,27 +2,17 @@ package ftbsc.lll.mapper.impl; import com.google.auto.service.AutoService; import ftbsc.lll.exceptions.MalformedMappingsException; -import ftbsc.lll.mapper.AbstractMapper; -import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.mapper.IMappingFormat; +import ftbsc.lll.mapper.tools.Mapper; import ftbsc.lll.mapper.tools.data.ClassData; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** - * A {@link IMapper} capable of parsing SRG mappings. + * A {@link IMappingFormat} capable of parsing SRG mappings. */ -@AutoService(IMapper.class) -public class SRGMapper extends AbstractMapper { - - /** - * A {@link Map} tying each obfuscated name to its class data. - * Done this way because SRG files provide mapped descriptors in advance, - * so we can populate this right a way in a less expensive way. - */ - private final Map<String, ClassData> mappingsInverted = new HashMap<>(); - +@AutoService(IMappingFormat.class) +public class SRGMapper implements IMappingFormat { @Override public boolean claim(List<String> lines) { @@ -33,33 +23,47 @@ public class SRGMapper extends AbstractMapper { || firstLineTokens[0].equals("FD:")); } - @Override - public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + /** + * Builds the two mappers, and returns one of the two depending on the flag. + * Since the SRG format contains descriptor mappings, it's possible to process + * this right away. + * @param lines the lines to read + * @param ignoreErrors try to ignore errors and keep going + * @param inverted whether it should return the inverted one + * @return the {@link Mapper}, inverted depending on the flag + * @throws MalformedMappingsException if an error is encountered and ignoreErrors is false + */ + protected Mapper buildMapper(List<String> lines, boolean ignoreErrors, boolean inverted) throws MalformedMappingsException { + Mapper mapper = new Mapper(); + Mapper invertedMapper = new Mapper(); for(int i = 0; i < lines.size(); i++) { String[] tokens = lines.get(i).trim().split(" "); switch(tokens[0]) { case "CL:": if(tokens.length != 3) break; - this.registerClass(tokens[1], tokens[2]); + this.registerClass(mapper, invertedMapper, tokens[1], tokens[2]); continue; case "MD:": case "FD:": - if(this.processMemberTokens(tokens)) + if(this.processMemberTokens(mapper, invertedMapper, tokens)) continue; break; } if(!ignoreErrors) throw new MalformedMappingsException(i, "wrong number of space-separated tokens"); } + return inverted ? invertedMapper : mapper; } /** * Processes a line, broken up into tokens. + * @param mapper the {@link Mapper} with normal mappings + * @param invertedMapper the {@link Mapper} with inverted mappings * @param tokens the tokens * @return whether it was a valid mapping */ - private boolean processMemberTokens(String[] tokens) { + private boolean processMemberTokens(Mapper mapper, Mapper invertedMapper, String[] tokens) { boolean field; if(tokens[0].equals("MD:")) { if(tokens.length != 5) @@ -78,25 +82,29 @@ public class SRGMapper extends AbstractMapper { split = tokens[obfPosition].split("/"); String memberNameObf = split[split.length - 1]; String parentObf = tokens[obfPosition].substring(0, tokens[obfPosition].length() - split[split.length - 1].length() - 1); - if(field) this.registerMember(parent, parentObf, memberName, memberNameObf, null, null); - else this.registerMember(parent, parentObf, memberName, memberNameObf, tokens[2], tokens[4]); + this.registerMember(mapper, invertedMapper, parent, parentObf, memberName, memberNameObf, + field ? null : tokens[2], field ? null : tokens[4]); return true; } /** * Registers a class in the mapper, if it isn't already. + * @param mapper the {@link Mapper} with normal mappings + * @param invertedMapper the {@link Mapper} with inverted mappings * @param name the name * @param nameMapped the mapped name */ - private void registerClass(String name, String nameMapped) { - if(this.mappings.containsKey(name)) + private void registerClass(Mapper mapper, Mapper invertedMapper, String name, String nameMapped) { + if(mapper.getRawMappings().containsKey(name)) return; - this.mappings.put(name, new ClassData(name, nameMapped)); - this.mappingsInverted.put(nameMapped, new ClassData(nameMapped, name)); + mapper.getRawMappings().put(name, new ClassData(name, nameMapped)); + invertedMapper.getRawMappings().put(nameMapped, new ClassData(nameMapped, name)); } /** * Registers a class member. The descriptors should be null for fields. + * @param mapper the {@link Mapper} with normal mappings + * @param invertedMapper the {@link Mapper} with inverted mappings * @param parent the parent's plain internal name * @param parentMapped the parent's mapped internal name * @param name the member's plain name @@ -104,11 +112,12 @@ public class SRGMapper extends AbstractMapper { * @param descriptor the member's plain descriptor, may be null * @param descriptorMapped the member's mapped descriptor, may be null */ - private void registerMember(String parent, String parentMapped, String name, String nameMapped, + private void registerMember(Mapper mapper, Mapper invertedMapper, String parent, + String parentMapped, String name, String nameMapped, String descriptor, String descriptorMapped) { - this.registerClass(parent, parentMapped); - ClassData data = this.mappings.get(parent); - ClassData dataReverse = this.mappings.get(data.nameMapped); + this.registerClass(mapper, invertedMapper, parent, parentMapped); + ClassData data = mapper.getClassData(parent); + ClassData dataReverse = invertedMapper.getClassData(data.nameMapped); if(descriptor == null || descriptorMapped == null) { //field data.addField(name, nameMapped); @@ -121,15 +130,12 @@ public class SRGMapper extends AbstractMapper { } @Override - public IMapper getInverted() { - SRGMapper inverted = new SRGMapper(); - inverted.mappings.putAll(this.mappingsInverted); - inverted.mappingsInverted.putAll(this.mappings); //in case some weirdo calls getInverted() twice - return inverted; + public Mapper getMapper(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + return this.buildMapper(lines, ignoreErrors, false); } @Override - protected AbstractMapper newInstance() { //not really used but whatever - return new SRGMapper(); + public Mapper getInvertedMapper(List<String> lines, boolean ignoreErrors) { + return this.buildMapper(lines, ignoreErrors, true); } }
\ No newline at end of file diff --git a/src/main/java/ftbsc/lll/mapper/impl/TSRGMapper.java b/src/main/java/ftbsc/lll/mapper/impl/TSRGMapper.java index 838a84d..222fafc 100644 --- a/src/main/java/ftbsc/lll/mapper/impl/TSRGMapper.java +++ b/src/main/java/ftbsc/lll/mapper/impl/TSRGMapper.java @@ -2,20 +2,20 @@ package ftbsc.lll.mapper.impl; import com.google.auto.service.AutoService; import ftbsc.lll.exceptions.MalformedMappingsException; -import ftbsc.lll.mapper.AbstractMapper; -import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.mapper.IMappingFormat; +import ftbsc.lll.mapper.tools.Mapper; import ftbsc.lll.mapper.tools.data.ClassData; import java.util.List; import java.util.regex.Pattern; /** - * A {@link IMapper} capable of parsing TSRG (an intermediary + * A {@link IMappingFormat} capable of parsing TSRG (an intermediary * format used by Forge) files. */ -@AutoService(IMapper.class) -public class TSRGMapper extends AbstractMapper { +@AutoService(IMappingFormat.class) +public class TSRGMapper implements IMappingFormat { @Override public boolean claim(List<String> lines) { @@ -24,7 +24,8 @@ public class TSRGMapper extends AbstractMapper { } @Override - public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + public Mapper getMapper(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + Mapper result = new Mapper(); String currentClass = ""; for(int i = 1; i < lines.size(); i++) { //start from 1 to skip header String currentLine = lines.get(i); @@ -32,22 +33,18 @@ public class TSRGMapper extends AbstractMapper { String[] tokens = currentLine.trim().split(" "); if(isMember) { if(tokens.length == 2) //field - this.mappings.get(currentClass).addField(tokens[0], tokens[1]); + result.getClassData(currentClass).addField(tokens[0], tokens[1]); else if(tokens.length == 3)//method - this.mappings.get(currentClass).addMethod(tokens[0], tokens[2], tokens[1]); //add child + result.getClassData(currentClass).addMethod(tokens[0], tokens[2], tokens[1]); //add child else if(!ignoreErrors) throw new MalformedMappingsException(i, "wrong number of space-separated tokens"); } else { if(tokens.length == 2) { ClassData s = new ClassData(tokens[0], tokens[1]); currentClass = s.name; - this.mappings.put(s.name, s); + result.getRawMappings().put(s.name, s); } else if(!ignoreErrors) throw new MalformedMappingsException(i, "wrong number of space-separated tokens"); } } - } - - @Override - protected AbstractMapper newInstance() { - return new TSRGMapper(); + return result; } } diff --git a/src/main/java/ftbsc/lll/mapper/impl/TinyV2Mapper.java b/src/main/java/ftbsc/lll/mapper/impl/TinyV2Mapper.java index b7b58a1..1810b84 100644 --- a/src/main/java/ftbsc/lll/mapper/impl/TinyV2Mapper.java +++ b/src/main/java/ftbsc/lll/mapper/impl/TinyV2Mapper.java @@ -2,18 +2,18 @@ package ftbsc.lll.mapper.impl; import com.google.auto.service.AutoService; import ftbsc.lll.exceptions.MalformedMappingsException; -import ftbsc.lll.mapper.AbstractMapper; -import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.mapper.IMappingFormat; +import ftbsc.lll.mapper.tools.Mapper; import ftbsc.lll.mapper.tools.data.ClassData; import java.util.List; import java.util.regex.Pattern; /** - * A {@link IMapper} capable of parsing TinyV2 mappings + * A {@link IMappingFormat} capable of parsing TinyV2 mappings. */ -@AutoService(IMapper.class) -public class TinyV2Mapper extends AbstractMapper { +@AutoService(IMappingFormat.class) +public class TinyV2Mapper implements IMappingFormat { @Override public boolean claim(List<String> lines) { @@ -22,7 +22,8 @@ public class TinyV2Mapper extends AbstractMapper { } @Override - public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + public Mapper getMapper(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { + Mapper result = new Mapper(); String currentClass = ""; for(int i = 1; i < lines.size(); i++) { String currentLine = lines.get(i); @@ -32,7 +33,7 @@ public class TinyV2Mapper extends AbstractMapper { case 0: //classes if(tokens.length == 3) { if(tokens[0].charAt(0) == 'c') { - this.mappings.put(tokens[1], new ClassData(tokens[1], tokens[2])); + result.getRawMappings().put(tokens[1], new ClassData(tokens[1], tokens[2])); currentClass = tokens[1]; } else if(!ignoreErrors) throw new MalformedMappingsException(i, "root-level element must be class"); @@ -48,12 +49,12 @@ public class TinyV2Mapper extends AbstractMapper { case 'm': //methods if(tokens.length == 4) break; - this.mappings.get(currentClass).addMethod(tokens[2], tokens[3], tokens[1]); + result.getClassData(currentClass).addMethod(tokens[2], tokens[3], tokens[1]); continue; case 'f': //fields if(tokens.length == 4) break; - this.mappings.get(currentClass).addField(tokens[2], tokens[3], tokens[1]); + result.getClassData(currentClass).addField(tokens[2], tokens[3], tokens[1]); continue; } break; @@ -61,12 +62,8 @@ public class TinyV2Mapper extends AbstractMapper { break; } if(!ignoreErrors) - throw new MalformedMappingsException(i, "wrong number of space-separated tokens"); + throw new MalformedMappingsException(i, "wrong number of tab-separated tokens"); } - } - - @Override - protected AbstractMapper newInstance() { - return new TinyV2Mapper(); + return result; } } |