Skip to content

Commit dbf4c0a

Browse files
authored
Introduced sourceFileEncoding setting to config.json (#730)
* Introduced sourceFileEncoding setting to config.json - `sourceFileEncoding` setting added to `UserdevConfigV2`. Kept as `String` to make serialisation easier. Could be Charset but would then require custom serialisation logic. - `sourceFileEncoding` added to `TaskGenerateUserdevConfig`. This one is a `Charset` because type safety in build scripts is a good idea, but I added an overload taking `String` for convenience of people who don't care. - `Deobfuscator` now uses UTF-8 - it looks like the string it's writing was originally read as UTF-8 in the method called on the line above.
1 parent c077542 commit dbf4c0a

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

src/common/java/net/minecraftforge/gradle/common/config/UserdevConfigV2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.io.ByteArrayInputStream;
2424
import java.io.InputStream;
25+
import java.nio.charset.Charset;
26+
import java.nio.charset.StandardCharsets;
2527
import java.util.ArrayList;
2628
import java.util.Collections;
2729
import java.util.HashMap;
@@ -44,6 +46,7 @@ public static UserdevConfigV2 get(byte[] data) {
4446
public String patchesModifiedPrefix;
4547
private Boolean notchObf; //This is a Boolean so we can set to null and it won't be printed in the json.
4648
private List<String> universalFilters;
49+
private String sourceFileCharset = StandardCharsets.UTF_8.name();
4750

4851
public void setNotchObf(boolean value) {
4952
this.notchObf = value ? true : null;
@@ -53,6 +56,17 @@ public boolean getNotchObf() {
5356
return this.notchObf == null ? false : this.notchObf;
5457
}
5558

59+
public void setSourceFileCharset(String value) {
60+
if (!Charset.isSupported(value)) {
61+
throw new IllegalArgumentException("Unsupported charset: " + value);
62+
}
63+
sourceFileCharset = value;
64+
}
65+
66+
public String getSourceFileCharset() {
67+
return sourceFileCharset;
68+
}
69+
5670
public void addUniversalFilter(String value) {
5771
if (universalFilters == null)
5872
universalFilters = new ArrayList<>();

src/common/java/net/minecraftforge/gradle/common/util/McpNames.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020

2121
package net.minecraftforge.gradle.common.util;
2222

23-
import java.io.BufferedReader;
2423
import java.io.File;
2524
import java.io.IOException;
2625
import java.io.InputStream;
2726
import java.io.InputStreamReader;
28-
import java.io.OutputStream;
2927
import java.io.StringReader;
30-
import java.io.StringWriter;
28+
import java.nio.charset.Charset;
3129
import java.nio.charset.StandardCharsets;
3230
import java.util.ArrayList;
3331
import java.util.Deque;
@@ -43,7 +41,6 @@
4341
import java.util.stream.Collectors;
4442
import java.util.zip.ZipEntry;
4543
import java.util.zip.ZipFile;
46-
import java.util.zip.ZipOutputStream;
4744

4845
import de.siegmar.fastcsv.reader.CsvContainer;
4946
import de.siegmar.fastcsv.reader.CsvReader;
@@ -97,21 +94,18 @@ private McpNames(String hash, Map<String, String> names, Map<String, String> doc
9794
}
9895

9996
public String rename(InputStream stream, boolean javadocs) throws IOException {
100-
return rename(stream, javadocs, true);
97+
return rename(stream, javadocs, true, StandardCharsets.UTF_8);
10198
}
10299

103100
public String rename(InputStream stream, boolean javadocs, boolean lambdas) throws IOException {
104-
List<String> input = new ArrayList<>();
105-
StringWriter writer = new StringWriter();
106-
IOUtils.copy(stream, writer, StandardCharsets.UTF_8);
107-
String data = writer.toString();
108-
109-
try (BufferedReader reader = new BufferedReader(new StringReader(data))) {
110-
String line = null;
111-
while ((line = reader.readLine()) != null) {
112-
input.add(line);
113-
}
114-
}
101+
return rename(stream, javadocs, lambdas, StandardCharsets.UTF_8);
102+
}
103+
104+
public String rename(InputStream stream, boolean javadocs, boolean lambdas, Charset sourceFileCharset)
105+
throws IOException {
106+
107+
String data = IOUtils.toString(stream, sourceFileCharset);
108+
List<String> input = IOUtils.readLines(new StringReader(data));
115109

116110
//Reader doesn't give us the empty line if the file ends with a newline.. so add one.
117111
if (data.charAt(data.length() - 1) == '\r' || data.charAt(data.length() - 1) == '\n')

src/patcher/java/net/minecraftforge/gradle/patcher/task/TaskGenerateUserdevConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.File;
2424
import java.io.IOException;
25+
import java.nio.charset.Charset;
2526
import java.nio.charset.StandardCharsets;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
@@ -79,6 +80,7 @@ public class TaskGenerateUserdevConfig extends DefaultTask {
7980
private String patchesModifiedPrefix;
8081
private boolean notchObf = false;
8182
private List<String> universalFilters;
83+
private Charset sourceFileEncoding = StandardCharsets.UTF_8;
8284

8385
@Inject
8486
public TaskGenerateUserdevConfig(@Nonnull final Project project) {
@@ -113,6 +115,7 @@ public void apply() throws IOException {
113115
json.patchesOriginalPrefix = this.patchesOriginalPrefix;
114116
json.patchesModifiedPrefix = this.patchesModifiedPrefix;
115117
json.setNotchObf(this.notchObf);
118+
json.setSourceFileCharset(this.sourceFileEncoding.name());
116119
if (this.universalFilters != null)
117120
this.universalFilters.forEach(json::addUniversalFilter);
118121
}
@@ -339,6 +342,17 @@ public void setNotchObf(boolean value) {
339342
this.notchObf = value;
340343
}
341344

345+
@Input
346+
public Charset getSourceFileEncoding() {
347+
return this.sourceFileEncoding;
348+
}
349+
public void setSourceFileEncoding(Charset value) {
350+
this.sourceFileEncoding = value;
351+
}
352+
public void setSourceFileEncoding(String value) {
353+
this.sourceFileEncoding = Charset.forName(value);
354+
}
355+
342356
@Input
343357
@Optional
344358
public List<String> getUniversalFilters() {

src/userdev/java/net/minecraftforge/gradle/userdev/MinecraftUserRepo.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import java.io.FileOutputStream;
8080
import java.io.IOException;
8181
import java.io.InputStream;
82+
import java.nio.charset.Charset;
8283
import java.nio.charset.StandardCharsets;
8384
import java.nio.file.FileVisitResult;
8485
import java.nio.file.Files;
@@ -503,7 +504,7 @@ private File findPom(String mapping, String rand) throws IOException {
503504
if (ret == null) {
504505
return null;
505506
}
506-
FileUtils.writeByteArrayToFile(pom, ret.getBytes());
507+
FileUtils.writeByteArrayToFile(pom, ret.getBytes(StandardCharsets.UTF_8));
507508
cache.save();
508509
Utils.updateHash(pom, HashFunction.SHA1);
509510
}
@@ -667,7 +668,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
667668
File parentAT = project.file("build/" + at.getName() + "/parent_at.cfg");
668669
if (!parentAT.getParentFile().exists())
669670
parentAT.getParentFile().mkdirs();
670-
Files.write(parentAT.toPath(), baseAT.toString().getBytes());
671+
Files.write(parentAT.toPath(), baseAT.toString().getBytes(StandardCharsets.UTF_8));
671672
at.setAts(parentAT);
672673
}
673674

@@ -878,7 +879,8 @@ private void copyResources(ZipOutputStream zip, Set<String> added, boolean inclu
878879
ZipEntry _new = new ZipEntry(name);
879880
_new.setTime(0);
880881
zip.putNextEntry(_new);
881-
IOUtils.writeLines(kv.getValue(), "\n", zip);
882+
// JAR File Specification requires UTF-8 encoding here
883+
IOUtils.writeLines(kv.getValue(), "\n", zip, StandardCharsets.UTF_8);
882884
added.add(name);
883885
}
884886
}
@@ -1142,6 +1144,8 @@ private File findSource(String mapping, boolean generate) throws IOException {
11421144
sources.getParentFile().mkdirs();
11431145

11441146
boolean addJavadocs = parent == null || parent.getConfigV2() == null || parent.getConfigV2().processor == null;
1147+
Charset sourceFileCharset = parent == null || parent.getConfigV2() == null ? StandardCharsets.UTF_8 :
1148+
Charset.forName(parent.getConfigV2().getSourceFileCharset());
11451149
debug(" Renaming Sources, Javadocs: " + addJavadocs);
11461150
try(ZipInputStream zin = new ZipInputStream(new FileInputStream(patched));
11471151
ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(sources))) {
@@ -1151,8 +1155,10 @@ private File findSource(String mapping, boolean generate) throws IOException {
11511155
zout.putNextEntry(Utils.getStableEntry(name));
11521156

11531157
if (name.endsWith(".java")) {
1154-
String mapped = map.rename(zin, addJavadocs && vanilla.contains(name.substring(0, name.length() - 5)));
1155-
IOUtils.write(mapped, zout);
1158+
String mapped = map.rename(zin,
1159+
addJavadocs && vanilla.contains(name.substring(0, name.length() - 5)),
1160+
true, sourceFileCharset);
1161+
IOUtils.write(mapped, zout, sourceFileCharset);
11561162
} else {
11571163
IOUtils.copy(zin, zout);
11581164
}

src/userdev/java/net/minecraftforge/gradle/userdev/util/Deobfuscator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.io.FileInputStream;
4848
import java.io.FileOutputStream;
4949
import java.io.IOException;
50+
import java.nio.charset.StandardCharsets;
5051
import java.util.Random;
5152
import java.util.zip.ZipEntry;
5253
import java.util.zip.ZipInputStream;
@@ -165,7 +166,7 @@ public File deobfSources(File original, String mappings, String... cachePath) th
165166

166167
if (_old.getName().endsWith(".java")) {
167168
String mapped = map.rename(zin, false);
168-
IOUtils.write(mapped, zout);
169+
IOUtils.write(mapped, zout, StandardCharsets.UTF_8);
169170
} else {
170171
IOUtils.copy(zin, zout);
171172
}

0 commit comments

Comments
 (0)