Skip to content

Commit 92a38b2

Browse files
committed
Fix possible crash while remapping refmap
1 parent 36b3070 commit 92a38b2

File tree

1 file changed

+117
-111
lines changed
  • src/main/java/io/github/fabriccompatibilitylayers/modremappingapi/impl/remapper/resource

1 file changed

+117
-111
lines changed

src/main/java/io/github/fabriccompatibilitylayers/modremappingapi/impl/remapper/resource/RefmapJson.java

Lines changed: 117 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -58,155 +58,161 @@ private String mapRefMapEntry(String mixinClass, String old, MappingTree tree, S
5858
}
5959

6060
private String mapRefMapEntry(String mixinClass, String old, TinyRemapper remapper) {
61-
TrRemapper trRemapper = remapper.getEnvironment().getRemapper();
62-
List<String> supers = ModRemappingAPIImpl.getCurrentContext().getMixinData().getMixin2TargetMap().get(mixinClass);
63-
// format is:
64-
// owner + name + quantifier + (desc == null || desc.startsWith("(") ? "" : ":") + desc + (tail != null ? " -> " : "") + tail
65-
String owner; // can be ""
66-
String name; // can be ""
67-
String quantifier; // can be ""
68-
String desc; // can be ""
69-
String tail; // can be ""
70-
71-
// read the entry
72-
{
73-
String rest;
74-
// get tail
61+
try {
62+
TrRemapper trRemapper = remapper.getEnvironment().getRemapper();
63+
List<String> supers = ModRemappingAPIImpl.getCurrentContext().getMixinData().getMixin2TargetMap().get(mixinClass);
64+
// format is:
65+
// owner + name + quantifier + (desc == null || desc.startsWith("(") ? "" : ":") + desc + (tail != null ? " -> " : "") + tail
66+
String owner; // can be ""
67+
String name; // can be ""
68+
String quantifier; // can be ""
69+
String desc; // can be ""
70+
String tail; // can be ""
71+
72+
// read the entry
7573
{
76-
String arrow = " -> ";
77-
int arrowPosition = old.indexOf(arrow);
78-
if (arrowPosition == -1) { // tail == null
79-
tail = "";
80-
rest = old;
81-
} else {
82-
rest = old.substring(0, arrowPosition);
83-
tail = old.substring(arrowPosition + arrow.length());
74+
String rest;
75+
// get tail
76+
{
77+
String arrow = " -> ";
78+
int arrowPosition = old.indexOf(arrow);
79+
if (arrowPosition == -1) { // tail == null
80+
tail = "";
81+
rest = old;
82+
} else {
83+
rest = old.substring(0, arrowPosition);
84+
tail = old.substring(arrowPosition + arrow.length());
85+
}
8486
}
85-
}
8687

87-
// get desc
88-
{
89-
int separatorPosition = rest.indexOf(":");
90-
if (separatorPosition == -1) { // separator == null
91-
int parenthesisPosition = rest.indexOf("(");
92-
if (parenthesisPosition == -1) {
93-
desc = "";
88+
// get desc
89+
{
90+
int separatorPosition = rest.indexOf(":");
91+
if (separatorPosition == -1) { // separator == null
92+
int parenthesisPosition = rest.indexOf("(");
93+
if (parenthesisPosition == -1) {
94+
desc = "";
95+
} else {
96+
// if there's no ':', then there must be an opening bracket or **the desc is null**
97+
desc = rest.substring(parenthesisPosition);
98+
rest = rest.substring(0, parenthesisPosition);
99+
}
94100
} else {
95-
// if there's no ':', then there must be an opening bracket or **the desc is null**
96-
desc = rest.substring(parenthesisPosition);
97-
rest = rest.substring(0, parenthesisPosition);
101+
desc = rest.substring(separatorPosition + 1);
102+
rest = rest.substring(0, separatorPosition);
98103
}
99-
} else {
100-
desc = rest.substring(separatorPosition + 1);
101-
rest = rest.substring(0, separatorPosition);
102104
}
103-
}
104105

105-
// get owner
106-
{
107-
if (rest.startsWith("L")) { // owner != null
108-
int endPosition = rest.indexOf(";");
109-
if (endPosition == -1) {
110-
throw new RuntimeException(
111-
"Cannot parse refmap entry of class " + mixinClass + ": it starts with 'L', and doesn't contain a ';': " + old);
106+
// get owner
107+
{
108+
if (rest.startsWith("L")) { // owner != null
109+
int endPosition = rest.indexOf(";");
110+
if (endPosition == -1) {
111+
throw new RuntimeException(
112+
"Cannot parse refmap entry of class " + mixinClass + ": it starts with 'L', and doesn't contain a ';': " + old);
113+
} else {
114+
owner = rest.substring(1, endPosition);
115+
rest = rest.substring(endPosition + 1); // we don't want the ';' here
116+
}
112117
} else {
113-
owner = rest.substring(1, endPosition);
114-
rest = rest.substring(endPosition + 1); // we don't want the ';' here
118+
owner = "";
115119
}
116-
} else {
117-
owner = "";
118120
}
119-
}
120121

121-
// get quantifier
122-
{
123-
// try to find either '{', '+' or '*'
124-
int bracesPosition = rest.indexOf("{");
125-
if (bracesPosition == -1)
126-
bracesPosition = rest.indexOf("*");
127-
if (bracesPosition == -1)
128-
bracesPosition = rest.indexOf("+");
129-
130-
if (bracesPosition == -1) {
131-
// try the * and +
132-
quantifier = "";
133-
} else {
134-
quantifier = rest.substring(bracesPosition);
135-
rest = rest.substring(0, bracesPosition);
122+
// get quantifier
123+
{
124+
// try to find either '{', '+' or '*'
125+
int bracesPosition = rest.indexOf("{");
126+
if (bracesPosition == -1)
127+
bracesPosition = rest.indexOf("*");
128+
if (bracesPosition == -1)
129+
bracesPosition = rest.indexOf("+");
130+
131+
if (bracesPosition == -1) {
132+
// try the * and +
133+
quantifier = "";
134+
} else {
135+
quantifier = rest.substring(bracesPosition);
136+
rest = rest.substring(0, bracesPosition);
137+
}
136138
}
137-
}
138139

139-
// get name
140-
{
141-
name = rest; // only name is left
140+
// get name
141+
{
142+
name = rest; // only name is left
142143
// if (name.isEmpty()) {
143144
// throw new RuntimeException("Cannot parse refmap entry of class " + mixinClass +
144145
// ": the name is \"\", so something went wrong: owner = \"" + owner + "\", name = \"" + name +
145146
// "\", quantifier = \"" + quantifier + "\", desc = \"" + desc + "\", tail = \"" + tail +
146147
// "\", old = \"" + old + "\"");
147148
// }
149+
}
148150
}
149-
}
150151

151-
// for now just stop here, most stuff doesn't use quantifiers or tails
152-
if (!quantifier.isEmpty())
153-
throw new RuntimeException("Quantifiers are not yet supported: " + old);
154-
if (!tail.isEmpty())
155-
throw new RuntimeException("Tails are not yet supported: " + tail);
152+
// for now just stop here, most stuff doesn't use quantifiers or tails
153+
if (!quantifier.isEmpty())
154+
throw new RuntimeException("Quantifiers are not yet supported: " + old);
155+
if (!tail.isEmpty())
156+
throw new RuntimeException("Tails are not yet supported: " + tail);
156157

157-
// do the actual mapping
158+
// do the actual mapping
158159

159-
// it's a class
160-
if (owner.isEmpty() && desc.isEmpty()) {
161-
return trRemapper.map(name);
162-
}
160+
// it's a class
161+
if (owner.isEmpty() && desc.isEmpty()) {
162+
return trRemapper.map(name);
163+
}
163164

164-
// it's a method
165-
if (desc.startsWith("(") && desc.contains(")")) {
166-
if (owner.isEmpty()) { // it's an @Invoker
167-
if (supers == null || supers.isEmpty()) {
168-
throw new RuntimeException("Can't find target class for mixin " + mixinClass);
169-
}
165+
// it's a method
166+
if (desc.startsWith("(") && desc.contains(")")) {
167+
if (owner.isEmpty()) { // it's an @Invoker
168+
if (supers == null || supers.isEmpty()) {
169+
throw new RuntimeException("Can't find target class for mixin " + mixinClass);
170+
}
170171

171-
final String originalName = name;
172-
String newDesc = trRemapper.mapMethodDesc(desc);
172+
final String originalName = name;
173+
String newDesc = trRemapper.mapMethodDesc(desc);
173174

174-
if (!originalName.isEmpty()) {
175-
for (String own : supers) {
176-
name = trRemapper.mapMethodName(own, name, desc);
175+
if (!originalName.isEmpty()) {
176+
for (String own : supers) {
177+
name = trRemapper.mapMethodName(own, name, desc);
177178

178-
if (!originalName.equals(name)) {
179-
return name + newDesc;
179+
if (!originalName.equals(name)) {
180+
return name + newDesc;
181+
}
180182
}
181183
}
182-
}
183184

184-
return originalName + newDesc;
185-
} else { // just a normal method
186-
return "L" + trRemapper.map(owner) + ";" + trRemapper.mapMethodName(owner, name, desc) + trRemapper.mapMethodDesc(desc);
185+
return originalName + newDesc;
186+
} else { // just a normal method
187+
return "L" + trRemapper.map(owner) + ";" + trRemapper.mapMethodName(owner, name, desc) + trRemapper.mapMethodDesc(desc);
188+
}
187189
}
188-
}
189190

190-
// it's an @Accessor
191-
if (owner.isEmpty()) {
192-
if (supers == null || supers.isEmpty()) {
193-
throw new RuntimeException("Can't find target class for mixin " + mixinClass);
194-
}
191+
// it's an @Accessor
192+
if (owner.isEmpty()) {
193+
if (supers == null || supers.isEmpty()) {
194+
throw new RuntimeException("Can't find target class for mixin " + mixinClass);
195+
}
195196

196-
final String originalName = name;
197+
final String originalName = name;
197198

198-
for (String own : supers) {
199-
name = trRemapper.mapFieldName(own, name, desc);
199+
for (String own : supers) {
200+
name = trRemapper.mapFieldName(own, name, desc);
200201

201-
if (!originalName.equals(name)) {
202-
return name;
202+
if (!originalName.equals(name)) {
203+
return name;
204+
}
203205
}
206+
207+
return originalName;
204208
}
205209

206-
return originalName;
210+
// just a normal field
211+
return "L" + trRemapper.map(owner) + ";" + trRemapper.mapFieldName(owner, name, desc) + (!desc.isEmpty() ? ":" + trRemapper.mapDesc(desc) : "");
212+
} catch (Throwable t) {
213+
System.err.println("Error while remapping refmap entry: " + old + " for mixin " + mixinClass);
214+
System.err.println(t);
215+
return old;
207216
}
208-
209-
// just a normal field
210-
return "L" + trRemapper.map(owner) + ";" + trRemapper.mapFieldName(owner, name, desc) + ":" + trRemapper.mapDesc(desc);
211217
}
212218
}

0 commit comments

Comments
 (0)