Skip to content

Commit

Permalink
only show file chooser for runmap task, update chardet lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty committed Dec 31, 2023
1 parent da013eb commit 0f5c389
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
2 changes: 1 addition & 1 deletion de.peeeq.wurstscript/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

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;
import java.nio.file.Files;

public class FileReading {

private static final UniversalDetector detector = new UniversalDetector();

/**
* get a reader for a file
*/
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class W3InstallationData {

private File selectedFolder;

private boolean shouldAskForPath = false;

static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Expand All @@ -35,8 +37,9 @@ public W3InstallationData(Optional<File> gameExe, Optional<GameVersion> 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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
}

Expand Down

0 comments on commit 0f5c389

Please sign in to comment.