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/Utils.java | |
| 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/Utils.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/utils/Utils.java | 64 |
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; | |||
| 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 | } |