packer (WIP)

This commit is contained in:
2026-03-24 13:42:55 +00:00
parent d9911a63dc
commit db8c17864f
12 changed files with 815 additions and 406 deletions
@@ -981,16 +981,19 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
final ObjectNode outputNode = mutableObject(manifest, "output");
final ObjectNode pipelineNode = mutableObject(outputNode, "pipeline");
final ArrayNode palettesNode = pipelineNode.putArray("palettes");
for (Map<String, Object> palette : request.selectedPalettes()) {
for (int paletteOrder = 0; paletteOrder < request.selectedPalettes().size(); paletteOrder += 1) {
final Map<String, Object> palette = request.selectedPalettes().get(paletteOrder);
final List<Integer> originalArgb8888 = integerList(palette.get("originalArgb8888"));
final List<Integer> convertedRgb565 = integerList(palette.get("convertedRgb565"));
if (originalArgb8888.isEmpty() || convertedRgb565.isEmpty()) {
throw new IllegalArgumentException("Each selected palette must contain originalArgb8888 and convertedRgb565 entries.");
}
final ObjectNode paletteNode = palettesNode.addObject();
final ArrayNode originalNode = paletteNode.putArray("originalArgb8888");
paletteNode.put("index", paletteIndex(palette, paletteOrder));
final ObjectNode payloadNode = paletteNode.putObject("palette");
final ArrayNode originalNode = payloadNode.putArray("originalArgb8888");
originalArgb8888.forEach(originalNode::add);
final ArrayNode convertedNode = paletteNode.putArray("convertedRgb565");
final ArrayNode convertedNode = payloadNode.putArray("convertedRgb565");
convertedRgb565.forEach(convertedNode::add);
}
if (palettesNode.isEmpty()) {
@@ -998,6 +1001,14 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
}
}
private int paletteIndex(Map<String, Object> palette, int fallbackIndex) {
final Object rawIndex = palette.get("index");
if (rawIndex instanceof Number number && number.intValue() >= 0) {
return number.intValue();
}
return fallbackIndex;
}
private List<Integer> integerList(Object value) {
if (!(value instanceof Iterable<?> iterable)) {
return List.of();
@@ -18,6 +18,7 @@ import java.util.*;
public final class PackerAssetDeclarationParser {
private static final Set<Integer> SUPPORTED_SCHEMA_VERSIONS = Set.of(1);
private static final int MAX_PIPELINE_PALETTES = 64;
private final ObjectMapper mapper;
@@ -270,11 +271,96 @@ public final class PackerAssetDeclarationParser {
true));
return;
}
if ("palettes".equals(key)) {
pipelineMetadata.put(key, validatePaletteDeclarations(valueNode, diagnostics, manifestPath));
return;
}
pipelineMetadata.put(key, valueNode.deepCopy());
});
return Map.copyOf(pipelineMetadata);
}
private JsonNode validatePaletteDeclarations(
final JsonNode node,
final List<PackerDiagnostic> diagnostics,
final Path manifestPath) {
if (!node.isArray()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Field 'output.pipeline.palettes' must be an array of palette declarations.",
manifestPath,
true));
return node.deepCopy();
}
final Set<Integer> seenIndices = new HashSet<>();
int paletteCount = 0;
for (JsonNode entry : node) {
paletteCount += 1;
if (!entry.isObject()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Each palette declaration must be an object with non-negative integer 'index' and object 'palette'.",
manifestPath,
true));
continue;
}
final JsonNode indexNode = entry.path("index");
final JsonNode paletteNode = entry.path("palette");
if (!indexNode.isInt() || indexNode.intValue() < 0) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Each palette declaration must define a non-negative integer 'index'.",
manifestPath,
true));
} else if (!seenIndices.add(indexNode.intValue())) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Field 'output.pipeline.palettes' contains duplicate palette index " + indexNode.intValue() + ".",
manifestPath,
true));
}
if (!paletteNode.isObject()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Each palette declaration must define an object 'palette' payload.",
manifestPath,
true));
continue;
}
final JsonNode originalNode = paletteNode.path("originalArgb8888");
final JsonNode convertedNode = paletteNode.path("convertedRgb565");
if (!originalNode.isArray() || !convertedNode.isArray()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Each palette payload must contain array fields 'originalArgb8888' and 'convertedRgb565'.",
manifestPath,
true));
continue;
}
}
if (paletteCount > MAX_PIPELINE_PALETTES) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Field 'output.pipeline.palettes' cannot declare more than " + MAX_PIPELINE_PALETTES + " palettes.",
manifestPath,
true));
}
return node.deepCopy();
}
private void rejectLegacyInputs(
final JsonNode node,
final List<PackerDiagnostic> diagnostics,
@@ -179,6 +179,7 @@ public final class PackerAssetDetailsService {
palettes.add(normalized);
}
}
palettes.sort(Comparator.comparingInt(palette -> ((Number) palette.get("index")).intValue()));
return List.copyOf(palettes);
}
@@ -186,8 +187,13 @@ public final class PackerAssetDetailsService {
if (!(paletteNode instanceof ObjectNode paletteObject)) {
return Map.of();
}
final JsonNode originalNode = paletteObject.path("originalArgb8888");
final JsonNode convertedNode = paletteObject.path("convertedRgb565");
final JsonNode indexNode = paletteObject.path("index");
final JsonNode payloadNode = paletteObject.path("palette");
if (!indexNode.isInt() || indexNode.intValue() < 0 || !(payloadNode instanceof ObjectNode payloadObject)) {
return Map.of();
}
final JsonNode originalNode = payloadObject.path("originalArgb8888");
final JsonNode convertedNode = payloadObject.path("convertedRgb565");
if (!originalNode.isArray() || !convertedNode.isArray()) {
return Map.of();
}
@@ -206,6 +212,7 @@ public final class PackerAssetDetailsService {
convertedRgb565.add(colorNode.intValue());
}
return Map.of(
"index", indexNode.intValue(),
"originalArgb8888", List.copyOf(originalArgb8888),
"convertedRgb565", List.copyOf(convertedRgb565));
}
@@ -505,8 +505,10 @@ final class FileSystemPackerWorkspaceServiceTest {
final var manifest = MAPPER.readTree(projectRoot.resolve("assets/ui/atlas/asset.json").toFile());
assertEquals(2, manifest.path("output").path("pipeline").path("palettes").size());
assertEquals(0xFFFF0000, manifest.path("output").path("pipeline").path("palettes").get(0).path("originalArgb8888").get(0).asInt());
assertEquals(0x001F, manifest.path("output").path("pipeline").path("palettes").get(1).path("convertedRgb565").get(0).asInt());
assertEquals(0, manifest.path("output").path("pipeline").path("palettes").get(0).path("index").asInt());
assertEquals(0xFFFF0000, manifest.path("output").path("pipeline").path("palettes").get(0).path("palette").path("originalArgb8888").get(0).asInt());
assertEquals(1, manifest.path("output").path("pipeline").path("palettes").get(1).path("index").asInt());
assertEquals(0x001F, manifest.path("output").path("pipeline").path("palettes").get(1).path("palette").path("convertedRgb565").get(0).asInt());
}
@Test
@@ -91,6 +91,83 @@ final class PackerAssetDeclarationParserTest {
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("output.pipeline")));
}
@Test
void parsesPaletteDeclarationsUsingExplicitIndexContract() throws Exception {
final Path manifest = tempDir.resolve("asset.json");
Files.writeString(manifest, """
{
"schema_version": 1,
"asset_uuid": "uuid-palette-contract",
"name": "palette_asset",
"type": "tile_bank",
"output": {
"format": "TILES/indexed_v1",
"codec": "NONE",
"pipeline": {
"palettes": [
{
"index": 1,
"palette": {
"originalArgb8888": [4294901760],
"convertedRgb565": [63488]
}
}
]
}
},
"preload": { "enabled": true }
}
""");
final var result = parser.parse(manifest);
assertTrue(result.valid());
assertTrue(result.declaration().outputPipelineMetadata().get("palettes").isArray());
assertEquals(1, result.declaration().outputPipelineMetadata().get("palettes").get(0).path("index").asInt());
assertEquals(63488, result.declaration().outputPipelineMetadata().get("palettes").get(0).path("palette").path("convertedRgb565").get(0).asInt());
}
@Test
void rejectsPaletteDeclarationsWithDuplicateIndices() throws Exception {
final Path manifest = tempDir.resolve("asset.json");
Files.writeString(manifest, """
{
"schema_version": 1,
"asset_uuid": "uuid-palette-duplicate",
"name": "palette_asset",
"type": "tile_bank",
"output": {
"format": "TILES/indexed_v1",
"codec": "NONE",
"pipeline": {
"palettes": [
{
"index": 0,
"palette": {
"originalArgb8888": [4294901760],
"convertedRgb565": [63488]
}
},
{
"index": 0,
"palette": {
"originalArgb8888": [4278255360],
"convertedRgb565": [2016]
}
}
]
}
},
"preload": { "enabled": true }
}
""");
final var result = parser.parse(manifest);
assertFalse(result.valid());
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("duplicate palette index 0")));
}
@Test
void rejectsMalformedJsonWithStructuralDiagnostic() {
final var result = parser.parse(PackerFixtureLocator.fixtureRoot("workspaces/invalid-malformed/assets/bad/asset.json"));
@@ -63,9 +63,11 @@ final class PackerAssetDetailsServiceTest {
final ObjectNode manifest = (ObjectNode) mapper.readTree(manifestPath.toFile());
final ObjectNode pipeline = ((ObjectNode) manifest.path("output")).putObject("pipeline");
final var palettes = pipeline.putArray("palettes");
palettes.addObject()
final ObjectNode declaration = palettes.addObject();
declaration.put("index", 3);
declaration.putObject("palette")
.putArray("originalArgb8888").add(0xFFFF0000).add(0xFF00FF00);
((ObjectNode) palettes.get(0))
((ObjectNode) declaration.path("palette"))
.putArray("convertedRgb565").add(0xF800).add(0x07E0);
mapper.writerWithDefaultPrettyPrinter().writeValue(manifestPath.toFile(), manifest);
@@ -73,6 +75,7 @@ final class PackerAssetDetailsServiceTest {
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forAssetId(1)));
assertEquals(1, result.details().pipelinePalettes().size());
assertEquals(3, result.details().pipelinePalettes().getFirst().get("index"));
assertEquals(
List.of(0xFFFF0000, 0xFF00FF00),
result.details().pipelinePalettes().getFirst().get("originalArgb8888"));