From 2b60bacad6454ca40172dd0d0a7d381b15552282 Mon Sep 17 00:00:00 2001
From: Jeremy Penner Cels
+
Data
+
From: aric/mic/Gr/Heads
diff --git a/index.js b/index.js index ccc9b1a..3f8cf31 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,9 @@ const makeCanvas = (w, h) => { } const canvasFromBitmap = (bitmap) => { + if (bitmap.length == 0 || bitmap[0].length == 0) { + return null + } const h = bitmap.length const w = bitmap[0].length * 2 const canvas = makeCanvas(w, h) @@ -473,6 +476,44 @@ const decodeProp = (data) => { return prop } +const decodeLimb = (data, limb) => { + limb.frames = [] + for (let iframe = 0; iframe < data.getUint8(0); iframe ++) { + limb.frames.push(data.getUint8(3 + iframe)) + } + const celOffsetsOff = 4 + limb.frames.length + // I don't understand this at all, but it seems to be correct? + const maxCelIndex = Math.max(...limb.frames) + 1 + limb.cels = [] + for (let icel = 0; icel <= maxCelIndex; icel ++) { + const celOff = data.getUint16(celOffsetsOff + (icel * 2), LE) + limb.cels.push(decodeCel(new DataView(data.buffer, data.byteOffset + celOff))) + } +} + +const decodeBody = (data) => { + const body = { + data: data, + headCelNumber: data.getUint8(19), + frozenWhenStands: data.getUint8(20), + frontFacingLimbOrder: [], + backFacingLimbOrder: [], + limbs: [] + } + for (let ilimb = 0; ilimb < 6; ilimb ++) { + body.frontFacingLimbOrder.push(data.getUint8(27 + ilimb)) + body.backFacingLimbOrder.push(data.getUint8(33 + ilimb)) + const limb = { + pattern: data.getUint8(21 + ilimb), + affectedByHeight: data.getUint8(39 + ilimb) + } + const limbOff = data.getUint16(7 + (ilimb * 2), LE) + decodeLimb(new DataView(data.buffer, limbOff), limb) + body.limbs.push(limb) + } + return body +} + const celsFromMask = (prop, celMask) => { const cels = [] for (let icel = 0; icel < 8; icel ++) { @@ -514,7 +555,7 @@ const compositeCels = (cels) => { if (cel.canvas) { ctx.drawImage(cel.canvas, (cel.xOffset + xRel - minX) * 8, -(cel.yOffset + yRel) - minY) } - xRel += cel.xRel + xRel += cel.xRel yRel += cel.yRel } return { canvas: canvas, xOffset: minX * 8, yOffset: minY, w: w, h: h } @@ -535,11 +576,19 @@ const textNode = (text, type = "span") => { return node } +const wrapLink = (element, href) => { + const link = document.createElement("a") + link.href = href + link.appendChild(element) + return link +} + const linkDetail = (element, filename) => { - const detailLink = document.createElement("a") - detailLink.href = `detail.html?f=${filename}` - detailLink.appendChild(element) - return detailLink + return wrapLink(element, `detail.html?f=${filename}`) +} + +const linkBody = (element, filename) => { + return wrapLink(element, `body.html?f=${filename}`) } const createAnimation = (prop, animation) => { @@ -608,9 +657,9 @@ const showCels = (prop, container) => { } } -const decodeBinary = async (filename) => { +const decodeBinary = async (filename, decoder) => { try { - const prop = decodeProp(await readBinary(filename)) + const prop = decoder(await readBinary(filename)) prop.filename = filename return prop } catch (e) { @@ -618,11 +667,11 @@ const decodeBinary = async (filename) => { } } -const showError = (e, filename) => { +const showError = (e, filename, link = (x,_) => x) => { const container = document.getElementById("errors") const errNode = document.createElement("p") console.error(e) - errNode.appendChild(linkDetail(textNode(filename, "b"), filename)) + errNode.appendChild(link(textNode(filename, "b"), filename)) errNode.appendChild(textNode(e.toString(), "p")) if (e.stack) { errNode.appendChild(textNode(e.stack.toString(), "pre")) @@ -630,26 +679,22 @@ const showError = (e, filename) => { container.appendChild(errNode) } -const displayFile = async (filename, container) => { - const prop = await decodeBinary(filename) +const displayFile = async (filename, container, decode, display, link = (x,_) => x) => { + const prop = await decodeBinary(filename, decode) if (prop.error) { container.parentNode.removeChild(container) - showError(prop.error, prop.filename) + showError(prop.error, prop.filename, link) } else { try { - if (prop.animations.length > 0) { - showAnimations(prop, container) - } else { - showStates(prop, container) - } + display(prop, container) } catch (e) { container.parentNode.removeChild(container) - showError(e, prop.filename) + showError(e, prop.filename, link) } } } -const displayList = async (indexFile, containerId) => { +const displayList = async (indexFile, containerId, decode, display, link = (x,_) => x) => { const response = await fetch(indexFile, { cache: "no-cache" }) const filenames = await response.json() const container = document.getElementById(containerId) @@ -659,12 +704,32 @@ const displayList = async (indexFile, containerId) => { fileContainer.style.margin = "2px" fileContainer.style.padding = "2px" fileContainer.style.display = "inline-block" - fileContainer.appendChild(linkDetail(textNode(filename, "div"), filename)) + fileContainer.appendChild(link(textNode(filename, "div"), filename)) container.appendChild(fileContainer) - if (filename != 'heads/fhead.bin') { - displayFile(filename, fileContainer) - } else { - fileContainer.appendChild(textNode("CW: Pixel genitals")) - } + displayFile(filename, fileContainer, decode, display, link) } } + +const displayProp = (prop, container) => { + if (prop.filename == 'heads/fhead.bin') { + container.appendChild(textNode("CW: Pixel genitals")) + } else if (prop.animations.length > 0) { + showAnimations(prop, container) + } else { + showStates(prop, container) + } +} + +const displayBody = (body, container) => { + for (const limb of body.limbs) { + showCels(limb, container) + } +} + +const displayPropList = async (indexFile, containerId) => { + await displayList(indexFile, containerId, decodeProp, displayProp, linkDetail) +} + +const displayBodyList = async (indexFile, containerId) => { + await displayList(indexFile, containerId, decodeBody, displayBody, linkBody) +} \ No newline at end of file diff --git a/misc.json b/misc.json index c4fd1e3..c001fab 100644 --- a/misc.json +++ b/misc.json @@ -1 +1 @@ -["misc/Avatar.bin","misc/Drag.bin","misc/Gunship.bin","misc/Peng_uppercase.bin","misc/Spid.bin","misc/Tank.bin","misc/Tentacle.bin","misc/angelwing.bin","misc/kenhead201.bin","misc/kenhead202.bin","misc/kenhead203.bin","misc/kenhead205.bin","misc/kenhead206.bin","misc/kenhead207.bin","misc/kenhead208.bin","misc/kenhead209.bin","misc/kenhead210.bin","misc/kenhead211.bin","misc/kenhead212.bin","misc/kenhead213.bin","misc/kenhead214.bin","misc/kenhead215.bin","misc/kenhead216.bin","misc/kenhead217.bin","misc/kenhead218.bin","misc/kenhead219.bin","misc/kenhead220.bin","misc/kenhead221.bin","misc/kenhead222.bin","misc/kenhead223.bin","misc/kenhead224.bin","misc/kenhead225.bin","misc/kenhead226.bin","misc/kenhead227.bin","misc/kenhead228.bin","misc/kenhead229.bin","misc/kenhead230.bin","misc/kenhead231.bin","misc/kenhead232.bin","misc/kenhead233.bin","misc/kenhead234.bin","misc/kenhead235.bin","misc/kenhead236.bin","misc/kenhead237.bin","misc/kenhead238.bin","misc/kenhead239.bin","misc/mouse0.bin","misc/newhab1.bin","misc/newhab10.bin","misc/newhab11.bin","misc/newhab13.bin","misc/newhab14.bin","misc/newhab15.bin","misc/newhab16.bin","misc/newhab17.bin","misc/newhab18.bin","misc/newhab19.bin","misc/newhab20.bin","misc/newhab21.bin","misc/newhab22.bin","misc/newhab23.bin","misc/newhab24.bin","misc/newhab3.bin","misc/newhab5.bin","misc/newhab6.bin","misc/newhab7.bin","misc/newhab8.bin","misc/newhab9.bin","misc/nillhead.bin"] \ No newline at end of file +["misc/angelwing.bin","misc/kenhead201.bin","misc/kenhead202.bin","misc/kenhead203.bin","misc/kenhead205.bin","misc/kenhead206.bin","misc/kenhead207.bin","misc/kenhead208.bin","misc/kenhead209.bin","misc/kenhead210.bin","misc/kenhead211.bin","misc/kenhead212.bin","misc/kenhead213.bin","misc/kenhead214.bin","misc/kenhead215.bin","misc/kenhead216.bin","misc/kenhead217.bin","misc/kenhead218.bin","misc/kenhead219.bin","misc/kenhead220.bin","misc/kenhead221.bin","misc/kenhead222.bin","misc/kenhead223.bin","misc/kenhead224.bin","misc/kenhead225.bin","misc/kenhead226.bin","misc/kenhead227.bin","misc/kenhead228.bin","misc/kenhead229.bin","misc/kenhead230.bin","misc/kenhead231.bin","misc/kenhead232.bin","misc/kenhead233.bin","misc/kenhead234.bin","misc/kenhead235.bin","misc/kenhead236.bin","misc/kenhead237.bin","misc/kenhead238.bin","misc/kenhead239.bin","misc/mouse0.bin","misc/newhab1.bin","misc/newhab10.bin","misc/newhab11.bin","misc/newhab13.bin","misc/newhab14.bin","misc/newhab15.bin","misc/newhab16.bin","misc/newhab17.bin","misc/newhab18.bin","misc/newhab19.bin","misc/newhab20.bin","misc/newhab21.bin","misc/newhab22.bin","misc/newhab23.bin","misc/newhab24.bin","misc/newhab3.bin","misc/newhab5.bin","misc/newhab6.bin","misc/newhab7.bin","misc/newhab8.bin","misc/newhab9.bin","misc/nillhead.bin"] \ No newline at end of file