-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathRunMap.java
More file actions
339 lines (296 loc) · 14 KB
/
Copy pathRunMap.java
File metadata and controls
339 lines (296 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package de.peeeq.wurstio.languageserver.requests;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import config.WurstProjectConfig;
import config.WurstProjectConfigData;
import de.peeeq.wurstio.gui.WurstGuiImpl;
import de.peeeq.wurstio.languageserver.ModelManager;
import de.peeeq.wurstio.languageserver.WFile;
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.gui.WurstGui;
import de.peeeq.wurstscript.utils.Utils;
import net.moonlightflower.wc3libs.port.GameVersion;
import net.moonlightflower.wc3libs.port.Orient;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.lsp4j.MessageType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME;
/**
* Created by peter on 16.05.16.
*/
public class RunMap extends MapRequest {
private @Nullable File customTarget = null;
public RunMap(WurstLanguageServer langServer, WFile workspaceRoot, Optional<String> wc3Path, Optional<File> map,
List<String> compileArgs, Optional<String> gameExePath) {
super(langServer, map, compileArgs, workspaceRoot, wc3Path, gameExePath);
safeCompilation = SafetyLevel.QuickAndDirty;
}
@Override
public Object execute(ModelManager modelManager) throws IOException {
WLogger.info("Execute RunMap, \nwc3Path =" + wc3Path
+ ",\n map = " + map
+ ",\n compileArgs = " + compileArgs
+ ",\n workspaceRoot = " + workspaceRoot
+ ",\n runArgs = " + compileArgs
);
if (modelManager.hasErrors()) {
throw new RequestFailedException(MessageType.Error, "Fix errors in your code before running.\n" + modelManager.getFirstErrorDescription());
}
WurstProjectConfigData projectConfig = WurstProjectConfig.INSTANCE.loadProject(workspaceRoot.getFile().toPath().resolve(FILE_NAME));
if (projectConfig == null) {
throw new RequestFailedException(MessageType.Error, FILE_NAME + " file doesn't exist or is invalid. " +
"Please install your project using grill or the wurst setup tool.");
}
// TODO use normal compiler for this, avoid code duplication
WurstGui gui = new WurstGuiImpl(getWorkspaceAbsolute());
try {
String ok = compileMap(modelManager, gui, projectConfig);
if (ok != null) return ok;
} catch (CompileError e) {
WLogger.info(e);
throw new RequestFailedException(MessageType.Error, "A compilation error occurred when running the map:\n" + e);
} catch (Exception e) {
WLogger.warning("Exception occurred", e);
throw new RequestFailedException(MessageType.Error, "An exception was thrown when running the map:\n" + e);
} finally {
if (gui.getErrorCount() == 0) {
gui.sendFinished();
}
}
return "ok"; // TODO
}
@Nullable
private String compileMap(ModelManager modelManager, WurstGui gui, WurstProjectConfigData projectConfig) throws Exception {
if (map.isPresent() && !map.get().exists()) {
throw new RequestFailedException(MessageType.Error, map.get().getAbsolutePath() + " does not exist.");
}
gui.sendProgress("Copying map");
// first we copy in same location to ensure validity
File buildDir = getBuildDir();
if (map.isPresent()) {
mapLastModified = getSourceMapLastModified(map.get());
mapPath = map.get().getAbsolutePath();
}
if (!runArgs.isHotReload()) {
File cacheTarget = ensureWritableTargetFile(
getCachedMapFile(),
"Run Map",
"The cached run map file is in use and cannot be updated.\nClose Warcraft III and click Retry, choose Rename to use a temporary file name, or Cancel.",
"Run canceled because the cached map file is in use."
);
cachedMapFileName = cacheTarget.getName();
}
Optional<File> testMap = map.map($ -> new File(buildDir, "WurstRunMap.w3x"));
CompilationResult result = compileScript(modelManager, gui, testMap, projectConfig, buildDir, false);
if (runArgs.isHotReload()) {
// call jhcr update
gui.sendProgress("Calling JHCR update");
callJhcrUpdate(result.script);
// if we are just reloading the mapscript with JHCR, we are done here
gui.sendProgress("update complete");
return "ok";
}
if (testMap.isPresent()) {
startGame(gui, result);
}
return null;
}
private void startGame(WurstGui gui, CompilationResult result) throws Exception {
Optional<File> cachedMapFile = Optional.ofNullable(getCachedMapFile());
injectMapData(gui, cachedMapFile, result);
timeTaker.beginPhase("Starting Warcraft 3");
gui.sendProgress("Starting Warcraft 3...");
File mapCopy = cachedMapFile.get();
Optional<GameVersion> detectedGameVersion = w3data.getWc3PatchVersion();
if (buildConfig.shouldCopyRunMapToWarcraftMapDir(detectedGameVersion)) {
mapCopy = copyToWarcraftMapDir(cachedMapFile.get());
}
WLogger.info("Starting wc3 ... ");
String path = "";
if (customTarget != null) {
path = new File(customTarget, cachedMapFile.get().getName()).getAbsolutePath();
} else if (mapCopy != null) {
path = mapCopy.getAbsolutePath();
}
if (!path.isEmpty()) {
// now start the map
File gameExe = w3data.getGameExe()
.orElseThrow(() -> new RequestFailedException(MessageType.Error, wc3Path + " does not exist."));
List<String> cmd = Lists.newArrayList(gameExe.getAbsolutePath());
Optional<String> wc3RunArgs = langServer.getConfigProvider().getWc3RunArgs();
if (!wc3RunArgs.isPresent() || StringUtils.isBlank(wc3RunArgs.get())) {
if (buildConfig.shouldUseReforgedLaunchArgs(detectedGameVersion)) {
cmd.add("-launch");
}
if (buildConfig.shouldUseClassicWindowArg(detectedGameVersion)) {
cmd.add("-window");
} else {
cmd.add("-windowmode");
cmd.add("windowed");
}
} else {
cmd.addAll(Arrays.asList(wc3RunArgs.get().split("\\s+")));
}
cmd.add("-loadfile");
cmd.add(path);
if (Orient.isLinuxSystem()) {
// run with wine
cmd.add(0, "wine");
}
timeTaker.endPhase();
gui.sendProgress("running " + cmd);
Runtime.getRuntime().exec(cmd.toArray(new String[0]));
timeTaker.endPhase();
timeTaker.printReport();
}
}
private void callJhcrUpdate(File mapScript) throws IOException, InterruptedException {
File mapScriptFolder = mapScript.getParentFile();
File commonJ = new File(mapScriptFolder, "common.j");
File blizzardJ = new File(mapScriptFolder, "blizzard.j");
if (!commonJ.exists()) {
throw new IOException("Could not find file " + commonJ.getAbsolutePath());
}
if (!blizzardJ.exists()) {
throw new IOException("Could not find file " + blizzardJ.getAbsolutePath());
}
Path customMapDataPath = getCustomMapDataPath();
ProcessBuilder pb = new ProcessBuilder(langServer.getConfigProvider().getJhcrExe(), "update", mapScript.getName(), "--asm",
"--preload-path", customMapDataPath.toAbsolutePath().toString());
pb.directory(mapScriptFolder);
Utils.ExecResult $ = Utils.exec(pb, Duration.ofSeconds(30), System.err::println);
}
/**
* Tries to find the path where the wc3 CustomMapData is stored.
* For example this could be in:
* C:\\Users\\Peter\\Documents\\Warcraft III\\CustomMapData
*/
private Path getCustomMapDataPath() {
String customMapDataPath = langServer.getConfigProvider().getConfig("customMapDataPath", "");
if (!customMapDataPath.isEmpty()) {
return Paths.get(customMapDataPath);
}
if (Orient.isMacSystem()) {
return Paths.get(System.getProperty("user.home"),
"Library", "Application Support", "Blizzard", "Warcraft III", "CustomMapData");
}
Path documents;
try {
documents = FileSystemView.getFileSystemView().getDefaultDirectory().toPath();
} catch (Throwable t) {
WLogger.info(t);
Path homeFolder = Paths.get(System.getProperty("user.home"));
documents = homeFolder.resolve("Documents");
}
return documents.resolve(Paths.get("Warcraft III", "CustomMapData"));
}
@NotNull
private String getWorkspaceAbsolute() {
try {
return workspaceRoot.getFile().getAbsolutePath();
} catch (FileNotFoundException e) {
throw new RequestFailedException(MessageType.Error, "Could not open workspace root: ", e);
}
}
/**
* Copies the map to the wc3 map directory
* <p>
* This directory depends on warcraft version and whether we are on windows or wine is used.
*/
private File copyToWarcraftMapDir(File testMap) throws IOException {
String testMapName = "WurstTestMap.w3x";
for (String arg : compileArgs) {
if (arg.startsWith("-runmapTarget")) {
String path = arg.substring(arg.indexOf(" ") + 1);
// copy the map to the specified directory
customTarget = new File(path);
if (customTarget.exists() && customTarget.isDirectory()) {
File testMap2 = new File(customTarget, testMapName);
Files.copy(testMap, testMap2);
return testMap2;
} else {
WLogger.severe("Directory specified via -runmapTarget does not exists or is not a directory");
}
}
}
File myDocumentsFolder = FileSystemView.getFileSystemView().getDefaultDirectory();
Optional<String> documentPath = findMapDocumentPath(testMapName, myDocumentsFolder);
// copy the map to the appropriate directory
Optional<File> testFolder = documentPath.map(path -> new File(path, "Maps" + File.separator + "Test"));
if (testFolder.isPresent() && (testFolder.get().mkdirs() || testFolder.get().exists())) {
File testMap2 = new File(testFolder.get(), testMapName);
while (true) {
try {
Files.copy(testMap, testMap2);
break;
} catch (IOException ex) {
JFrame jf = new JFrame();
jf.setAlwaysOnTop(true);
Object[] options = { "Retry", "Rename", "Cancel" };
int result = JOptionPane.showOptionDialog(jf, "Can't write to target map file, it's probably in use.",
"Run Map", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, null);
if (result == JOptionPane.CANCEL_OPTION) {
return null;
} else if(result == JOptionPane.NO_OPTION) {
testMap2 = new File(testFolder.get(), testMapName + RandomStringUtils.randomNumeric(3));
}
}
}
return testMap2;
} else {
WLogger.severe("Could not create Test folder");
}
return null;
}
private Optional<String> findMapDocumentPath(String testMapName, File myDocumentsFolder) {
Optional<String> documentPath = Optional.of(
langServer.getConfigProvider().getMapDocumentPath().orElseGet(
() -> myDocumentsFolder.getAbsolutePath() + File.separator + "Warcraft III"));
if (!new File(documentPath.get()).exists()) {
WLogger.info("Warcraft folder " + documentPath + " does not exist.");
if (Orient.isMacSystem()) {
// macOS 1.29+: ~/Library/Application Support/Blizzard/Warcraft III
documentPath = Optional.of(System.getProperty("user.home")
+ "/Library/Application Support/Blizzard/Warcraft III");
} else {
// Linux: try Wine default path
documentPath = Optional.of(System.getProperty("user.home")
+ "/.wine/drive_c/users/" + System.getProperty("user.name") + "/My Documents/Warcraft III");
}
if (!new File(documentPath.get()).exists()) {
WLogger.severe("Severe: Warcraft folder " + documentPath + " does not exist.");
}
}
if (buildConfig.shouldUseInstallDirForMaps(w3data.getWc3PatchVersion())) {
// 1.27 and lower compat
WLogger.info("Version 1.27 or lower detected, changing file location");
documentPath = wc3Path;
} else {
// For 1.28+ the wc3/maps/test folder must not contain a map of the same name
Optional<File> oldFile = wc3Path.map(
w3p -> new File(w3p, "Maps" + File.separator + "Test" + File.separator + testMapName));
if (oldFile.isPresent() && oldFile.get().exists()) {
if (!oldFile.get().delete()) {
WLogger.severe("Cannot delete old Wurst Test Map");
}
}
}
return documentPath;
}
}