Skip to content

Commit

Permalink
fix: Remove Buffer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjgardner committed Jul 15, 2024
1 parent 8b55bcb commit 10d5e70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/mcaddon/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ async function sliceImage(
const baseName = basename(src, extname(src))

const namespace = baseName.replace(/\W|\.\@\$\%/g, "_");
const image = await imagescript.Image.decode(
!src.startsWith("data")
? await readFile(src)
: Buffer.from(src.split(",")[1], "base64"),
);

const imgSrc = src.startsWith("data") ? new TextEncoder().encode(src) : (await readFile(src));

const image = await imagescript.Image.decode(imgSrc);
const images = [];

const width = image.width / gridSize;
const height = image.height / gridSize;

const terrainData = {};
const terrainData: Record<string, {
textures: string | string[],
carried_texture?: string,
}> = {};

const blocksData: {
[key: string]:
Expand Down Expand Up @@ -184,7 +186,12 @@ export default async function img2mcaddon(
// Download source
const response = await fetch(src);
const buffer = await response.arrayBuffer();
img = `data:${response.headers.get("Content-Type") ?? "image/png"};base64,${Buffer.from(buffer).toString("base64")}`;

const data = new Uint8Array(buffer);

img = `data:${response.headers.get("Content-Type") ?? "image/png"};base64,${btoa(
new TextDecoder().decode(data),
)}`;
}

const addon = await sliceImage(img.toString(), gridSize, resolution);
Expand Down
2 changes: 1 addition & 1 deletion src/mcfunction/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function img2mcfunction(
imgSrc: string,
blocks: PaletteSource,
offset: [number, number, number] = [0, 0, 0],
) {
): Promise<string> {
const frames = await decode(imgSrc);

const len = Math.min(256, frames.length);
Expand Down

0 comments on commit 10d5e70

Please sign in to comment.