dev/glyph-bank-alignment (#3)
JaCoCo Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 60.67% (15274/25176) * Branch Coverage: 53.64% (5782/10779) * Lines of Code: 25176 * Cyclomatic Complexity: 9960 #### Quality Gates Summary Output truncated.
Test / Build skipped: 11, passed: 545
Intrepid/Prometeu/Studio/pipeline/head This commit looks good

Reviewed-on: #3
Co-authored-by: bQUARKz <bquarkz@gmail.com>
Co-committed-by: bQUARKz <bquarkz@gmail.com>
This commit was merged in pull request #3.
This commit is contained in:
2026-04-10 06:14:07 +00:00
committed by bquarkz
parent 11b24b0707
commit dd6c9dd718
45 changed files with 332 additions and 211 deletions
@@ -0,0 +1,4 @@
package p.packer.models;
public record PackerGlyphBankRequirements(int tileSize) {
}
@@ -1,4 +0,0 @@
package p.packer.models;
public record PackerTileBankRequirements(int tileSize) {
}
@@ -17,21 +17,21 @@ import java.util.Optional;
import java.util.Set;
public class PackerAssetWalker {
private static final int TILE_BANK_EMITTED_SHEET_SIZE = 256;
private static final int TILE_BANK_MAX_PALETTES = 64;
private static final Set<OutputFormatCatalog> TILE_BANK_SUPPORTED_FORMATS = Set.of(
OutputFormatCatalog.TILES_INDEXED_V1
private static final int GLYPH_BANK_EMITTED_SHEET_SIZE = 256;
private static final int GLYPH_BANK_MAX_PALETTES = 64;
private static final Set<OutputFormatCatalog> GLYPH_BANK_SUPPORTED_FORMATS = Set.of(
OutputFormatCatalog.GLYPH_INDEXED_V1
);
private static final Set<OutputFormatCatalog> SOUND_BANK_SUPPORTED_FORMATS = Set.of(
OutputFormatCatalog.SOUND_V1
);
private final PackerTileBankWalker tileBankWalker;
private final PackerGlyphBankWalker glyphBankWalker;
private final PackerSoundBankWalker soundBankWalker;
public PackerAssetWalker(final ObjectMapper mapper) {
this.tileBankWalker = new PackerTileBankWalker(mapper);
this.glyphBankWalker = new PackerGlyphBankWalker(mapper);
this.soundBankWalker = new PackerSoundBankWalker(mapper);
}
@@ -47,18 +47,18 @@ public class PackerAssetWalker {
final Optional<PackerAssetCacheEntry> priorAssetCache) {
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
switch (declaration.assetFamily()) {
case TILE_BANK -> {
case GLYPH_BANK -> {
var metadata = declaration.outputMetadata();
if (MapUtils.isEmpty(metadata)) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.WARNING,
PackerDiagnosticCategory.HYGIENE,
"Output metadata for tile bank cannot be empty, using default values",
"Output metadata for glyph bank cannot be empty, using default values",
assetRoot,
false));
metadata = Map.of("tile_size", "16x16");
}
final var requirementBuildResult = buildTileBankRequirements(declaration, metadata);
final var requirementBuildResult = buildGlyphBankRequirements(declaration, metadata);
if (requirementBuildResult.hasError()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
@@ -68,11 +68,11 @@ public class PackerAssetWalker {
true));
return new PackerWalkResult(List.of(), diagnostics);
}
diagnostics.addAll(tileBankDeclarationDiagnostics(assetRoot, declaration, requirementBuildResult.requirements));
final var walkResult = tileBankWalker.walk(assetRoot, requirementBuildResult.requirements, priorAssetCache);
diagnostics.addAll(glyphBankDeclarationDiagnostics(assetRoot, declaration, requirementBuildResult.requirements));
final var walkResult = glyphBankWalker.walk(assetRoot, requirementBuildResult.requirements, priorAssetCache);
diagnostics.addAll(walkResult.diagnostics());
diagnostics.addAll(tileBankSelectedArtifactDiagnostics(assetRoot, declaration, walkResult));
diagnostics.addAll(tileBankFragileIndexDiagnostics(declaration, walkResult));
diagnostics.addAll(glyphBankSelectedArtifactDiagnostics(assetRoot, declaration, walkResult));
diagnostics.addAll(glyphBankFragileIndexDiagnostics(declaration, walkResult));
return new PackerWalkResult(walkResult.probeResults(), diagnostics);
}
case SOUND_BANK -> {
@@ -81,7 +81,7 @@ public class PackerAssetWalker {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.WARNING,
PackerDiagnosticCategory.HYGIENE,
"Output metadata for tile bank cannot be empty, using default values",
"Output metadata for sound bank cannot be empty, using default values",
assetRoot,
false));
metadata = Map.of("sample_rate", "44100", "channels", "1");
@@ -113,15 +113,15 @@ public class PackerAssetWalker {
return PackerWalkResult.EMPTY;
}
private RequirementBuildResult<PackerTileBankRequirements> buildTileBankRequirements(
private RequirementBuildResult<PackerGlyphBankRequirements> buildGlyphBankRequirements(
final PackerAssetDeclaration declaration,
final Map<String, String> metadata) {
if (!TILE_BANK_SUPPORTED_FORMATS.contains(declaration.outputFormat())) {
return RequirementBuildResult.fail("Unsupported output format for tile bank: " + declaration.outputFormat());
if (!GLYPH_BANK_SUPPORTED_FORMATS.contains(declaration.outputFormat())) {
return RequirementBuildResult.fail("Unsupported output format for glyph bank: " + declaration.outputFormat());
}
final var tileSizeStr = metadata.get("tile_size");
if (StringUtils.isBlank(tileSizeStr)) {
return RequirementBuildResult.fail("Tile size metadata for tile bank cannot be empty");
return RequirementBuildResult.fail("Tile size metadata for glyph bank cannot be empty");
}
final int tileSize;
switch (tileSizeStr) {
@@ -135,16 +135,16 @@ public class PackerAssetWalker {
tileSize = 32;
} break;
default: {
return RequirementBuildResult.fail("Unsupported tile size for tile bank: " + tileSizeStr);
return RequirementBuildResult.fail("Unsupported tile size for glyph bank: " + tileSizeStr);
}
}
return RequirementBuildResult.success(new PackerTileBankRequirements(tileSize));
return RequirementBuildResult.success(new PackerGlyphBankRequirements(tileSize));
}
private List<PackerDiagnostic> tileBankDeclarationDiagnostics(
private List<PackerDiagnostic> glyphBankDeclarationDiagnostics(
final Path assetRoot,
final PackerAssetDeclaration declaration,
final PackerTileBankRequirements requirements) {
final PackerGlyphBankRequirements requirements) {
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
final List<PackerAssetArtifactSelection> artifacts = declaration.artifacts();
final Set<Integer> seenIndices = new java.util.HashSet<>();
@@ -169,8 +169,8 @@ public class PackerAssetWalker {
}
}
final int tileCapacity = (TILE_BANK_EMITTED_SHEET_SIZE / requirements.tileSize())
* (TILE_BANK_EMITTED_SHEET_SIZE / requirements.tileSize());
final int tileCapacity = (GLYPH_BANK_EMITTED_SHEET_SIZE / requirements.tileSize())
* (GLYPH_BANK_EMITTED_SHEET_SIZE / requirements.tileSize());
if (artifacts.size() > tileCapacity) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
@@ -191,18 +191,18 @@ public class PackerAssetWalker {
assetRoot,
true));
}
if (declaredPalettes.size() > TILE_BANK_MAX_PALETTES) {
if (declaredPalettes.size() > GLYPH_BANK_MAX_PALETTES) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Tile bank cannot declare more than " + TILE_BANK_MAX_PALETTES + " palettes.",
"Glyph bank cannot declare more than " + GLYPH_BANK_MAX_PALETTES + " palettes.",
assetRoot,
true));
}
return diagnostics;
}
private List<PackerDiagnostic> tileBankSelectedArtifactDiagnostics(
private List<PackerDiagnostic> glyphBankSelectedArtifactDiagnostics(
final Path assetRoot,
final PackerAssetDeclaration declaration,
final PackerWalkResult walkResult) {
@@ -225,7 +225,7 @@ public class PackerAssetWalker {
return diagnostics;
}
private List<PackerDiagnostic> tileBankFragileIndexDiagnostics(
private List<PackerDiagnostic> glyphBankFragileIndexDiagnostics(
final PackerAssetDeclaration declaration,
final PackerWalkResult walkResult) {
final List<DeclaredPalette> declaredPalettes = declaredPalettes(declaration);
@@ -7,19 +7,17 @@ import p.packer.models.*;
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class PackerTileBankWalker extends PackerAbstractBankWalker<PackerTileBankRequirements> {
public class PackerGlyphBankWalker extends PackerAbstractBankWalker<PackerGlyphBankRequirements> {
private static final int MAX_COLORS_PER_PALETTE = 15;
private static final int COLOR_KEY_RGB = 0x00FF00FF;
private static final Set<String> SUPPORTED_MIME_TYPES = Set.of(
"image/png"
);
public PackerTileBankWalker(ObjectMapper mapper) {
public PackerGlyphBankWalker(ObjectMapper mapper) {
super(mapper);
}
@@ -42,7 +40,7 @@ public class PackerTileBankWalker extends PackerAbstractBankWalker<PackerTileBan
protected PackerProbeResult processFileProbe(
final PackerFileProbe fileProbe,
final Path assetRoot,
final PackerTileBankRequirements requirements) {
final PackerGlyphBankRequirements requirements) {
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
final var image = readImage(fileProbe, diagnostics);
@@ -63,7 +61,7 @@ public class PackerTileBankWalker extends PackerAbstractBankWalker<PackerTileBan
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Invalid tile dimensions for " + fileProbe.path().getFileName()
"Invalid glyph dimensions for " + fileProbe.path().getFileName()
+ ": expected " + requirements.tileSize() + "x" + requirements.tileSize()
+ " but got " + width + "x" + height,
fileProbe.path(),
@@ -140,7 +138,6 @@ public class PackerTileBankWalker extends PackerAbstractBankWalker<PackerTileBan
return new PackerProbeResult(fileProbe, Map.of("tile", tile, "palette", palette), diagnostics);
}
@SuppressWarnings("unchecked")
private boolean hasReusableTileMetadata(final Map<String, Object> metadata) {
final Object tileValue = metadata.get("tile");
if (!(tileValue instanceof Map<?, ?> tile)) {
@@ -37,7 +37,7 @@ import java.util.*;
import java.util.stream.Stream;
public final class FileSystemPackerWorkspaceService implements PackerWorkspaceService {
private static final int TILE_BANK_COLOR_KEY_RGB565 = 0xF81F;
private static final int GLYPH_BANK_COLOR_KEY_RGB565 = 0xF81F;
private final ObjectMapper mapper;
private final PackerWorkspaceFoundation workspaceFoundation;
@@ -816,7 +816,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
if (declaration == null) {
return List.of();
}
if (declaration.assetFamily() == AssetFamilyCatalog.TILE_BANK) {
if (declaration.assetFamily() == AssetFamilyCatalog.GLYPH_BANK) {
return List.of();
}
return List.of(new PackerDiagnostic(
@@ -878,7 +878,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
"size", packedAsset.payload().length,
"decoded_size", packedAsset.decodedSize(),
"codec", packedAsset.codec(),
"metadata", packedAsset.metadata())));
"metadata", packedAsset.runtimeMetadata())));
if (packedAsset.preloadEnabled()) {
final int slot = nextPreloadSlotByBankType.getOrDefault(packedAsset.bankType(), 0);
nextPreloadSlotByBankType.put(packedAsset.bankType(), slot + 1);
@@ -888,7 +888,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
}
assetTableMetadata.add(new LinkedHashMap<>(Map.of(
"asset_id", packedAsset.assetId(),
"metadata", packedAsset.metadata())));
"metadata", packedAsset.toolingMetadata())));
}
final byte[] headerBytes = canonicalJsonBytes(Map.of(
@@ -909,10 +909,10 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
private PackedAsset packRuntimeAsset(PackerRuntimeAsset runtimeAsset) {
final PackerAssetDeclaration declaration = runtimeAsset.parsedDeclaration().declaration();
if (declaration == null || declaration.assetFamily() != AssetFamilyCatalog.TILE_BANK) {
if (declaration == null || declaration.assetFamily() != AssetFamilyCatalog.GLYPH_BANK) {
throw new IllegalStateException("Unsupported pack output family for current implementation.");
}
return packTileBank(runtimeAsset, declaration);
return packGlyphBank(runtimeAsset, declaration);
}
private boolean isEffectivelyIncludedInPackSet(PackerRuntimeAsset runtimeAsset) {
@@ -923,7 +923,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
return declaration == null || !declaration.artifacts().isEmpty();
}
private PackedAsset packTileBank(PackerRuntimeAsset runtimeAsset, PackerAssetDeclaration declaration) {
private PackedAsset packGlyphBank(PackerRuntimeAsset runtimeAsset, PackerAssetDeclaration declaration) {
final int tileSize = parseTileSize(declaration.outputMetadata().get("tile_size"));
final int width = 256;
final int height = 256;
@@ -955,35 +955,38 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
});
final byte[] packedPixels = packNibbles(sheetPixels);
final byte[] paletteBytes = emitTileBankPalettes(declaration);
final byte[] paletteBytes = emitGlyphBankPalettes(declaration);
final byte[] payload = new byte[packedPixels.length + paletteBytes.length];
System.arraycopy(packedPixels, 0, payload, 0, packedPixels.length);
System.arraycopy(paletteBytes, 0, payload, packedPixels.length, paletteBytes.length);
final LinkedHashMap<String, Object> metadata = new LinkedHashMap<>();
metadata.put("tile_size", tileSize);
metadata.put("width", width);
metadata.put("height", height);
metadata.put("palette_count", 64);
metadata.put("palette_authored", countAuthoredTileBankPalettes(declaration));
metadata.put("codec", Map.of());
metadata.put("pipeline", normalizeTileBankRuntimePipelineMetadata(declaration));
final LinkedHashMap<String, Object> runtimeMetadata = new LinkedHashMap<>();
runtimeMetadata.put("tile_size", tileSize);
runtimeMetadata.put("width", width);
runtimeMetadata.put("height", height);
runtimeMetadata.put("palette_count", 64);
runtimeMetadata.put("palette_authored", countAuthoredGlyphBankPalettes(declaration));
runtimeMetadata.put("codec", Map.of());
declaration.outputMetadata().forEach((key, value) -> {
if (!"tile_size".equals(key)) {
metadata.putIfAbsent(key, value);
runtimeMetadata.putIfAbsent(key, value);
}
});
final LinkedHashMap<String, Object> toolingMetadata = new LinkedHashMap<>(runtimeMetadata);
toolingMetadata.put("pipeline", normalizeToolingPipelineMetadata(declaration));
final PackerRegistryEntry registryEntry = runtimeAsset.registryEntry()
.orElseThrow(() -> new IllegalStateException("Packed runtime asset must be registered"));
return new PackedAsset(
registryEntry.assetId(),
declaration.name(),
"TILES",
"GLYPH",
"NONE",
payload,
width * height + 2048,
metadata,
runtimeMetadata,
toolingMetadata,
declaration.preloadEnabled());
}
@@ -1006,7 +1009,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
return packed;
}
private byte[] emitTileBankPalettes(PackerAssetDeclaration declaration) {
private byte[] emitGlyphBankPalettes(PackerAssetDeclaration declaration) {
final byte[] bytes = new byte[64 * 16 * 2];
final JsonNode palettesNode = declaration.outputPipelineMetadata().get("palettes");
if (!(palettesNode instanceof com.fasterxml.jackson.databind.node.ArrayNode palettesArray)) {
@@ -1023,8 +1026,8 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
continue;
}
final int paletteBaseOffset = (paletteIndex * 16) * 2;
bytes[paletteBaseOffset] = (byte) (TILE_BANK_COLOR_KEY_RGB565 & 0xFF);
bytes[paletteBaseOffset + 1] = (byte) ((TILE_BANK_COLOR_KEY_RGB565 >>> 8) & 0xFF);
bytes[paletteBaseOffset] = (byte) (GLYPH_BANK_COLOR_KEY_RGB565 & 0xFF);
bytes[paletteBaseOffset + 1] = (byte) ((GLYPH_BANK_COLOR_KEY_RGB565 >>> 8) & 0xFF);
final JsonNode convertedNode = paletteNode.path("convertedRgb565");
if (!convertedNode.isArray()) {
continue;
@@ -1039,16 +1042,13 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
return bytes;
}
private int countAuthoredTileBankPalettes(PackerAssetDeclaration declaration) {
private int countAuthoredGlyphBankPalettes(PackerAssetDeclaration declaration) {
final JsonNode palettesNode = declaration.outputPipelineMetadata().get("palettes");
return palettesNode instanceof ArrayNode palettesArray ? palettesArray.size() : 0;
}
private Map<String, Object> normalizeTileBankRuntimePipelineMetadata(PackerAssetDeclaration declaration) {
final LinkedHashMap<String, Object> pipeline = new LinkedHashMap<>(
PackerReadMessageMapper.normalizeMetadata(declaration.outputPipelineMetadata()));
pipeline.remove("palettes");
return pipeline;
private Map<String, Object> normalizeToolingPipelineMetadata(PackerAssetDeclaration declaration) {
return new LinkedHashMap<>(PackerReadMessageMapper.normalizeMetadata(declaration.outputPipelineMetadata()));
}
private byte[] buildPrelude(int headerLength) {
@@ -1111,7 +1111,8 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
String codec,
byte[] payload,
int decodedSize,
Map<String, Object> metadata,
Map<String, Object> runtimeMetadata,
Map<String, Object> toolingMetadata,
boolean preloadEnabled) {
}
@@ -121,7 +121,7 @@ public final class PackerAssetDeclarationParser {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Field 'type' must be one of: tile_bank, palette_bank, sound_bank.",
"Field 'type' must be one of: glyph_bank, palette_bank, sound_bank.",
manifestPath,
true));
return null;
@@ -29,7 +29,7 @@ final class PackerOutputContractCatalog {
codecs.addAll(outputFormat.availableCodecs());
} else {
final String normalizedFormat = outputFormat.manifestValue().trim().toUpperCase(Locale.ROOT);
if (normalizedFormat.startsWith("TILES/")
if (normalizedFormat.startsWith("GLYPH/")
|| normalizedFormat.startsWith("PALETTE/")
|| normalizedFormat.startsWith("SOUND/")
|| normalizedFormat.startsWith("AUDIO/")) {
@@ -54,7 +54,7 @@ final class PackerOutputContractCatalog {
private static List<PackerCodecConfigurationField> metadataFieldsFor(OutputFormatCatalog outputFormat) {
return switch (outputFormat) {
case TILES_INDEXED_V1 -> List.of(new PackerCodecConfigurationField(
case GLYPH_INDEXED_V1 -> List.of(new PackerCodecConfigurationField(
"tile_size",
"TileSize",
PackerCodecConfigurationFieldType.ENUM,