Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions de.peeeq.wurstscript/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ dependencies {
implementation 'commons-lang:commons-lang:2.6'
implementation 'com.github.albfernandez:juniversalchardet:2.4.0'
implementation 'org.xerial:sqlite-jdbc:3.46.1.3'
implementation 'com.github.inwc3:jmpq3:e28f6999c0'
implementation 'com.github.inwc3:wc3libs:ac41f780a5e2dfc35310be4ed3267f23ab3fea44'
implementation 'com.github.inwc3:JMPQ3:v2.0.1'
implementation 'com.github.inwc3:wc3libs:5ad2e5be4c480bb14112222521e6bc5571d00076'
implementation 'com.github.wurstscript:wurst-project-config:348fcd4ef5'
implementation 'org.slf4j:slf4j-api:2.0.17'
implementation 'ch.qos.logback:logback-classic:1.5.20'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void main(String[] args) {
List<String> mergedArgs = new ArrayList<>(asList(args));
if (workspaceroot != null) {
WLogger.info("workspaceroot: " + workspaceroot);
List<String> argsList = getCompileArgs(WFile.create(workspaceroot));
List<String> argsList = getCompileArgs(WFile.create(workspaceroot), runArgs.isBuild());
WLogger.info("workspaceroot: " + (argsList == null));
mergedArgs.addAll(argsList);
compileArgs = new RunArgs(mergedArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
import de.peeeq.wurstscript.types.TypesHelper;
import de.peeeq.wurstscript.utils.LineOffsets;
import de.peeeq.wurstscript.utils.NotNullList;
import de.peeeq.wurstscript.utils.TempDir;
import de.peeeq.wurstscript.utils.Utils;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4j.MessageType;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Function;
Expand Down Expand Up @@ -697,11 +697,8 @@ private CompilationUnit processMap(File file) {
// extract mapscript:
try {
byte[] tempBytes = mapMpq.extractFile("war3map.j");
File tempFile = File.createTempFile("war3map", ".j", TempDir.get()); // TODO work directly with bytes without temp file
tempFile.deleteOnExit();
Files.write(tempBytes, tempFile);

if (isWurstGenerated(tempFile)) {
if (isWurstGenerated(tempBytes)) {
// the war3map.j file was generated by wurst
// this should not be the case, as we will get duplicate function errors in this case
throw new AbortCompilationException(
Expand All @@ -716,12 +713,8 @@ private CompilationUnit processMap(File file) {
throw new AbortCompilationException("Could not create Wurst folder at " + wurstFolder + ".");
}
File wurstwar3map = new File(wurstFolder, "war3map.j");
wurstwar3map.delete();
if (tempFile.renameTo(wurstwar3map)) {
return parseFile(wurstwar3map);
} else {
throw new Error("Could not move war3map.j from " + tempFile + " to " + wurstwar3map);
}
java.nio.file.Files.write(wurstwar3map.toPath(), tempBytes);
return parseFile(wurstwar3map);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
Expand All @@ -730,11 +723,12 @@ private CompilationUnit processMap(File file) {

}

private boolean isWurstGenerated(File tempFile) {
try (FileReader fr = new FileReader(tempFile); BufferedReader in = new BufferedReader(fr)) {
private boolean isWurstGenerated(byte[] contents) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(contents), StandardCharsets.UTF_8))) {
String firstLine = in.readLine();
WLogger.info("firstLine = '" + firstLine + "'");
return firstLine.equals(JassPrinter.WURST_COMMENT);
return JassPrinter.WURST_COMMENT.equals(firstLine);
} catch (IOException e) {
WLogger.severe(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -90,7 +91,7 @@ private static CompletableFuture<Object> buildMap(WurstLanguageServer server, Ex
}

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

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

public static List<String> getCompileArgs(WFile rootPath, String... additionalArgs) {
return getCompileArgs(rootPath, false, additionalArgs);
}

/**
* Reads the workspace run-args file. A leading '-' is shared by run and build;
* a leading '+' is build-only and is normalized to '-' for the compiler.
*/
public static List<String> getCompileArgs(WFile rootPath, boolean forBuild, String... additionalArgs) {
try {
Path configFile = Paths.get(rootPath.toString(), "wurst_run.args");
if (Files.exists(configFile)) {
try (Stream<String> lines = Files.lines(configFile)) {
List<String> args = Stream.concat(
lines.filter(s -> s.startsWith("-")),
Stream.of(additionalArgs)
).collect(Collectors.toList());
List<String> args = new ArrayList<>(lines
.map(String::trim)
.filter(s -> !s.isEmpty() && !s.startsWith("#"))
.filter(s -> s.startsWith("-") || (forBuild && s.startsWith("+")))
.map(s -> forBuild && s.startsWith("+") ? "-" + s.substring(1) : s)
.collect(Collectors.toList()));
if (forBuild) {
addBuildDefault(args, "-opt");
addBuildDefault(args, "-inline");
addBuildDefault(args, "-localOptimizations");
}
args.addAll(List.of(additionalArgs));
return WurstBuildConfig.fromWorkspaceRoot(rootPath).applyToCompileArgs(args);
}
} else {

String cfg = String.join("\n", defaultArgs) + "\n";
String cfg = String.join("\n", defaultArgs)
+ "\n+opt\n+inline\n+localOptimizations\n";
Files.write(configFile, cfg.getBytes(Charsets.UTF_8));
return WurstBuildConfig.fromWorkspaceRoot(rootPath).applyToCompileArgs(
Stream.concat(defaultArgs.stream(), Stream.of(additionalArgs)).collect(Collectors.toList())
);
List<String> args = new ArrayList<>(defaultArgs);
if (forBuild) {
args.add("-opt");
args.add("-inline");
args.add("-localOptimizations");
}
args.addAll(List.of(additionalArgs));
return WurstBuildConfig.fromWorkspaceRoot(rootPath).applyToCompileArgs(args);
}
} catch (IOException e) {
throw new RuntimeException("Could not access wurst_run.args config file", e);
}
}

private static void addBuildDefault(List<String> args, String option) {
if (!args.contains(option)) {
args.add(option);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public Object execute(ModelManager modelManager) throws IOException {
// TODO use normal compiler for this, avoid code duplication
WurstGui gui = new WurstGuiImpl(getWorkspaceAbsolute());
try {
warnAboutRunOptimizations(gui);
String ok = compileMap(modelManager, gui, projectConfig);
if (ok != null) return ok;
} catch (CompileError e) {
Expand All @@ -91,6 +92,17 @@ public Object execute(ModelManager modelManager) throws IOException {
return "ok"; // TODO
}

private void warnAboutRunOptimizations(WurstGui gui) {
if (!runArgs.isOptimize() && !runArgs.isInline() && !runArgs.isLocalOptimizations()) {
return;
}
String message = "Run map is using compiler optimizations (opt/inline/localOptimizations), "
+ "which can significantly slow the build. Put these options behind '+' in wurst_run.args "
+ "or use Build Map to produce an optimized release map.";
WLogger.warning(message);
gui.sendProgress(message);
}

@Nullable
private String compileMap(ModelManager modelManager, WurstGui gui, WurstProjectConfigData projectConfig) throws Exception {
if (map.isPresent() && !map.get().exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import de.peeeq.wurstio.mpq.MpqEditorFactory;
import de.peeeq.wurstscript.RunArgs;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.utils.TempDir;
import net.moonlightflower.wc3libs.bin.Wc3BinOutputStream;
import net.moonlightflower.wc3libs.bin.app.IMP;

Expand Down Expand Up @@ -297,9 +296,7 @@ public static void extractImportsFromMap(File mapFile, RunArgs runArgs) {
try {
File projectFolder = mapFile.getParentFile();
File importDirectory = getImportDirectory(projectFolder);
File tempMap = getCopyOfMap(mapFile);

extractImportsFrom(importDirectory, tempMap, runArgs);
extractImportsFrom(importDirectory, mapFile, runArgs);
} catch (Exception e) {
WLogger.severe(e);
JOptionPane.showMessageDialog(null, "Could not export objects (2): " + e.getMessage());
Expand Down Expand Up @@ -551,13 +548,6 @@ private static ImportResult insertImportedFiles_Cached(MpqEditor mpq, List<File>
return new ImportResult(filesProcessed, filesUpdated, filesDeleted, duration, !importsChanged);
}

private static File getCopyOfMap(File mapFile) throws IOException {
File mapTemp = File.createTempFile("temp", "w3x", TempDir.get());
mapTemp.deleteOnExit();
Files.copy(mapFile, mapTemp);
return mapTemp;
}

private static File getImportDirectory(File projectFolder) {
return new File(projectFolder, "imports");
}
Expand Down
Loading
Loading