Skip to content

Commit

Permalink
Fix invalid formatting breaking build, now it just prints warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJoDeveloping committed Nov 13, 2019
1 parent 09e1d53 commit 7228c3f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import org.apache.commons.lang3.tuple.Pair;
import org.gradle.api.logging.Logger;

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

public static McpNames load(File data) throws IOException {
public static McpNames load(File data, Logger logger) throws IOException {
Map<String, String> names = new HashMap<>();
Map<String, String> docs = new HashMap<>();
try (ZipFile zip = new ZipFile(data)) {
Expand All @@ -75,17 +77,19 @@ public static McpNames load(File data) throws IOException {
}
}

return new McpNames(HashFunction.SHA1.hash(data), names, docs);
return new McpNames(HashFunction.SHA1.hash(data), names, docs, logger);
}

private Map<String, String> names;
private Map<String, String> docs;
public final String hash;
private final Logger logger;

private McpNames(String hash, Map<String, String> names, Map<String, String> docs) {
private McpNames(String hash, Map<String, String> names, Map<String, String> docs, Logger logger) {
this.hash = hash;
this.names = names;
this.docs = docs;
this.logger = logger;
}

public String rename(InputStream stream, boolean javadocs) throws IOException {
Expand Down Expand Up @@ -123,7 +127,7 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
String javadoc = docs.get(matcher.group("name"));
if (!Strings.isNullOrEmpty(javadoc))
insertAboveAnnotations(lines, JavadocAdder.buildJavadoc(matcher.group("indent"), javadoc, true));

currentMethodIndent = matcher.group("indent").length();
// worked, so return and don't try the fields.
return;
}
Expand All @@ -143,9 +147,23 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
if(matcher.find()) {
//we maintain a stack of the current (inner) class in com.example.ClassName$Inner format (along with indentation)
//if the stack is not empty we are entering a new inner class
int parentBlockIndentation = innerClasses.isEmpty() ? 0 : innerClasses.peek().getRight();
int currentIndentation = matcher.group("indent").length();
if (parentBlockIndentation >= currentIndentation) {
//We haven't closed a previous class correctly, so we backtrack now.
innerClasses.removeIf(p -> p.getRight() >= currentIndentation);
}
String currentClass = (innerClasses.isEmpty() ? _package : innerClasses.peek().getLeft() + "$") + matcher.group("name");
innerClasses.push(Pair.of(currentClass, matcher.group("indent").length()));
if (parentBlockIndentation >= currentIndentation) {
logger.warn("Trying to recover inner class tracking, assuming " + currentClass + " is defined in line " + (lines.size() + 1));
}

innerClasses.push(Pair.of(currentClass, currentIndentation));
String javadoc = docs.get(currentClass);
if (currentMethodIndent != 0) {
logger.warn("In " + currentClass + ":" + (lines.size() + 1) + " there likely is a method inner class, which we can't handle properly");
}
currentMethodIndent = -1;
if (!Strings.isNullOrEmpty(javadoc)) {
insertAboveAnnotations(lines, JavadocAdder.buildJavadoc(matcher.group("indent"), javadoc, true));
}
Expand All @@ -156,12 +174,15 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
//detect curly braces for inner class stacking/end identification
matcher = CLOSING_CURLY_BRACE.matcher(line);
if(matcher.find()){
if (matcher.group("indent").length() == currentMethodIndent) {
currentMethodIndent = -1;
}
if(!innerClasses.isEmpty()) {
int len = matcher.group("indent").length();
if (len == innerClasses.peek().getRight()) {
innerClasses.pop();
} else if (len < innerClasses.peek().getRight()) {
throw new IllegalArgumentException("Failed to properly track class blocks around class " + innerClasses.peek().getLeft() + ":" + (lines.size() + 1));
logger.warn("Tracking inner classes failed around " + innerClasses.peek().getLeft() + ":" + (lines.size() + 1) +", classes may be annotated incorrectly. Is your indentation off?g");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/java/net/minecraftforge/gradle/mcp/MCPRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private McpNames loadMCPNames(String name, File data) throws IOException {
McpNames map = mapCache.get(name);
String hash = HashFunction.SHA1.hash(data);
if (map == null || !hash.equals(map.hash)) {
map = McpNames.load(data);
map = McpNames.load(data, project.getLogger());
mapCache.put(name, map);
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ private McpNames loadMCPNames(String name, File data) throws IOException {
McpNames map = mapCache.get(name);
String hash = HashFunction.SHA1.hash(data);
if (map == null || !hash.equals(map.hash)) {
map = McpNames.load(data);
map = McpNames.load(data, project.getLogger());
mapCache.put(name, map);
}
return map;
Expand Down Expand Up @@ -1010,7 +1010,7 @@ private File findSource(String mapping, boolean generate) throws IOException {
if (cache.isSame() && sources.exists()) {
debug(" Cache hit");
} else if (sources.exists() || generate) {
McpNames map = McpNames.load(names);
McpNames map = McpNames.load(names, project.getLogger());

if (!sources.getParentFile().exists())
sources.getParentFile().mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void apply() throws IOException {

MappingFile obf_to_srg = MappingFile.load(srg);
MappingFile ret = new MappingFile();
McpNames map = McpNames.load(names);
McpNames map = McpNames.load(names, getLogger());
obf_to_srg.getPackages().forEach(e -> ret.addPackage(e.getMapped(), e.getMapped()));
obf_to_srg.getClasses().forEach(cls -> {
ret.addClass(cls.getMapped(), cls.getMapped());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public File deobfSources(File original, String mappings, String... cachePath) th
.add("orig", original);

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

try (ZipInputStream zin = new ZipInputStream(new FileInputStream(input));
ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(output))) {
Expand Down

0 comments on commit 7228c3f

Please sign in to comment.