SrgContainer.java
3.94 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.core.runtime;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.io.Files;
import joptsimple.internal.Strings;
public class SrgContainer
{
private final Map<String, String> packageMap = new HashMap<String, String>();
private final Map<String, String> classMap = new HashMap<String, String>();
private final Map<SrgField, SrgField> fieldMap = new HashMap<SrgField, SrgField>();
private final Map<SrgMethod, SrgMethod> methodMap = new HashMap<SrgMethod, SrgMethod>();
private final Map<String, String> reversePackageMap = new HashMap<String, String>();
private final Map<String, String> reverseClassMap = new HashMap<String, String>();
private final Map<SrgField, SrgField> reverseFieldMap = new HashMap<SrgField, SrgField>();
private final Map<SrgMethod, SrgMethod> reverseMethodMap = new HashMap<SrgMethod, SrgMethod>();
public void readSrg(File srg) throws IOException
{
for (String line : Files.readLines(srg, Charset.defaultCharset()))
{
if (Strings.isNullOrEmpty(line) || line.startsWith("#"))
{
continue;
}
String type = line.substring(0, 2);
String[] args = line.substring(4).split(" ");
if ("PK".equals(type))
{
this.packageMap.put(args[0], args[1]);
this.reversePackageMap.put(args[1], args[0]);
}
else if ("CL".equals(type))
{
this.classMap.put(args[0], args[1]);
this.reverseClassMap.put(args[1], args[0]);
}
else if ("FD".equals(type))
{
SrgField field1 = new SrgField(args[0]);
SrgField field2 = new SrgField(args[1]);
this.fieldMap.put(field1, field2);
this.reverseFieldMap.put(field2, field1);
}
else if ("MD".equals(type))
{
SrgMethod method1 = new SrgMethod(args[0], args[1]);
SrgMethod method2 = new SrgMethod(args[2], args[3]);
this.methodMap.put(method1, method2);
this.reverseMethodMap.put(method2, method1);
}
}
}
public SrgMethod getMethodMapping(String owner, String name, boolean reverse)
{
Map<SrgMethod, SrgMethod> map = reverse ? this.reverseMethodMap : this.methodMap;
for (Entry<SrgMethod, SrgMethod> mapping : map.entrySet())
{
SrgMethod method = mapping.getKey();
if (owner.equals(method.getOwner()) && name.equals(method.getSimpleName()))
{
return mapping.getValue();
}
}
return null;
}
public SrgField getFieldMapping(String owner, String name, boolean reverse)
{
Map<SrgField, SrgField> map = reverse ? this.reverseFieldMap : this.fieldMap;
for (Entry<SrgField, SrgField> mapping : map.entrySet())
{
SrgField field = mapping.getKey();
if (owner.equals(field.getOwner()) && name.equals(field.getName()))
{
return mapping.getValue();
}
}
return null;
}
public SrgMethod getMethodMapping(SrgMethod methodName)
{
return this.methodMap.get(methodName);
}
public SrgField getFieldMapping(SrgField fieldName)
{
return this.fieldMap.get(fieldName);
}
public String getClassMapping(String className)
{
return this.classMap.get(className);
}
public String getPackageMapping(String packageName)
{
return this.packageMap.get(packageName);
}
}