|
1 | 1 | package tests.wurstscript.tests; |
2 | 2 |
|
3 | 3 | import org.wurstscript.projectconfig.WurstProjectConfigData; |
| 4 | +import org.wurstscript.projectconfig.WurstProjectBuildMapData; |
| 5 | +import org.wurstscript.projectconfig.WurstProjectBuildPlayer; |
4 | 6 | import de.peeeq.wurstio.languageserver.WFile; |
5 | 7 | import de.peeeq.wurstio.languageserver.ProjectConfigBuilder; |
6 | 8 | import de.peeeq.wurstio.languageserver.WurstBuildConfig; |
7 | 9 | import de.peeeq.wurstio.languageserver.WurstCommands; |
8 | 10 | import de.peeeq.wurstio.utils.W3InstallationData; |
| 11 | +import net.moonlightflower.wc3libs.bin.app.MapHeader; |
9 | 12 | import net.moonlightflower.wc3libs.port.GameVersion; |
10 | 13 | import org.testng.annotations.Test; |
11 | 14 |
|
12 | 15 | import java.io.File; |
13 | 16 | import java.lang.reflect.Method; |
| 17 | +import java.nio.charset.StandardCharsets; |
14 | 18 | import java.nio.file.Files; |
15 | 19 | import java.nio.file.Path; |
16 | 20 | import java.util.List; |
@@ -223,6 +227,100 @@ public void configInjectionPrefersPinnedPatchOverDetectedInstallVersion() throws |
223 | 227 | ); |
224 | 228 | } |
225 | 229 |
|
| 230 | + @Test |
| 231 | + public void mapHeaderConfigAcceptsAnArchiveWithoutAnHm3wPrefix() throws Exception { |
| 232 | + Path mapWithoutHeader = Files.createTempFile("wurst-map-without-header", ".w3x"); |
| 233 | + byte[] original = new byte[1024]; |
| 234 | + original[0] = 'M'; |
| 235 | + original[1] = 'P'; |
| 236 | + original[2] = 'Q'; |
| 237 | + original[3] = 0x1a; |
| 238 | + Files.write(mapWithoutHeader, original); |
| 239 | + |
| 240 | + Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod( |
| 241 | + "applyMapHeader", WurstProjectConfigData.class, File.class, int.class, String.class, int.class |
| 242 | + ); |
| 243 | + applyMapHeader.setAccessible(true); |
| 244 | + |
| 245 | + applyMapHeader.invoke( |
| 246 | + null, |
| 247 | + new WurstProjectConfigData( |
| 248 | + "Test", |
| 249 | + List.of(), |
| 250 | + new WurstProjectBuildMapData("Configured map", null, null, null, null, List.of(), List.of()), |
| 251 | + null, |
| 252 | + null |
| 253 | + ), |
| 254 | + mapWithoutHeader.toFile(), |
| 255 | + 3, |
| 256 | + "Existing map", |
| 257 | + 0x1234 |
| 258 | + ); |
| 259 | + |
| 260 | + assertEquals(Files.readAllBytes(mapWithoutHeader)[0], (byte) 'H'); |
| 261 | + assertEquals(MapHeader.ofFile(mapWithoutHeader.toFile()).getMaxPlayersCount(), 3); |
| 262 | + assertEquals(Files.size(mapWithoutHeader), original.length + 512); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + public void mapHeaderConfigDoesNotReadAnArchiveWhenNothingNeedsChanging() throws Exception { |
| 267 | + Path mapWithoutHeader = Files.createTempFile("wurst-map-without-header-noop", ".w3x"); |
| 268 | + byte[] original = new byte[1024]; |
| 269 | + original[0] = 'M'; |
| 270 | + original[1] = 'P'; |
| 271 | + original[2] = 'Q'; |
| 272 | + original[3] = 0x1a; |
| 273 | + Files.write(mapWithoutHeader, original); |
| 274 | + |
| 275 | + Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod( |
| 276 | + "applyMapHeader", WurstProjectConfigData.class, File.class, int.class, String.class, int.class |
| 277 | + ); |
| 278 | + applyMapHeader.setAccessible(true); |
| 279 | + applyMapHeader.invoke(null, WurstProjectConfigData.empty(), mapWithoutHeader.toFile(), 0, null, 0); |
| 280 | + |
| 281 | + assertEquals(Files.readAllBytes(mapWithoutHeader), original); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void mapHeaderConfigPreservesExistingMapNameWhenOnlyPlayersChange() throws Exception { |
| 286 | + Path mapWithoutHeader = Files.createTempFile("wurst-map-without-header-name", ".w3x"); |
| 287 | + byte[] original = new byte[1024]; |
| 288 | + original[0] = 'M'; |
| 289 | + original[1] = 'P'; |
| 290 | + original[2] = 'Q'; |
| 291 | + original[3] = 0x1a; |
| 292 | + Files.write(mapWithoutHeader, original); |
| 293 | + |
| 294 | + Method applyMapHeader = ProjectConfigBuilder.class.getDeclaredMethod( |
| 295 | + "applyMapHeader", WurstProjectConfigData.class, File.class, int.class, String.class, int.class |
| 296 | + ); |
| 297 | + applyMapHeader.setAccessible(true); |
| 298 | + |
| 299 | + applyMapHeader.invoke( |
| 300 | + null, |
| 301 | + new WurstProjectConfigData( |
| 302 | + "Test", |
| 303 | + List.of(), |
| 304 | + new WurstProjectBuildMapData( |
| 305 | + "", null, null, null, null, |
| 306 | + List.of(new WurstProjectBuildPlayer(0, null, null, null, null)), |
| 307 | + List.of() |
| 308 | + ), |
| 309 | + null, |
| 310 | + null |
| 311 | + ), |
| 312 | + mapWithoutHeader.toFile(), |
| 313 | + 4, |
| 314 | + "Existing map", |
| 315 | + 0x1234 |
| 316 | + ); |
| 317 | + |
| 318 | + byte[] result = Files.readAllBytes(mapWithoutHeader); |
| 319 | + assertEquals(MapHeader.ofFile(mapWithoutHeader.toFile()).getMaxPlayersCount(), 1); |
| 320 | + assertEquals(MapHeader.ofFile(mapWithoutHeader.toFile()).getFlags(), 0x1234); |
| 321 | + assertTrue(new String(result, StandardCharsets.UTF_8).contains("Existing map")); |
| 322 | + } |
| 323 | + |
226 | 324 | private static String calculateProjectConfigHash(File buildDir) throws Exception { |
227 | 325 | Method method = ProjectConfigBuilder.class.getDeclaredMethod( |
228 | 326 | "calculateProjectConfigHash", |
|
0 commit comments