aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/impl/TSRGMapper.java
blob: 0fc648952189666b8cbd358f6c9282766147c931 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package ftbsc.lll.mapper.impl;

import ftbsc.lll.exceptions.MappingNotFoundException;
import ftbsc.lll.mapper.IMapper;
import ftbsc.lll.mapper.tools.ClassData;

import java.util.HashMap;
import java.util.Map;

/**
 * Parses a .tsrg file into a mapper capable of converting from
 * plain names to obfuscated ones and vice versa.
 */
public class TSRGMapper implements IMapper {

   /**
    * A Map containing the deobfuscated names as keys and information about
    * each class as values.
    */
   private final Map<String, ClassData> mappings = new HashMap<>();

   /**
    * Reads the given lines of text and attempts to interpret them as
    * mappings of the given type.
    * @param lines the lines to read
    */
   @Override
   public void populate(Iterable<String> lines) {
      String currentClass = "";
      for(String l : lines) {
         if(l == null) continue;
         if(l.startsWith("\t"))
            mappings.get(currentClass).addMember(l);
         else {
            String[] sp = l.split(" ");
            ClassData s = new ClassData(sp[0], sp[1]);
            currentClass = s.unobf;
            mappings.put(s.unobf, s);
         }
      }
   }

   /**
    * 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
    */
   @Override
   public String obfuscateClass(String name)  {
      ClassData data = mappings.get(name.replace('.', '/'));
      if(data == null)
         throw new MappingNotFoundException(name);
      else return data.obf;
   }

   /**
    * 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 optional descriptor of the member, may be null or partial
    * @return the obfuscated name of the given member
    * @throws MappingNotFoundException if no mapping is found
    */
   @Override
   public String obfuscateMember(String parentName, String memberName, String methodDescriptor) {
      ClassData data = mappings.get(parentName.replace('.', '/'));
      if(data == null)
         throw new MappingNotFoundException(parentName + "::" + memberName);
      return data.get(memberName, methodDescriptor);
   }
}