-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMapRequest.java
More file actions
927 lines (808 loc) · 38.7 KB
/
MapRequest.java
File metadata and controls
927 lines (808 loc) · 38.7 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
package de.peeeq.wurstio.languageserver.requests;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import config.WurstProjectConfigData;
import de.peeeq.wurstio.Pjass;
import de.peeeq.wurstio.TimeTaker;
import de.peeeq.wurstio.UtilsIO;
import de.peeeq.wurstio.WurstCompilerJassImpl;
import de.peeeq.wurstio.languageserver.ModelManager;
import de.peeeq.wurstio.languageserver.ProjectConfigBuilder;
import de.peeeq.wurstio.languageserver.WFile;
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
import de.peeeq.wurstio.map.importer.ImportFile;
import de.peeeq.wurstio.mpq.MpqEditor;
import de.peeeq.wurstio.mpq.MpqEditorFactory;
import de.peeeq.wurstio.utils.W3InstallationData;
import de.peeeq.wurstscript.RunArgs;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.ast.CompilationUnit;
import de.peeeq.wurstscript.ast.WImport;
import de.peeeq.wurstscript.ast.WPackage;
import de.peeeq.wurstscript.ast.WurstModel;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.gui.WurstGui;
import de.peeeq.wurstscript.jassAst.JassProg;
import de.peeeq.wurstscript.jassprinter.JassPrinter;
import de.peeeq.wurstscript.luaAst.LuaCompilationUnit;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.translation.lua.translation.LuaTranslator;
import de.peeeq.wurstscript.utils.LineOffsets;
import de.peeeq.wurstscript.utils.Utils;
import net.moonlightflower.wc3libs.bin.app.W3I;
import net.moonlightflower.wc3libs.port.GameVersion;
import net.moonlightflower.wc3libs.port.Orient;
import org.apache.commons.lang.StringUtils;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.services.LanguageClient;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.NonWritableChannelException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
public abstract class MapRequest extends UserRequest<Object> {
protected final WurstLanguageServer langServer;
protected final Optional<File> map;
protected final List<String> compileArgs;
protected final WFile workspaceRoot;
protected final RunArgs runArgs;
protected final Optional<String> wc3Path;
protected final W3InstallationData w3data;
protected final TimeTaker timeTaker;
public static long mapLastModified = 0L;
public static String mapPath = "";
private static Long lastMapModified = 0L;
private static String lastMapPath = "";
protected String cachedMapFileName = "";
public static final String BUILD_CONFIGURED_SCRIPT_NAME = "01_war3mapj_with_config.j.txt";
public static final String BUILD_COMPILED_JASS_NAME = "02_compiled.j.txt";
public static final String BUILD_COMPILED_LUA_NAME = "02_compiled.lua";
public static final String BUILD_JHCR_SCRIPT_NAME = "03_jhcr_war3map.j";
/**
* makes the compilation slower, but more safe by discarding results from the editor and working on a copy of the model
*/
protected SafetyLevel safeCompilation = SafetyLevel.KindOfSafe;
public TimeTaker getTimeTaker() {
return timeTaker;
}
enum SafetyLevel {
QuickAndDirty, KindOfSafe
}
public static class CompilationResult {
public File script;
public File w3i;
}
public MapRequest(WurstLanguageServer langServer, Optional<File> map, List<String> compileArgs, WFile workspaceRoot,
Optional<String> wc3Path, Optional<String> gameExePath) {
this.langServer = langServer;
this.map = map;
this.compileArgs = compileArgs;
this.workspaceRoot = workspaceRoot;
this.runArgs = new RunArgs(compileArgs);
this.wc3Path = wc3Path;
if (gameExePath.isPresent() && StringUtils.isNotBlank(gameExePath.get())) {
this.w3data = new W3InstallationData(Optional.of(new File(gameExePath.get())), Optional.empty());
} else {
this.w3data = getBestW3InstallationData();
}
if (runArgs.isMeasureTimes()) {
this.timeTaker = new TimeTaker.Recording();
} else {
this.timeTaker = new TimeTaker.Default();
}
}
@Override
public void handleException(LanguageClient languageClient, Throwable err, CompletableFuture<Object> resFut) {
if (err instanceof RequestFailedException) {
RequestFailedException rfe = (RequestFailedException) err;
languageClient.showMessage(new MessageParams(rfe.getMessageType(), rfe.getMessage()));
resFut.complete(new Object());
} else {
super.handleException(languageClient, err, resFut);
}
}
protected File compileMap(File projectFolder, WurstGui gui, Optional<File> mapCopy, RunArgs runArgs, WurstModel model,
WurstProjectConfigData projectConfigData, boolean isProd) {
try (@Nullable MpqEditor mpqEditor = MpqEditorFactory.getEditor(mapCopy)) {
if (mpqEditor != null && !mpqEditor.canWrite()) {
WLogger.severe("The supplied map is invalid/corrupted/protected and Wurst cannot write to it.\n" +
"Please supply a valid .w3x input map that can be opened in the world editor.");
throw new NonWritableChannelException();
}
WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(timeTaker, projectFolder, gui, mpqEditor, runArgs);
compiler.setMapFile(mapCopy);
purgeUnimportedFiles(model);
gui.sendProgress("Check program");
compiler.checkProg(model);
if (gui.getErrorCount() > 0) {
throw new RequestFailedException(MessageType.Warning, "Could not compile project: ", gui.getErrorList().get(0));
}
print("translating program ... ");
compiler.translateProgToIm(model);
if (gui.getErrorCount() > 0) {
throw new RequestFailedException(MessageType.Error, "Could not compile project (error in translation): " + gui.getErrorList().get(0));
}
timeTaker.measure("Runinng Compiletime Functions", () -> compiler.runCompiletime(projectConfigData, isProd, runArgs.isCompiletimeCache()));
if (runArgs.isLua()) {
print("Translating program to Lua ... ");
Optional<LuaCompilationUnit> luaCode = Optional.ofNullable(compiler.transformProgToLua());
if (!luaCode.isPresent()) {
print("Could not compile project\n");
throw new RuntimeException("Could not compile project (error in LUA translation)");
}
StringBuilder sb = new StringBuilder();
luaCode.get().print(sb, 0);
String compiledMapScript = sb.toString();
LuaTranslator.assertNoLeakedHashtableNativeCalls(compiledMapScript);
LuaTranslator.assertNoLeakedGetHandleIdCalls(compiledMapScript);
File buildDir = getBuildDir();
File outFile = new File(buildDir, BUILD_COMPILED_LUA_NAME);
Files.write(compiledMapScript.getBytes(Charsets.UTF_8), outFile);
return outFile;
} else {
print("Translating program to jass ... ");
compiler.transformProgToJass();
Optional<JassProg> jassProg = Optional.ofNullable(compiler.getProg());
if (!jassProg.isPresent()) {
print("Could not compile project\n");
throw new RuntimeException("Could not compile project (error in JASS translation)");
}
gui.sendProgress("Printing program");
JassPrinter printer = new JassPrinter(!runArgs.isOptimize(), jassProg.get());
String compiledMapScript = printer.printProg();
File buildDir = getBuildDir();
File outFile = new File(buildDir, BUILD_COMPILED_JASS_NAME);
Files.write(compiledMapScript.getBytes(Charsets.UTF_8), outFile);
if (!runArgs.isDisablePjass()) {
gui.sendProgress("Running PJass");
timeTaker.beginPhase("Pjass execution");
Pjass.Result pJassResult = Pjass.runPjass(outFile,
new File(buildDir, "common.j").getAbsolutePath(),
new File(buildDir, "blizzard.j").getAbsolutePath());
WLogger.info(pJassResult.getMessage());
if (!pJassResult.isOk()) {
for (CompileError err : pJassResult.getErrors()) {
gui.sendError(err);
}
throw new RuntimeException("Could not compile project (PJass error)");
}
timeTaker.endPhase();
}
if (runArgs.isHotStartmap()) {
gui.sendProgress("Running JHCR");
return runJassHotCodeReload(outFile);
}
return outFile;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected File runJassHotCodeReload(File mapScript) throws IOException, InterruptedException {
File buildDir = getBuildDir();
File commonJ = new File(buildDir, "common.j");
File blizzardJ = new File(buildDir, "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());
}
ProcessBuilder pb = new ProcessBuilder(langServer.getConfigProvider().getJhcrExe(), "init", commonJ.getName(), blizzardJ.getName(), mapScript.getName());
pb.directory(buildDir);
Utils.exec(pb, Duration.ofSeconds(30), System.err::println);
return renameJhcrOutput(buildDir);
}
/**
* removes everything compilation unit which is neither
* - inside a wurst folder
* - a jass file
* - imported by a file in a wurst folder
*/
private void purgeUnimportedFiles(WurstModel model) {
Set<CompilationUnit> imported = model.stream()
.filter(cu -> isInProjectWurstFolder(cu.getCuInfo().getFile())
|| isProjectWar3MapScript(cu.getCuInfo().getFile())
|| isRequiredBuildJass(cu.getCuInfo().getFile()))
.collect(Collectors.toSet());
addImports(imported, imported);
model.removeIf(cu -> !imported.contains(cu));
}
private boolean isProjectWar3MapScript(String file) {
if (!file.endsWith("war3map.j")) {
return false;
}
Path p = Paths.get(file).toAbsolutePath().normalize();
Path projectWurstFolder;
try {
projectWurstFolder = workspaceRoot.getPath().resolve("wurst").toAbsolutePath().normalize();
} catch (FileNotFoundException e) {
return false;
}
return p.startsWith(projectWurstFolder) && java.nio.file.Files.exists(p);
}
private boolean isInProjectWurstFolder(String file) {
Path p = Paths.get(file).toAbsolutePath().normalize();
Path projectWurstFolder;
try {
projectWurstFolder = workspaceRoot.getPath().resolve("wurst").toAbsolutePath().normalize();
} catch (FileNotFoundException e) {
return false;
}
return p.startsWith(projectWurstFolder)
&& java.nio.file.Files.exists(p)
&& Utils.isWurstFile(file);
}
private boolean isRequiredBuildJass(String file) {
Path p = Paths.get(file).toAbsolutePath().normalize();
Path buildDir;
try {
buildDir = workspaceRoot.getPath().resolve("_build").toAbsolutePath().normalize();
} catch (FileNotFoundException e) {
return false;
}
if (!p.startsWith(buildDir)) {
return false;
}
String name = p.getFileName().toString();
return name.equals("common.j") || name.equals("blizzard.j");
}
protected File getBuildDir() {
File buildDir;
try {
buildDir = new File(workspaceRoot.getFile(), "_build");
} catch (FileNotFoundException e) {
throw new RuntimeException("Cannot get build dir", e);
}
if (!buildDir.exists()) {
UtilsIO.mkdirs(buildDir);
}
return buildDir;
}
private void addImports(Set<CompilationUnit> result, Set<CompilationUnit> toAdd) {
Set<CompilationUnit> imported =
toAdd.stream()
.flatMap((CompilationUnit cu) -> cu.getPackages().stream())
.flatMap((WPackage p) -> p.getImports().stream())
.map(WImport::attrImportedPackage)
.filter(Objects::nonNull)
.map(WPackage::attrCompilationUnit)
.collect(Collectors.toSet());
boolean changed = result.addAll(imported);
if (changed) {
// recursive call terminates, as there are only finitely many compilation units
addImports(result, imported);
}
}
protected void print(String s) {
WLogger.info(s);
}
protected void println(String s) {
WLogger.info(s);
}
protected File compileScript(WurstGui gui, ModelManager modelManager, List<String> compileArgs, Optional<File> mapCopy,
WurstProjectConfigData projectConfigData, boolean isProd, File scriptFile) throws Exception {
RunArgs runArgs = new RunArgs(compileArgs);
gui.sendProgress("Compiling Script");
print("Compile Script : ");
for (File dep : modelManager.getDependencyWurstFiles()) {
WLogger.debug("dep: " + dep.getPath());
}
print("Dependencies done.");
if (safeCompilation != RunMap.SafetyLevel.QuickAndDirty) {
// it is safer to rebuild the project, instead of taking the current editor state
gui.sendProgress("Cleaning project");
modelManager.clean();
gui.sendProgress("Building project");
modelManager.buildProject();
}
replaceBaseScriptWithConfig(modelManager, scriptFile);
if (modelManager.hasErrors()) {
for (CompileError compileError : modelManager.getParseErrors()) {
gui.sendError(compileError);
}
throw new RequestFailedException(MessageType.Warning, "Cannot run code with syntax errors.");
}
WurstModel model = modelManager.getModel();
if (safeCompilation != RunMap.SafetyLevel.QuickAndDirty) {
// compilation will alter the model (e.g. remove unused imports),
// so it is safer to create a copy
model = ModelManager.copy(model);
}
return compileMap(modelManager.getProjectPath(), gui, mapCopy, runArgs, model, projectConfigData, isProd);
}
private static void replaceBaseScriptWithConfig(ModelManager modelManager, File scriptFile) throws IOException {
Optional<CompilationUnit> war3mapJ = modelManager.getModel()
.stream()
.filter((CompilationUnit cu) -> cu.getCuInfo().getFile().endsWith("war3map.j"))
.findFirst();
if (war3mapJ.isPresent()) {
modelManager.syncCompilationUnitContent(WFile.create(war3mapJ.get().getCuInfo().getFile()),
java.nio.file.Files.readString(scriptFile.toPath()));
}
}
/**
* Checks if we can use the cached map
*/
protected boolean canUseCachedMap(File cachedMap) {
if (!cachedMap.exists()) {
WLogger.info("No cached map found");
return false;
}
// Check if source map is newer
if (map.isPresent() && map.get().lastModified() > cachedMap.lastModified()) {
WLogger.info("Source map is newer than cache");
return false;
}
WLogger.info("Using cached map from previous build");
return true;
}
/**
* Gets or creates the cached map file location
*/
protected File getCachedMapFile() {
File buildDir = getBuildDir();
File cacheDir = new File(buildDir, "cache");
if (!cacheDir.exists()) {
UtilsIO.mkdirs(cacheDir);
}
return new File(cacheDir, resolveCachedMapFileName());
}
private String resolveCachedMapFileName() {
if (!cachedMapFileName.isEmpty()) {
return cachedMapFileName;
}
return resolveCachedMapFileName(runArgs.isLua());
}
private String resolveCachedMapFileName(boolean luaMode) {
if (!map.isPresent()) {
return luaMode ? "cached_map_lua.w3x" : "cached_map_jass.w3x";
}
File inputMap = map.get();
String inputName = inputMap.getName();
int dot = inputName.lastIndexOf('.');
String baseName = dot > 0 ? inputName.substring(0, dot) : inputName;
// Keep only filesystem-safe characters and avoid collisions for same basename from different folders.
String safeBase = baseName.replaceAll("[^a-zA-Z0-9._-]", "_");
String pathHash = Integer.toUnsignedString(inputMap.getAbsolutePath().hashCode(), 36);
String modeSuffix = luaMode ? "lua" : "jass";
return safeBase + "_" + pathHash + "_" + modeSuffix + "_cached.w3x";
}
protected File ensureWritableTargetFile(File targetFile, String dialogTitle, String lockMessage,
String cancelMessage) {
File currentTarget = targetFile;
while (isLocked(currentTarget)) {
int choice = showTargetLockedDialog(dialogTitle, lockMessage);
if (choice == 0 || choice == JOptionPane.CLOSED_OPTION) {
throw new RequestFailedException(MessageType.Warning, cancelMessage);
} else if (choice == 1) {
continue;
} else if (choice == 2) {
currentTarget = createTemporaryTargetFile(currentTarget);
}
}
return currentTarget;
}
private int showTargetLockedDialog(String dialogTitle, String message) {
JFrame frame = new JFrame();
frame.setAlwaysOnTop(true);
Object[] options = { "Cancel", "Retry", "Rename" };
return JOptionPane.showOptionDialog(
frame,
message,
dialogTitle,
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[1]
);
}
private File createTemporaryTargetFile(File currentTarget) {
String currentName = currentTarget.getName();
String baseName = currentName.endsWith(".w3x")
? currentName.substring(0, currentName.length() - 4)
: currentName;
try {
File parent = currentTarget.getParentFile();
if (parent == null) {
parent = getBuildDir();
}
File tempTarget = java.nio.file.Files.createTempFile(parent.toPath(), baseName + "_", ".w3x").toFile();
tempTarget.deleteOnExit();
WLogger.info("Using temporary map target due to locked file: " + tempTarget.getAbsolutePath());
return tempTarget;
} catch (IOException e) {
throw new RequestFailedException(MessageType.Error, "Could not create temporary map target file.", e);
}
}
private boolean isLocked(File targetMap) {
if (!targetMap.exists()) {
return false;
}
try (FileChannel ignored = FileChannel.open(targetMap.toPath(), StandardOpenOption.WRITE)) {
return false;
} catch (IOException e) {
return true;
}
}
/**
* Ensures cached map exists and is up to date
*/
protected File ensureCachedMap(WurstGui gui) throws IOException {
File cachedMap = getCachedMapFile();
cleanupOppositeModeCacheAndOutputs();
if (!map.isPresent()) {
throw new RequestFailedException(MessageType.Error, "No source map provided");
}
File sourceMap = map.get();
// If cached map doesn't exist or source is newer, update cache
if (!cachedMap.exists() || sourceMap.lastModified() > cachedMap.lastModified()) {
WLogger.info("Updating cached map from source");
gui.sendProgress("Updating cached map");
Files.copy(sourceMap, cachedMap);
}
return cachedMap;
}
private void cleanupOppositeModeCacheAndOutputs() {
if (cachedMapFileName.isEmpty()) {
File cacheDir = new File(getBuildDir(), "cache");
String oppositeModeCacheName = resolveCachedMapFileName(!runArgs.isLua());
java.nio.file.Path oppositeModeCache = new File(cacheDir, oppositeModeCacheName).toPath();
try {
java.nio.file.Files.deleteIfExists(oppositeModeCache);
} catch (IOException e) {
WLogger.warning("Could not delete opposite-mode cached map: " + oppositeModeCache + " (" + e.getMessage() + ")");
}
}
File buildDir = getBuildDir();
File oppositeCompiledOutput = runArgs.isLua()
? new File(buildDir, BUILD_COMPILED_JASS_NAME)
: new File(buildDir, BUILD_COMPILED_LUA_NAME);
try {
java.nio.file.Files.deleteIfExists(oppositeCompiledOutput.toPath());
} catch (IOException e) {
WLogger.warning("Could not delete opposite-mode compiled output: " + oppositeCompiledOutput + " (" + e.getMessage() + ")");
}
}
protected CompilationResult compileScript(ModelManager modelManager, WurstGui gui, Optional<File> testMap,
WurstProjectConfigData projectConfigData, File buildDir,
boolean isProd) throws Exception {
if (!runArgs.isHotReload()) {
// Ensure we're working with the cached map
File cachedMap = ensureCachedMap(gui);
// Update testMap to point to cached map
testMap = Optional.of(cachedMap);
}
CompilationResult result;
if (runArgs.isHotReload()) {
result = new CompilationResult();
result.script = new File(buildDir, BUILD_CONFIGURED_SCRIPT_NAME);
if (!result.script.exists()) {
result.script = new File(new File(workspaceRoot.getFile(), "wurst"), "war3map.j");
}
if (!result.script.exists()) {
throw new RequestFailedException(MessageType.Error, "Could not find war3map.j file");
}
} else {
timeTaker.beginPhase("load map script");
File scriptFile = loadMapScript(getMapForScriptExtraction(testMap), modelManager, gui);
timeTaker.endPhase();
result = applyProjectConfig(gui, testMap, buildDir, projectConfigData, scriptFile, BUILD_CONFIGURED_SCRIPT_NAME);
}
// Compile the script
result.script = compileScript(gui, modelManager, compileArgs, testMap, projectConfigData, isProd, result.script);
Optional<WurstModel> model = Optional.ofNullable(modelManager.getModel());
if (!model.isPresent()
|| model
.get()
.stream()
.noneMatch((CompilationUnit cu) -> cu.getCuInfo().getFile().endsWith("war3map.j"))) {
println("No 'war3map.j' file could be found inside the map nor inside the wurst folder");
println("If you compile the map with WurstPack once, this file should be in your wurst-folder. ");
println("We will try to start the map now, but it will probably fail. ");
}
return result;
}
protected void injectMapData(WurstGui gui, Optional<File> testMap, CompilationResult result) throws Exception {
gui.sendProgress("Injecting map data");
timeTaker.beginPhase("Injecting map data");
// Work directly with the cached map
File cachedMap = getCachedMapFile();
if (!cachedMap.exists()) {
throw new RequestFailedException(MessageType.Error, "Cached map does not exist");
}
try (MpqEditor mpqEditor = MpqEditorFactory.getEditor(Optional.of(cachedMap))) {
String mapScriptName;
if (runArgs.isLua()) {
mapScriptName = "war3map.lua";
injectExternalLuaFiles(result.script);
} else {
mapScriptName = "war3map.j";
}
// Delete old scripts (both root and scripts/ subdirectory locations)
if (mpqEditor.hasFile("war3map.j")) {
mpqEditor.deleteFile("war3map.j");
}
if (mpqEditor.hasFile("scripts\\war3map.j")) {
mpqEditor.deleteFile("scripts\\war3map.j");
}
if (mpqEditor.hasFile("war3map.lua")) {
mpqEditor.deleteFile("war3map.lua");
}
if (mpqEditor.hasFile("scripts\\war3map.lua")) {
mpqEditor.deleteFile("scripts\\war3map.lua");
}
// Insert new script
mpqEditor.insertFile(mapScriptName, result.script);
// Insert w3i if it changed
if (result.w3i != null) {
String w3iHash = ImportFile.calculateFileHash(result.w3i);
Optional<ImportFile.CacheManifest> manifestOpt = ImportFile.getCachedManifest(mpqEditor);
boolean w3iChanged = true;
if (manifestOpt.isPresent() && manifestOpt.get().w3iConfigMatches(w3iHash)) {
WLogger.info("W3I file unchanged, skipping injection");
w3iChanged = false;
}
if (w3iChanged) {
WLogger.info("W3I file changed, injecting");
if (mpqEditor.hasFile(W3I.GAME_PATH.getName())) {
mpqEditor.deleteFile(W3I.GAME_PATH.getName());
}
mpqEditor.insertFile(W3I.GAME_PATH.getName(), result.w3i);
// Update manifest
ImportFile.CacheManifest manifest = manifestOpt.orElse(new ImportFile.CacheManifest());
manifest.setW3iConfig(w3iHash);
ImportFile.saveManifest(mpqEditor, manifest);
}
}
// CRITICAL: Import files into THIS mpq editor instance
gui.sendProgress("Importing resource files");
timeTaker.beginPhase("Importing files");
try {
ImportFile.ImportResult importResult = ImportFile.importFilesFromImports(
workspaceRoot.getFile(),
mpqEditor
);
WLogger.info("Import result: " + importResult.toString());
} catch (Exception e) {
WLogger.severe("Failed to import files: " + e.getMessage());
throw e;
}
timeTaker.endPhase();
}
timeTaker.endPhase();
WLogger.info("Cached map size after injection: " + (cachedMap.length() / 1024 / 1024) + " MB");
}
protected File executeBuildMapPipeline(ModelManager modelManager, WurstGui gui, WurstProjectConfigData projectConfig) throws Exception {
if (!map.isPresent()) {
throw new RequestFailedException(MessageType.Error, "Map is null");
}
if (!map.get().exists()) {
throw new RequestFailedException(MessageType.Error, map.get().getAbsolutePath() + " does not exist.");
}
mapLastModified = map.get().lastModified();
mapPath = map.get().getAbsolutePath();
gui.sendProgress("Copying map");
File buildDir = getBuildDir();
File targetMapFile = getBuildOutputMapFile(projectConfig, buildDir);
targetMapFile = ensureWritableBuildOutput(targetMapFile, false);
CompilationResult result = compileScript(modelManager, gui, Optional.of(targetMapFile), projectConfig, buildDir, true);
injectMapData(gui, Optional.of(targetMapFile), result);
targetMapFile = ensureWritableBuildOutput(targetMapFile, true);
java.nio.file.Files.copy(getCachedMapFile().toPath(), targetMapFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
gui.sendProgress("Finalizing map");
try (MpqEditor mpq = MpqEditorFactory.getEditor(Optional.of(targetMapFile))) {
if (mpq != null) {
// Strip internal Wurst cache files — they are dev-only metadata and should
// not be present in the distributed map.
if (mpq.hasFile("wurst_cache_manifest.txt")) mpq.deleteFile("wurst_cache_manifest.txt");
if (mpq.hasFile("wurst_object_cache.txt")) mpq.deleteFile("wurst_object_cache.txt");
mpq.closeWithCompression();
}
}
gui.sendProgress("Done.");
return targetMapFile;
}
protected File ensureWritableBuildOutput(File targetMapFile, boolean isFinalWrite) {
String lockMessage = isFinalWrite
? "The output map file is still in use and cannot be replaced.\nClick Retry, choose Rename to use a temporary file name, or Cancel."
: "The output map file is in use and cannot be replaced.\nClose Warcraft III and click Retry, choose Rename to use a temporary file name, or Cancel.";
return ensureWritableTargetFile(
targetMapFile,
"Build Map",
lockMessage,
"Build canceled because output map target is in use."
);
}
private static File getBuildOutputMapFile(WurstProjectConfigData projectConfig, File buildDir) {
String fileName = projectConfig.getBuildMapData().getFileName();
return new File(buildDir, fileName.isEmpty() ? projectConfig.getProjectName() + ".w3x" : fileName + ".w3x");
}
private static boolean startsWith(byte[] data, byte[] prefix) {
if (data.length < prefix.length) return false;
for (int i = 0; i < prefix.length; i++) {
if (data[i] != prefix[i]) return false;
}
return true;
}
protected File loadMapScript(Optional<File> mapCopy, ModelManager modelManager, WurstGui gui) throws Exception {
File scriptFile = new File(new File(workspaceRoot.getFile(), "wurst"), "war3map.j");
// If runargs are no extract, either use existing or throw error
// Otherwise try loading from map, if map was saved with wurst, try existing script, otherwise error
if (!mapCopy.isPresent() || runArgs.isNoExtractMapScript()) {
System.out.println("No extract map script enabled - not extracting.");
if (scriptFile.exists()) {
System.out.println("war3map.j exists at wurst root.");
ensureScriptIsSynced(modelManager, scriptFile);
return scriptFile;
} else {
throw new CompileError(new WPos("", new LineOffsets(), 0, 0),
"RunArg noExtractMapScript is set but no war3map.j is provided inside the wurst folder");
}
}
if (MapRequest.mapLastModified > lastMapModified || !MapRequest.mapPath.equals(lastMapPath)) {
lastMapModified = MapRequest.mapLastModified;
lastMapPath = MapRequest.mapPath;
System.out.println("Map not cached yet, extracting script");
byte[] extractedScript = extractMapScript(mapCopy);
if (extractedScript == null) {
if (scriptFile.exists()) {
String msg = "No war3map.j in map file, using old extracted file";
WLogger.info(msg);
} else {
CompileError err = new CompileError(new WPos(mapCopy.toString(), new LineOffsets(), 0, 0),
"No war3map.j found in map file.");
gui.showInfoMessage(err.getMessage());
WLogger.severe(err);
}
} else if (startsWith(extractedScript, JassPrinter.WURST_COMMENT_RAW.getBytes(StandardCharsets.UTF_8))) {
WLogger.info("map has already been compiled with wurst");
// file generated by wurst, do not use
if (scriptFile.exists()) {
String msg = "Cannot use war3map.j from map file, because it already was compiled with wurst. " + "Using war3map.j from Wurst directory instead.";
WLogger.info(msg);
} else {
CompileError err = new CompileError(new WPos(mapCopy.toString(), new LineOffsets(), 0, 0),
"Cannot use war3map.j from map file, because it already was compiled with wurst. " + "Please add war3map.j to the wurst directory.");
gui.showInfoMessage(err.getMessage());
WLogger.severe(err);
}
} else {
WLogger.info("new map, use extracted");
// write mapfile from map to workspace
Files.write(extractedScript, scriptFile);
ensureScriptIsSynced(modelManager, scriptFile);
}
} else {
System.out.println("Map not modified, not extracting script");
}
if (scriptFile.exists()) {
ensureScriptIsSynced(modelManager, scriptFile);
}
return scriptFile;
}
private static void ensureScriptIsSynced(ModelManager modelManager, File scriptFile) {
CompilationUnit compilationUnit = modelManager.getCompilationUnit(WFile.create(scriptFile));
if (compilationUnit == null) {
modelManager.syncCompilationUnit(WFile.create(scriptFile));
}
}
protected CompilationResult applyProjectConfig(WurstGui gui, Optional<File> testMap, File buildDir, WurstProjectConfigData projectConfig, File scriptFile,
String outputScriptName) {
AtomicReference<CompilationResult> result = new AtomicReference<>();
gui.sendProgress("Applying Map Config...");
timeTaker.measure("Applying Map Config", () -> {
try {
result.set(ProjectConfigBuilder.apply(projectConfig, testMap.get(), scriptFile, buildDir, runArgs, w3data, outputScriptName));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return result.get();
}
protected Optional<File> getMapForScriptExtraction(Optional<File> mapCopy) {
if (map.isPresent()) {
return map;
}
return mapCopy;
}
protected byte[] extractMapScript(Optional<File> mapCopy) throws Exception {
if (!mapCopy.isPresent()) {
return null;
}
try (@Nullable MpqEditor mpqEditor = MpqEditorFactory.getEditor(mapCopy, true)) {
if (mpqEditor.hasFile("war3map.j")) {
return mpqEditor.extractFile("war3map.j");
}
if (mpqEditor.hasFile("scripts\\war3map.j")) {
return mpqEditor.extractFile("scripts\\war3map.j");
}
}
return null;
}
protected File renameJhcrOutput(File buildDir) throws IOException {
File rawJhcrScript = new File(buildDir, "jhcr_war3map.j");
if (!rawJhcrScript.exists()) {
throw new IOException("Could not find file " + rawJhcrScript.getAbsolutePath());
}
File renamedJhcrScript = new File(buildDir, BUILD_JHCR_SCRIPT_NAME);
java.nio.file.Files.move(rawJhcrScript.toPath(), renamedJhcrScript.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return renamedJhcrScript;
}
private W3InstallationData getBestW3InstallationData() throws RequestFailedException {
if (Orient.isLinuxSystem()) {
// no Warcraft installation supported on Linux
return new W3InstallationData(Optional.empty(), Optional.empty());
}
if (wc3Path.isPresent() && StringUtils.isNotBlank(wc3Path.get())) {
W3InstallationData w3data = new W3InstallationData(langServer, new File(wc3Path.get()),
this instanceof RunMap && !runArgs.isHotReload());
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, this instanceof RunMap && !runArgs.isHotReload());
}
}
private void injectExternalLuaFiles(File script) {
File luaDir;
try {
luaDir = new File(workspaceRoot.getFile(), "lua");
} catch (FileNotFoundException e) {
throw new RuntimeException("Cannot get build dir", e);
}
if (!luaDir.exists()) {
return;
}
try (var fileStream = java.nio.file.Files.walk(luaDir.toPath())) {
List<Path> luaFiles = fileStream
.filter(java.nio.file.Files::isRegularFile)
.sorted(Comparator.comparing(path -> {
Path relative = luaDir.toPath().relativize(path);
return relative.toString().replace('\\', '/').toLowerCase(Locale.ROOT);
}))
.collect(Collectors.toList());
if (luaFiles.isEmpty()) {
return;
}
List<byte[]> preChunks = new ArrayList<>();
List<byte[]> postChunks = new ArrayList<>();
for (Path luaFile : luaFiles) {
byte[] bytes = java.nio.file.Files.readAllBytes(luaFile);
String fileName = luaFile.getFileName().toString();
if (fileName.startsWith("pre_")) {
preChunks.add(bytes);
} else {
postChunks.add(bytes);
}
}
if (!preChunks.isEmpty()) {
byte[] existingBytes = java.nio.file.Files.readAllBytes(script.toPath());
java.nio.file.Files.write(script.toPath(), new byte[0]);
for (byte[] preChunk : preChunks) {
java.nio.file.Files.write(script.toPath(), preChunk, StandardOpenOption.APPEND);
}
java.nio.file.Files.write(script.toPath(), existingBytes, StandardOpenOption.APPEND);
}
for (byte[] postChunk : postChunks) {
java.nio.file.Files.write(script.toPath(), postChunk, StandardOpenOption.APPEND);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}