Skip to content

Commit

Permalink
upd: .mcaddon structure (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjgardner authored Jul 19, 2024
1 parent e0b912e commit 4886fa6
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 185 deletions.
31 changes: 29 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
"program": "${workspaceFolder}/example/rgb.ts",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "C:\\Users\\jason\\.deno\\bin\\deno.EXE",
"runtimeArgs": ["run", "--unstable", "--inspect-wait", "--allow-all"],
"runtimeArgs": [
"run",
"--unstable",
"--inspect-wait",
"--allow-all"
],
"attachSimplePort": 9229
},
{
Expand All @@ -35,6 +40,28 @@
"./tests/test.mcstructure"
],
"attachSimplePort": 9229
},
{
"request": "launch",
"name": "Debug mcaddon mosaic",
"type": "node",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "C:\\Users\\jason\\.deno\\bin\\deno.EXE",
"runtimeArgs": [
"run",
"-A",
"--inspect-wait",
"./src/mcaddon/cli.ts",
"--resolution",
"256",
"--src",
"./test/connected/lobby_ceiling.png",
"--dest",
"./build/test.mcaddon",
"--axis",
"y"
],
"attachSimplePort": 9229
}
]
}
}
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"deno.enable": false,
"deno.lint": true,
"deno.unstable": true,
"cSpell.words": ["imagescript", "mcstructure"]
}
"bun.debugTerminal.enabled": true,
"bun.debugTerminal.stopOnEntry": false,
"cSpell.words": [
"imagescript",
"mcstructure"
]
}
72 changes: 72 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/_decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GIF, Image, type Frame } from "imagescript";
import { readFile } from "node:fs/promises";
import { MAX_HEIGHT, MAX_WIDTH } from "./_constants.ts";

type DecodedFrames =
export type DecodedFrames =
| GIF
| Array<Image | Frame>;

Expand Down Expand Up @@ -60,10 +60,12 @@ async function decodeBase64(
* Decode an image from a URL, file path, or base64 string.\
* Returns an array of resized frames.
* @param path Image URL, file path, or base64 string
* @param clamp Whether to resize frames above the max width/height
* @returns Array of decoded frames
*/
export default async function decode(
path: string,
clamp = false,
): Promise<DecodedFrames> {
let img = null;

Expand All @@ -79,6 +81,10 @@ export default async function decode(
img = await decodeImageFile(path, await readFile(path));
}

if (!clamp) {
return img;
}

// Resize every frame above the max width/height
const frames = img?.map((i: Image | Frame) =>
i.height > MAX_HEIGHT
Expand All @@ -88,5 +94,5 @@ export default async function decode(
: i
) ?? [];

return frames as DecodedFrames;
return frames satisfies DecodedFrames;
}
12 changes: 12 additions & 0 deletions src/_lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ export async function parseDbInput(
export function hex2rgb(hex: string): RGB {
return hex.match(/[^#]{1,2}/g)?.map((x) => Number.parseInt(x, 16)) as RGB;
}

export function rgb2hex(rgb: RGB): string {
return `#${rgb[0].toString(16).padStart(2, "0")}${rgb[1].toString(16).padStart(2, "0")}${rgb[2].toString(16).padStart(2, "0")}`;
}

export function uint8arrayToBase64(arr: Uint8Array): string {
return btoa(new TextDecoder("utf8").decode(arr));
}

export function base642uint8array(str: string): Uint8Array {
return new TextEncoder().encode(atob(str));
}
88 changes: 53 additions & 35 deletions src/mcaddon/cli.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,66 @@
import { basename } from "node:path";
import { writeFile} from "node:fs/promises";
import { writeFile } from "node:fs/promises";
import { parseArgs } from "node:util";
import process from "node:process";
import img2mcaddon from "./mod.ts";
import type { Axis } from "../types.ts";

async function createAddon(
src: string | URL,
gridSize = 16,
resolution = 16,
dest?: string,
src: string | URL,
gridSize = 3,
resolution = 16,
dest?: string,
axis?: Axis
) {
const addon = await img2mcaddon(src, gridSize, resolution);
const addon = await img2mcaddon(src, gridSize, resolution, axis ?? "z");

const addonDest = dest ?? `${basename(src instanceof URL ? src.pathname : src).replace(/\.\w+$/, "")}.mcaddon`;
const addonDest =
dest ??
`${basename(src instanceof URL ? src.pathname : src).replace(/\.\w+$/, "")}.mcaddon`;

await writeFile(addonDest, addon);
await writeFile(addonDest, addon);

console.log(`Created ${addonDest}`);
console.log(`Created ${addonDest}`);
}

if (import.meta.main) {
const { values: { src, gridSize, resolution, dest } } = parseArgs({
args: process.argv.slice(2),
options: {
src: {
type: "string",
multiple: false,
},
gridSize: {
type: "string",
multiple: false,
default: "16",
},
resolution: {
type: "string",
multiple: false,
default: "16",
},
dest: {
type: "string",
multiple: false,
},
},
});
const {
values: { src, gridSize, resolution, dest, axis },
} = parseArgs({
args: process.argv.slice(2),
options: {
src: {
type: "string",
multiple: false,
},
gridSize: {
type: "string",
multiple: false,
default: "3",
},
resolution: {
type: "string",
multiple: false,
default: "16",
},
dest: {
type: "string",
multiple: false,
},
axis: {
type: "string",
multiple: false,
default: "z",
}
},
});

await createAddon(src, Number(gridSize), Number(resolution), dest);
process.exit(0);
}
await createAddon(
src,
Math.max(1, Number(gridSize)),
Math.max(16, Math.min(1024, Number(resolution))),
dest,
axis as Axis
);
process.exit(0);
}
Loading

0 comments on commit 4886fa6

Please sign in to comment.