Compare commits
No commits in common. "d9faea4c1c90b56f0ea406abaaf529444e095b2d" and "74743a1824ef97ea9af11ee802b9a7b376ace720" have entirely different histories.
d9faea4c1c
...
74743a1824
7 changed files with 61 additions and 128 deletions
1
msagent.js/.gitignore
vendored
1
msagent.js/.gitignore
vendored
|
@ -1 +0,0 @@
|
|||
core/obj
|
|
@ -2,25 +2,17 @@
|
|||
// Integer types from WASI-libc
|
||||
using usize = unsigned int;
|
||||
using u32 = unsigned int;
|
||||
using u16 = unsigned short;
|
||||
using u8 = unsigned char;
|
||||
|
||||
template<class T>
|
||||
constexpr auto LowOrderShortOf(T item) {
|
||||
return static_cast<u16>(item & 0xffff);
|
||||
}
|
||||
#define LOWORD(x) (x & 0xffff)
|
||||
#define LOBYTE(x) (x & 0xff)
|
||||
|
||||
template<class T>
|
||||
constexpr auto LowOrderByteOf(T item) {
|
||||
return static_cast<u8>(item & 0xff);
|
||||
}
|
||||
#define PUBLIC __attribute__((visibility("default"))) extern "C"
|
||||
|
||||
#define PUBLIC extern "C" __attribute__((visibility("default")))
|
||||
|
||||
PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgData, usize pTrgSize) {
|
||||
const u8* lSrcPtr = pSrcData;
|
||||
PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTrgData, usize pTrgSize) {
|
||||
const u8* lSrcPtr = (const u8*)pSrcData;
|
||||
const u8* lSrcEnd = lSrcPtr + pSrcSize;
|
||||
u8* lTrgPtr = pTrgData;
|
||||
u8* lTrgPtr = (u8*)pTrgData;
|
||||
u8* lTrgEnd = lTrgPtr + pTrgSize;
|
||||
u32 lSrcQuad;
|
||||
u8 lTrgByte;
|
||||
|
@ -46,15 +38,15 @@ PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgDat
|
|||
lSrcPtr += 5;
|
||||
|
||||
while((lSrcPtr < lSrcEnd) && (lTrgPtr < lTrgEnd)) {
|
||||
lSrcQuad = *reinterpret_cast<const u32*>(lSrcPtr - sizeof(u32));
|
||||
lSrcQuad = *(const u32*)(lSrcPtr - sizeof(u32));
|
||||
|
||||
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount))) {
|
||||
if(lSrcQuad & (1 << LOWORD(lBitCount))) {
|
||||
lSrcOffset = 1;
|
||||
|
||||
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount + 1))) {
|
||||
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount + 2))) {
|
||||
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount + 3))) {
|
||||
lSrcQuad >>= LowOrderShortOf(lBitCount + 4);
|
||||
if(lSrcQuad & (1 << LOWORD(lBitCount + 1))) {
|
||||
if(lSrcQuad & (1 << LOWORD(lBitCount + 2))) {
|
||||
if(lSrcQuad & (1 << LOWORD(lBitCount + 3))) {
|
||||
lSrcQuad >>= LOWORD(lBitCount + 4);
|
||||
lSrcQuad &= 0x000FFFFF;
|
||||
if(lSrcQuad == 0x000FFFFF) {
|
||||
break;
|
||||
|
@ -64,19 +56,19 @@ PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgDat
|
|||
|
||||
lSrcOffset = 2;
|
||||
} else {
|
||||
lSrcQuad >>= LowOrderShortOf(lBitCount + 4);
|
||||
lSrcQuad >>= LOWORD(lBitCount + 4);
|
||||
lSrcQuad &= 0x00000FFF;
|
||||
lSrcQuad += 577;
|
||||
lBitCount += 16;
|
||||
}
|
||||
} else {
|
||||
lSrcQuad >>= LowOrderShortOf(lBitCount + 3);
|
||||
lSrcQuad >>= LOWORD(lBitCount + 3);
|
||||
lSrcQuad &= 0x000001FF;
|
||||
lSrcQuad += 65;
|
||||
lBitCount += 12;
|
||||
}
|
||||
} else {
|
||||
lSrcQuad >>= LowOrderShortOf(lBitCount + 2);
|
||||
lSrcQuad >>= LOWORD(lBitCount + 2);
|
||||
lSrcQuad &= 0x0000003F;
|
||||
lSrcQuad += 1;
|
||||
lBitCount += 8;
|
||||
|
@ -84,16 +76,16 @@ PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgDat
|
|||
|
||||
lSrcPtr += (lBitCount / 8);
|
||||
lBitCount &= 7;
|
||||
lRunLgth = *reinterpret_cast<const u32*>(lSrcPtr - sizeof(u32));
|
||||
lRunLgth = *(const u32*)(lSrcPtr - sizeof(u32));
|
||||
lRunCount = 0;
|
||||
while(lRunLgth & (1 << LowOrderShortOf(lBitCount + lRunCount))) {
|
||||
while(lRunLgth & (1 << LOWORD(lBitCount + lRunCount))) {
|
||||
lRunCount++;
|
||||
if(lRunCount > 11) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
lRunLgth >>= LowOrderShortOf(lBitCount + lRunCount + 1);
|
||||
lRunLgth >>= LOWORD(lBitCount + lRunCount + 1);
|
||||
lRunLgth &= (1 << lRunCount) - 1;
|
||||
lRunLgth += 1 << lRunCount;
|
||||
lRunLgth += lSrcOffset;
|
||||
|
@ -111,10 +103,10 @@ PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgDat
|
|||
lRunLgth--;
|
||||
}
|
||||
} else {
|
||||
lSrcQuad >>= LowOrderShortOf(lBitCount + 1);
|
||||
lSrcQuad >>= LOWORD(lBitCount + 1);
|
||||
lBitCount += 9;
|
||||
|
||||
lTrgByte = LowOrderByteOf(lSrcQuad);
|
||||
lTrgByte = LOBYTE(lSrcQuad);
|
||||
*(lTrgPtr++) = lTrgByte;
|
||||
}
|
||||
|
||||
|
@ -122,5 +114,5 @@ PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgDat
|
|||
lBitCount &= 7;
|
||||
}
|
||||
|
||||
return static_cast<usize>(lTrgPtr - pTrgData);
|
||||
return (usize)(lTrgPtr - (u8*)pTrgData);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,8 @@ function compressWASMGetMemory(): WebAssembly.Memory {
|
|||
// Decompress Agent compressed data. This compression algorithm sucks.
|
||||
// [dest] is to be preallocated to the decompressed data size.
|
||||
export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
||||
// Grow the WASM heap if needed.
|
||||
// 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();
|
||||
|
@ -40,7 +41,7 @@ export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
|||
// Call the WASM compression routine
|
||||
let nrBytesDecompressed = compressWasmGetExports().agentDecompressWASM(0, src.length, src.length, dest.length);
|
||||
|
||||
if (nrBytesDecompressed != dest.length) throw new Error(`Decompression failed: Output ${nrBytesDecompressed} != expected ${dest.length}`);
|
||||
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.
|
||||
|
|
|
@ -10,85 +10,44 @@ function randint(min: number, max: number) {
|
|||
return Math.floor(Math.random() * (max - min) + min);
|
||||
}
|
||||
|
||||
enum AnimState {
|
||||
Idle = 0,
|
||||
Cancel = 1,
|
||||
Playing = 2
|
||||
}
|
||||
|
||||
// an externally resolveable promise
|
||||
class ExternallyResolveablePromise<T> {
|
||||
private rescb: (t: T) => void;
|
||||
private rejectcb: (e: Error) => void;
|
||||
public promise: Promise<T>;
|
||||
|
||||
constructor() {
|
||||
let rescb;
|
||||
let rejectcb;
|
||||
|
||||
this.promise = new Promise<T>((res, rej) => {
|
||||
rescb = res;
|
||||
rejectcb = rej;
|
||||
});
|
||||
|
||||
this.rescb = rescb!;
|
||||
this.rejectcb = rejectcb!;
|
||||
}
|
||||
|
||||
resolve(t: T) {
|
||||
this.rescb(t);
|
||||
}
|
||||
|
||||
reject(e: Error) {
|
||||
this.rejectcb(e);
|
||||
}
|
||||
|
||||
raw() {
|
||||
return this.promise;
|
||||
}
|
||||
}
|
||||
|
||||
// animation state (used during animation playback)
|
||||
class AgentAnimationState {
|
||||
char: Agent;
|
||||
anim: AcsAnimation;
|
||||
private cancelled: boolean = false;
|
||||
|
||||
private animState: AnimState;
|
||||
private finishPromise = new ExternallyResolveablePromise<void>();
|
||||
|
||||
finishCallback: () => void;
|
||||
frameIndex = 0;
|
||||
|
||||
interval = 0;
|
||||
|
||||
constructor(char: Agent, anim: AcsAnimation) {
|
||||
constructor(char: Agent, anim: AcsAnimation, finishCallback: () => void) {
|
||||
this.char = char;
|
||||
this.anim = anim;
|
||||
this.animState = AnimState.Idle;
|
||||
this.finishCallback = finishCallback;
|
||||
}
|
||||
|
||||
// start playing the animation
|
||||
async play() {
|
||||
this.animState = AnimState.Playing;
|
||||
play() {
|
||||
this.nextFrame();
|
||||
await this.finishPromise.raw();
|
||||
|
||||
this.animState = AnimState.Idle;
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
|
||||
async cancel() {
|
||||
this.animState = AnimState.Cancel;
|
||||
await this.finishPromise.raw();
|
||||
cancel() {
|
||||
this.cancelled = true;
|
||||
clearTimeout(this.interval);
|
||||
}
|
||||
|
||||
nextFrame() {
|
||||
requestAnimationFrame(() => {
|
||||
if (this.cancelled) return;
|
||||
|
||||
// Handle animation branching, if it is required
|
||||
let bi = this.anim.frameInfo[this.frameIndex].branchInfo;
|
||||
if (bi.length != 0) {
|
||||
let biCopy = [...bi];
|
||||
|
||||
// This happens more often then you'd think, but this basically handles
|
||||
// a branch that will always be taken.
|
||||
// a branch that will always be taken.
|
||||
//
|
||||
// This is often used for looping from my understanding?
|
||||
if (bi.length == 1 && bi[0].branchFrameProbability == 100) {
|
||||
|
@ -102,7 +61,7 @@ class AgentAnimationState {
|
|||
// Handles the off chance that there is a branch info list that sums less than 100%.
|
||||
// (Office Logo 'Idle3', Victor has a couple, ...)
|
||||
//
|
||||
// I'm not entirely sure the correct action in this case but I just
|
||||
// I'm not entirely sure the correct action in this case but I just
|
||||
// have this do nothing.
|
||||
if (totalProbability != 100) {
|
||||
let nothingBranchItem = new AcsBranchInfo();
|
||||
|
@ -126,26 +85,10 @@ class AgentAnimationState {
|
|||
}
|
||||
}
|
||||
|
||||
// Actually draw the requested frame
|
||||
let fi = this.anim.frameInfo[this.frameIndex];
|
||||
|
||||
this.char.drawAnimationFrame(fi);
|
||||
|
||||
// If we've requested to cancel the animation and we're in a branch loop,
|
||||
// then this is how we get out of it.
|
||||
if (this.animState == AnimState.Cancel) {
|
||||
if (fi.branchExitFrameIndex != -2) {
|
||||
this.frameIndex = fi.branchExitFrameIndex;
|
||||
} else {
|
||||
this.finishPromise.resolve();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this.frameIndex += 1;
|
||||
}
|
||||
this.char.drawAnimationFrame(this.anim.frameInfo[this.frameIndex++]);
|
||||
|
||||
if (this.frameIndex >= this.anim.frameInfo.length) {
|
||||
this.finishPromise.resolve();
|
||||
this.finishCallback();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -423,32 +366,30 @@ export class Agent {
|
|||
}
|
||||
|
||||
// add promise versions later.
|
||||
async playAnimation(index: number): Promise<void> {
|
||||
playAnimation(index: number, finishCallback: () => void) {
|
||||
if (this.animState != null) {
|
||||
await this.animState.cancel();
|
||||
this.animState.cancel();
|
||||
this.animState = null;
|
||||
}
|
||||
|
||||
let animInfo = this.data.animInfo[index];
|
||||
|
||||
// Create and start the animation state
|
||||
this.animState = new AgentAnimationState(this, animInfo.animationData);
|
||||
await this.animState.play();
|
||||
this.animState = null;
|
||||
}
|
||||
|
||||
async playAnimationByName(name: string): Promise<void> {
|
||||
let index = this.data.animInfo.findIndex((n) => n.name == name);
|
||||
if (index !== -1) return this.playAnimation(index);
|
||||
//throw new Error(`Unknown animation \"${name}\"`);
|
||||
return;
|
||||
}
|
||||
|
||||
async exitAnimation(): Promise<void> {
|
||||
if (this.animState != null) {
|
||||
await this.animState.cancel();
|
||||
this.animState = new AgentAnimationState(this, animInfo.animationData, () => {
|
||||
this.animState = null;
|
||||
}
|
||||
// (After this we need to play the restpose animation or whatever exit is applicable)
|
||||
finishCallback();
|
||||
});
|
||||
this.animState.play();
|
||||
}
|
||||
|
||||
playAnimationByName(name: string, finishCallback: () => void) {
|
||||
let index = this.data.animInfo.findIndex((n) => n.name == name);
|
||||
if (index !== -1) this.playAnimation(index, finishCallback);
|
||||
}
|
||||
|
||||
playAnimationByNamePromise(name: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
this.playAnimationByName(name, () => res());
|
||||
});
|
||||
}
|
||||
|
||||
setUsername(username: string, color: string) {
|
||||
|
|
|
@ -397,7 +397,7 @@ export class MSAgentClient {
|
|||
let animMsg = msg as MSAgentAnimationMessage;
|
||||
let user = this.users.find((u) => u.username === animMsg.data.username);
|
||||
if (!user || user.muted) return;
|
||||
await user.agent.playAnimationByName(animMsg.data.anim);
|
||||
await user.agent.playAnimationByNamePromise(animMsg.data.anim);
|
||||
await user.doAnim('rest');
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ input.addEventListener('change', async () => {
|
|||
agent.addToDom(mount);
|
||||
|
||||
agent.show();
|
||||
await agent.playAnimationByName("Show");
|
||||
await agent.playAnimationByNamePromise("Show");
|
||||
console.log('Agent created');
|
||||
});
|
||||
|
||||
|
@ -40,7 +40,7 @@ form.addEventListener('submit', async (e) => {
|
|||
agent.addToDom(document.body);
|
||||
|
||||
agent.show();
|
||||
await agent.playAnimationByName("Show");
|
||||
await agent.playAnimationByNamePromise("Show");
|
||||
|
||||
console.log(`Loaded agent from ${url}`);
|
||||
});
|
||||
|
|
|
@ -20,7 +20,7 @@ export class User {
|
|||
async doAnim(action: string) {
|
||||
// @ts-ignore
|
||||
for (let anim of this.animations[action]) {
|
||||
await this.agent.playAnimationByName(anim);
|
||||
await this.agent.playAnimationByNamePromise(anim);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue