Text api: Real "obfuscating", config settings for text api

This commit is contained in:
Bixilon 2020-12-16 19:46:22 +01:00
parent b5bc70ab7c
commit a1e7c8cbf7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 49 additions and 9 deletions

View File

@ -214,6 +214,8 @@ public class Configuration {
case NETWORK_FAKE_CLIENT_BRAND -> json.getAsJsonObject("network").get("fake-network-brand").getAsBoolean(); case NETWORK_FAKE_CLIENT_BRAND -> json.getAsJsonObject("network").get("fake-network-brand").getAsBoolean();
case NETWORK_SHOW_LAN_SERVERS -> json.getAsJsonObject("network").get("show-lan-servers").getAsBoolean(); case NETWORK_SHOW_LAN_SERVERS -> json.getAsJsonObject("network").get("show-lan-servers").getAsBoolean();
case DEBUG_VERIFY_ASSETS -> json.getAsJsonObject("debug").get("verify-assets").getAsBoolean(); case DEBUG_VERIFY_ASSETS -> json.getAsJsonObject("debug").get("verify-assets").getAsBoolean();
case CHAT_COLORED -> json.getAsJsonObject("chat").get("colored").getAsBoolean();
case CHAT_OBFUSCATED -> json.getAsJsonObject("chat").get("obfuscated").getAsBoolean();
}; };
} }
if (path instanceof ConfigurationPaths.IntegerPaths integerPath) { if (path instanceof ConfigurationPaths.IntegerPaths integerPath) {
@ -240,6 +242,8 @@ public class Configuration {
case NETWORK_FAKE_CLIENT_BRAND -> input.getAsJsonObject("network").addProperty("fake-network-brand", bool); case NETWORK_FAKE_CLIENT_BRAND -> input.getAsJsonObject("network").addProperty("fake-network-brand", bool);
case NETWORK_SHOW_LAN_SERVERS -> input.getAsJsonObject("network").addProperty("show-lan-servers", bool); case NETWORK_SHOW_LAN_SERVERS -> input.getAsJsonObject("network").addProperty("show-lan-servers", bool);
case DEBUG_VERIFY_ASSETS -> input.getAsJsonObject("debug").addProperty("verify-assets", bool); case DEBUG_VERIFY_ASSETS -> input.getAsJsonObject("debug").addProperty("verify-assets", bool);
case CHAT_COLORED -> input.getAsJsonObject("chat").addProperty("colored", bool);
case CHAT_OBFUSCATED -> input.getAsJsonObject("chat").addProperty("obfuscated", bool);
} }
} else if (data instanceof Integer integer) { } else if (data instanceof Integer integer) {
switch ((ConfigurationPaths.IntegerPaths) path) { switch ((ConfigurationPaths.IntegerPaths) path) {

View File

@ -36,7 +36,9 @@ public abstract class ConfigurationPaths {
public enum BooleanPaths implements ConfigurationPath { public enum BooleanPaths implements ConfigurationPath {
NETWORK_FAKE_CLIENT_BRAND, NETWORK_FAKE_CLIENT_BRAND,
NETWORK_SHOW_LAN_SERVERS, NETWORK_SHOW_LAN_SERVERS,
DEBUG_VERIFY_ASSETS DEBUG_VERIFY_ASSETS,
CHAT_COLORED,
CHAT_OBFUSCATED
} }
public enum IntegerPaths implements ConfigurationPath { public enum IntegerPaths implements ConfigurationPath {

View File

@ -13,7 +13,10 @@
package de.bixilon.minosoft.data.text; package de.bixilon.minosoft.data.text;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.config.ConfigurationPaths;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition; import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.Util;
import de.bixilon.minosoft.util.hash.BetterHashSet; import de.bixilon.minosoft.util.hash.BetterHashSet;
import javafx.animation.Animation; import javafx.animation.Animation;
import javafx.animation.KeyFrame; import javafx.animation.KeyFrame;
@ -162,18 +165,30 @@ public class TextComponent extends ChatComponent {
@Override @Override
public ObservableList<Node> getJavaFXText(ObservableList<Node> nodes) { public ObservableList<Node> getJavaFXText(ObservableList<Node> nodes) {
Text text = new Text(this.text); Text text = new Text(this.text);
if (this.color == null) { text.setFill(Color.WHITE);
text.setFill(Color.WHITE); if (Minosoft.getConfig().getBoolean(ConfigurationPaths.BooleanPaths.CHAT_COLORED) && this.color != null) {
} else {
text.setFill(Color.web(this.color.toString())); text.setFill(Color.web(this.color.toString()));
} }
this.formatting.forEach((chatFormattingCode -> { this.formatting.forEach((chatFormattingCode -> {
if (chatFormattingCode instanceof PreChatFormattingCodes code) { if (chatFormattingCode instanceof PreChatFormattingCodes code) {
switch (code) { switch (code) {
case OBFUSCATED -> { case OBFUSCATED -> {
Timeline flasher = new Timeline(new KeyFrame(Duration.seconds(1), e -> text.setVisible(false)), new KeyFrame(Duration.seconds(2), e -> text.setVisible(true))); // ToDo: potential memory leak: Stop timeline, when TextComponent isn't shown anymore
flasher.setCycleCount(Animation.INDEFINITE); Timeline obfuscatedTimeline;
flasher.play(); if (Minosoft.getConfig().getBoolean(ConfigurationPaths.BooleanPaths.CHAT_OBFUSCATED)) {
obfuscatedTimeline = new Timeline(new KeyFrame(Duration.millis(50), e -> {
char[] chars = text.getText().toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] = Util.getRandomChar(ProtocolDefinition.OBFUSCATED_CHARS);
}
text.setText(new String(chars));
}));
} else {
obfuscatedTimeline = new Timeline(new KeyFrame(Duration.millis(500), e -> text.setVisible(false)), new KeyFrame(Duration.millis(1000), e -> text.setVisible(true)));
}
obfuscatedTimeline.setCycleCount(Animation.INDEFINITE);
obfuscatedTimeline.play();
text.getStyleClass().add("obfuscated");
} }
case BOLD -> text.setStyle("-fx-font-weight: bold;"); case BOLD -> text.setStyle("-fx-font-weight: bold;");
case STRIKETHROUGH -> text.setStyle("-fx-strikethrough: true;"); case STRIKETHROUGH -> text.setStyle("-fx-strikethrough: true;");

View File

@ -54,6 +54,9 @@ public final class ProtocolDefinition {
public static final int SECTIONS_PER_CHUNK = 16; public static final int SECTIONS_PER_CHUNK = 16;
public static final int BLOCKS_PER_SECTION = SECTION_WIDTH_X * SECTION_HEIGHT_Y * SECTION_WIDTH_X; public static final int BLOCKS_PER_SECTION = SECTION_WIDTH_X * SECTION_HEIGHT_Y * SECTION_WIDTH_X;
public static final char[] OBFUSCATED_CHARS = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".toCharArray();
static { static {
// java does (why ever) not allow to directly assign a null // java does (why ever) not allow to directly assign a null
InetAddress temp; InetAddress temp;

View File

@ -37,7 +37,7 @@ import java.util.zip.*;
public final class Util { public final class Util {
public static final Pattern UUID_FIX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); // thanks https://www.spigotmc.org/threads/free-code-easily-convert-between-trimmed-and-full-uuids.165615 public static final Pattern UUID_FIX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); // thanks https://www.spigotmc.org/threads/free-code-easily-convert-between-trimmed-and-full-uuids.165615
public static final String RANDOM_STRING_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; public static final char[] RANDOM_STRING_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
public static final String LINE_SEPARATOR = System.getProperty("line.separator"); public static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final Random THREAD_LOCAL_RANDOM = ThreadLocalRandom.current(); private static final Random THREAD_LOCAL_RANDOM = ThreadLocalRandom.current();
@ -264,11 +264,19 @@ public final class Util {
public static String generateRandomString(int length) { public static String generateRandomString(int length) {
StringBuilder sb = new StringBuilder(length); StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
sb.append(RANDOM_STRING_CHARS.charAt(THREAD_LOCAL_RANDOM.nextInt(RANDOM_STRING_CHARS.length()))); sb.append(getRandomChar(RANDOM_STRING_CHARS));
} }
return sb.toString(); return sb.toString();
} }
public static char getRandomChar(char[] chars) {
return chars[(THREAD_LOCAL_RANDOM.nextInt(chars.length))];
}
public static char getRandomChar() {
return (char) THREAD_LOCAL_RANDOM.nextInt();
}
public static String getStringBetween(String search, String first, String second) { public static String getStringBetween(String search, String first, String second) {
String result = search.substring(search.indexOf(first) + first.length()); String result = search.substring(search.indexOf(first) + first.length());
return result.substring(0, result.indexOf(second)); return result.substring(0, result.indexOf(second));

View File

@ -7,6 +7,10 @@
"game": { "game": {
"render-distance": 12 "render-distance": 12
}, },
"chat": {
"colored": true,
"obfuscated": true
},
"network": { "network": {
"fake-network-brand": false, "fake-network-brand": false,
"show-lan-servers": true "show-lan-servers": true

View File

@ -198,3 +198,7 @@
.ping-no-connection { .ping-no-connection {
-fx-text-fill: red; -fx-text-fill: red;
} }
.obfuscated {
-fx-font-family: "Hack";
}