Skip to content

Commit 3effd09

Browse files
authored
Fix Lua interface dispatch aliases for module thistype methods (#1257)
1 parent da1cf37 commit 3effd09

14 files changed

Lines changed: 688 additions & 103 deletions

File tree

de.peeeq.wurstscript/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ dependencies {
108108
implementation 'commons-lang:commons-lang:2.6'
109109
implementation 'com.github.albfernandez:juniversalchardet:2.4.0'
110110
implementation 'org.xerial:sqlite-jdbc:3.46.1.3'
111-
implementation 'com.github.inwc3:jmpq3:e28f6999c0'
112-
implementation 'com.github.inwc3:wc3libs:ac41f780a5e2dfc35310be4ed3267f23ab3fea44'
111+
implementation 'com.github.inwc3:JMPQ3:v2.0.1'
112+
implementation 'com.github.inwc3:wc3libs:5ad2e5be4c480bb14112222521e6bc5571d00076'
113113
implementation 'com.github.wurstscript:wurst-project-config:348fcd4ef5'
114114
implementation 'org.slf4j:slf4j-api:2.0.17'
115115
implementation 'ch.qos.logback:logback-classic:1.5.20'

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static void main(String[] args) {
137137
List<String> mergedArgs = new ArrayList<>(asList(args));
138138
if (workspaceroot != null) {
139139
WLogger.info("workspaceroot: " + workspaceroot);
140-
List<String> argsList = getCompileArgs(WFile.create(workspaceroot));
140+
List<String> argsList = getCompileArgs(WFile.create(workspaceroot), runArgs.isBuild());
141141
WLogger.info("workspaceroot: " + (argsList == null));
142142
mergedArgs.addAll(argsList);
143143
compileArgs = new RunArgs(mergedArgs);

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/WurstCompilerJassImpl.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
import de.peeeq.wurstscript.types.TypesHelper;
3434
import de.peeeq.wurstscript.utils.LineOffsets;
3535
import de.peeeq.wurstscript.utils.NotNullList;
36-
import de.peeeq.wurstscript.utils.TempDir;
3736
import de.peeeq.wurstscript.utils.Utils;
3837
import org.eclipse.jdt.annotation.Nullable;
3938
import org.eclipse.lsp4j.MessageType;
4039
import org.jetbrains.annotations.NotNull;
4140

4241
import java.io.*;
4342
import java.lang.ref.WeakReference;
43+
import java.nio.charset.StandardCharsets;
4444
import java.util.*;
4545
import java.util.Map.Entry;
4646
import java.util.function.Function;
@@ -697,11 +697,8 @@ private CompilationUnit processMap(File file) {
697697
// extract mapscript:
698698
try {
699699
byte[] tempBytes = mapMpq.extractFile("war3map.j");
700-
File tempFile = File.createTempFile("war3map", ".j", TempDir.get()); // TODO work directly with bytes without temp file
701-
tempFile.deleteOnExit();
702-
Files.write(tempBytes, tempFile);
703700

704-
if (isWurstGenerated(tempFile)) {
701+
if (isWurstGenerated(tempBytes)) {
705702
// the war3map.j file was generated by wurst
706703
// this should not be the case, as we will get duplicate function errors in this case
707704
throw new AbortCompilationException(
@@ -716,12 +713,8 @@ private CompilationUnit processMap(File file) {
716713
throw new AbortCompilationException("Could not create Wurst folder at " + wurstFolder + ".");
717714
}
718715
File wurstwar3map = new File(wurstFolder, "war3map.j");
719-
wurstwar3map.delete();
720-
if (tempFile.renameTo(wurstwar3map)) {
721-
return parseFile(wurstwar3map);
722-
} else {
723-
throw new Error("Could not move war3map.j from " + tempFile + " to " + wurstwar3map);
724-
}
716+
java.nio.file.Files.write(wurstwar3map.toPath(), tempBytes);
717+
return parseFile(wurstwar3map);
725718
} catch (RuntimeException e) {
726719
throw e;
727720
} catch (Exception e) {
@@ -730,11 +723,12 @@ private CompilationUnit processMap(File file) {
730723

731724
}
732725

733-
private boolean isWurstGenerated(File tempFile) {
734-
try (FileReader fr = new FileReader(tempFile); BufferedReader in = new BufferedReader(fr)) {
726+
private boolean isWurstGenerated(byte[] contents) {
727+
try (BufferedReader in = new BufferedReader(new InputStreamReader(
728+
new ByteArrayInputStream(contents), StandardCharsets.UTF_8))) {
735729
String firstLine = in.readLine();
736730
WLogger.info("firstLine = '" + firstLine + "'");
737-
return firstLine.equals(JassPrinter.WURST_COMMENT);
731+
return JassPrinter.WURST_COMMENT.equals(firstLine);
738732
} catch (IOException e) {
739733
WLogger.severe(e);
740734
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/WurstCommands.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.nio.file.Files;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
16+
import java.util.ArrayList;
1617
import java.util.List;
1718
import java.util.Optional;
1819
import java.util.concurrent.CompletableFuture;
@@ -90,7 +91,7 @@ private static CompletableFuture<Object> buildMap(WurstLanguageServer server, Ex
9091
}
9192

9293
Optional<File> map = mapPath.map(File::new);
93-
List<String> compileArgs = getCompileArgs(workspaceRoot);
94+
List<String> compileArgs = getCompileArgs(workspaceRoot, true);
9495
return server.worker().handle(new BuildMap(server, workspaceRoot, wc3Path, map, compileArgs)).thenApply(x -> x);
9596
}
9697

@@ -121,27 +122,54 @@ private static Optional<String> getString(JsonObject options, String key) {
121122
private static final List<String> defaultArgs = ImmutableList.of("-runcompiletimefunctions", "-injectobjects", "-stacktraces");
122123

123124
public static List<String> getCompileArgs(WFile rootPath, String... additionalArgs) {
125+
return getCompileArgs(rootPath, false, additionalArgs);
126+
}
127+
128+
/**
129+
* Reads the workspace run-args file. A leading '-' is shared by run and build;
130+
* a leading '+' is build-only and is normalized to '-' for the compiler.
131+
*/
132+
public static List<String> getCompileArgs(WFile rootPath, boolean forBuild, String... additionalArgs) {
124133
try {
125134
Path configFile = Paths.get(rootPath.toString(), "wurst_run.args");
126135
if (Files.exists(configFile)) {
127136
try (Stream<String> lines = Files.lines(configFile)) {
128-
List<String> args = Stream.concat(
129-
lines.filter(s -> s.startsWith("-")),
130-
Stream.of(additionalArgs)
131-
).collect(Collectors.toList());
137+
List<String> args = new ArrayList<>(lines
138+
.map(String::trim)
139+
.filter(s -> !s.isEmpty() && !s.startsWith("#"))
140+
.filter(s -> s.startsWith("-") || (forBuild && s.startsWith("+")))
141+
.map(s -> forBuild && s.startsWith("+") ? "-" + s.substring(1) : s)
142+
.collect(Collectors.toList()));
143+
if (forBuild) {
144+
addBuildDefault(args, "-opt");
145+
addBuildDefault(args, "-inline");
146+
addBuildDefault(args, "-localOptimizations");
147+
}
148+
args.addAll(List.of(additionalArgs));
132149
return WurstBuildConfig.fromWorkspaceRoot(rootPath).applyToCompileArgs(args);
133150
}
134151
} else {
135-
136-
String cfg = String.join("\n", defaultArgs) + "\n";
152+
String cfg = String.join("\n", defaultArgs)
153+
+ "\n+opt\n+inline\n+localOptimizations\n";
137154
Files.write(configFile, cfg.getBytes(Charsets.UTF_8));
138-
return WurstBuildConfig.fromWorkspaceRoot(rootPath).applyToCompileArgs(
139-
Stream.concat(defaultArgs.stream(), Stream.of(additionalArgs)).collect(Collectors.toList())
140-
);
155+
List<String> args = new ArrayList<>(defaultArgs);
156+
if (forBuild) {
157+
args.add("-opt");
158+
args.add("-inline");
159+
args.add("-localOptimizations");
160+
}
161+
args.addAll(List.of(additionalArgs));
162+
return WurstBuildConfig.fromWorkspaceRoot(rootPath).applyToCompileArgs(args);
141163
}
142164
} catch (IOException e) {
143165
throw new RuntimeException("Could not access wurst_run.args config file", e);
144166
}
145167
}
146168

169+
private static void addBuildDefault(List<String> args, String option) {
170+
if (!args.contains(option)) {
171+
args.add(option);
172+
}
173+
}
174+
147175
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/RunMap.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public Object execute(ModelManager modelManager) throws IOException {
7272
// TODO use normal compiler for this, avoid code duplication
7373
WurstGui gui = new WurstGuiImpl(getWorkspaceAbsolute());
7474
try {
75+
warnAboutRunOptimizations(gui);
7576
String ok = compileMap(modelManager, gui, projectConfig);
7677
if (ok != null) return ok;
7778
} catch (CompileError e) {
@@ -91,6 +92,17 @@ public Object execute(ModelManager modelManager) throws IOException {
9192
return "ok"; // TODO
9293
}
9394

95+
private void warnAboutRunOptimizations(WurstGui gui) {
96+
if (!runArgs.isOptimize() && !runArgs.isInline() && !runArgs.isLocalOptimizations()) {
97+
return;
98+
}
99+
String message = "Run map is using compiler optimizations (opt/inline/localOptimizations), "
100+
+ "which can significantly slow the build. Put these options behind '+' in wurst_run.args "
101+
+ "or use Build Map to produce an optimized release map.";
102+
WLogger.warning(message);
103+
gui.sendProgress(message);
104+
}
105+
94106
@Nullable
95107
private String compileMap(ModelManager modelManager, WurstGui gui, WurstProjectConfigData projectConfig) throws Exception {
96108
if (map.isPresent() && !map.get().exists()) {

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/map/importer/ImportFile.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import de.peeeq.wurstio.mpq.MpqEditorFactory;
77
import de.peeeq.wurstscript.RunArgs;
88
import de.peeeq.wurstscript.WLogger;
9-
import de.peeeq.wurstscript.utils.TempDir;
109
import net.moonlightflower.wc3libs.bin.Wc3BinOutputStream;
1110
import net.moonlightflower.wc3libs.bin.app.IMP;
1211

@@ -297,9 +296,7 @@ public static void extractImportsFromMap(File mapFile, RunArgs runArgs) {
297296
try {
298297
File projectFolder = mapFile.getParentFile();
299298
File importDirectory = getImportDirectory(projectFolder);
300-
File tempMap = getCopyOfMap(mapFile);
301-
302-
extractImportsFrom(importDirectory, tempMap, runArgs);
299+
extractImportsFrom(importDirectory, mapFile, runArgs);
303300
} catch (Exception e) {
304301
WLogger.severe(e);
305302
JOptionPane.showMessageDialog(null, "Could not export objects (2): " + e.getMessage());
@@ -551,13 +548,6 @@ private static ImportResult insertImportedFiles_Cached(MpqEditor mpq, List<File>
551548
return new ImportResult(filesProcessed, filesUpdated, filesDeleted, duration, !importsChanged);
552549
}
553550

554-
private static File getCopyOfMap(File mapFile) throws IOException {
555-
File mapTemp = File.createTempFile("temp", "w3x", TempDir.get());
556-
mapTemp.deleteOnExit();
557-
Files.copy(mapFile, mapTemp);
558-
return mapTemp;
559-
}
560-
561551
private static File getImportDirectory(File projectFolder) {
562552
return new File(projectFolder, "imports");
563553
}

0 commit comments

Comments
 (0)