Search in sources :

Example 1 with MappingTransformer

use of net.md_5.specialsource.transformer.MappingTransformer in project SpecialSource by md-5.

the class JarMapping method loadMappingsDir.

/**
 * Load mappings from an MCP directory
 *
 * @param dirname MCP directory name, local file or remote URL ending in '/'
 * @param reverse If true, swap input and output
 * @param ignoreCsv If true, ignore fields.csv and methods.csv (but not
 * packages.csv)
 * @param numericSrgNames If true, load numeric "srg" names (num->mcp
 * instead of obf->mcp)
 */
private void loadMappingsDir(String dirname, boolean reverse, boolean ignoreCsv, boolean numericSrgNames) throws IOException {
    File dir = new File(dirname);
    if (!FileLocator.isHTTPURL(dirname) && !dir.isDirectory()) {
        throw new IllegalArgumentException("loadMappingsDir(" + dir + "): not a directory");
    }
    String sep = System.getProperty("file.separator");
    List<File> srgFiles = new ArrayList<File>();
    File joinedSrg = FileLocator.getFile(dirname + sep + "joined.srg");
    if (joinedSrg.exists()) {
        // FML/MCP client/server joined
        srgFiles.add(joinedSrg);
    } else {
        // vanilla MCP separated sides
        File serverSrg = FileLocator.getFile(dirname + sep + "server.srg");
        File clientSrg = FileLocator.getFile(dirname + sep + "client.srg");
        if (serverSrg.exists()) {
            srgFiles.add(serverSrg);
        }
        if (clientSrg.exists()) {
            srgFiles.add(clientSrg);
        }
    }
    if (srgFiles.size() == 0) {
        throw new IOException("loadMappingsDir(" + dirname + "): no joined.srg, client.srg, or server.srg found");
    }
    // Read output names through csv mappings, if available & enabled
    File fieldsCsv = FileLocator.getFile(dirname + sep + "fields.csv");
    File methodsCsv = FileLocator.getFile(dirname + sep + "methods.csv");
    // FML repackaging, optional
    File packagesCsv = FileLocator.getFile(dirname + sep + "packages.csv");
    MinecraftCodersPack outputTransformer;
    MappingTransformer inputTransformer;
    if (numericSrgNames) {
        // Wants numeric "srg" names -> descriptive "csv" names. To accomplish this:
        // 1. load obf->mcp (descriptive "csv") as chainMappings
        // 2. load again but chaining input (obf) through mcp, and ignoring csv on output
        // 3. result: mcp->srg, similar to MCP ./reobfuscate --srgnames
        JarMapping chainMappings = new JarMapping();
        chainMappings.loadMappingsDir(dirname, reverse, false, /*ignoreCsv*/
        false);
        inputTransformer = new ChainingTransformer(new JarRemapper(chainMappings));
        // keep numeric srg as output
        ignoreCsv = true;
    } else {
        inputTransformer = null;
    }
    if (fieldsCsv.exists() && methodsCsv.exists()) {
        outputTransformer = new MinecraftCodersPack(ignoreCsv ? null : fieldsCsv, ignoreCsv ? null : methodsCsv, packagesCsv);
    } else {
        outputTransformer = null;
    }
    for (File srg : srgFiles) {
        loadMappings(new BufferedReader(new FileReader(srg)), inputTransformer, outputTransformer, reverse);
    }
}
Also used : MinecraftCodersPack(net.md_5.specialsource.transformer.MinecraftCodersPack) MappingTransformer(net.md_5.specialsource.transformer.MappingTransformer) ChainingTransformer(net.md_5.specialsource.transformer.ChainingTransformer)

Example 2 with MappingTransformer

use of net.md_5.specialsource.transformer.MappingTransformer in project SpecialSource by md-5.

the class JarMapping method loadMappings.

/**
 * @param filename A filename of a .srg/.csrg or an MCP directory of
 * .srg+.csv, local or remote
 * @param reverse Swap input and output mappings
 * @param numericSrgNames When reading mapping directory, load numeric "srg"
 * instead obfuscated names
 * @param inShadeRelocation Apply relocation on mapping input
 * @param outShadeRelocation Apply relocation on mapping output
 * @throws IOException
 */
public void loadMappings(String filename, boolean reverse, boolean numericSrgNames, String inShadeRelocation, String outShadeRelocation) throws IOException {
    // Optional shade relocation, on input or output names
    MappingTransformer inputTransformer = null;
    MappingTransformer outputTransformer = null;
    if (inShadeRelocation != null) {
        inputTransformer = new MavenShade(inShadeRelocation);
    }
    if (outShadeRelocation != null) {
        outputTransformer = new MavenShade(outShadeRelocation);
    }
    if (new File(filename).isDirectory() || filename.endsWith("/")) {
        if (inputTransformer != null || outputTransformer != null) {
            // yet
            throw new IllegalArgumentException("loadMappings(" + filename + "): shade relocation not supported on directories");
        }
        loadMappingsDir(filename, reverse, false, numericSrgNames);
    } else {
        if (numericSrgNames) {
            throw new IllegalArgumentException("loadMappings(" + filename + "): numeric only supported on directories, not files");
        }
        try (BufferedReader reader = new BufferedReader(new FileReader(FileLocator.getFile(filename)))) {
            loadMappings(reader, inputTransformer, outputTransformer, reverse);
        }
    }
}
Also used : MappingTransformer(net.md_5.specialsource.transformer.MappingTransformer) MavenShade(net.md_5.specialsource.transformer.MavenShade)

Aggregations

MappingTransformer (net.md_5.specialsource.transformer.MappingTransformer)2 ChainingTransformer (net.md_5.specialsource.transformer.ChainingTransformer)1 MavenShade (net.md_5.specialsource.transformer.MavenShade)1 MinecraftCodersPack (net.md_5.specialsource.transformer.MinecraftCodersPack)1