summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/utils/Utils.java
diff options
context:
space:
mode:
authorGravatar Joseph Burton2020-05-03 21:06:38 +0100
committerGravatar GitHub2020-05-03 21:06:38 +0100
commit854f4d49407e45d67dd5754afd21a7e59970ca5b (patch)
tree582e3245786f9723b5895b0c8d41087b0e6bb416 /src/main/java/cuchaz/enigma/utils/Utils.java
parentRewrite search dialog (#233) (diff)
downloadenigma-fork-854f4d49407e45d67dd5754afd21a7e59970ca5b.tar.gz
enigma-fork-854f4d49407e45d67dd5754afd21a7e59970ca5b.tar.xz
enigma-fork-854f4d49407e45d67dd5754afd21a7e59970ca5b.zip
Multiplayer support (#221)
* First pass on multiplayer * Apply review suggestions * Dedicated Enigma server * Don't jump to references when other users do stuff * Better UI + translations * french translation * Apply review suggestions * Document the protocol * Fix most issues with scrolling. * Apply review suggestions * Fix zip hash issues + add a bit more logging * Optimize zip hash * Fix a couple of login bugs * Add message log and user list * Make Message an abstract class * Make status bar work, add chat box * Hide message log/users list when not connected * Fix status bar not resetting entirely * Run stop server task on server thread to prevent multithreading race conditions * Add c2s message to packet id list * Fix message scroll bar not scrolling to the end * Formatting * User list size -> ushort * Combine contains and remove check * Check removal before sending packet * Add password to login packet * Fix the GUI closing the rename text field when someone else renames something * Update fr_fr.json * oups * Make connection/server create dialogs not useless if it fails once * Refactor UI state updating * Fix imports * Fix Collab menu * Fix NPE when rename not allowed * Make the log file a configurable option * Don't use modified UTF * Update fr_fr.json * Bump version to 0.15.4 * Apparently I can't spell neither words nor semantic versions Co-authored-by: Yanis48 <doublecraft.official@gmail.com> Co-authored-by: 2xsaiko <git@dblsaiko.net>
Diffstat (limited to 'src/main/java/cuchaz/enigma/utils/Utils.java')
-rw-r--r--src/main/java/cuchaz/enigma/utils/Utils.java64
1 files changed, 61 insertions, 3 deletions
diff --git a/src/main/java/cuchaz/enigma/utils/Utils.java b/src/main/java/cuchaz/enigma/utils/Utils.java
index b8f2ec2..b45b00d 100644
--- a/src/main/java/cuchaz/enigma/utils/Utils.java
+++ b/src/main/java/cuchaz/enigma/utils/Utils.java
@@ -15,6 +15,8 @@ import com.google.common.io.CharStreams;
15import org.objectweb.asm.Opcodes; 15import org.objectweb.asm.Opcodes;
16 16
17import javax.swing.*; 17import javax.swing.*;
18import javax.swing.text.BadLocationException;
19import javax.swing.text.JTextComponent;
18import java.awt.*; 20import java.awt.*;
19import java.awt.event.MouseEvent; 21import java.awt.event.MouseEvent;
20import java.io.IOException; 22import java.io.IOException;
@@ -22,13 +24,16 @@ import java.io.InputStream;
22import java.io.InputStreamReader; 24import java.io.InputStreamReader;
23import java.net.URI; 25import java.net.URI;
24import java.net.URISyntaxException; 26import java.net.URISyntaxException;
27import java.nio.charset.StandardCharsets;
25import java.nio.file.Files; 28import java.nio.file.Files;
26import java.nio.file.Path; 29import java.nio.file.Path;
27import java.util.Comparator; 30import java.security.MessageDigest;
31import java.security.NoSuchAlgorithmException;
32import java.util.*;
28import java.util.List; 33import java.util.List;
29import java.util.Locale;
30import java.util.StringJoiner;
31import java.util.stream.Collectors; 34import java.util.stream.Collectors;
35import java.util.zip.ZipEntry;
36import java.util.zip.ZipFile;
32 37
33public class Utils { 38public class Utils {
34 39
@@ -98,6 +103,19 @@ public class Utils {
98 manager.setInitialDelay(oldDelay); 103 manager.setInitialDelay(oldDelay);
99 } 104 }
100 105
106 public static Rectangle safeModelToView(JTextComponent component, int modelPos) {
107 if (modelPos < 0) {
108 modelPos = 0;
109 } else if (modelPos >= component.getText().length()) {
110 modelPos = component.getText().length();
111 }
112 try {
113 return component.modelToView(modelPos);
114 } catch (BadLocationException e) {
115 throw new RuntimeException(e);
116 }
117 }
118
101 public static boolean getSystemPropertyAsBoolean(String property, boolean defValue) { 119 public static boolean getSystemPropertyAsBoolean(String property, boolean defValue) {
102 String value = System.getProperty(property); 120 String value = System.getProperty(property);
103 return value == null ? defValue : Boolean.parseBoolean(value); 121 return value == null ? defValue : Boolean.parseBoolean(value);
@@ -111,6 +129,34 @@ public class Utils {
111 } 129 }
112 } 130 }
113 131
132 public static byte[] zipSha1(Path path) throws IOException {
133 MessageDigest digest;
134 try {
135 digest = MessageDigest.getInstance("SHA-1");
136 } catch (NoSuchAlgorithmException e) {
137 // Algorithm guaranteed to be supported
138 throw new RuntimeException(e);
139 }
140 try (ZipFile zip = new ZipFile(path.toFile())) {
141 List<? extends ZipEntry> entries = Collections.list(zip.entries());
142 // only compare classes (some implementations may not generate directory entries)
143 entries.removeIf(entry -> !entry.getName().toLowerCase(Locale.ROOT).endsWith(".class"));
144 // different implementations may add zip entries in a different order
145 entries.sort(Comparator.comparing(ZipEntry::getName));
146 byte[] buffer = new byte[8192];
147 for (ZipEntry entry : entries) {
148 digest.update(entry.getName().getBytes(StandardCharsets.UTF_8));
149 try (InputStream in = zip.getInputStream(entry)) {
150 int n;
151 while ((n = in.read(buffer)) != -1) {
152 digest.update(buffer, 0, n);
153 }
154 }
155 }
156 }
157 return digest.digest();
158 }
159
114 public static String caplisiseCamelCase(String input){ 160 public static String caplisiseCamelCase(String input){
115 StringJoiner stringJoiner = new StringJoiner(" "); 161 StringJoiner stringJoiner = new StringJoiner(" ");
116 for (String word : input.toLowerCase(Locale.ROOT).split("_")) { 162 for (String word : input.toLowerCase(Locale.ROOT).split("_")) {
@@ -118,4 +164,16 @@ public class Utils {
118 } 164 }
119 return stringJoiner.toString(); 165 return stringJoiner.toString();
120 } 166 }
167
168 public static boolean isBlank(String input) {
169 if (input == null) {
170 return true;
171 }
172 for (int i = 0; i < input.length(); i++) {
173 if (!Character.isWhitespace(input.charAt(i))) {
174 return false;
175 }
176 }
177 return true;
178 }
121} 179}