Skip to content

Commit d54d514

Browse files
committed
Preserve W3I player count in synthesized headers
1 parent 73c59e4 commit d54d514

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static MapRequest.CompilationResult apply(WurstProjectConfigData projectC
107107
w3I.write(result.w3i);
108108

109109
// Apply map header (this is cheap, so we always do it)
110-
applyMapHeader(projectConfig, targetMap);
110+
applyMapHeader(projectConfig, targetMap, w3I.getPlayers().size());
111111

112112
// Update the manifest with new config hash (must open writable to insert)
113113
try (MpqEditor mpq = MpqEditorFactory.getEditor(Optional.of(targetMap), false)) {
@@ -356,7 +356,8 @@ private static void setVolatilePlayerConfig(WurstProjectBuildPlayer wplayer, W3I
356356
}
357357
}
358358

359-
private static void applyMapHeader(WurstProjectConfigData projectConfig, File targetMap) throws IOException {
359+
private static void applyMapHeader(WurstProjectConfigData projectConfig, File targetMap,
360+
int existingPlayerCount) throws IOException {
360361
boolean shouldWrite = false;
361362
WurstProjectBuildMapData buildMapData = projectConfig.buildMapData();
362363
if (buildMapData.players().isEmpty() && StringUtils.isBlank(buildMapData.name())) {
@@ -367,12 +368,15 @@ private static void applyMapHeader(WurstProjectConfigData projectConfig, File ta
367368
// directly with its MPQ archive. MapHeader.ofFile only reads the prefix,
368369
// so use a new header in that case; writeToMapFile will insert it before
369370
// the archive.
370-
MapHeader mapHeader = startsWithMpqArchive(targetMap)
371+
boolean hasNoMapHeader = startsWithMpqArchive(targetMap);
372+
MapHeader mapHeader = hasNoMapHeader
371373
? new MapHeader()
372374
: MapHeader.ofFile(targetMap);
373375
if (!buildMapData.players().isEmpty()) {
374376
mapHeader.setMaxPlayersCount(buildMapData.players().size());
375377
shouldWrite = true;
378+
} else if (hasNoMapHeader) {
379+
mapHeader.setMaxPlayersCount(existingPlayerCount);
376380
}
377381
if (StringUtils.isNotBlank(buildMapData.name())) {
378382
mapHeader.setMapName(buildMapData.name());

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/WurstBuildConfigTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void mapHeaderConfigAcceptsAnArchiveWithoutAnHm3wPrefix() throws Exceptio
236236
Files.write(mapWithoutHeader, original);
237237

238238
Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod(
239-
"applyMapHeader", WurstProjectConfigData.class, File.class
239+
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class
240240
);
241241
applyMapHeader.setAccessible(true);
242242

@@ -249,11 +249,12 @@ public void mapHeaderConfigAcceptsAnArchiveWithoutAnHm3wPrefix() throws Exceptio
249249
null,
250250
null
251251
),
252-
mapWithoutHeader.toFile()
252+
mapWithoutHeader.toFile(),
253+
3
253254
);
254255

255256
assertEquals(Files.readAllBytes(mapWithoutHeader)[0], (byte) 'H');
256-
assertEquals(MapHeader.ofFile(mapWithoutHeader.toFile()).getMaxPlayersCount(), 0);
257+
assertEquals(MapHeader.ofFile(mapWithoutHeader.toFile()).getMaxPlayersCount(), 3);
257258
assertEquals(Files.size(mapWithoutHeader), original.length + 512);
258259
}
259260

@@ -268,10 +269,10 @@ public void mapHeaderConfigDoesNotReadAnArchiveWhenNothingNeedsChanging() throws
268269
Files.write(mapWithoutHeader, original);
269270

270271
Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod(
271-
"applyMapHeader", WurstProjectConfigData.class, File.class
272+
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class
272273
);
273274
applyMapHeader.setAccessible(true);
274-
applyMapHeader.invoke(null, WurstProjectConfigData.empty(), mapWithoutHeader.toFile());
275+
applyMapHeader.invoke(null, WurstProjectConfigData.empty(), mapWithoutHeader.toFile(), 0);
275276

276277
assertEquals(Files.readAllBytes(mapWithoutHeader), original);
277278
}

0 commit comments

Comments
 (0)