diff --git a/msagent.js/core/src/index.ts b/msagent.js/core/src/index.ts index 0b5df58..823640f 100644 --- a/msagent.js/core/src/index.ts +++ b/msagent.js/core/src/index.ts @@ -8,3 +8,5 @@ export * from './structs/image.js'; export * from './types.js'; export * from './decompress.js'; + +export * from './parse.js'; diff --git a/msagent.js/core/src/parse.ts b/msagent.js/core/src/parse.ts new file mode 100644 index 0000000..33f099d --- /dev/null +++ b/msagent.js/core/src/parse.ts @@ -0,0 +1,46 @@ +import { BufferStream, SeekDir } from './buffer.js'; + +import { LOCATION } from './structs/core.js'; +import { AcsCharacterInfo } from './structs/character.js'; +import { AcsAnimationEntry } from './structs/animation.js'; +import { AcsImageEntry } from './structs/image.js'; + +// Data +export class AcsData { + characterInfo = new AcsCharacterInfo(); + animInfo: AcsAnimationEntry[] = []; + images: AcsImageEntry[] = []; +} + +export function agentCharacterParseACS(buffer: BufferStream): AcsData { + // Make sure the magic is correct for the ACS file. + if (buffer.readU32LE() != 0xabcdabc3) { + throw new Error('The provided data buffer does not contain valid ACS data.'); + } + + let acsData = new AcsData(); + + // Read the rest of the header. + let characterInfoLocation = LOCATION.read(buffer); + let animationInfoLocation = LOCATION.read(buffer); + let imageInfoLocation = LOCATION.read(buffer); + let audioInfoLocation = LOCATION.read(buffer); + + buffer.withOffset(characterInfoLocation.offset, () => { + acsData.characterInfo = AcsCharacterInfo.read(buffer); + }); + + buffer.withOffset(animationInfoLocation.offset, () => { + acsData.animInfo = buffer.readCountedList(() => { + return AcsAnimationEntry.read(buffer); + }); + }); + + buffer.withOffset(imageInfoLocation.offset, () => { + acsData.images = buffer.readCountedList(() => { + return AcsImageEntry.read(buffer); + }); + }); + + return acsData; +} diff --git a/msagent.js/web/src/character.ts b/msagent.js/web/src/character.ts index dfe6c2c..a089783 100644 --- a/msagent.js/web/src/character.ts +++ b/msagent.js/web/src/character.ts @@ -1,18 +1,6 @@ -import { BufferStream, SeekDir } from '@msagent.js/core'; - -import { LOCATION } from '@msagent.js/core'; -import { AcsCharacterInfo } from '@msagent.js/core'; -import { AcsAnimationEntry } from '@msagent.js/core'; -import { AcsImageEntry } from '@msagent.js/core'; +import { agentCharacterParseACS, AcsData, BufferStream } from '@msagent.js/core'; import { Agent } from './agent.js'; -// Data -export class AcsData { - characterInfo = new AcsCharacterInfo(); - animInfo: AcsAnimationEntry[] = []; - images: AcsImageEntry[] = []; -} - // Cache of ACS data per character (for agentCreateCharacterFromUrl) let acsDataCache = new Map(); @@ -21,47 +9,10 @@ export function agentPurgeACSCache() { acsDataCache.clear(); } -export function agentCharacterParseACS(buffer: BufferStream): AcsData { - // Make sure the magic is correct for the ACS file. - if (buffer.readU32LE() != 0xabcdabc3) { - throw new Error('The provided data buffer does not contain valid ACS data.'); - } - - let acsData = new AcsData(); - - // Read the rest of the header. - let characterInfoLocation = LOCATION.read(buffer); - let animationInfoLocation = LOCATION.read(buffer); - let imageInfoLocation = LOCATION.read(buffer); - let audioInfoLocation = LOCATION.read(buffer); - - buffer.withOffset(characterInfoLocation.offset, () => { - acsData.characterInfo = AcsCharacterInfo.read(buffer); - }); - - buffer.withOffset(animationInfoLocation.offset, () => { - acsData.animInfo = buffer.readCountedList(() => { - return AcsAnimationEntry.read(buffer); - }); - }); - - buffer.withOffset(imageInfoLocation.offset, () => { - acsData.images = buffer.readCountedList(() => { - return AcsImageEntry.read(buffer); - }); - }); - - return acsData; -} - -export function agentCreateCharacter(data: AcsData): Agent { - return new Agent(data); -} - export async function agentCreateCharacterFromUrl(url: string): Promise { // just return the cache object if (acsDataCache.has(url)) { - return agentCreateCharacter(acsDataCache.get(url)!); + return new Agent(acsDataCache.get(url)!); } else { let res = await fetch(url); let data = await res.arrayBuffer(); @@ -70,6 +21,6 @@ export async function agentCreateCharacterFromUrl(url: string): Promise { let acsData = agentCharacterParseACS(new BufferStream(buffer)); acsDataCache.set(url, acsData); - return agentCreateCharacter(acsData); + return new Agent(acsData); } } diff --git a/webapp/src/ts/main.ts b/webapp/src/ts/main.ts index b71a18a..023586b 100644 --- a/webapp/src/ts/main.ts +++ b/webapp/src/ts/main.ts @@ -1,5 +1,5 @@ import { MSWindow, MSWindowStartPosition } from './MSWindow.js'; -import { agentInit } from '@msagent.js/web'; +import { agentInit, agentPurgeACSCache } from '@msagent.js/web'; import { MSAgentClient } from './client.js'; import { Config } from '../../config.js'; import { RunCommand } from './commands.js'; @@ -36,6 +36,7 @@ function roomInit() { user.agent.remove(); } roomInit(); + agentPurgeACSCache(); loggingIn = false; elements.logonButton.disabled = false; logonWindow.show(); diff --git a/webapp/src/ts/user.ts b/webapp/src/ts/user.ts index 1cad4f2..d8c2b8f 100644 --- a/webapp/src/ts/user.ts +++ b/webapp/src/ts/user.ts @@ -1,4 +1,4 @@ -import { Agent } from '@msagent-chat/msagent.js'; +import { Agent } from '@msagent.js/web'; import { AgentAnimationConfig } from '@msagent-chat/protocol'; export class User {