package eu.crushedpixel.replaymod.reflection;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
/**
*
A helper class for working with obfuscated field names.
* In the development environment the mappings file will automatically loaded. You can provide the location of a custom mappings file by
* providing the system property {@code sevencommons.mappingsFile}.
* @author diesieben07
* @author CrushedPixel
*/
public final class MCPNames {
private static final Map fields;
private static final Map methods;
public static final MCPEnvironment env = new MCPEnvironment();
static {
if (use()) {
String mappingsDir = "./../build/unpacked/mappings/";
ResourceLocation fieldsLocation = new ResourceLocation("assets/replaymod/fields.csv");
ResourceLocation methodsLocation = new ResourceLocation("assets/replaymod/methods.csv");
InputStream fieldsIs = MCPNames.class.getClassLoader().getResourceAsStream("fields.csv");
InputStream methodsIs = MCPNames.class.getClassLoader().getResourceAsStream("methods.csv");
fields = readMappings(fieldsIs);
methods = readMappings(methodsIs);
} else {
methods = fields = null;
}
}
/**
* Whether the code is running in a development environment or not.
* @return true if the code is running in development mode (use MCP instead of SRG names)
*/
public static boolean use() {
return env.isMCPEnvironment();
}
/**
* Get the correct name for the given SRG field based on the context.
* @param srg the SRG name for a field
* @return the input if the code is running outside of development mode or the matching MCP name otherwise
*/
public static String field(String srg) {
if (use()) {
String mcp = fields.get(srg);
if (mcp == null) {
// no mapping
return srg;
}
return mcp;
} else {
return srg;
}
}
/**
* Get the correct name for the given SRG method based on the context.
* @param srg the SRG name for a method
* @return the input if the code is running outside of development mode or the matching MCP name otherwise
*/
public static String method(String srg) {
if (use()) {
String mcp = methods.get(srg);
if (mcp == null) {
// no mapping
return srg;
}
return mcp;
} else {
return srg;
}
}
private static Map readMappings(InputStream is) {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
MCPFileParser fileParser = new MCPFileParser();
while(br.ready()) {
fileParser.processLine(br.readLine());
}
return fileParser.getResult();
} catch (IOException e) {
throw new RuntimeException("Couldn't read SRG->MCP mappings", e);
}
}
private static class MCPFileParser implements LineProcessor