summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/IMapper.java
blob: 8adf860df1a1b93af140e2009a46d954c587cc78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ftbsc.lll.mapper;

import ftbsc.lll.exceptions.MappingNotFoundException;

import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;

/**
 * A generic obfuscation mapper.
 */
public interface IMapper {

   /**
    * Reads the given lines of text and attempts to interpret them as
    * mappings of the given type.
    * @param lines the lines to read
    */
   void populate(Iterable<String> lines);

   /**
    * Gets the obfuscated name of the class.
    * @param name the unobfuscated internal name of the desired class
    * @return the obfuscated name of the class
    * @throws MappingNotFoundException if no mapping is found
    */
   String obfuscateClass(String name);

   /**
    * Gets the obfuscated name of a class member (field or method).
    * @param parentName the unobfuscated internal name of the parent class
    * @param memberName the field name or method signature
    * @param methodDescriptor the descriptor of the member
    * @return the obfuscated name of the given member
    * @throws MappingNotFoundException if no mapping is found
    */
   String obfuscateMember(String parentName, String memberName, String methodDescriptor);

   /**
    * Loads all valid parsers available in the classpath (via the Java Service API),
    * attempts to parse the given lines into mappings, and returns all built mappers
    * that succeeded without throwing errors or ftbsc.lll.exceptions.
    * @param lines the lines of the mapping file
    * @return a {@link Set} of mappers that could interpret the given input
    */
   static Set<IMapper> getMappers(Iterable<String> lines) {
      Set<IMapper> parsed = new HashSet<>();
      for(IMapper mapper: ServiceLoader.load(IMapper.class)) {
         try {
            mapper.populate(lines);
            parsed.add(mapper);
         } catch(Throwable ignored) {}
      }
      return parsed;
   }
}