Skip to content

Commit 659e909

Browse files
committed
Preserve W3I map name in synthesized headers
1 parent d54d514 commit 659e909

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 2 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, w3I.getPlayers().size());
110+
applyMapHeader(projectConfig, targetMap, w3I.getPlayers().size(), w3I.getMapName());
111111

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

359359
private static void applyMapHeader(WurstProjectConfigData projectConfig, File targetMap,
360-
int existingPlayerCount) throws IOException {
360+
int existingPlayerCount, String existingMapName) throws IOException {
361361
boolean shouldWrite = false;
362362
WurstProjectBuildMapData buildMapData = projectConfig.buildMapData();
363363
if (buildMapData.players().isEmpty() && StringUtils.isBlank(buildMapData.name())) {
@@ -378,6 +378,9 @@ private static void applyMapHeader(WurstProjectConfigData projectConfig, File ta
378378
} else if (hasNoMapHeader) {
379379
mapHeader.setMaxPlayersCount(existingPlayerCount);
380380
}
381+
if (hasNoMapHeader && StringUtils.isBlank(buildMapData.name())) {
382+
mapHeader.setMapName(existingMapName);
383+
}
381384
if (StringUtils.isNotBlank(buildMapData.name())) {
382385
mapHeader.setMapName(buildMapData.name());
383386
shouldWrite = true;

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.wurstscript.projectconfig.WurstProjectConfigData;
44
import org.wurstscript.projectconfig.WurstProjectBuildMapData;
5+
import org.wurstscript.projectconfig.WurstProjectBuildPlayer;
56
import de.peeeq.wurstio.languageserver.WFile;
67
import de.peeeq.wurstio.languageserver.ProjectConfigBuilder;
78
import de.peeeq.wurstio.languageserver.WurstBuildConfig;
@@ -13,6 +14,7 @@
1314

1415
import java.io.File;
1516
import java.lang.reflect.Method;
17+
import java.nio.charset.StandardCharsets;
1618
import java.nio.file.Files;
1719
import java.nio.file.Path;
1820
import java.util.List;
@@ -236,7 +238,7 @@ public void mapHeaderConfigAcceptsAnArchiveWithoutAnHm3wPrefix() throws Exceptio
236238
Files.write(mapWithoutHeader, original);
237239

238240
Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod(
239-
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class
241+
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class, String.class
240242
);
241243
applyMapHeader.setAccessible(true);
242244

@@ -250,7 +252,8 @@ public void mapHeaderConfigAcceptsAnArchiveWithoutAnHm3wPrefix() throws Exceptio
250252
null
251253
),
252254
mapWithoutHeader.toFile(),
253-
3
255+
3,
256+
"Existing map"
254257
);
255258

256259
assertEquals(Files.readAllBytes(mapWithoutHeader)[0], (byte) 'H');
@@ -269,14 +272,52 @@ public void mapHeaderConfigDoesNotReadAnArchiveWhenNothingNeedsChanging() throws
269272
Files.write(mapWithoutHeader, original);
270273

271274
Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod(
272-
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class
275+
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class, String.class
273276
);
274277
applyMapHeader.setAccessible(true);
275-
applyMapHeader.invoke(null, WurstProjectConfigData.empty(), mapWithoutHeader.toFile(), 0);
278+
applyMapHeader.invoke(null, WurstProjectConfigData.empty(), mapWithoutHeader.toFile(), 0, null);
276279

277280
assertEquals(Files.readAllBytes(mapWithoutHeader), original);
278281
}
279282

283+
@Test
284+
public void mapHeaderConfigPreservesExistingMapNameWhenOnlyPlayersChange() throws Exception {
285+
Path mapWithoutHeader = Files.createTempFile("wurst-map-without-header-name", ".w3x");
286+
byte[] original = new byte[1024];
287+
original[0] = 'M';
288+
original[1] = 'P';
289+
original[2] = 'Q';
290+
original[3] = 0x1a;
291+
Files.write(mapWithoutHeader, original);
292+
293+
Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod(
294+
"applyMapHeader", WurstProjectConfigData.class, File.class, int.class, String.class
295+
);
296+
applyMapHeader.setAccessible(true);
297+
298+
applyMapHeader.invoke(
299+
null,
300+
new WurstProjectConfigData(
301+
"Test",
302+
List.of(),
303+
new WurstProjectBuildMapData(
304+
"", null, null, null, null,
305+
List.of(new WurstProjectBuildPlayer(0, null, null, null, null)),
306+
List.of()
307+
),
308+
null,
309+
null
310+
),
311+
mapWithoutHeader.toFile(),
312+
4,
313+
"Existing map"
314+
);
315+
316+
byte[] result = Files.readAllBytes(mapWithoutHeader);
317+
assertEquals(MapHeader.ofFile(mapWithoutHeader.toFile()).getMaxPlayersCount(), 1);
318+
assertTrue(new String(result, StandardCharsets.UTF_8).contains("Existing map"));
319+
}
320+
280321
private static String calculateProjectConfigHash(File buildDir) throws Exception {
281322
Method method = ProjectConfigBuilder.class.getDeclaredMethod(
282323
"calculateProjectConfigHash",

0 commit comments

Comments
 (0)