Studio scene pack contract for runtime SCENE binary payload
This commit is contained in:
+102
@@ -125,6 +125,108 @@ final class PackerRuntimeAssetMaterializerTest {
|
||||
assertTrue(file.contentBytes().get().length > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sceneBankPackingProjectionIncludesAcceptedRequestArtifact() throws Exception {
|
||||
final var materializer = new PackerRuntimeAssetMaterializer(new PackerAssetWalker(MAPPER));
|
||||
final Path assetRoot = Files.createDirectories(tempDir.resolve("scene-packing"));
|
||||
final Path manifestPath = assetRoot.resolve("asset.json");
|
||||
Files.writeString(manifestPath, "{}");
|
||||
Files.writeString(assetRoot.resolve("scene-bank.pack-request.json"), """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"format": "SCENE/tilemap_v1",
|
||||
"version": 1,
|
||||
"layers": [
|
||||
{
|
||||
"active": true,
|
||||
"glyph_asset_id": 7,
|
||||
"tile_size": 16,
|
||||
"parallax_factor_x": 1.0,
|
||||
"parallax_factor_y": 1.0,
|
||||
"width": 2,
|
||||
"height": 2,
|
||||
"tiles": [
|
||||
{ "active": true, "glyph_id": 1, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false }
|
||||
]
|
||||
},
|
||||
{
|
||||
"active": false,
|
||||
"glyph_asset_id": 0,
|
||||
"tile_size": 0,
|
||||
"parallax_factor_x": 1.0,
|
||||
"parallax_factor_y": 1.0,
|
||||
"width": 2,
|
||||
"height": 2,
|
||||
"tiles": [
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false }
|
||||
]
|
||||
},
|
||||
{
|
||||
"active": false,
|
||||
"glyph_asset_id": 0,
|
||||
"tile_size": 0,
|
||||
"parallax_factor_x": 1.0,
|
||||
"parallax_factor_y": 1.0,
|
||||
"width": 2,
|
||||
"height": 2,
|
||||
"tiles": [
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false }
|
||||
]
|
||||
},
|
||||
{
|
||||
"active": false,
|
||||
"glyph_asset_id": 0,
|
||||
"tile_size": 0,
|
||||
"parallax_factor_x": 1.0,
|
||||
"parallax_factor_y": 1.0,
|
||||
"width": 2,
|
||||
"height": 2,
|
||||
"tiles": [
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false },
|
||||
{ "active": false, "glyph_id": 0, "palette_id": 0, "flip_x": false, "flip_y": false }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
final PackerAssetDeclaration declaration = new PackerAssetDeclaration(
|
||||
1,
|
||||
"uuid",
|
||||
"scene_asset",
|
||||
AssetFamilyCatalog.SCENE_BANK,
|
||||
List.of(),
|
||||
OutputFormatCatalog.SCENE_TILED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
Map.of(),
|
||||
Map.of(),
|
||||
true);
|
||||
final var materialized = materializer.materialize(
|
||||
assetRoot,
|
||||
manifestPath,
|
||||
Optional.of(new PackerRegistryEntry(1, "uuid", "scene_asset", true)),
|
||||
new PackerAssetDeclarationParseResult(declaration, List.of()),
|
||||
Optional.empty(),
|
||||
PackerRuntimeMaterializationConfig.packingBuild());
|
||||
|
||||
assertEquals(1, materialized.runtimeAsset().walkProjection().buildCandidateFiles().size());
|
||||
final var file = materialized.runtimeAsset().walkProjection().buildCandidateFiles().getFirst();
|
||||
assertEquals("scene-bank.pack-request.json", file.relativePath());
|
||||
assertTrue(file.contentBytes().isPresent());
|
||||
assertTrue(file.metadata().get("scenePackRequest") instanceof p.packer.messages.assets.ScenePackRequest);
|
||||
}
|
||||
|
||||
private static PackerAssetDeclaration declaration(Map<String, String> metadata) {
|
||||
return declaration(metadata, List.of(), Map.of());
|
||||
}
|
||||
|
||||
+196
-1
@@ -1,6 +1,7 @@
|
||||
package p.packer.services;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -20,7 +21,9 @@ import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -351,6 +354,72 @@ final class FileSystemPackerWorkspaceServiceTest {
|
||||
assertEquals(0, assetTableMetadata.get(0).path("metadata").path("pipeline").path("palettes").get(0).path("index").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void packWorkspaceRemapsGlyphTileIndicesUsingGlyphMaterialSchema() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("pack-workspace-remap-material"));
|
||||
final Path assetRoot = projectRoot.resolve("assets/ui/atlas");
|
||||
final Path manifestPath = assetRoot.resolve("asset.json");
|
||||
writeTwoColorTilePng(assetRoot.resolve("confirm.png"), 16);
|
||||
|
||||
final ObjectNode manifest = (ObjectNode) MAPPER.readTree(manifestPath.toFile());
|
||||
final ObjectNode output = (ObjectNode) manifest.path("output");
|
||||
output.putObject("metadata").put("tile_size", "16x16");
|
||||
final ObjectNode pipeline = output.putObject("pipeline");
|
||||
final var palettes = pipeline.putArray("palettes");
|
||||
final ObjectNode palette = palettes.addObject();
|
||||
palette.put("index", 0);
|
||||
palette.putObject("palette")
|
||||
.putArray("originalArgb8888").add(0xFF00FF00).add(0xFFFF0000);
|
||||
((ObjectNode) palette.path("palette"))
|
||||
.putArray("convertedRgb565").add(0x07E0).add(0xF800);
|
||||
final var artifacts = manifest.putArray("artifacts");
|
||||
artifacts.addObject()
|
||||
.put("artifact_id", "art-confirm")
|
||||
.put("file", "confirm.png")
|
||||
.put("index", 0);
|
||||
MAPPER.writerWithDefaultPrettyPrinter().writeValue(manifestPath.toFile(), manifest);
|
||||
writeGlyphMaterialJson(assetRoot, """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"schemas": [
|
||||
{
|
||||
"schema_id": "schema-1",
|
||||
"status": "ok",
|
||||
"version": 1,
|
||||
"material_indices": {
|
||||
"1": "#00FF00",
|
||||
"2": "#FF0000"
|
||||
},
|
||||
"artifact_bindings": [
|
||||
{
|
||||
"artifact_id": "art-confirm",
|
||||
"fingerprint": "%s"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"palettes": [
|
||||
{
|
||||
"palette_id": "palette-1",
|
||||
"schema_id": "schema-1",
|
||||
"version": 1,
|
||||
"colors": [ "#00FF00", "#FF0000" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(fingerprint(assetRoot.resolve("confirm.png"))));
|
||||
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
final var result = service.packWorkspace(new PackWorkspaceRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
final byte[] assetsPa = Files.readAllBytes(projectRoot.resolve("build/assets.pa"));
|
||||
final int payloadOffset = (int) readLeLong(assetsPa, 12);
|
||||
final var assetTable = MAPPER.readTree(projectRoot.resolve("build/asset_table.json").toFile());
|
||||
final int assetOffset = assetTable.get(0).path("offset").asInt();
|
||||
assertEquals(0x21, assetsPa[payloadOffset + assetOffset] & 0xFF);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateAndPackIgnoreBlockingDiagnosticsFromUnselectedTileFiles() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("pack-ignore-unselected"));
|
||||
@@ -538,7 +607,84 @@ final class FileSystemPackerWorkspaceServiceTest {
|
||||
assertEquals("Layer 1", supportFile.path("layers").get(0).path("name").asText());
|
||||
assertEquals(16, supportFile.path("layers").get(0).path("width").asInt());
|
||||
assertEquals(16, supportFile.path("layers").get(0).path("height").asInt());
|
||||
assertEquals("layer-1.tmx", supportFile.path("layers").get(0).path("tilemap").asText());
|
||||
assertEquals("scene.tmx", supportFile.path("layers").get(0).path("tilemap").asText());
|
||||
assertEquals(1.0d, supportFile.path("layers").get(0).path("parallax_factor_x").asDouble());
|
||||
assertEquals(1.0d, supportFile.path("layers").get(0).path("parallax_factor_y").asDouble());
|
||||
assertEquals(0, supportFile.path("layers").get(0).path("default_palette_id").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void packValidationBlocksSceneBankWhenAcceptedRequestArtifactIsMissing() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("scene-pack-missing-request");
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
final CreateAssetResult createResult = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"scenes/overworld",
|
||||
"overworld",
|
||||
AssetFamilyCatalog.SCENE_BANK,
|
||||
OutputFormatCatalog.SCENE_TILED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
false));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, createResult.status());
|
||||
enableSceneLayer(projectRoot.resolve("assets/scenes/overworld/scene-bank.studio.json"));
|
||||
|
||||
final var result = service.validatePackWorkspace(new ValidatePackWorkspaceRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.PARTIAL, result.status());
|
||||
assertFalse(result.canPack());
|
||||
assertTrue(result.assets().stream()
|
||||
.flatMap(asset -> asset.diagnostics().stream())
|
||||
.anyMatch(diagnostic -> diagnostic.message().contains("Accepted Scene Bank request artifact is missing")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void packsSceneBankFromAcceptedRequestArtifactWithoutParsingEditorFiles() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("scene-pack-success");
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
final CreateAssetResult createResult = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"scenes/overworld",
|
||||
"overworld",
|
||||
AssetFamilyCatalog.SCENE_BANK,
|
||||
OutputFormatCatalog.SCENE_TILED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, createResult.status());
|
||||
final Path assetRoot = projectRoot.resolve("assets/scenes/overworld");
|
||||
enableSceneLayer(assetRoot.resolve("scene-bank.studio.json"));
|
||||
Files.writeString(assetRoot.resolve("layer-1.tmx"), "editor surface should be ignored by packer");
|
||||
writeScenePackRequest(assetRoot.resolve("scene-bank.pack-request.json"));
|
||||
|
||||
final var result = service.packWorkspace(new PackWorkspaceRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
final byte[] assetsPa = Files.readAllBytes(projectRoot.resolve("build/assets.pa"));
|
||||
final int headerLength = readLeInt(assetsPa, 8);
|
||||
final long payloadOffset = readLeLong(assetsPa, 12);
|
||||
final byte[] payload = java.util.Arrays.copyOfRange(assetsPa, (int) payloadOffset, assetsPa.length);
|
||||
assertEquals('S', payload[0]);
|
||||
assertEquals('C', payload[1]);
|
||||
assertEquals('N', payload[2]);
|
||||
assertEquals('E', payload[3]);
|
||||
final var assetTable = MAPPER.readTree(projectRoot.resolve("build/asset_table.json").toFile());
|
||||
assertEquals(1, assetTable.size());
|
||||
assertEquals("SCENE", assetTable.get(0).path("bank_type").asText());
|
||||
assertEquals("NONE", assetTable.get(0).path("codec").asText());
|
||||
assertEquals(expectedSceneDecodedSize(4, 256), assetTable.get(0).path("decoded_size").asInt());
|
||||
assertEquals(1, readLeShort(payload, 4));
|
||||
assertEquals(4, readLeShort(payload, 6));
|
||||
assertEquals(0b0000_0001, payload[12] & 0xFF);
|
||||
assertEquals(7, readLeInt(payload, 13));
|
||||
assertEquals(16, payload[17] & 0xFF);
|
||||
assertEquals(16, readLeInt(payload, 28));
|
||||
assertEquals(16, readLeInt(payload, 32));
|
||||
assertEquals(256, readLeInt(payload, 36));
|
||||
assertEquals(0b0000_0011, payload[44] & 0xFF);
|
||||
assertEquals(2, payload[45] & 0xFF);
|
||||
assertEquals(11, readLeShort(payload, 46));
|
||||
assertTrue(headerLength > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1582,6 +1728,46 @@ final class FileSystemPackerWorkspaceServiceTest {
|
||||
Files.writeString(assetRoot.resolve("glyph-material.json"), content);
|
||||
}
|
||||
|
||||
private String fingerprint(Path path) throws Exception {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(path)));
|
||||
}
|
||||
|
||||
private void enableSceneLayer(Path supportFilePath) throws Exception {
|
||||
final ObjectNode supportFile = (ObjectNode) MAPPER.readTree(supportFilePath.toFile());
|
||||
((ObjectNode) supportFile.path("layers").get(0)).put("enabled", true);
|
||||
MAPPER.writerWithDefaultPrettyPrinter().writeValue(supportFilePath.toFile(), supportFile);
|
||||
}
|
||||
|
||||
private void writeScenePackRequest(Path path) throws Exception {
|
||||
final ObjectNode root = MAPPER.createObjectNode();
|
||||
root.put("schema_version", 1);
|
||||
root.put("format", "SCENE/tilemap_v1");
|
||||
root.put("version", 1);
|
||||
final ArrayNode layers = root.putArray("layers");
|
||||
for (int layerIndex = 0; layerIndex < 4; layerIndex += 1) {
|
||||
final ObjectNode layer = layers.addObject();
|
||||
final boolean active = layerIndex == 0;
|
||||
layer.put("active", active);
|
||||
layer.put("glyph_asset_id", active ? 7 : 0);
|
||||
layer.put("tile_size", active ? 16 : 0);
|
||||
layer.put("parallax_factor_x", 1.0);
|
||||
layer.put("parallax_factor_y", 1.0);
|
||||
layer.put("width", 16);
|
||||
layer.put("height", 16);
|
||||
final ArrayNode tiles = layer.putArray("tiles");
|
||||
for (int tileIndex = 0; tileIndex < 256; tileIndex += 1) {
|
||||
final ObjectNode tile = tiles.addObject();
|
||||
final boolean tileActive = active && tileIndex == 0;
|
||||
tile.put("active", tileActive);
|
||||
tile.put("glyph_id", tileActive ? 11 : 0);
|
||||
tile.put("palette_id", tileActive ? 2 : 0);
|
||||
tile.put("flip_x", tileActive);
|
||||
tile.put("flip_y", false);
|
||||
}
|
||||
}
|
||||
MAPPER.writerWithDefaultPrettyPrinter().writeValue(path.toFile(), root);
|
||||
}
|
||||
|
||||
private static int readLeInt(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xFF)
|
||||
| ((bytes[offset + 1] & 0xFF) << 8)
|
||||
@@ -1589,6 +1775,11 @@ final class FileSystemPackerWorkspaceServiceTest {
|
||||
| ((bytes[offset + 3] & 0xFF) << 24);
|
||||
}
|
||||
|
||||
private static int readLeShort(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xFF)
|
||||
| ((bytes[offset + 1] & 0xFF) << 8);
|
||||
}
|
||||
|
||||
private static long readLeLong(byte[] bytes, int offset) {
|
||||
return ((long) bytes[offset] & 0xFF)
|
||||
| (((long) bytes[offset + 1] & 0xFF) << 8)
|
||||
@@ -1600,6 +1791,10 @@ final class FileSystemPackerWorkspaceServiceTest {
|
||||
| (((long) bytes[offset + 7] & 0xFF) << 56);
|
||||
}
|
||||
|
||||
private static int expectedSceneDecodedSize(int layerCount, int tilesPerLayer) {
|
||||
return layerCount * (16 + (tilesPerLayer * 8));
|
||||
}
|
||||
|
||||
private static int readLeUnsignedShort(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xFF) | ((bytes[offset + 1] & 0xFF) << 8);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user