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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 1.9 (in progress)

- `-slkobjects` (opt-in, in `wurst_run.args` or on the command line) moves a built map's object modifications into
SLK tables and profile files with wc3libs' `ObjMerger`, which the game loads faster. Whatever the tables cannot
hold stays in a smaller object file, and the build log reports what was merged and what was kept and why. Map
builds merge the output map and Run Map launches an SLK copy; the cached map is left as it was. Classic
`wc3Patch` targets are refused. See `docs/OBJECT_DATA_SLK.md`.

- The local optimisations no longer skip the condition of an if whose branches both start with a return or a loop
exit. The branch merger moved the equal first statement in front of the if, so `if eff(b) > 0 ... return else
return` became a plain `return` and `eff` was never called (Jass and Lua, `-localOptimizations`). A statement which
Expand Down
2 changes: 1 addition & 1 deletion de.peeeq.wurstscript/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ dependencies {
implementation 'com.github.albfernandez:juniversalchardet:2.4.0'
implementation 'org.xerial:sqlite-jdbc:3.46.1.3'
implementation 'com.github.inwc3:JMPQ3:v2.0.2'
implementation 'com.github.inwc3:wc3libs:0542c41400'
implementation 'com.github.inwc3:wc3libs:19bf6ba502'
implementation 'com.github.wurstscript:wurst-project-config:1897c53'
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 @@ -19,6 +19,7 @@
import de.peeeq.wurstio.languageserver.WurstBuildConfig;
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
import de.peeeq.wurstio.languageserver.WurstGuiLsp;
import de.peeeq.wurstio.map.MapObjectMerger;
import de.peeeq.wurstio.map.importer.ImportFile;
import de.peeeq.wurstio.mpq.MpqEditor;
import de.peeeq.wurstio.mpq.MpqEditorFactory;
Expand Down Expand Up @@ -882,6 +883,7 @@ protected File executeBuildMapPipeline(ModelManager modelManager, WurstGui gui,
throw new RequestFailedException(MessageType.Error, map.get().getAbsolutePath() + " does not exist.");
}

requireObjectMergeTarget();
mapLastModified = getSourceMapLastModified(map.get());
mapPath = map.get().getAbsolutePath();

Expand Down Expand Up @@ -915,6 +917,8 @@ protected File executeBuildMapPipeline(ModelManager modelManager, WurstGui gui,
// 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");
// Only the published copy: the cached map keeps its object files for the next incremental build.
mergeObjectData(gui, mpq);
timeTaker.beginPhase("Finalize map");
mpq.closeWithCompression();
timeTaker.endPhase();
Expand All @@ -928,6 +932,37 @@ protected File executeBuildMapPipeline(ModelManager modelManager, WurstGui gui,
return targetMapFile;
}

/**
* {@code -slkobjects} writes whole tables of the bundled Warcraft III 3.x game data into the map. A classic
* client would play its objects with that data and read rows its own tables do not have, so the merge needs a
* Reforged target. Called before compiling, so a misconfigured build fails at once.
*/
protected void requireObjectMergeTarget() {
if (!runArgs.isSlkObjects()) {
return;
}
Optional<WurstBuildConfig.Wc3Patch> patch = buildConfig.wc3Patch();
if (patch.isPresent() && patch.get() != WurstBuildConfig.Wc3Patch.REFORGED) {
throw new RequestFailedException(MessageType.Error, "-slkobjects needs a Reforged target, but wurst.build pins wc3Patch "
+ buildConfig.wc3PatchName().orElse(patch.get().name()) + ". The merge writes Reforged game data tables into the map.");
}
}

/**
* With {@code -slkobjects}, moves the object data of the map open in {@code mpq} into SLK tables and profile
* files ({@link MapObjectMerger}). Only ever applied to a copy that is published or launched, never to the cached
* map: the next build reads the object files of the cached map and compares them with the ones it generates.
*/
protected void mergeObjectData(WurstGui gui, MpqEditor mpq) throws Exception {
if (!runArgs.isSlkObjects()) {
return;
}
gui.sendProgress("Moving object data into SLK tables");
timeTaker.beginPhase("Move object data into SLK tables");
MapObjectMerger.merge(mpq);
timeTaker.endPhase();
}

private void writeRequestedScript(CompilationResult result) throws IOException {
if (runArgs.getOutFile() == null || result.script == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import de.peeeq.wurstio.languageserver.WFile;
import de.peeeq.wurstio.languageserver.WurstBuildConfig;
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
import de.peeeq.wurstio.mpq.MpqEditor;
import de.peeeq.wurstio.mpq.MpqEditorFactory;
import de.peeeq.wurstio.utils.W3InstallationData;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.attributes.CompileError;
Expand Down Expand Up @@ -114,6 +116,9 @@ private Object compileMap(ModelManager modelManager, WurstGui gui, WurstProjectC
if (map.isPresent() && !map.get().exists()) {
throw new RequestFailedException(MessageType.Error, map.get().getAbsolutePath() + " does not exist.");
}
if (!runArgs.isHotReload()) {
requireObjectMergeTarget();
}

gui.sendProgress("Copying map");

Expand Down Expand Up @@ -177,6 +182,7 @@ private Object compileMap(ModelManager modelManager, WurstGui gui, WurstProjectC
private void startGame(WurstGui gui, CompilationResult result) throws Exception {
Optional<File> cachedMapFile = Optional.ofNullable(getCachedMapFile());
injectMapData(gui, cachedMapFile, result);
File launchMap = runArgs.isSlkObjects() ? slkLaunchCopy(gui, cachedMapFile.get()) : cachedMapFile.get();

timeTaker.beginPhase("Starting Warcraft 3");
gui.sendProgress("Starting Warcraft 3...");
Expand All @@ -191,7 +197,7 @@ private void startGame(WurstGui gui, CompilationResult result) throws Exception
// Warcraft III folder, then retry with that selection (which also re-decides map placement).
while (true) {
try {
launchGame(gui, launchData, cachedMapFile.get());
launchGame(gui, launchData, launchMap);
timeTaker.endPhase();
timeTaker.printReport();
return;
Expand All @@ -205,6 +211,23 @@ private void startGame(WurstGui gui, CompilationResult result) throws Exception
}
}

/** With {@code -slkobjects} the game starts an SLK copy of the cached map, which keeps its object files. */
private File slkLaunchCopy(WurstGui gui, File cachedMap) throws Exception {
String name = cachedMap.getName();
int dot = name.lastIndexOf('.');
File target = ensureWritableTargetFile(
new File(cachedMap.getParentFile(), (dot > 0 ? name.substring(0, dot) : name) + "_slk.w3x"),
"Run Map",
"The SLK 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 SLK map file is in use."
);
java.nio.file.Files.copy(cachedMap.toPath(), target.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
try (MpqEditor mpq = MpqEditorFactory.getEditor(Optional.of(target))) {
mergeObjectData(gui, mpq);
}
return target;
}

private void launchGame(WurstGui gui, W3InstallationData launchData, File cachedMapFile) throws IOException {
Optional<GameVersion> detectedGameVersion = launchData.getWc3PatchVersion();
File mapCopy = cachedMapFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package de.peeeq.wurstio.map;

import de.peeeq.wurstio.mpq.MpqEditor;
import de.peeeq.wurstscript.WLogger;
import net.moonlightflower.wc3libs.app.ObjMerger;
import net.moonlightflower.wc3libs.app.objMerge.BundledGameData;
import net.moonlightflower.wc3libs.app.objMerge.LayeredGameData;
import net.moonlightflower.wc3libs.app.objMerge.MemoryGameData;
import net.moonlightflower.wc3libs.app.objMerge.ObjType;
import net.moonlightflower.wc3libs.bin.ObjMod;
import net.moonlightflower.wc3libs.bin.Wc3BinInputStream;
import net.moonlightflower.wc3libs.bin.app.MapFlag;
import net.moonlightflower.wc3libs.bin.app.W3I;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* Moves a built map's object modifications ({@code war3map.w3u} and the others) into SLK tables and profile files
* with wc3libs' {@link ObjMerger} ({@code -slkobjects}). A map's table replaces the game's table of the same path,
* so whole tables are written, starting from the bundled game data of the data set the map's {@code war3map.w3i}
* selects, under the tables and profile files the map already ships. What the formats cannot hold stays in a
* residual object modification file.
*/
public final class MapObjectMerger {
private MapObjectMerger() {
}

/**
* Merges the object modifications of the map open in {@code mpq} in place.
*
* @return the merge report, or null when the map has no object modifications
*/
public static ObjMerger.Report merge(MpqEditor mpq) throws Exception {
String w3iPath = W3I.GAME_PATH.getName();
if (!mpq.hasFile(w3iPath)) {
throw new IOException("Cannot merge object data: the map has no " + w3iPath
+ ", which selects the game data its objects are based on.");
}
W3I w3i = new W3I(mpq.extractFile(w3iPath));

// Tables and profile files the map ships replace the game's, so they are the base of the merge.
MemoryGameData mapOverrides = new MemoryGameData();
for (String path : ObjMerger.gameDataPaths()) {
if (mpq.hasFile(path)) {
mapOverrides.put(path, mpq.extractFile(path));
}
}
ObjMerger merger = new ObjMerger(new LayeredGameData(mapOverrides,
BundledGameData.forDataSet(w3i.getGameDataSet(), w3i.getFlag(MapFlag.MELEE_MAP))));

for (ObjType type : ObjType.values()) {
String path = type.getObjModPath().getName();
if (!mpq.hasFile(path)) {
continue;
}
ObjMod<?> objMod = ObjMod.create(type.getObjModClass());
try (Wc3BinInputStream in = new Wc3BinInputStream(new ByteArrayInputStream(mpq.extractFile(path)))) {
objMod.read(in);
}
merger.add(objMod);
}
if (merger.getObjMods().isEmpty()) {
WLogger.info("Object merge: the map has no object modifications");
return null;
}

ObjMerger.Result result = merger.merge();
for (Map.Entry<String, byte[]> file : result.getFiles().entrySet()) {
if (mpq.hasFile(file.getKey())) {
mpq.deleteFile(file.getKey());
}
mpq.insertFile(file.getKey(), file.getValue());
}
for (String path : result.getRemovedFiles()) {
if (mpq.hasFile(path)) {
mpq.deleteFile(path);
}
}
logReport(result);
return result.getReport();
}

private static void logReport(ObjMerger.Result result) throws IOException {
ObjMerger.Report report = result.getReport();
WLogger.info("Object merge (game data " + BundledGameData.getVersion() + "): " + report);
for (Map.Entry<ObjType, List<String>> merged : report.getMerged().entrySet()) {
if (!merged.getValue().isEmpty()) {
WLogger.info(" merged " + merged.getValue().size() + " " + merged.getKey() + " objects");
}
}
for (ObjMerger.Report.Skipped skipped : report.getSkipped()) {
WLogger.info(" kept in object data: " + skipped);
}
WLogger.info(" wrote " + String.join(", ", result.getFiles().keySet())
+ (result.getRemovedFiles().isEmpty() ? "" : "; removed " + String.join(", ", result.getRemovedFiles())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RunArgs {
private final RunOption optionNodebug;
private final RunOption optionInjectCompiletimeObjects;
private final RunOption optionExportW3o;
private final RunOption optionSlkObjects;
private final RunOption optionExtractImports;
private final RunOption optionExportObjects;
private final RunOption optionStartServer;
Expand Down Expand Up @@ -113,6 +114,8 @@ public RunArgs(String... args) {
optionRunCompileTimeFunctions = addOption("runcompiletimefunctions", "Run all compiletime functions found in the scripts.");
optionInjectCompiletimeObjects = addOption("injectobjects", "Injects the objects generated by compiletime functions into the map.");
optionExportW3o = addOption("exportw3o", "Export combined map and compiletime object data to objectEditingOutput/wurstCreatedObjects.w3o beside the compiled map.");
optionSlkObjects = addOption("slkobjects", "Move the built map's object data into SLK tables and profile files, which the game loads faster. "
+ "What these cannot hold stays in the object files.");
// optimization
optionOpt = addOption("opt", "Enables identifier name compression and whitespace removal.");
optionInline = addOption("inline", "Enables function inlining.");
Expand Down Expand Up @@ -331,6 +334,10 @@ public boolean isExportW3o() {
return optionExportW3o.isSet;
}

public boolean isSlkObjects() {
return optionSlkObjects.isSet;
}

public List<File> getAdditionalLibDirs() {
return Collections.unmodifiableList(libDirs);
}
Expand Down
Loading
Loading