Fix writing server.properties with wrong encoding (#6322)

Fixes #6321
This commit is contained in:
Prof-Bloodstone 2021-08-04 09:48:15 +02:00 committed by GitHub
parent 6226208d64
commit d239eca9dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,21 +8,18 @@ Makes less git noise, as it won't update the date every single time
Use -DPaper.skipServerPropertiesComments=true flag to disable writing it Use -DPaper.skipServerPropertiesComments=true flag to disable writing it
diff --git a/src/main/java/net/minecraft/server/dedicated/Settings.java b/src/main/java/net/minecraft/server/dedicated/Settings.java diff --git a/src/main/java/net/minecraft/server/dedicated/Settings.java b/src/main/java/net/minecraft/server/dedicated/Settings.java
index 745a6735fba8343329ec31723da914fad16ae134..2713d9acc51c6c5cc51bdbbe63477343ecb29154 100644 index 745a6735fba8343329ec31723da914fad16ae134..232c305b63df19ea923772192c97de65411531dd 100644
--- a/src/main/java/net/minecraft/server/dedicated/Settings.java --- a/src/main/java/net/minecraft/server/dedicated/Settings.java
+++ b/src/main/java/net/minecraft/server/dedicated/Settings.java +++ b/src/main/java/net/minecraft/server/dedicated/Settings.java
@@ -1,9 +1,11 @@ @@ -1,6 +1,8 @@
package net.minecraft.server.dedicated; package net.minecraft.server.dedicated;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
+import java.io.BufferedWriter; // Paper +
+import java.io.BufferedOutputStream; // Paper
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
+import java.io.OutputStreamWriter; // Paper
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
@@ -18,11 +20,13 @@ import org.apache.logging.log4j.Logger; @@ -18,11 +20,13 @@ import org.apache.logging.log4j.Logger;
import joptsimple.OptionSet; // CraftBukkit import joptsimple.OptionSet; // CraftBukkit
@ -37,32 +34,25 @@ index 745a6735fba8343329ec31723da914fad16ae134..2713d9acc51c6c5cc51bdbbe63477343
// CraftBukkit start // CraftBukkit start
private OptionSet options = null; private OptionSet options = null;
@@ -79,9 +83,54 @@ public abstract class Settings<T extends Settings<T>> { @@ -79,9 +83,47 @@ public abstract class Settings<T extends Settings<T>> {
} }
// CraftBukkit end // CraftBukkit end
OutputStream outputstream = Files.newOutputStream(path); OutputStream outputstream = Files.newOutputStream(path);
+ // Paper start - disable writing comments to properties file + // Paper start - disable writing comments to properties file
+ OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputstream); + BufferedOutputStream bufferedOutputStream = !skipComments ? new BufferedOutputStream(outputstream) : new BufferedOutputStream(outputstream) {
+ BufferedWriter bufferedwriter = !skipComments ? new BufferedWriter(outputStreamWriter) : new BufferedWriter(outputStreamWriter) {
+ private boolean isRightAfterNewline = true; // If last written char was newline + private boolean isRightAfterNewline = true; // If last written char was newline
+ private boolean isComment = false; // Are we writing comment currently? + private boolean isComment = false; // Are we writing comment currently?
+ +
+ @Override + @Override
+ public void write(@NotNull String str) throws IOException { + public void write(@NotNull byte[] b) throws IOException {
+ char[] ch = str.toCharArray(); + this.write(b, 0, b.length);
+ this.write(ch);
+ } + }
+ +
+ @Override + @Override
+ public void write(@NotNull char[] cbuf) throws IOException { + public void write(@NotNull byte[] bbuf, int off, int len) throws IOException {
+ this.write(cbuf, 0, cbuf.length);
+ }
+
+ @Override
+ public void write(@NotNull char[] cbuf, int off, int len) throws IOException {
+ int latest_offset = off; // The latest offset, updated when comment ends + int latest_offset = off; // The latest offset, updated when comment ends
+ for (int index = off; index < off + len; ++index ) { + for (int index = off; index < off + len; ++index ) {
+ char c = cbuf[index]; + byte c = bbuf[index];
+ boolean isNewline = (c == '\n' || c == '\r'); + boolean isNewline = (c == '\n' || c == '\r');
+ if (isNewline && isComment) { + if (isNewline && isComment) {
+ // Comment has ended + // Comment has ended
@ -73,7 +63,7 @@ index 745a6735fba8343329ec31723da914fad16ae134..2713d9acc51c6c5cc51bdbbe63477343
+ isComment = true; + isComment = true;
+ if (index != latest_offset) { + if (index != latest_offset) {
+ // We got some non-comment data earlier + // We got some non-comment data earlier
+ super.write(cbuf, latest_offset, index-latest_offset); + super.write(bbuf, latest_offset, index-latest_offset);
+ } + }
+ } + }
+ isRightAfterNewline = isNewline; // Store for next iteration + isRightAfterNewline = isNewline; // Store for next iteration
@ -81,7 +71,7 @@ index 745a6735fba8343329ec31723da914fad16ae134..2713d9acc51c6c5cc51bdbbe63477343
+ } + }
+ if (latest_offset < off+len && !isComment) { + if (latest_offset < off+len && !isComment) {
+ // We have some unwritten data, that isn't part of a comment + // We have some unwritten data, that isn't part of a comment
+ super.write(cbuf, latest_offset, (off + len) - latest_offset); + super.write(bbuf, latest_offset, (off + len) - latest_offset);
+ } + }
+ } + }
+ }; + };
@ -89,7 +79,7 @@ index 745a6735fba8343329ec31723da914fad16ae134..2713d9acc51c6c5cc51bdbbe63477343
try { try {
- this.properties.store(outputstream, "Minecraft server properties"); - this.properties.store(outputstream, "Minecraft server properties");
+ this.properties.store(bufferedwriter, "Minecraft server properties"); // Paper - use bufferedwriter + this.properties.store(bufferedOutputStream, "Minecraft server properties"); // Paper - use bufferedOutputStream
} catch (Throwable throwable) { } catch (Throwable throwable) {
if (outputstream != null) { if (outputstream != null) {
try { try {