2323import java .io .File ;
2424import java .io .FileOutputStream ;
2525import java .io .IOException ;
26- import java .io .InputStream ;
27- import java .io .InputStreamReader ;
28- import java .io .StringWriter ;
2926import java .nio .charset .StandardCharsets ;
30- import java .util .HashMap ;
31- import java .util .Map ;
32- import java .util .regex .Matcher ;
33- import java .util .regex .Pattern ;
3427import java .util .zip .ZipFile ;
3528import java .util .zip .ZipOutputStream ;
3629
4033import org .gradle .api .tasks .OutputFile ;
4134import org .gradle .api .tasks .TaskAction ;
4235
43- import de .siegmar .fastcsv .reader .CsvContainer ;
44- import de .siegmar .fastcsv .reader .CsvReader ;
45- import de .siegmar .fastcsv .reader .CsvRow ;
36+ import net .minecraftforge .gradle .common .util .McpNames ;
4637import net .minecraftforge .gradle .common .util .Utils ;
4738
4839public class TaskApplyMappings extends DefaultTask {
49- private static final Pattern SRG = Pattern .compile ("func_[0-9]+_[a-zA-Z_]+|field_[0-9]+_[a-zA-Z_]+|p_[\\ w]+_\\ d+_\\ b" );
50-
5140 private File mappings ;
5241 private File input ;
5342 private File output = getProject ().file ("build/" + getName () + "/output.zip" );
5443
5544 @ TaskAction
5645 public void apply () throws IOException {
57- Map <String , String > names = new HashMap <>();
58- try (ZipFile zip = new ZipFile (getMappings ())) {
59- zip .stream ().filter (e -> e .getName ().endsWith (".csv" )).forEach (e -> {
60- CsvReader reader = new CsvReader ();
61- reader .setContainsHeader (true );
62- try {
63- CsvContainer csv = reader .read (new InputStreamReader (zip .getInputStream (e )));
64- for (CsvRow row : csv .getRows ()) {
65- if (row .getField ("searge" ) != null ) {
66- names .put (row .getField ("searge" ), row .getField ("name" ));
67- } else {
68- names .put (row .getField ("param" ), row .getField ("name" ));
69- }
70- }
71- } catch (IOException e1 ) {
72- throw new RuntimeException (e1 );
73- }
74- });
75- }
46+ McpNames names = McpNames .load (getMappings ());
7647
7748 try (ZipFile zin = new ZipFile (getInput ())) {
7849 try (FileOutputStream fos = new FileOutputStream (getOutput ());
@@ -83,7 +54,7 @@ public void apply() throws IOException {
8354 if (!e .getName ().endsWith (".java" )) {
8455 IOUtils .copy (zin .getInputStream (e ), out );
8556 } else {
86- out .write (replace (zin .getInputStream (e ), names ).getBytes (StandardCharsets .UTF_8 ));
57+ out .write (names . rename (zin .getInputStream (e ), false ).getBytes (StandardCharsets .UTF_8 ));
8758 }
8859 out .closeEntry ();
8960 } catch (IOException e1 ) {
@@ -94,20 +65,6 @@ public void apply() throws IOException {
9465 }
9566 }
9667
97- private String replace (InputStream stream , Map <String , String > names ) throws IOException {
98- StringWriter writer = new StringWriter ();
99- IOUtils .copy (stream , writer , StandardCharsets .UTF_8 );
100- StringBuffer buf = new StringBuffer ();
101- Matcher matcher = SRG .matcher (writer .toString ());
102- while (matcher .find ()) {
103- String name = names .get (matcher .group ());
104- matcher .appendReplacement (buf , name == null ? matcher .group () : name );
105- }
106- matcher .appendTail (buf );
107- return buf .toString ();
108- }
109-
110-
11168 @ InputFile
11269 public File getInput () {
11370 return input ;
0 commit comments