reindent ProguardParser, allow for mojmap caching and add .editorconfig
All checks were successful
Publish to snapshot maven / build (push) Successful in 13s

This commit is contained in:
moehreag 2024-05-19 15:14:00 +02:00
parent 8ce3afa7d6
commit 087234d3f9
4 changed files with 110 additions and 81 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
tab_width = 4
trim_trailing_whitespace = true
indent_style = tab

View file

@ -25,7 +25,7 @@ import org.objectweb.asm.commons.ClassRemapper;
public class NonsenseRemapper { public class NonsenseRemapper {
public static void run(String minecraftVersion, Path inputJar, Path outputJar, boolean skipMetaInf, boolean renameParameters) throws IOException, InterruptedException { public static void run(String minecraftVersion, Path inputJar, Path outputJar, boolean skipMetaInf, boolean renameParameters) throws IOException, InterruptedException {
MappingData data = ProguardParser.read(MojmapProvider.get(minecraftVersion).orElseThrow()).reverse(); MappingData data = ProguardParser.read(MojmapProvider.get(minecraftVersion, outputJar.resolveSibling("client-"+minecraftVersion+".txt")).orElseThrow()).reverse();
Mapper mapper = new Mapper(data); Mapper mapper = new Mapper(data);

View file

@ -1,97 +1,97 @@
package org.ecorous.esnesnon.nonsense_remapper.parser; package org.ecorous.esnesnon.nonsense_remapper.parser;
import org.ecorous.esnesnon.nonsense_remapper.api.data.Member;
import org.ecorous.esnesnon.nonsense_remapper.api.data.MappingData; import org.ecorous.esnesnon.nonsense_remapper.api.data.MappingData;
import org.ecorous.esnesnon.nonsense_remapper.api.data.Member;
public class ProguardParser { public class ProguardParser {
public static MappingData read(String mappings) { public static MappingData read(String mappings) {
String[] lines = mappings.split("\n"); String[] lines = mappings.split("\n");
MappingData data = new MappingData(); MappingData data = new MappingData();
String currentClass = null; String currentClass = null;
for (String line : lines) { for (String line : lines) {
if (line.contains("#")) { if (line.contains("#")) {
line = line.substring(0, line.indexOf('#')); line = line.substring(0, line.indexOf('#'));
} }
line = line.trim(); line = line.trim();
if (line.isEmpty()) { if (line.isEmpty()) {
continue; continue;
} }
int arrowIndex = line.indexOf("->"); int arrowIndex = line.indexOf("->");
if (arrowIndex == -1) { if (arrowIndex == -1) {
throw new IllegalStateException("Missing arrow in mapping"); throw new IllegalStateException("Missing arrow in mapping");
} }
String obf = line.substring(0, arrowIndex).trim(); String obf = line.substring(0, arrowIndex).trim();
String deobf = line.substring(arrowIndex + 2).trim(); String deobf = line.substring(arrowIndex + 2).trim();
if (line.endsWith(":")) { if (line.endsWith(":")) {
deobf = deobf.substring(0, deobf.length() - 1); deobf = deobf.substring(0, deobf.length() - 1);
currentClass = obf.replace('.', '/'); currentClass = obf.replace('.', '/');
data.classes().put(currentClass, deobf.replace('.', '/')); data.classes().put(currentClass, deobf.replace('.', '/'));
} else { } else {
if (currentClass == null) { if (currentClass == null) {
throw new IllegalStateException("Member mapping specified before a class"); throw new IllegalStateException("Member mapping specified before a class");
} }
int spaceIndex = obf.indexOf(' '); int spaceIndex = obf.indexOf(' ');
if (spaceIndex == -1) { if (spaceIndex == -1) {
throw new IllegalStateException("Missing member signature"); throw new IllegalStateException("Missing member signature");
} }
String type = obf.substring(0, spaceIndex); String type = obf.substring(0, spaceIndex);
String signature = obf.substring(spaceIndex + 1); String signature = obf.substring(spaceIndex + 1);
int colonIndex = type.lastIndexOf(':'); int colonIndex = type.lastIndexOf(':');
if (colonIndex != -1) if (colonIndex != -1)
type = type.substring(colonIndex + 1); type = type.substring(colonIndex + 1);
if (signature.contains("(") && signature.endsWith(")")) { if (signature.contains("(") && signature.endsWith(")")) {
// method // method
String name = signature.substring(0, signature.indexOf('(')); String name = signature.substring(0, signature.indexOf('('));
String args = signature.substring(signature.indexOf('(') + 1, signature.length() - 1); String args = signature.substring(signature.indexOf('(') + 1, signature.length() - 1);
StringBuilder descriptor = new StringBuilder(); StringBuilder descriptor = new StringBuilder();
descriptor.append('('); descriptor.append('(');
if (!args.isEmpty()) { if (!args.isEmpty()) {
for (String arg : args.split(",")) { for (String arg : args.split(",")) {
descriptor.append(convertType(arg)); descriptor.append(convertType(arg));
} }
} }
descriptor.append(')'); descriptor.append(')');
descriptor.append(convertType(type)); descriptor.append(convertType(type));
data.methods().put(new Member(currentClass, name, descriptor.toString()), deobf); data.methods().put(new Member(currentClass, name, descriptor.toString()), deobf);
} else } else
data.fields().put(new Member(currentClass, signature, convertType(type)), deobf); data.fields().put(new Member(currentClass, signature, convertType(type)), deobf);
} }
} }
return data; return data;
} }
private static String convertType(String type) { private static String convertType(String type) {
int dimensions = 0; int dimensions = 0;
while (type.endsWith("[]")) { while (type.endsWith("[]")) {
type = type.substring(0, type.length() - 2); type = type.substring(0, type.length() - 2);
++dimensions; ++dimensions;
} }
String result = switch (type) { String result = switch (type) {
case "void" -> "V"; case "void" -> "V";
case "boolean" -> "Z"; case "boolean" -> "Z";
case "byte" -> "B"; case "byte" -> "B";
case "short" -> "S"; case "short" -> "S";
case "int" -> "I"; case "int" -> "I";
case "long" -> "J"; case "long" -> "J";
case "float" -> "F"; case "float" -> "F";
case "double" -> "D"; case "double" -> "D";
case "char" -> "C"; case "char" -> "C";
default -> 'L' + type.replace('.', '/') + ';'; default -> 'L' + type.replace('.', '/') + ';';
}; };
return "[".repeat(dimensions) + result; return "[".repeat(dimensions) + result;
} }
} }

View file

@ -4,12 +4,24 @@ import com.google.gson.*;
import org.ecorous.esnesnon.nonsense_remapper.Constants; import org.ecorous.esnesnon.nonsense_remapper.Constants;
import org.ecorous.esnesnon.nonsense_remapper.HttpHelper; import org.ecorous.esnesnon.nonsense_remapper.HttpHelper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
public class MojmapProvider { public class MojmapProvider {
public static Optional<String> get(String gameVersion) { public static Optional<String> get(String gameVersion, Path cacheFile) {
return HttpHelper.getJson(Constants.VERSION_MANIFEST).map(manifest -> { if (Files.exists(cacheFile)){
try {
return Optional.of(Files.readString(cacheFile, StandardCharsets.UTF_8));
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}
return HttpHelper.getJson(Constants.VERSION_MANIFEST).flatMap(manifest -> {
String versionName; String versionName;
if (gameVersion.startsWith("latest-")) { if (gameVersion.startsWith("latest-")) {
versionName = manifest.get("latest").getAsJsonObject().get(gameVersion.split("-")[1]).getAsString(); versionName = manifest.get("latest").getAsJsonObject().get(gameVersion.split("-")[1]).getAsString();
@ -27,10 +39,18 @@ public class MojmapProvider {
String mappingsUrl = versionManifest String mappingsUrl = versionManifest
.get("downloads").getAsJsonObject().get("client_mappings").getAsJsonObject().get("url").getAsString(); .get("downloads").getAsJsonObject().get("client_mappings").getAsJsonObject().get("url").getAsString();
return HttpHelper.getString(mappingsUrl).orElseThrow(); return HttpHelper.getString(mappingsUrl).map(s -> {
try {
Files.writeString(cacheFile, s);
} catch (IOException e) {
// TODO
e.printStackTrace();
}
return s;
});
} }
} }
return null; return Optional.empty();
}); });
} }