From 0f5c38938f9b6b47c018d481d4c4f15c1dfb7711 Mon Sep 17 00:00:00 2001 From: Frotty Date: Sun, 31 Dec 2023 12:06:16 +0100 Subject: [PATCH] only show file chooser for runmap task, update chardet lib --- de.peeeq.wurstscript/build.gradle | 2 +- .../src/main/java/de/peeeq/wurstio/Main.java | 2 +- .../languageserver/requests/MapRequest.java | 4 +- .../de/peeeq/wurstio/utils/FileReading.java | 46 +++++-------------- .../wurstio/utils/W3InstallationData.java | 12 +++-- 5 files changed, 25 insertions(+), 41 deletions(-) diff --git a/de.peeeq.wurstscript/build.gradle b/de.peeeq.wurstscript/build.gradle index 5ac5b4e93..61347452b 100644 --- a/de.peeeq.wurstscript/build.gradle +++ b/de.peeeq.wurstscript/build.gradle @@ -111,7 +111,7 @@ dependencies { implementation group: 'org.apache.velocity', name: 'velocity', version: '1.7' // Chardet for guessing the file-encoding of a source-file - implementation group: 'net.sourceforge.jchardet', name: 'jchardet', version: '1.0' + implementation 'com.github.albfernandez:juniversalchardet:2.4.0' // Crigges' jmpq implementation group: 'com.github.inwc3', name: 'jmpq3', version: '264c54cfc8' diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/Main.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/Main.java index 8344be4ce..7bf555997 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/Main.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/Main.java @@ -171,7 +171,7 @@ public static void main(String[] args) { if (projectConfig != null && target.isPresent()) { ProjectConfigBuilder.apply(projectConfig, target.get().toFile(), scriptFile, buildDir.toFile(), - runArgs, new W3InstallationData(null, Paths.get(workspaceroot).toFile())); + runArgs, new W3InstallationData(null, Paths.get(workspaceroot).toFile(), false)); WLogger.info("map build success"); System.out.println("Build succeeded. Output file: <" + target.get().toAbsolutePath() + ">"); diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java index 89f745668..814b7b86d 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java @@ -443,14 +443,14 @@ private W3InstallationData getBestW3InstallationData() throws RequestFailedExcep return new W3InstallationData(Optional.empty(), Optional.empty()); } if (wc3Path.isPresent() && StringUtils.isNotBlank(wc3Path.get())) { - W3InstallationData w3data = new W3InstallationData(langServer, new File(wc3Path.get())); + W3InstallationData w3data = new W3InstallationData(langServer, new File(wc3Path.get()), this instanceof RunMap); if (w3data.getWc3PatchVersion().isEmpty()) { throw new RequestFailedException(MessageType.Error, "Could not find Warcraft III installation at specified path: " + wc3Path); } return w3data; } else { - return new W3InstallationData(langServer); + return new W3InstallationData(langServer, this instanceof RunMap); } } diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/FileReading.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/FileReading.java index 4fdc1f22a..ddebf38b8 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/FileReading.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/FileReading.java @@ -2,8 +2,7 @@ import com.google.common.base.Charsets; import de.peeeq.wurstscript.WLogger; -import org.mozilla.intl.chardet.nsDetector; -import org.mozilla.intl.chardet.nsPSMDetector; +import org.mozilla.universalchardet.UniversalDetector; import java.io.*; import java.nio.charset.Charset; @@ -11,6 +10,8 @@ public class FileReading { + private static final UniversalDetector detector = new UniversalDetector(); + /** * get a reader for a file */ @@ -20,47 +21,24 @@ private static Reader getFileReader(File file, Charset cs) throws IOException { /** * get a reader for a file and guess the charset using jchardet - * http://jchardet.sourceforge.net/ */ public static Reader getFileReader(File file) throws IOException { - try (InputStream fis = Files.newInputStream(file.toPath()); - BufferedInputStream imp = new BufferedInputStream(fis)) { - - nsDetector det = new nsDetector(nsPSMDetector.ALL); - - final String[] charset = new String[1]; - - det.Init(cs -> charset[0] = cs); + try (InputStream fis = Files.newInputStream(file.toPath())) { - byte[] buf = new byte[1024]; - int len; - boolean done = false; - boolean isAscii = true; + byte[] buf = new byte[4096]; - while ((len = imp.read(buf, 0, buf.length)) != -1) { - - // Check if the stream is only ascii. - if (isAscii) - isAscii = det.isAscii(buf, len); - - // DoIt if non-ascii and not done yet. - if (!isAscii && !done) - done = det.DoIt(buf, len, false); + int nread; + while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { + detector.handleData(buf, 0, nread); } + detector.dataEnd(); - det.DataEnd(); - - if (isAscii) { - charset[0] = "ASCII"; - } + String encoding = detector.getDetectedCharset(); - String encoding = charset[0]; + detector.reset(); if (encoding == null) { - // throw new IOException("Could not get encoding for " + - // file.getAbsolutePath()); - WLogger.severe("Could not get encoding for " - + file.getAbsolutePath()); + WLogger.severe("Could not get encoding for " + file.getAbsolutePath()); return getFileReader(file, Charsets.UTF_8); } else { return getFileReader(file, Charset.forName(encoding)); diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/W3InstallationData.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/W3InstallationData.java index 57b44119c..158419467 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/W3InstallationData.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/W3InstallationData.java @@ -21,6 +21,8 @@ public class W3InstallationData { private File selectedFolder; + private boolean shouldAskForPath = false; + static { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); @@ -35,8 +37,9 @@ public W3InstallationData(Optional gameExe, Optional version) } /** Evaluates the game path and version by discovering the system environment. */ - public W3InstallationData(WurstLanguageServer languageServer) { + public W3InstallationData(WurstLanguageServer languageServer, boolean shouldAskForPath) { this.languageServer = languageServer; + this.shouldAskForPath = shouldAskForPath; discoverExePath(); discoverVersion(); } @@ -45,8 +48,9 @@ public W3InstallationData(WurstLanguageServer languageServer) { * Evaluates the game path and version, attempting to use the provided path if possible, before discovering the * system environment. */ - public W3InstallationData(WurstLanguageServer languageServer, File wc3Path) { + public W3InstallationData(WurstLanguageServer languageServer, File wc3Path, boolean shouldAskForPath) { this.languageServer = languageServer; + this.shouldAskForPath = shouldAskForPath; if (!Orient.isWindowsSystem()) { WLogger.warning("Game path configuration only works on windows"); discoverExePath(); @@ -89,7 +93,9 @@ private void discoverExePath() { WLogger.info("Discovered game path: " + gameExe); } catch (NotFoundException | UnsupportedPlatformException e) { WLogger.warning("Can't find game installation directory: " + e.getMessage()); - showFileChooser(); + if (shouldAskForPath) { + showFileChooser(); + } } }