• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 228

29 Nov 2023 05:00PM UTC coverage: 62.48% (-0.09%) from 62.574%
228

push

circleci

web-flow
Show dialog for choosing game path, cleanup (#1083)

* show dialog for choosing game path

* cleanup code

* remove logs and refactor

* remove confusing mpq error, make some mpq loads readonly

17295 of 27681 relevant lines covered (62.48%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/utils/W3InstallationData.java
1
package de.peeeq.wurstio.utils;
2

3
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
4
import de.peeeq.wurstscript.WLogger;
5
import net.moonlightflower.wc3libs.bin.GameExe;
6
import net.moonlightflower.wc3libs.port.*;
7
import net.moonlightflower.wc3libs.port.win.WinGameExeFinder;
8

9
import javax.swing.*;
10
import java.io.File;
11
import java.io.IOException;
12
import java.lang.reflect.InvocationTargetException;
13
import java.util.Optional;
14

15
public class W3InstallationData {
16
    private final WurstLanguageServer languageServer;
17

18
    private Optional<File> gameExe = Optional.empty();
×
19

20
    private Optional<GameVersion> version = Optional.empty();
×
21

22
    private File selectedFolder;
23

24
    static {
25
        try {
26
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
×
27
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ignored) {
×
28
        }
×
29
    }
×
30

31
    public W3InstallationData(Optional<File> gameExe, Optional<GameVersion> version) {
×
32
        this.languageServer = null;
×
33
        this.gameExe = gameExe;
×
34
        this.version = version;
×
35
    }
×
36

37
    /** Evaluates the game path and version by discovering the system environment. */
38
    public W3InstallationData(WurstLanguageServer languageServer) {
×
39
        this.languageServer = languageServer;
×
40
        discoverExePath();
×
41
        discoverVersion();
×
42
    }
×
43

44
    /**
45
     * Evaluates the game path and version, attempting to use the provided path if possible, before discovering the
46
     * system environment.
47
     */
48
    public W3InstallationData(WurstLanguageServer languageServer, File wc3Path) {
×
49
        this.languageServer = languageServer;
×
50
        if (!Orient.isWindowsSystem()) {
×
51
            WLogger.warning("Game path configuration only works on windows");
×
52
            discoverExePath();
×
53
            discoverVersion();
×
54
            return;
×
55
        }
56

57
        loadFromPath(wc3Path);
×
58

59
        if (!gameExe.isPresent()) {
×
60
            WLogger.warning("The provided wc3 path wasn't suitable. Falling back to discovery.");
×
61
            discoverExePath();
×
62
            discoverVersion();
×
63
        }
64
    }
×
65

66
    private void loadFromPath(File wc3Path) {
67
        try {
68
            gameExe = Optional.ofNullable(WinGameExeFinder.fromDirIgnoreVersion(wc3Path));
×
69
        } catch (NotFoundException e) {
×
70
            WLogger.severe(e);
×
71
        }
×
72
        WLogger.info("Game Executable from path: " + gameExe);
×
73

74
        version = gameExe.flatMap(exe -> {
×
75
            try {
76
                return Optional.ofNullable(GameExe.getVersion(exe));
×
77
            } catch (IOException e) {
×
78
                WLogger.severe(e);
×
79
            }
80

81
            return Optional.empty();
×
82
        });
83
        WLogger.info("Parsed custom game version from executable: " + version);
×
84
    }
×
85

86
    private void discoverExePath() {
87
        try {
88
            gameExe = Optional.ofNullable(new StdGameExeFinder().get());
×
89
            WLogger.info("Discovered game path: " + gameExe);
×
90
        } catch (NotFoundException | UnsupportedPlatformException e) {
×
91
            WLogger.warning("Can't find game installation directory: " + e.getMessage());
×
92
            showFileChooser();
×
93
        }
×
94
    }
×
95

96
    private void showFileChooser() {
97
        try {
98
            SwingUtilities.invokeAndWait(() -> {
×
99
                JFileChooser fileChooser = new JFileChooser();
×
100
                fileChooser.setDialogTitle("Select Warcraft III installation directory");
×
101
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
×
102
                JDialog dialog = new JDialog();
×
103
                dialog.setAlwaysOnTop(true);
×
104

105
                int result = fileChooser.showOpenDialog(dialog);
×
106
                if (result == JFileChooser.APPROVE_OPTION) {
×
107
                    selectedFolder = fileChooser.getSelectedFile();
×
108
                } else {
109
                    WLogger.warning("No directory selected");
×
110
                }
111
            });
×
112

113
            loadFromPath(selectedFolder);
×
114

115
            if (gameExe.isPresent()) {
×
116
                languageServer.getRemoteEndpoint().notify("wurst/updateGamePath", selectedFolder.getAbsolutePath());
×
117
            }
118
        } catch (InterruptedException | InvocationTargetException ex) {
×
119
            WLogger.warning("Choosing game path failed", ex);
×
120
        }
×
121
    }
×
122

123
    private void discoverVersion() {
124
        try {
125
            version = Optional.ofNullable(new StdGameVersionFinder().get());
×
126
            WLogger.info("Parsed game version: " + version);
×
127
        } catch (NotFoundException e) {
×
128
            WLogger.warning("Wurst compiler failed to determine game version", e);
×
129
        } catch (UnsupportedPlatformException e) {
×
130
            WLogger.warning("Wurst compiler cannot determine game version: " + e.getMessage());
×
131
        }
×
132
    }
×
133

134
    /**
135
     * @return The wc3 patch version or empty if none has been found
136
     */
137
    public Optional<GameVersion> getWc3PatchVersion() {
138
        return version;
×
139
    }
140

141
    /**
142
     * @return The wc3 path or empty if none has been found
143
     */
144
    public Optional<File> getGameExe() {
145
        return gameExe;
×
146
    }
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc