Skip to content

Commit

Permalink
Add sprite sheet stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeLonewolf committed Dec 15, 2023
1 parent 8453a8d commit c389b37
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
36 changes: 36 additions & 0 deletions scripts/stats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Style from "../src/js/style.js";
import config from "../src/config.js";
import { Command, Option } from "commander";
import fs from "node:fs";

const program = new Command();
program
Expand All @@ -18,6 +19,18 @@ program
.addOption(
new Option("-s, --layer-size", "size of all layers").conflicts("allJson")
)
.addOption(
new Option(
"-ss1, --spritesheet-1x-size",
"size of 1x sprite sheet"
).conflicts("allJson")
)
.addOption(
new Option(
"-ss2, --spritesheet-2x-size",
"size of 2x sprite sheet"
).conflicts("allJson")
)
.option("-loc, --locales <locale1 locale2...>", "language codes", ["mul"])
.option("-j, --all-json", "output all stats in JSON")
.option("-pp, --pretty", "pretty-print JSON output");
Expand Down Expand Up @@ -45,6 +58,27 @@ if (opts.layerCount) {
process.exit();
}

function spriteSheetSize(single) {
let size = single ? "" : "@2x";
return (
fs.statSync(`dist/sprites/sprite${size}.png`).size +
fs.statSync(`dist/sprites/sprite${size}.json`).size
);
}

const spriteSheet1xSize = spriteSheetSize(true);
const spriteSheet2xSize = spriteSheetSize(false);

if (opts.spritesheet1xSize) {
console.log(spriteSheet1xSize);
process.exit();
}

if (opts.spritesheet2xSize) {
console.log(spriteSheet2xSize);
process.exit();
}

const styleSize = JSON.stringify(layers).length;

if (opts.layerSize) {
Expand All @@ -58,6 +92,8 @@ const stats = {
layerCount,
styleSize,
layerGroup: {},
spriteSheet1xSize,
spriteSheet2xSize,
};

for (let i = 0; i < layerCount; i++) {
Expand Down
16 changes: 15 additions & 1 deletion scripts/stats_compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@ const layersRow = mdCompareRow(
);

const sizeRow = mdCompareRow(
"Size (b)",
"StyleJSON Size (b)",
stats1.styleSize,
stats2.styleSize,
difference.styleSize
);

const ss1xRow = mdCompareRow(
"1x Sprite Sheet Size (b)",
stats1.spriteSheet1xSize,
stats2.spriteSheet1xSize,
difference.spriteSheet1xSize
);

const ss2xRow = mdCompareRow(
"2x Sprite Sheet Size (b)",
stats1.spriteSheet2xSize,
stats2.spriteSheet2xSize,
difference.spriteSheet2xSize
);

printTable("Style size statistics", [layersRow, sizeRow]);

/**
Expand Down

0 comments on commit c389b37

Please sign in to comment.