2013-02-21 06:02:24 +00:00
|
|
|
/**
|
2013-02-28 05:18:22 +00:00
|
|
|
* Copyright (c) 2012-2013, md_5. All rights reserved.
|
2013-02-21 06:02:24 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* The name of the author may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
package net.md_5.specialsource;
|
|
|
|
|
|
|
|
import au.com.bytecode.opencsv.CSVReader;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For reading a .srg through MCP's fields.csv and methods.csv
|
|
|
|
* Maps func_### and field_### in input srg to "descriptive" names
|
|
|
|
*/
|
|
|
|
public class CSVMappingTransformer extends JarMappingLoadTransformer {
|
|
|
|
|
2013-02-27 21:35:32 +00:00
|
|
|
private final Map<String, String> fieldMap; // numeric srg name field_### -> descriptive csv name
|
|
|
|
private final Map<String, String> methodMap; // numeric srg name func_### -> descriptive csv name
|
|
|
|
private final Map<String, String> classPackageMap; // class src name -> repackaged full class name
|
2013-02-21 06:02:24 +00:00
|
|
|
|
2013-02-27 21:35:32 +00:00
|
|
|
public CSVMappingTransformer(File fieldsCsv, File methodsCsv, File packagesCsv) throws IOException {
|
2013-02-21 06:02:24 +00:00
|
|
|
fieldMap = new HashMap<String, String>();
|
|
|
|
methodMap = new HashMap<String, String>();
|
|
|
|
|
2013-02-28 05:56:07 +00:00
|
|
|
if (fieldsCsv != null && fieldsCsv.exists()) {
|
|
|
|
readIntoMap(fieldsCsv, fieldMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (methodsCsv != null && methodsCsv.exists()) {
|
|
|
|
readIntoMap(methodsCsv, methodMap);
|
|
|
|
}
|
2013-02-27 21:35:32 +00:00
|
|
|
|
2013-02-28 05:56:07 +00:00
|
|
|
if (packagesCsv != null && packagesCsv.exists()) {
|
2013-02-27 21:35:32 +00:00
|
|
|
// repackaged (FML)
|
|
|
|
classPackageMap = new HashMap<String, String>();
|
|
|
|
|
|
|
|
Map<String, String> packages = new HashMap<String, String>();
|
|
|
|
readIntoMap(packagesCsv, packages);
|
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : packages.entrySet()) {
|
|
|
|
String classSimpleName = entry.getKey();
|
|
|
|
String newPackageName = entry.getValue();
|
|
|
|
|
|
|
|
classPackageMap.put("net/minecraft/src/" + classSimpleName, newPackageName + "/" + classSimpleName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// flat package (vanilla MCP)
|
|
|
|
classPackageMap = null;
|
|
|
|
}
|
2013-02-21 06:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void readIntoMap(File file, Map<String, String> map) throws IOException {
|
|
|
|
CSVReader csvReader = new CSVReader(new FileReader(file));
|
|
|
|
String[] line;
|
|
|
|
|
|
|
|
while ((line = csvReader.readNext()) != null) {
|
|
|
|
if (line.length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-27 21:35:32 +00:00
|
|
|
if (line.length < 2) {
|
2013-02-21 06:02:24 +00:00
|
|
|
throw new IllegalArgumentException("Invalid csv line: " + line);
|
|
|
|
}
|
|
|
|
|
2013-02-27 21:35:32 +00:00
|
|
|
String key = line[0];
|
|
|
|
String value = line[1];
|
2013-02-21 06:02:24 +00:00
|
|
|
|
2013-02-27 21:35:32 +00:00
|
|
|
map.put(key, value);
|
2013-02-21 06:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-10 18:32:08 +00:00
|
|
|
public String transformFieldName(String className, String fieldName) {
|
2013-02-28 05:56:07 +00:00
|
|
|
return fieldMap.containsKey(fieldName) ? fieldMap.get(fieldName) : fieldName;
|
2013-02-21 06:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-10 18:59:39 +00:00
|
|
|
public String transformMethodName(String className, String methodName, String methodDescriptor) {
|
2013-02-28 05:56:07 +00:00
|
|
|
return methodMap.containsKey(methodName) ? methodMap.get(methodName) : methodName;
|
2013-02-21 06:02:24 +00:00
|
|
|
}
|
2013-02-27 21:35:32 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String transformClassName(String className) {
|
|
|
|
if (classPackageMap == null) {
|
|
|
|
return className;
|
|
|
|
}
|
|
|
|
|
|
|
|
String newPackage = classPackageMap.get(className);
|
|
|
|
if (newPackage == null) {
|
|
|
|
return className;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JarRemapper.mapTypeName(className, null, classPackageMap, className);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String transformMethodDescriptor(String oldDescriptor) {
|
|
|
|
if (classPackageMap == null) {
|
|
|
|
return oldDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
MethodDescriptorTransformer methodDescriptorTransformer = new MethodDescriptorTransformer(null, classPackageMap);
|
|
|
|
return methodDescriptorTransformer.transform(oldDescriptor);
|
|
|
|
}
|
2013-02-21 06:02:24 +00:00
|
|
|
}
|