Compare commits
No commits in common. "883ef7357c29fdc7bddd85439edacbeaebca3251" and "d97004a6ddbd7613a99736d45c53ed5547aabd1d" have entirely different histories.
883ef7357c
...
d97004a6dd
7 changed files with 51 additions and 93 deletions
|
@ -1,6 +1,5 @@
|
|||
{
|
||||
"name": "@msagent.js/core",
|
||||
"description": "msagent.js core. Provides structures, parsing, and bitmap utilties for higher level usage",
|
||||
"version": "0.1.0",
|
||||
"packageManager": "yarn@4.2.2",
|
||||
"devDependencies": {
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
import { WasmModule, WebassemblyOwnMemoryExports } from './wasm_module';
|
||||
// Please note that the "meaningless" shifts of 0 are to force
|
||||
// the value to be a 32-bit integer. Do not remove them.
|
||||
|
||||
// WASM exports for the decompression module.
|
||||
interface CompressWasmExports extends WebassemblyOwnMemoryExports {
|
||||
agentDecompressWASM: (pSource: number, sourceLen: number, pDest: number, destLen: number) => number;
|
||||
let compressWasm: WebAssembly.WebAssemblyInstantiatedSource;
|
||||
|
||||
interface CompressWasmExports {
|
||||
memory: WebAssembly.Memory;
|
||||
agentDecompressWASM: any;
|
||||
}
|
||||
|
||||
let compressWasm = new WasmModule<CompressWasmExports>(new URL('decompress.wasm', import.meta.url));
|
||||
|
||||
// Initalize the decompression module
|
||||
export async function compressInit() {
|
||||
await compressWasm.Initalize();
|
||||
let url = new URL('decompress.wasm', import.meta.url);
|
||||
compressWasm = await WebAssembly.instantiateStreaming(fetch(url));
|
||||
}
|
||||
|
||||
function compressWasmGetExports() {
|
||||
return compressWasm.Exports;
|
||||
return compressWasm.instance.exports as any as CompressWasmExports;
|
||||
}
|
||||
|
||||
function compressWASMGetMemory(): WebAssembly.Memory {
|
||||
|
@ -30,9 +32,14 @@ function compressWASMGetMemory(): WebAssembly.Memory {
|
|||
export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
||||
// Grow the WASM heap if needed. Funnily enough, this code is never hit in most
|
||||
// ACSes, so IDK if it's even needed
|
||||
compressWasm.growHeapTo(src.length + dest.length);
|
||||
|
||||
let memory = compressWASMGetMemory();
|
||||
if (memory.buffer.byteLength < src.length + dest.length) {
|
||||
// A WebAssembly page is 64kb, so we need to grow at least that much
|
||||
let npages = Math.floor((src.length + dest.length) / 65535) + 1;
|
||||
console.log('Need to grow WASM heap', npages, 'pages', '(current byteLength is', memory.buffer.byteLength, ', we need', src.length + dest.length, ')');
|
||||
memory.grow(npages);
|
||||
}
|
||||
|
||||
let copyBuffer = new Uint8Array(memory.buffer);
|
||||
|
||||
// Copy source to memory[0]. This will make things a bit simpler
|
||||
|
@ -43,7 +50,6 @@ export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
|||
|
||||
if (nrBytesDecompressed != dest.length) throw new Error(`decompression failed: ${nrBytesDecompressed} != ${dest.length}`);
|
||||
|
||||
// The uncompressed data is located at memory[src.length..dest.length].
|
||||
// Copy it into the destination buffer.
|
||||
// Dest will be memory[src.length..dest.length]
|
||||
dest.set(copyBuffer.slice(src.length, src.length + dest.length), 0);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { AcsImageEntry } from './structs/image';
|
||||
import { RGBAColor } from './structs/core';
|
||||
|
||||
// probably should be in a utility module
|
||||
function dwAlign(off: number): number {
|
||||
|
@ -11,6 +10,9 @@ function dwAlign(off: number): number {
|
|||
return ul;
|
||||
}
|
||||
|
||||
import { BufferStream, SeekDir } from './buffer';
|
||||
import { RGBAColor } from './structs/core';
|
||||
|
||||
/// Draws an ACS image to a newly allocated buffer.
|
||||
/// This function normalizes the agent 8bpp DIB format to a saner RGBA format,
|
||||
/// that can be directly converted to an ImageData for drawing to a web canvas.
|
||||
|
@ -18,17 +20,29 @@ function dwAlign(off: number): number {
|
|||
/// However, that should be done (and is done) by a higher level web layer.
|
||||
export function imageDrawToBuffer(imageEntry: AcsImageEntry, palette: RGBAColor[]) {
|
||||
let rgbaBuffer = new Uint32Array(imageEntry.image.width * imageEntry.image.height);
|
||||
|
||||
let buffer = imageEntry.image.data;
|
||||
let bufStream = new BufferStream(buffer);
|
||||
|
||||
// Next, draw the rows converted to RGBA, top down (so it's drawn correctly,
|
||||
// and in the RGBA format we want to return)
|
||||
let rows = new Array<Uint8Array>(imageEntry.image.height - 1);
|
||||
|
||||
// Read all the rows bottom-up first. This idiosyncracy is due to the fact
|
||||
// that the bitmap data is actually formatted to be used as a GDI DIB
|
||||
// (device-independent bitmap), so it inherits all the strange baggage from that.
|
||||
for (let y = imageEntry.image.height - 1; y >= 0; --y) {
|
||||
let row = bufStream.subBuffer(imageEntry.image.width).raw();
|
||||
rows[y] = row.slice(0, imageEntry.image.width);
|
||||
|
||||
// Seek to the next DWORD aligned spot to get to the next row.
|
||||
// For most images this may mean not seeking at all.
|
||||
bufStream.seek(dwAlign(bufStream.tell()), SeekDir.BEG);
|
||||
}
|
||||
|
||||
// Next, draw the rows converted to RGBA, top down (so it's drawn as you'd expect)
|
||||
for (let y = 0; y < imageEntry.image.height - 1; ++y) {
|
||||
// flip y so it's all top down properly
|
||||
let yy = imageEntry.image.height - 1 - y;
|
||||
let rowStartOffset = yy * dwAlign(imageEntry.image.width);
|
||||
|
||||
let row = rows[y];
|
||||
for (let x = 0; x < imageEntry.image.width; ++x) {
|
||||
rgbaBuffer[y * imageEntry.image.width + x] = palette[buffer[rowStartOffset + x]].to_rgba();
|
||||
rgbaBuffer[y * imageEntry.image.width + x] = palette[row[x]].to_rgba();
|
||||
}
|
||||
}
|
||||
return rgbaBuffer;
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
export interface WebassemblyOwnMemoryExports extends WebAssembly.Exports {
|
||||
memory: WebAssembly.Memory;
|
||||
}
|
||||
|
||||
// A helper for WASM modules.
|
||||
// This currently only works for modules that export their own memory,
|
||||
// and do not import it.
|
||||
//
|
||||
// This covers all of the modules in the core so far, so
|
||||
// this is perfectly fine, but it's still worth noting.
|
||||
export class WasmModule<TExports extends WebassemblyOwnMemoryExports> {
|
||||
private module: WebAssembly.WebAssemblyInstantiatedSource | null = null;
|
||||
private url: URL;
|
||||
|
||||
constructor(url: URL) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
async Initalize(): Promise<void> {
|
||||
this.module = await WebAssembly.instantiateStreaming(fetch(this.url));
|
||||
}
|
||||
|
||||
get Module() {
|
||||
if (this.module == null) throw new Error('WasmModule<T> has not been initalized');
|
||||
return this.module;
|
||||
}
|
||||
|
||||
get Exports() {
|
||||
let module = this.Module;
|
||||
return module.instance.exports as TExports;
|
||||
}
|
||||
|
||||
// Grows the WebAssembly heap if required.
|
||||
growHeapTo(newSize: number) {
|
||||
let exports = this.Exports;
|
||||
let memory: WebAssembly.Memory = exports.memory;
|
||||
if (memory.buffer.byteLength < newSize) {
|
||||
// A WebAssembly page is 64kb, so we need to grow at least a single page,
|
||||
// even if it would be relatively wasteful to do so.
|
||||
let npages = Math.floor(newSize / 65535) + 1;
|
||||
memory.grow(npages);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,11 +5,6 @@ import { Agent } from './agent.js';
|
|||
let acsDataCache = new Map<string, AcsData>();
|
||||
|
||||
// Purges the ACS cache.
|
||||
//
|
||||
// FIXME: A smarter way to do this would probably be instead reference count
|
||||
// AcsData instances, then when an agent is disposed decrement reference count
|
||||
// (or leave it at 0). Once it's 0 then a globally running interval can remove
|
||||
// all keys that have no refcount.
|
||||
export function agentPurgeACSCache() {
|
||||
acsDataCache.clear();
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@ input.addEventListener('change', async () => {
|
|||
agent.addToDom(mount);
|
||||
|
||||
agent.show();
|
||||
await agent.playAnimationByNamePromise("Show");
|
||||
console.log('Agent created');
|
||||
});
|
||||
|
||||
|
@ -32,15 +31,13 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||
});
|
||||
|
||||
let form = document.getElementById('acsUrlForm') as HTMLFormElement;
|
||||
form.addEventListener('submit', async (e) => {
|
||||
form.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
let url = (document.getElementById('acsUrl') as HTMLInputElement).value;
|
||||
let agent = await msagent.agentCreateCharacterFromUrl(url)
|
||||
w.agents.push(agent);
|
||||
agent.addToDom(document.body);
|
||||
|
||||
agent.show();
|
||||
await agent.playAnimationByNamePromise("Show");
|
||||
|
||||
console.log(`Loaded agent from ${url}`);
|
||||
msagent.agentCreateCharacterFromUrl(url).then((agent) => {
|
||||
w.agents.push(agent);
|
||||
agent.addToDom(document.body);
|
||||
agent.show();
|
||||
console.log(`Loaded agent from ${url}`);
|
||||
});
|
||||
});
|
||||
|
|
17
yarn.lock
17
yarn.lock
|
@ -3619,11 +3619,11 @@ __metadata:
|
|||
linkType: hard
|
||||
|
||||
"node-abi@npm:^3.3.0":
|
||||
version: 3.67.0
|
||||
resolution: "node-abi@npm:3.67.0"
|
||||
version: 3.65.0
|
||||
resolution: "node-abi@npm:3.65.0"
|
||||
dependencies:
|
||||
semver: "npm:^7.3.5"
|
||||
checksum: 10c0/72ce2edbdfb84745bc201a4e48aa7146fd88a0d2c80046b6b17f28439c9a7683eab846f40f1e819349c31f7d9331ed5c50d1e741208d938dd5f38b29cab2275e
|
||||
checksum: 10c0/112672015d8f27d6be2f18d64569f28f5d6a15a94cc510da513c69c3e3ab5df6dac196ef13ff115a8fadb69b554974c47ef89b4f6350a2b02de2bca5c23db1e5
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
@ -4246,7 +4246,7 @@ __metadata:
|
|||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"semver@npm:^7.3.5, semver@npm:^7.5.2, semver@npm:^7.5.4, semver@npm:^7.6.0":
|
||||
"semver@npm:^7.3.5, semver@npm:^7.3.8, semver@npm:^7.5.2, semver@npm:^7.5.4, semver@npm:^7.6.0":
|
||||
version: 7.6.2
|
||||
resolution: "semver@npm:7.6.2"
|
||||
bin:
|
||||
|
@ -4255,15 +4255,6 @@ __metadata:
|
|||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"semver@npm:^7.3.8":
|
||||
version: 7.6.3
|
||||
resolution: "semver@npm:7.6.3"
|
||||
bin:
|
||||
semver: bin/semver.js
|
||||
checksum: 10c0/88f33e148b210c153873cb08cfe1e281d518aaa9a666d4d148add6560db5cd3c582f3a08ccb91f38d5f379ead256da9931234ed122057f40bb5766e65e58adaf
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"seq-queue@npm:^0.0.5":
|
||||
version: 0.0.5
|
||||
resolution: "seq-queue@npm:0.0.5"
|
||||
|
|
Loading…
Reference in a new issue