Skip to content

Commit 7228c3f

Browse files
Fix invalid formatting breaking build, now it just prints warnings
1 parent 09e1d53 commit 7228c3f

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

src/common/java/net/minecraftforge/gradle/common/util/McpNames.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import de.siegmar.fastcsv.reader.CsvReader;
4545
import de.siegmar.fastcsv.reader.CsvRow;
4646
import org.apache.commons.lang3.tuple.Pair;
47+
import org.gradle.api.logging.Logger;
4748

4849
public class McpNames {
4950
private static final String NEWLINE = System.getProperty("line.separator");
@@ -53,8 +54,9 @@ public class McpNames {
5354
private static final Pattern CLASS_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: )*|\\t*)([\\w|@]*\\s)*(class|interface|@interface|enum) (?<name>[\\w]+)");
5455
private static final Pattern CLOSING_CURLY_BRACE = Pattern.compile("^(?<indent>(?: )*|\\t*)}");
5556
private static final Pattern PACKAGE_DECL = Pattern.compile("^[\\s]*package(\\s)*(?<name>[\\w|.]+);$");
57+
private int currentMethodIndent = -1;
5658

57-
public static McpNames load(File data) throws IOException {
59+
public static McpNames load(File data, Logger logger) throws IOException {
5860
Map<String, String> names = new HashMap<>();
5961
Map<String, String> docs = new HashMap<>();
6062
try (ZipFile zip = new ZipFile(data)) {
@@ -75,17 +77,19 @@ public static McpNames load(File data) throws IOException {
7577
}
7678
}
7779

78-
return new McpNames(HashFunction.SHA1.hash(data), names, docs);
80+
return new McpNames(HashFunction.SHA1.hash(data), names, docs, logger);
7981
}
8082

8183
private Map<String, String> names;
8284
private Map<String, String> docs;
8385
public final String hash;
86+
private final Logger logger;
8487

85-
private McpNames(String hash, Map<String, String> names, Map<String, String> docs) {
88+
private McpNames(String hash, Map<String, String> names, Map<String, String> docs, Logger logger) {
8689
this.hash = hash;
8790
this.names = names;
8891
this.docs = docs;
92+
this.logger = logger;
8993
}
9094

9195
public String rename(InputStream stream, boolean javadocs) throws IOException {
@@ -123,7 +127,7 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
123127
String javadoc = docs.get(matcher.group("name"));
124128
if (!Strings.isNullOrEmpty(javadoc))
125129
insertAboveAnnotations(lines, JavadocAdder.buildJavadoc(matcher.group("indent"), javadoc, true));
126-
130+
currentMethodIndent = matcher.group("indent").length();
127131
// worked, so return and don't try the fields.
128132
return;
129133
}
@@ -143,9 +147,23 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
143147
if(matcher.find()) {
144148
//we maintain a stack of the current (inner) class in com.example.ClassName$Inner format (along with indentation)
145149
//if the stack is not empty we are entering a new inner class
150+
int parentBlockIndentation = innerClasses.isEmpty() ? 0 : innerClasses.peek().getRight();
151+
int currentIndentation = matcher.group("indent").length();
152+
if (parentBlockIndentation >= currentIndentation) {
153+
//We haven't closed a previous class correctly, so we backtrack now.
154+
innerClasses.removeIf(p -> p.getRight() >= currentIndentation);
155+
}
146156
String currentClass = (innerClasses.isEmpty() ? _package : innerClasses.peek().getLeft() + "$") + matcher.group("name");
147-
innerClasses.push(Pair.of(currentClass, matcher.group("indent").length()));
157+
if (parentBlockIndentation >= currentIndentation) {
158+
logger.warn("Trying to recover inner class tracking, assuming " + currentClass + " is defined in line " + (lines.size() + 1));
159+
}
160+
161+
innerClasses.push(Pair.of(currentClass, currentIndentation));
148162
String javadoc = docs.get(currentClass);
163+
if (currentMethodIndent != 0) {
164+
logger.warn("In " + currentClass + ":" + (lines.size() + 1) + " there likely is a method inner class, which we can't handle properly");
165+
}
166+
currentMethodIndent = -1;
149167
if (!Strings.isNullOrEmpty(javadoc)) {
150168
insertAboveAnnotations(lines, JavadocAdder.buildJavadoc(matcher.group("indent"), javadoc, true));
151169
}
@@ -156,12 +174,15 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
156174
//detect curly braces for inner class stacking/end identification
157175
matcher = CLOSING_CURLY_BRACE.matcher(line);
158176
if(matcher.find()){
177+
if (matcher.group("indent").length() == currentMethodIndent) {
178+
currentMethodIndent = -1;
179+
}
159180
if(!innerClasses.isEmpty()) {
160181
int len = matcher.group("indent").length();
161182
if (len == innerClasses.peek().getRight()) {
162183
innerClasses.pop();
163184
} else if (len < innerClasses.peek().getRight()) {
164-
throw new IllegalArgumentException("Failed to properly track class blocks around class " + innerClasses.peek().getLeft() + ":" + (lines.size() + 1));
185+
logger.warn("Tracking inner classes failed around " + innerClasses.peek().getLeft() + ":" + (lines.size() + 1) +", classes may be annotated incorrectly. Is your indentation off?g");
165186
}
166187
}
167188
}

src/mcp/java/net/minecraftforge/gradle/mcp/MCPRepo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ private McpNames loadMCPNames(String name, File data) throws IOException {
381381
McpNames map = mapCache.get(name);
382382
String hash = HashFunction.SHA1.hash(data);
383383
if (map == null || !hash.equals(map.hash)) {
384-
map = McpNames.load(data);
384+
map = McpNames.load(data, project.getLogger());
385385
mapCache.put(name, map);
386386
}
387387
return map;

src/userdev/java/net/minecraftforge/gradle/userdev/MinecraftUserRepo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ private McpNames loadMCPNames(String name, File data) throws IOException {
819819
McpNames map = mapCache.get(name);
820820
String hash = HashFunction.SHA1.hash(data);
821821
if (map == null || !hash.equals(map.hash)) {
822-
map = McpNames.load(data);
822+
map = McpNames.load(data, project.getLogger());
823823
mapCache.put(name, map);
824824
}
825825
return map;
@@ -1010,7 +1010,7 @@ private File findSource(String mapping, boolean generate) throws IOException {
10101010
if (cache.isSame() && sources.exists()) {
10111011
debug(" Cache hit");
10121012
} else if (sources.exists() || generate) {
1013-
McpNames map = McpNames.load(names);
1013+
McpNames map = McpNames.load(names, project.getLogger());
10141014

10151015
if (!sources.getParentFile().exists())
10161016
sources.getParentFile().mkdirs();

src/userdev/java/net/minecraftforge/gradle/userdev/tasks/GenerateSRG.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void apply() throws IOException {
4848

4949
MappingFile obf_to_srg = MappingFile.load(srg);
5050
MappingFile ret = new MappingFile();
51-
McpNames map = McpNames.load(names);
51+
McpNames map = McpNames.load(names, getLogger());
5252
obf_to_srg.getPackages().forEach(e -> ret.addPackage(e.getMapped(), e.getMapped()));
5353
obf_to_srg.getClasses().forEach(cls -> {
5454
ret.addClass(cls.getMapped(), cls.getMapped());

src/userdev/java/net/minecraftforge/gradle/userdev/util/Deobfuscator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public File deobfSources(File original, String mappings, String... cachePath) th
154154
.add("orig", original);
155155

156156
if (!cache.isSame() || !output.exists()) {
157-
McpNames map = McpNames.load(names);
157+
McpNames map = McpNames.load(names, project.getLogger());
158158

159159
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(input));
160160
ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(output))) {

0 commit comments

Comments
 (0)