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

wurstscript / WurstScript / 220

22 Nov 2023 12:57PM UTC coverage: 62.564% (-0.04%) from 62.602%
220

Pull #1081

circleci

Frotty
Update MapRequest.java
Pull Request #1081: More performance improvements

17307 of 27663 relevant lines covered (62.56%)

0.63 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/languageserver/requests/RunMap.java
1
package de.peeeq.wurstio.languageserver.requests;
2

3
import com.google.common.collect.Lists;
4
import com.google.common.io.Files;
5
import config.WurstProjectConfig;
6
import config.WurstProjectConfigData;
7
import de.peeeq.wurstio.TimeTaker;
8
import de.peeeq.wurstio.gui.WurstGuiImpl;
9
import de.peeeq.wurstio.languageserver.ConfigProvider;
10
import de.peeeq.wurstio.languageserver.ModelManager;
11
import de.peeeq.wurstio.languageserver.ProjectConfigBuilder;
12
import de.peeeq.wurstio.languageserver.WFile;
13
import de.peeeq.wurstio.mpq.MpqEditor;
14
import de.peeeq.wurstio.mpq.MpqEditorFactory;
15
import de.peeeq.wurstscript.WLogger;
16
import de.peeeq.wurstscript.attributes.CompileError;
17
import de.peeeq.wurstscript.gui.WurstGui;
18
import de.peeeq.wurstscript.utils.Utils;
19
import net.moonlightflower.wc3libs.bin.app.W3I;
20
import net.moonlightflower.wc3libs.port.GameVersion;
21
import net.moonlightflower.wc3libs.port.Orient;
22
import org.apache.commons.lang.RandomStringUtils;
23
import org.apache.commons.lang.StringUtils;
24
import org.eclipse.lsp4j.MessageType;
25
import org.jetbrains.annotations.NotNull;
26
import org.jetbrains.annotations.Nullable;
27

28
import javax.swing.*;
29
import javax.swing.filechooser.FileSystemView;
30
import java.io.File;
31
import java.io.FileNotFoundException;
32
import java.io.IOException;
33
import java.nio.file.Path;
34
import java.nio.file.Paths;
35
import java.time.Duration;
36
import java.util.List;
37
import java.util.Optional;
38
import java.util.Arrays;
39

40
import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME;
41
import static net.moonlightflower.wc3libs.port.GameVersion.*;
42

43
/**
44
 * Created by peter on 16.05.16.
45
 */
