diff options
| author | 2020-05-03 21:06:38 +0100 | |
|---|---|---|
| committer | 2020-05-03 21:06:38 +0100 | |
| commit | 854f4d49407e45d67dd5754afd21a7e59970ca5b (patch) | |
| tree | 582e3245786f9723b5895b0c8d41087b0e6bb416 /src/main/java/cuchaz/enigma/utils | |
| parent | Rewrite search dialog (#233) (diff) | |
| download | enigma-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')
| -rw-r--r-- | src/main/java/cuchaz/enigma/utils/Message.java | 392 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/utils/Utils.java | 64 |
2 files changed, 453 insertions, 3 deletions
diff --git a/src/main/java/cuchaz/enigma/utils/Message.java b/src/main/java/cuchaz/enigma/utils/Message.java new file mode 100644 index 0000000..d7c5f23 --- /dev/null +++ b/src/main/java/cuchaz/enigma/utils/Message.java | |||
| @@ -0,0 +1,392 @@ | |||
| 1 | package cuchaz.enigma.utils; | ||
| 2 | |||
| 3 | import java.io.DataInput; | ||
| 4 | import java.io.DataOutput; | ||
| 5 | import java.io.IOException; | ||
| 6 | import java.util.Objects; | ||
| 7 | |||
| 8 | import cuchaz.enigma.network.packet.PacketHelper; | ||
| 9 | import cuchaz.enigma.translation.representation.entry.Entry; | ||
| 10 | |||
| 11 | public abstract class Message { | ||
| 12 | |||
| 13 | public final String user; | ||
| 14 | |||
| 15 | public static Chat chat(String user, String message) { | ||
| 16 | return new Chat(user, message); | ||
| 17 | } | ||
| 18 | |||
| 19 | public static Connect connect(String user) { | ||
| 20 | return new Connect(user); | ||
| 21 | } | ||
| 22 | |||
| 23 | public static Disconnect disconnect(String user) { | ||
| 24 | return new Disconnect(user); | ||
| 25 | } | ||
| 26 | |||
| 27 | public static EditDocs editDocs(String user, Entry<?> entry) { | ||
| 28 | return new EditDocs(user, entry); | ||
| 29 | } | ||
| 30 | |||
| 31 | public static MarkDeobf markDeobf(String user, Entry<?> entry) { | ||
| 32 | return new MarkDeobf(user, entry); | ||
| 33 | } | ||
| 34 | |||
| 35 | public static RemoveMapping removeMapping(String user, Entry<?> entry) { | ||
| 36 | return new RemoveMapping(user, entry); | ||
| 37 | } | ||
| 38 | |||
| 39 | public static Rename rename(String user, Entry<?> entry, String newName) { | ||
| 40 | return new Rename(user, entry, newName); | ||
| 41 | } | ||
| 42 | |||
| 43 | public abstract String translate(); | ||
| 44 | |||
| 45 | public abstract Type getType(); | ||
| 46 | |||
| 47 | public static Message read(DataInput input) throws IOException { | ||
| 48 | byte typeId = input.readByte(); | ||
| 49 | if (typeId < 0 || typeId >= Type.values().length) { | ||
| 50 | throw new IOException(String.format("Invalid message type ID %d", typeId)); | ||
| 51 | } | ||
| 52 | Type type = Type.values()[typeId]; | ||
| 53 | String user = input.readUTF(); | ||
| 54 | switch (type) { | ||
| 55 | case CHAT: | ||
| 56 | String message = input.readUTF(); | ||
| 57 | return chat(user, message); | ||
| 58 | case CONNECT: | ||
| 59 | return connect(user); | ||
| 60 | case DISCONNECT: | ||
| 61 | return disconnect(user); | ||
| 62 | case EDIT_DOCS: | ||
| 63 | Entry<?> entry = PacketHelper.readEntry(input); | ||
| 64 | return editDocs(user, entry); | ||
| 65 | case MARK_DEOBF: | ||
| 66 | entry = PacketHelper.readEntry(input); | ||
| 67 | return markDeobf(user, entry); | ||
| 68 | case REMOVE_MAPPING: | ||
| 69 | entry = PacketHelper.readEntry(input); | ||
| 70 | return removeMapping(user, entry); | ||
| 71 | case RENAME: | ||
| 72 | entry = PacketHelper.readEntry(input); | ||
| 73 | String newName = input.readUTF(); | ||
| 74 | return rename(user, entry, newName); | ||
| 75 | default: | ||
| 76 | throw new IllegalStateException("unreachable"); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | public void write(DataOutput output) throws IOException { | ||
| 81 | output.writeByte(getType().ordinal()); | ||
| 82 | PacketHelper.writeString(output, user); | ||
| 83 | } | ||
| 84 | |||
| 85 | private Message(String user) { | ||
| 86 | this.user = user; | ||
| 87 | } | ||
| 88 | |||
| 89 | @Override | ||
| 90 | public boolean equals(Object o) { | ||
| 91 | if (this == o) return true; | ||
| 92 | if (o == null || getClass() != o.getClass()) return false; | ||
| 93 | Message message = (Message) o; | ||
| 94 | return Objects.equals(user, message.user); | ||
| 95 | } | ||
| 96 | |||
| 97 | @Override | ||
| 98 | public int hashCode() { | ||
| 99 | return Objects.hash(user); | ||
| 100 | } | ||
| 101 | |||
| 102 | public enum Type { | ||
| 103 | CHAT, | ||
| 104 | CONNECT, | ||
| 105 | DISCONNECT, | ||
| 106 | EDIT_DOCS, | ||
| 107 | MARK_DEOBF, | ||
| 108 | REMOVE_MAPPING, | ||
| 109 | RENAME, | ||
| 110 | } | ||
| 111 | |||
| 112 | public static final class Chat extends Message { | ||
| 113 | |||
| 114 | public final String message; | ||
| 115 | |||
| 116 | private Chat(String user, String message) { | ||
| 117 | super(user); | ||
| 118 | this.message = message; | ||
| 119 | } | ||
| 120 | |||
| 121 | @Override | ||
| 122 | public void write(DataOutput output) throws IOException { | ||
| 123 | super.write(output); | ||
| 124 | PacketHelper.writeString(output, message); | ||
| 125 | } | ||
| 126 | |||
| 127 | @Override | ||
| 128 | public String translate() { | ||
| 129 | return String.format(I18n.translate("message.chat.text"), user, message); | ||
| 130 | } | ||
| 131 | |||
| 132 | @Override | ||
| 133 | public Type getType() { | ||
| 134 | return Type.CHAT; | ||
| 135 | } | ||
| 136 | |||
| 137 | @Override | ||
| 138 | public boolean equals(Object o) { | ||
| 139 | if (this == o) return true; | ||
| 140 | if (o == null || getClass() != o.getClass()) return false; | ||
| 141 | if (!super.equals(o)) return false; | ||
| 142 | Chat chat = (Chat) o; | ||
| 143 | return Objects.equals(message, chat.message); | ||
| 144 | } | ||
| 145 | |||
| 146 | @Override | ||
| 147 | public int hashCode() { | ||
| 148 | return Objects.hash(super.hashCode(), message); | ||
| 149 | } | ||
| 150 | |||
| 151 | @Override | ||
| 152 | public String toString() { | ||
| 153 | return String.format("Message.Chat { user: '%s', message: '%s' }", user, message); | ||
| 154 | } | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | public static final class Connect extends Message { | ||
| 159 | |||
| 160 | private Connect(String user) { | ||
| 161 | super(user); | ||
| 162 | } | ||
| 163 | |||
| 164 | @Override | ||
| 165 | public String translate() { | ||
| 166 | return String.format(I18n.translate("message.connect.text"), user); | ||
| 167 | } | ||
| 168 | |||
| 169 | @Override | ||
| 170 | public Type getType() { | ||
| 171 | return Type.CONNECT; | ||
| 172 | } | ||
| 173 | |||
| 174 | @Override | ||
| 175 | public String toString() { | ||
| 176 | return String.format("Message.Connect { user: '%s' }", user); | ||
| 177 | } | ||
| 178 | |||
| 179 | } | ||
| 180 | |||
| 181 | public static final class Disconnect extends Message { | ||
| 182 | |||
| 183 | private Disconnect(String user) { | ||
| 184 | super(user); | ||
| 185 | } | ||
| 186 | |||
| 187 | @Override | ||
| 188 | public String translate() { | ||
| 189 | return String.format(I18n.translate("message.disconnect.text"), user); | ||
| 190 | } | ||
| 191 | |||
| 192 | @Override | ||
| 193 | public Type getType() { | ||
| 194 | return Type.DISCONNECT; | ||
| 195 | } | ||
| 196 | |||
| 197 | @Override | ||
| 198 | public String toString() { | ||
| 199 | return String.format("Message.Disconnect { user: '%s' }", user); | ||
| 200 | } | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | public static final class EditDocs extends Message { | ||
| 205 | |||
| 206 | public final Entry<?> entry; | ||
| 207 | |||
| 208 | private EditDocs(String user, Entry<?> entry) { | ||
| 209 | super(user); | ||
| 210 | this.entry = entry; | ||
| 211 | } | ||
| 212 | |||
| 213 | @Override | ||
| 214 | public void write(DataOutput output) throws IOException { | ||
| 215 | super.write(output); | ||
| 216 | PacketHelper.writeEntry(output, entry); | ||
| 217 | } | ||
| 218 | |||
| 219 | @Override | ||
| 220 | public String translate() { | ||
| 221 | return String.format(I18n.translate("message.edit_docs.text"), user, entry); | ||
| 222 | } | ||
| 223 | |||
| 224 | @Override | ||
| 225 | public Type getType() { | ||
| 226 | return Type.EDIT_DOCS; | ||
| 227 | } | ||
| 228 | |||
| 229 | @Override | ||
| 230 | public boolean equals(Object o) { | ||
| 231 | if (this == o) return true; | ||
| 232 | if (o == null || getClass() != o.getClass()) return false; | ||
| 233 | if (!super.equals(o)) return false; | ||
| 234 | EditDocs editDocs = (EditDocs) o; | ||
| 235 | return Objects.equals(entry, editDocs.entry); | ||
| 236 | } | ||
| 237 | |||
| 238 | @Override | ||
| 239 | public int hashCode() { | ||
| 240 | return Objects.hash(super.hashCode(), entry); | ||
| 241 | } | ||
| 242 | |||
| 243 | @Override | ||
| 244 | public String toString() { | ||
| 245 | return String.format("Message.EditDocs { user: '%s', entry: %s }", user, entry); | ||
| 246 | } | ||
| 247 | |||
| 248 | } | ||
| 249 | |||
| 250 | public static final class MarkDeobf extends Message { | ||
| 251 | |||
| 252 | public final Entry<?> entry; | ||
| 253 | |||
| 254 | private MarkDeobf(String user, Entry<?> entry) { | ||
| 255 | super(user); | ||
| 256 | this.entry = entry; | ||
| 257 | } | ||
| 258 | |||
| 259 | @Override | ||
| 260 | public void write(DataOutput output) throws IOException { | ||
| 261 | super.write(output); | ||
| 262 | PacketHelper.writeEntry(output, entry); | ||
| 263 | } | ||
| 264 | |||
| 265 | @Override | ||
| 266 | public String translate() { | ||
| 267 | return String.format(I18n.translate("message.mark_deobf.text"), user, entry); | ||
| 268 | } | ||
| 269 | |||
| 270 | @Override | ||
| 271 | public Type getType() { | ||
| 272 | return Type.MARK_DEOBF; | ||
| 273 | } | ||
| 274 | |||
| 275 | @Override | ||
| 276 | public boolean equals(Object o) { | ||
| 277 | if (this == o) return true; | ||
| 278 | if (o == null || getClass() != o.getClass()) return false; | ||
| 279 | if (!super.equals(o)) return false; | ||
| 280 | MarkDeobf markDeobf = (MarkDeobf) o; | ||
| 281 | return Objects.equals(entry, markDeobf.entry); | ||
| 282 | } | ||
| 283 | |||
| 284 | @Override | ||
| 285 | public int hashCode() { | ||
| 286 | return Objects.hash(super.hashCode(), entry); | ||
| 287 | } | ||
| 288 | |||
| 289 | @Override | ||
| 290 | public String toString() { | ||
| 291 | return String.format("Message.MarkDeobf { user: '%s', entry: %s }", user, entry); | ||
| 292 | } | ||
| 293 | |||
| 294 | } | ||
| 295 | |||
| 296 | public static final class RemoveMapping extends Message { | ||
| 297 | |||
| 298 | public final Entry<?> entry; | ||
| 299 | |||
| 300 | private RemoveMapping(String user, Entry<?> entry) { | ||
| 301 | super(user); | ||
| 302 | this.entry = entry; | ||
| 303 | } | ||
| 304 | |||
| 305 | @Override | ||
| 306 | public void write(DataOutput output) throws IOException { | ||
| 307 | super.write(output); | ||
| 308 | PacketHelper.writeEntry(output, entry); | ||
| 309 | } | ||
| 310 | |||
| 311 | @Override | ||
| 312 | public String translate() { | ||
| 313 | return String.format(I18n.translate("message.remove_mapping.text"), user, entry); | ||
| 314 | } | ||
| 315 | |||
| 316 | @Override | ||
| 317 | public Type getType() { | ||
| 318 | return Type.REMOVE_MAPPING; | ||
| 319 | } | ||
| 320 | |||
| 321 | @Override | ||
| 322 | public boolean equals(Object o) { | ||
| 323 | if (this == o) return true; | ||
| 324 | if (o == null || getClass() != o.getClass()) return false; | ||
| 325 | if (!super.equals(o)) return false; | ||
| 326 | RemoveMapping that = (RemoveMapping) o; | ||
| 327 | return Objects.equals(entry, that.entry); | ||
| 328 | } | ||
| 329 | |||
| 330 | @Override | ||
| 331 | public int hashCode() { | ||
| 332 | return Objects.hash(super.hashCode(), entry); | ||
| 333 | } | ||
| 334 | |||
| 335 | @Override | ||
| 336 | public String toString() { | ||
| 337 | return String.format("Message.RemoveMapping { user: '%s', entry: %s }", user, entry); | ||
| 338 | } | ||
| 339 | |||
| 340 | } | ||
| 341 | |||
| 342 | public static final class Rename extends Message { | ||
| 343 | |||
| 344 | public final Entry<?> entry; | ||
| 345 | public final String newName; | ||
| 346 | |||
| 347 | private Rename(String user, Entry<?> entry, String newName) { | ||
| 348 | super(user); | ||
| 349 | this.entry = entry; | ||
| 350 | this.newName = newName; | ||
| 351 | } | ||
| 352 | |||
| 353 | @Override | ||
| 354 | public void write(DataOutput output) throws IOException { | ||
| 355 | super.write(output); | ||
| 356 | PacketHelper.writeEntry(output, entry); | ||
| 357 | PacketHelper.writeString(output, newName); | ||
| 358 | } | ||
| 359 | |||
| 360 | @Override | ||
| 361 | public String translate() { | ||
| 362 | return String.format(I18n.translate("message.rename.text"), user, entry, newName); | ||
| 363 | } | ||
| 364 | |||
| 365 | @Override | ||
| 366 | public Type getType() { | ||
| 367 | return Type.RENAME; | ||
| 368 | } | ||
| 369 | |||
| 370 | @Override | ||
| 371 | public boolean equals(Object o) { | ||
| 372 | if (this == o) return true; | ||
| 373 | if (o == null || getClass() != o.getClass()) return false; | ||
| 374 | if (!super.equals(o)) return false; | ||
| 375 | Rename rename = (Rename) o; | ||
| 376 | return Objects.equals(entry, rename.entry) && | ||
| 377 | Objects.equals(newName, rename.newName); | ||
| 378 | } | ||
| 379 | |||
| 380 | @Override | ||
| 381 | public int hashCode() { | ||
| 382 | return Objects.hash(super.hashCode(), entry, newName); | ||
| 383 | } | ||
| 384 | |||
| 385 | @Override | ||
| 386 | public String toString() { | ||
| 387 | return String.format("Message.Rename { user: '%s', entry: %s, newName: '%s' }", user, entry, newName); | ||
| 388 | } | ||
| 389 | |||
| 390 | } | ||
| 391 | |||
| 392 | } | ||
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; | |||
| 15 | import org.objectweb.asm.Opcodes; | 15 | import org.objectweb.asm.Opcodes; |
| 16 | 16 | ||
| 17 | import javax.swing.*; | 17 | import javax.swing.*; |
| 18 | import javax.swing.text.BadLocationException; | ||
| 19 | import javax.swing.text.JTextComponent; | ||
| 18 | import java.awt.*; | 20 | import java.awt.*; |
| 19 | import java.awt.event.MouseEvent; | 21 | import java.awt.event.MouseEvent; |
| 20 | import java.io.IOException; | 22 | import java.io.IOException; |
| @@ -22,13 +24,16 @@ import java.io.InputStream; | |||
| 22 | import java.io.InputStreamReader; | 24 | import java.io.InputStreamReader; |
| 23 | import java.net.URI; | 25 | import java.net.URI; |
| 24 | import java.net.URISyntaxException; | 26 | import java.net.URISyntaxException; |
| 27 | import java.nio.charset.StandardCharsets; | ||
| 25 | import java.nio.file.Files; | 28 | import java.nio.file.Files; |
| 26 | import java.nio.file.Path; | 29 | import java.nio.file.Path; |
| 27 | import java.util.Comparator; | 30 | import java.security.MessageDigest; |
| 31 | import java.security.NoSuchAlgorithmException; | ||
| 32 | import java.util.*; | ||
| 28 | import java.util.List; | 33 | import java.util.List; |
| 29 | import java.util.Locale; | ||
| 30 | import java.util.StringJoiner; | ||
| 31 | import java.util.stream.Collectors; | 34 | import java.util.stream.Collectors; |
| 35 | import java.util.zip.ZipEntry; | ||
| 36 | import java.util.zip.ZipFile; | ||
| 32 | 37 | ||
| 33 | public class Utils { | 38 | public 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 | } |