<commit_before>package net.minecraftforge.gradle.tasks;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.minecraftforge.gradle.delayed.DelayedFile;
import net.minecraftforge.gradle.tasks.abstractutil.CachedTask;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
import net.minecraftforge.srg2source.rangeapplier.SrgContainer;

import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import au.com.bytecode.opencsv.CSVReader;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class GenSrgTask extends CachedTask
{
    @InputFile
    private DelayedFile inSrg;
    @InputFile
    private DelayedFile inExc;

    @InputFiles
    private final LinkedList<File> extraExcs = new LinkedList<File>();
    @InputFiles
    private final LinkedList<File> extraSrgs = new LinkedList<File>();

    @InputFile
    private DelayedFile methodsCsv;
    @InputFile
    private DelayedFile fieldsCsv;
    
    @Cached
    @OutputFile
    private DelayedFile notchToSrg;

    @Cached
    @OutputFile
    private DelayedFile notchToMcp;

    @Cached
    @OutputFile
    private DelayedFile mcpToNotch;
    
    @Cached
    @OutputFile
    private DelayedFile mcpToSrg;
    
    @Cached
    @OutputFile
    private DelayedFile srgExc;
    
    @Cached
    @OutputFile
    private DelayedFile mcpExc;
    
    @TaskAction
    public void doTask() throws IOException
    {
        // csv data.  SRG -> MCP
        HashMap<String, String> methods = new HashMap<String, String>();
        HashMap<String, String> fields = new HashMap<String, String>();
        readCSVs(getMethodsCsv(), getFieldsCsv(), methods, fields);
        
        // Do SRG stuff
        SrgContainer inSrg = new SrgContainer().readSrg(getInSrg());
        Map<String, String> excRemap = readExtraSrgs(getExtraSrgs(), inSrg);
        writeOutSrgs(inSrg, methods, fields);
        
        // do EXC stuff
        writeOutExcs(excRemap, methods);
        
    }
    
    private static void readCSVs(File methodCsv, File fieldCsv, Map<String, String> methodMap, Map<String, String> fieldMap) throws IOException
    {
        
        // read methods
        CSVReader csvReader = RemapSourcesTask.getReader(methodCsv);
        for (String[] s : csvReader.readAll())
        {
            methodMap.put(s[0], s[1]);
        }

        // read fields
        csvReader = RemapSourcesTask.getReader(fieldCsv);
        for (String[] s : csvReader.readAll())
        {
            fieldMap.put(s[0], s[1]);
        }
    }
    
    private static Map<String, String> readExtraSrgs(FileCollection extras, SrgContainer inSrg)
    {
        SrgContainer extraSrg = new SrgContainer().readSrgs(extras);
        // Need to convert these to Notch-SRG names. and add them to the other one.
        
        HashMap<String, String> excRemap = new HashMap<String, String>(extraSrg.methodMap.size());
        
        // SRG -> notch map
        Map<String, String> classMap = inSrg.classMap.inverse();
        Map<MethodData, MethodData> methodMap = inSrg.methodMap.inverse();
        
        // rename methods
        for (Entry<MethodData, MethodData> e : extraSrg.methodMap.entrySet())
        {
            String notchName = remapMethodName(e.getValue().name, methodMap);
            String notchSig = remapSig(e.getValue().sig, classMap);
            inSrg.methodMap.put(new MethodData(notchName, notchSig), e.getValue());
            excRemap.put(e.getKey().name, e.getValue().name);
        }
        
        return excRemap;
    }
    
    private void writeOutSrgs(SrgContainer inSrg, Map<String, String> methods, Map<String, String> fields) throws IOException
    {
        // ensure folders exist
        Files.createParentDirs(getNotchToSrg());
        Files.createParentDirs(getNotchToMcp());
        Files.createParentDirs(getMcpToSrg());
        Files.createParentDirs(getMcpToNotch());
        
        // create streams
        BufferedWriter notch2Srg = Files.newWriter(getNotchToSrg(), Charsets.UTF_8);
        BufferedWriter notch2Mcp = Files.newWriter(getNotchToMcp(), Charsets.UTF_8);
        BufferedWriter mcpToSrg = Files.newWriter(getMcpToSrg(), Charsets.UTF_8);
        BufferedWriter mcpToNotch = Files.newWriter(getMcpToNotch(), Charsets.UTF_8);
        
        String line, temp, mcpName;
        // packages
        for (Entry<String, String> e : inSrg.packageMap.entrySet())
        {
            line = "PK: "+e.getKey()+" "+e.getValue();
            
            // nobody cares about the packages.
            notch2Srg.write(line);
            notch2Srg.newLine();
            
            notch2Mcp.write(line);
            notch2Mcp.newLine();
            
            mcpToSrg.write(line);
            mcpToSrg.newLine();
            
            mcpToNotch.write(line);
            mcpToNotch.newLine();
        }
        
        // classes
        for (Entry<String, String> e : inSrg.classMap.entrySet())
        {
            line = "CL: "+e.getKey()+" "+e.getValue();
            
            // same...
            notch2Srg.write(line);
            notch2Srg.newLine();
            
            // SRG and MCP have the same class names
            notch2Mcp.write(line);
            notch2Mcp.newLine();
            
            line = "CL: "+e.getValue()+" "+e.getValue();
            
            // reobf: same classes on both sides.
            mcpToSrg.write("CL: "+e.getValue()+" "+e.getValue());
            mcpToSrg.newLine();
            
            // output is notch
            mcpToNotch.write("CL: "+e.getValue()+" "+e.getKey());
            mcpToNotch.newLine();<commit_msg>fixed package SRG lines for reobfuscation<commit_after>package net.minecraftforge.gradle.tasks;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.minecraftforge.gradle.delayed.DelayedFile;
import net.minecraftforge.gradle.tasks.abstractutil.CachedTask;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
import net.minecraftforge.srg2source.rangeapplier.SrgContainer;

import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import au.com.bytecode.opencsv.CSVReader;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class GenSrgTask extends CachedTask
{
    @InputFile
    private DelayedFile inSrg;
    @InputFile
    private DelayedFile inExc;

    @InputFiles
    private final LinkedList<File> extraExcs = new LinkedList<File>();
    @InputFiles
    private final LinkedList<File> extraSrgs = new LinkedList<File>();

    @InputFile
    private DelayedFile methodsCsv;
    @InputFile
    private DelayedFile fieldsCsv;
    
    @Cached
    @OutputFile
    private DelayedFile notchToSrg;

    @Cached
    @OutputFile
    private DelayedFile notchToMcp;

    @Cached
    @OutputFile
    private DelayedFile mcpToNotch;
    
    @Cached
    @OutputFile
    private DelayedFile mcpToSrg;
    
    @Cached
    @OutputFile
    private DelayedFile srgExc;
    
    @Cached
    @OutputFile
    private DelayedFile mcpExc;
    
    @TaskAction
    public void doTask() throws IOException
    {
        // csv data.  SRG -> MCP
        HashMap<String, String> methods = new HashMap<String, String>();
        HashMap<String, String> fields = new HashMap<String, String>();
        readCSVs(getMethodsCsv(), getFieldsCsv(), methods, fields);
        
        // Do SRG stuff
        SrgContainer inSrg = new SrgContainer().readSrg(getInSrg());
        Map<String, String> excRemap = readExtraSrgs(getExtraSrgs(), inSrg);
        writeOutSrgs(inSrg, methods, fields);
        
        // do EXC stuff
        writeOutExcs(excRemap, methods);
        
    }
    
    private static void readCSVs(File methodCsv, File fieldCsv, Map<String, String> methodMap, Map<String, String> fieldMap) throws IOException
    {
        
        // read methods
        CSVReader csvReader = RemapSourcesTask.getReader(methodCsv);
        for (String[] s : csvReader.readAll())
        {
            methodMap.put(s[0], s[1]);
        }

        // read fields
        csvReader = RemapSourcesTask.getReader(fieldCsv);
        for (String[] s : csvReader.readAll())
        {
            fieldMap.put(s[0], s[1]);
        }
    }
    
    private static Map<String, String> readExtraSrgs(FileCollection extras, SrgContainer inSrg)
    {
        SrgContainer extraSrg = new SrgContainer().readSrgs(extras);
        // Need to convert these to Notch-SRG names. and add them to the other one.
        
        HashMap<String, String> excRemap = new HashMap<String, String>(extraSrg.methodMap.size());
        
        // SRG -> notch map
        Map<String, String> classMap = inSrg.classMap.inverse();
        Map<MethodData, MethodData> methodMap = inSrg.methodMap.inverse();
        
        // rename methods
        for (Entry<MethodData, MethodData> e : extraSrg.methodMap.entrySet())
        {
            String notchName = remapMethodName(e.getValue().name, methodMap);
            String notchSig = remapSig(e.getValue().sig, classMap);
            inSrg.methodMap.put(new MethodData(notchName, notchSig), e.getValue());
            excRemap.put(e.getKey().name, e.getValue().name);
        }
        
        return excRemap;
    }
    
    private void writeOutSrgs(SrgContainer inSrg, Map<String, String> methods, Map<String, String> fields) throws IOException
    {
        // ensure folders exist
        Files.createParentDirs(getNotchToSrg());
        Files.createParentDirs(getNotchToMcp());
        Files.createParentDirs(getMcpToSrg());
        Files.createParentDirs(getMcpToNotch());
        
        // create streams
        BufferedWriter notch2Srg = Files.newWriter(getNotchToSrg(), Charsets.UTF_8);
        BufferedWriter notch2Mcp = Files.newWriter(getNotchToMcp(), Charsets.UTF_8);
        BufferedWriter mcpToSrg = Files.newWriter(getMcpToSrg(), Charsets.UTF_8);
        BufferedWriter mcpToNotch = Files.newWriter(getMcpToNotch(), Charsets.UTF_8);
        
        String line, temp, mcpName;
        // packages
        for (Entry<String, String> e : inSrg.packageMap.entrySet())
        {
            line = "PK: "+e.getKey()+" "+e.getValue();
            
            // nobody cares about the packages.
            notch2Srg.write(line);
            notch2Srg.newLine();
            
            notch2Mcp.write(line);
            notch2Mcp.newLine();
            
            // No apckage changes from MCP to SRG names
            //mcpToSrg.write(line);
            //mcpToSrg.newLine();
            
            // reverse!
            mcpToNotch.write("PK: "+e.getValue()+" "+e.getKey());
            mcpToNotch.newLine();
        }
        
        // classes
        for (Entry<String, String> e : inSrg.classMap.entrySet())
        {
            line = "CL: "+e.getKey()+" "+e.getValue();
            
            // same...
            notch2Srg.write(line);
            notch2Srg.newLine();
            
            // SRG and MCP have the same class names
            notch2Mcp.write(line);
            notch2Mcp.newLine();
            
            line = "CL: "+e.getValue()+" "+e.getValue();
            
            // reobf: same classes on both sides.
            mcpToSrg.write("CL: "+e.getValue()+" "+e.getValue());
            mcpToSrg.newLine();
            
            // output is notch
            mcpToNotch.write("CL: "+e.getValue()+" "+e.getKey());
            mcpToNotch.newLine();
{'fraction_non_alphanumeric': 0.11006655574043261, 'fraction_numerical': 0.0033277870216306157, 'mean_word_length': 2.297942386831276, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226335', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3683, 'n_words': 741}