46
public class RunMap extends MapRequest {
47

48
    private @Nullable File customTarget = null;
×
49

50

51
    public RunMap(ConfigProvider configProvider, WFile workspaceRoot, Optional<String> wc3Path, Optional<File> map,
52
            List<String> compileArgs) {
53
        super(configProvider, map, compileArgs, workspaceRoot, wc3Path);
×
54
        safeCompilation = SafetyLevel.QuickAndDirty;
×
55
    }
×
56

57
    @Override
58
    public Object execute(ModelManager modelManager) throws IOException {
59
        WLogger.info("Execute RunMap, \nwc3Path =" + wc3Path
×
60
            + ",\n map = " + map
61
            + ",\n compileArgs = " + compileArgs
62
            + ",\n workspaceRoot = " + workspaceRoot
63
            + ",\n runArgs = " + compileArgs
64
        );
65

66
        if (modelManager.hasErrors()) {
×
67
            throw new RequestFailedException(MessageType.Error, "Fix errors in your code before running.\n" + modelManager.getFirstErrorDescription());
×
68
        }
69

70
        WurstProjectConfigData projectConfig = WurstProjectConfig.INSTANCE.loadProject(workspaceRoot.getFile().toPath().resolve(FILE_NAME));
×
71
        if (projectConfig == null) {
×
72
            throw new RequestFailedException(MessageType.Error, FILE_NAME + " file doesn't exist or is invalid. " +
×
73
                "Please install your project using grill or the wurst setup tool.");
74
        }
75

76
        // TODO use normal compiler for this, avoid code duplication
77
        WLogger.info("received runMap command: map=" + map + ", wc3dir=" + wc3Path + ", args=" + compileArgs);
×
78
        WurstGui gui = new WurstGuiImpl(getWorkspaceAbsolute());
×
79
        try {
80
            if (map.isPresent() && !map.get().exists()) {
×
81
                throw new RequestFailedException(MessageType.Error, map.get().getAbsolutePath() + " does not exist.");
×
82
            }
83

84
            gui.sendProgress("Copying map");
×
85

86
            // first we copy in same location to ensure validity
87
            File buildDir = getBuildDir();
×
88
            Optional<File> testMap = map.map($ -> new File(buildDir, "WurstRunMap.w3x"));
×
89
            CompilationResult result = compileScript(modelManager, gui, testMap, projectConfig, buildDir, false);
×
90

91
            if (runArgs.isHotReload()) {
×
92
                // call jhcr update
93
                gui.sendProgress("Calling JHCR update");
×
94
                callJhcrUpdate(result.script);
×
95

96
                // if we are just reloading the mapscript with JHCR, we are done here
97
                gui.sendProgress("update complete");
×
98
                return "ok";
×
99
            }
100

101

102
            if (testMap.isPresent()) {
×
103

104
                injectMapData(gui, testMap, result);
×
105

106
                File mapCopy = copyToWarcraftMapDir(testMap.get());
×
107

108
                gui.sendProgress("Starting Warcraft 3...");
×
109
                WLogger.info("Starting wc3 ... ");
×
110
                String path = "";
×
111
                if (customTarget != null) {
×
112
                    path = new File(customTarget, testMap.get().getName()).getAbsolutePath();
×
113
                } else if (mapCopy != null) {
×
114
                    path = mapCopy.getAbsolutePath();
×
115
                }
116

117

118
                if (path.length() > 0) {
×
119
                    // now start the map
120
                    File gameExe = w3data.getGameExe().get();
×
121
                    if (!w3data.getWc3PatchVersion().isPresent()) {
×
122
                        throw new RequestFailedException(MessageType.Error, wc3Path + " does not exist.");
×
123
                    }
124
                    List<String> cmd = Lists.newArrayList(gameExe.getAbsolutePath());
×
125
                    Optional<String> wc3RunArgs = configProvider.getWc3RunArgs();
×
126
                    if (!wc3RunArgs.isPresent() || StringUtils.isBlank(wc3RunArgs.get())) {
×
127
                        if (w3data.getWc3PatchVersion().get().compareTo(VERSION_1_32) >= 0) {
×
128
                            cmd.add("-launch");
×
129
                        }
130
                            if (w3data.getWc3PatchVersion().get().compareTo(VERSION_1_31) < 0) {
×
131
                                cmd.add("-window");
×
132
                            } else {
133
                                cmd.add("-windowmode");
×
134
                                cmd.add("windowed");
×
135
                            }
136
                    } else {
137
                            cmd.addAll(Arrays.asList(wc3RunArgs.get().split("\\s+")));
×
138
                    }
139
                    cmd.add("-loadfile");
×
140
                    cmd.add(path);
×
141

142
                    if (Orient.isLinuxSystem()) {
×
143
                        // run with wine
144
                        cmd.add(0, "wine");
×
145
                    }
146

147
                    gui.sendProgress("running " + cmd);
×
148
                    Runtime.getRuntime().exec(cmd.toArray(new String[0]));
×
149
                    timeTaker.endPhase();
×
150
                    timeTaker.printReport();
×
151
                }
152
            }
153
        } catch (CompileError e) {
×
154
            WLogger.info(e);
×
155
            throw new RequestFailedException(MessageType.Error, "A compilation error occurred when running the map:\n" + e);
×
156
        } catch (Exception e) {
×
157
            WLogger.warning("Exception occurred", e);
×
158
            throw new RequestFailedException(MessageType.Error, "An exception was thrown when running the map:\n" + e);
×
159
        } finally {
160
            if (gui.getErrorCount() == 0) {
×
161
                gui.sendFinished();
×
162
            }
163
        }
164
        return "ok"; // TODO
×
165
    }
166

167

168
    private void callJhcrUpdate(File mapScript) throws IOException, InterruptedException {
169
        File mapScriptFolder = mapScript.getParentFile();
×
170
        File commonJ = new File(mapScriptFolder, "common.j");
×
171
        File blizzardJ = new File(mapScriptFolder, "blizzard.j");
×
172
        if (!commonJ.exists()) {
×
173
            throw new IOException("Could not find file " + commonJ.getAbsolutePath());
×
174
        }
175

176
        if (!blizzardJ.exists()) {
×
177
            throw new IOException("Could not find file " + blizzardJ.getAbsolutePath());
×
178
        }
179

180
        Path customMapDataPath = getCustomMapDataPath();
×
181

182
        ProcessBuilder pb = new ProcessBuilder(configProvider.getJhcrExe(), "update", mapScript.getName(), "--asm",
×
183
            "--preload-path", customMapDataPath.toAbsolutePath().toString());
×
184
        pb.directory(mapScriptFolder);
×
185
        Utils.ExecResult $ = Utils.exec(pb, Duration.ofSeconds(30), System.err::println);
×
186
    }
×
187

188
    /**
189
     * Tries to find the path where the wc3 CustomMapData is stored.
190
     * For example this could be in:
191
     * C:\\Users\\Peter\\Documents\\Warcraft III\\CustomMapData
192
     */
193
    private Path getCustomMapDataPath() {
194
        String customMapDataPath = configProvider.getConfig("customMapDataPath", "");
×
195
        if (!customMapDataPath.isEmpty()) {
×
196
            return Paths.get(customMapDataPath);
×
197
        }
198

199
        Path documents;
200
        try {
201
            documents = FileSystemView.getFileSystemView().getDefaultDirectory().toPath();
×
202
        } catch (Throwable t) {
×
203
            WLogger.info(t);
×
204
            Path homeFolder = Paths.get(System.getProperty("user.home"));
×
205
            documents = homeFolder.resolve("Documents");
×
206
        }
×
207
        return documents.resolve(Paths.get("Warcraft III", "CustomMapData"));
×
208
    }
209

210
    @NotNull
211
    private String getWorkspaceAbsolute() {
212
        try {
213
            return workspaceRoot.getFile().getAbsolutePath();
×
214
        } catch (FileNotFoundException e) {
×
215
            throw new RequestFailedException(MessageType.Error, "Could not open workspace root: ", e);
×
216
        }
217
    }
218

219
    /**
220
     * Copies the map to the wc3 map directory
221
     * <p>
222
     * This directory depends on warcraft version and whether we are on windows or wine is used.
223
     */
224
    private File copyToWarcraftMapDir(File testMap) throws IOException {
225
        String testMapName = "WurstTestMap.w3x";
×
226
        for (String arg : compileArgs) {
×
227
            if (arg.startsWith("-runmapTarget")) {
×
228
                String path = arg.substring(arg.indexOf(" ") + 1);
×
229
                // copy the map to the specified directory
230
                customTarget = new File(path);
×
231
                if (customTarget.exists() && customTarget.isDirectory()) {
×
232
                    File testMap2 = new File(customTarget, testMapName);
×
233
                    Files.copy(testMap, testMap2);
×
234
                    return testMap2;
×
235
                } else {
236
                    WLogger.severe("Directory specified via -runmapTarget does not exists or is not a directory");
×
237
                }
238
            }
239
        }
×
240

241
        File myDocumentsFolder = FileSystemView.getFileSystemView().getDefaultDirectory();
×
242
        Optional<String> documentPath = findMapDocumentPath(testMapName, myDocumentsFolder);
×
243

244
        // copy the map to the appropriate directory
245
        Optional<File> testFolder = documentPath.map(path -> new File(path, "Maps" + File.separator + "Test"));
×
246
        if (testFolder.isPresent() && (testFolder.get().mkdirs() || testFolder.get().exists())) {
×
247
            File testMap2 = new File(testFolder.get(), testMapName);
×
248
            while (true) {
249
                try {
250
                    Files.copy(testMap, testMap2);
×
251
                    break;
×
252
                } catch (IOException ex) {
×
253
                    JFrame jf = new JFrame();
×
254
                    jf.setAlwaysOnTop(true);
×
255
                    Object[] options = { "Retry", "Rename", "Cancel" };
×
256
                    int result = JOptionPane.showOptionDialog(jf, "Can't write to target map file, it's probably in use.",
×
257
                        "Run Map", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
258
                        null, options, null);
259
                    if (result == JOptionPane.CANCEL_OPTION) {
×
260
                        return null;
×
261
                    } else if(result == JOptionPane.NO_OPTION) {
×
262
                        testMap2 = new File(testFolder.get(), testMapName + RandomStringUtils.randomNumeric(3));
×
263
                    }
264
                }
×
265

266
            }
267

268
            return testMap2;
×
269
        } else {
270
            WLogger.severe("Could not create Test folder");
×
271
        }
272
        return null;
×
273
    }
274

275
    private Optional<String> findMapDocumentPath(String testMapName, File myDocumentsFolder) {
276
        Optional<String> documentPath = Optional.of(
×
277
            configProvider.getMapDocumentPath().orElseGet(
×
278
                () -> myDocumentsFolder.getAbsolutePath() + File.separator + "Warcraft III"));
×
279

280
        if (!new File(documentPath.get()).exists()) {
×
281
            WLogger.info("Warcraft folder " + documentPath + " does not exist.");
×
282
            // Try wine default:
283
            documentPath = Optional.of(System.getProperty("user.home")
×
284
                + "/.wine/drive_c/users/" + System.getProperty("user.name") + "/My Documents/Warcraft III");
×
285
            if (!new File(documentPath.get()).exists()) {
×
286
                WLogger.severe("Severe: Wine Warcraft folder " + documentPath + " does not exist.");
×
287
            }
288
        }
289

290
        if (w3data.getWc3PatchVersion().get().compareTo(new GameVersion("1.27.9")) <= 0) {
×
291
            // 1.27 and lower compat
292
            WLogger.info("Version 1.27 or lower detected, changing file location");
×
293
            documentPath = wc3Path;
×
294
        } else {
295
            // For 1.28+ the wc3/maps/test folder must not contain a map of the same name
296
            Optional<File> oldFile = wc3Path.map(
×
297
                w3p -> new File(w3p, "Maps" + File.separator + "Test" + File.separator + testMapName));
×
298
            if (oldFile.isPresent() && oldFile.get().exists()) {
×
299
                if (!oldFile.get().delete()) {
×
300
                    WLogger.severe("Cannot delete old Wurst Test Map");
×
301
                }
302
            }
303
        }
304
        return documentPath;
×
305
    }
306

307

308
}
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