@lubomirklimes/character-runtime 0.0.0-stage → 0.1.0-preview.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/acs/acs15.d.ts +9 -0
- package/dist/acs/acs15.js +382 -0
- package/dist/acs/acs2-reader.d.ts +2 -0
- package/dist/acs/acs2-reader.js +277 -0
- package/dist/acs/binary.d.ts +1 -0
- package/dist/acs/binary.js +107 -0
- package/dist/acs/compound-file.d.ts +1 -0
- package/dist/acs/compound-file.js +326 -0
- package/dist/acs/document.d.ts +33 -0
- package/dist/acs/document.js +99 -0
- package/dist/acs/errors.d.ts +28 -0
- package/dist/acs/errors.js +41 -0
- package/dist/acs/image.d.ts +30 -0
- package/dist/acs/image.js +113 -0
- package/dist/acs/index.d.ts +11 -0
- package/dist/acs/index.js +10 -0
- package/dist/acs/lz.d.ts +7 -0
- package/dist/acs/lz.js +102 -0
- package/dist/acs/metadata.d.ts +14 -0
- package/dist/acs/metadata.js +126 -0
- package/dist/acs/model.d.ts +130 -0
- package/dist/acs/model.js +12 -0
- package/dist/acs/mouth.d.ts +37 -0
- package/dist/acs/mouth.js +43 -0
- package/dist/acs/wave.d.ts +53 -0
- package/dist/acs/wave.js +422 -0
- package/dist/browser/animation-frame-clock.d.ts +18 -0
- package/dist/browser/animation-frame-clock.js +42 -0
- package/dist/browser/canvas-renderer.d.ts +50 -0
- package/dist/browser/canvas-renderer.js +145 -0
- package/dist/browser/host.d.ts +84 -0
- package/dist/browser/host.js +206 -0
- package/dist/browser/index.d.ts +6 -0
- package/dist/browser/index.js +8 -0
- package/dist/browser/web-audio.d.ts +48 -0
- package/dist/browser/web-audio.js +205 -0
- package/dist/browser/web-speech.d.ts +131 -0
- package/dist/browser/web-speech.js +302 -0
- package/dist/core/activities.d.ts +10 -0
- package/dist/core/activities.js +479 -0
- package/dist/core/agent.d.ts +263 -0
- package/dist/core/agent.js +764 -0
- package/dist/core/audio.d.ts +17 -0
- package/dist/core/audio.js +174 -0
- package/dist/core/balloon.d.ts +43 -0
- package/dist/core/balloon.js +94 -0
- package/dist/core/clock.d.ts +32 -0
- package/dist/core/clock.js +51 -0
- package/dist/core/compositor.d.ts +22 -0
- package/dist/core/compositor.js +67 -0
- package/dist/core/events.d.ts +131 -0
- package/dist/core/events.js +1 -0
- package/dist/core/host.d.ts +29 -0
- package/dist/core/host.js +77 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +13 -0
- package/dist/core/lipsync.d.ts +41 -0
- package/dist/core/lipsync.js +209 -0
- package/dist/core/random.d.ts +52 -0
- package/dist/core/random.js +105 -0
- package/dist/core/request.d.ts +68 -0
- package/dist/core/request.js +131 -0
- package/dist/core/resources.d.ts +70 -0
- package/dist/core/resources.js +243 -0
- package/dist/core/scheduler.d.ts +25 -0
- package/dist/core/scheduler.js +766 -0
- package/dist/core/source.d.ts +21 -0
- package/dist/core/source.js +54 -0
- package/dist/core/speech.d.ts +91 -0
- package/dist/core/speech.js +86 -0
- package/dist/core/text.d.ts +38 -0
- package/dist/core/text.js +97 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/package.json +68 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lubomir Klimes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CharacterRuntime.JS
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A native TypeScript runtime for animated characters in the Microsoft Agent ACS format. It reads, decodes, composes and plays
|
|
4
|
+
characters as specified by [CharacterRuntime.Conformance](https://github.com/lubomirklimes/CharacterRuntime.Conformance) and passes
|
|
5
|
+
its shared conformance suite. Source code: [github.com/lubomirklimes/CharacterRuntime.JS](https://github.com/lubomirklimes/CharacterRuntime.JS).
|
|
6
|
+
|
|
7
|
+
## Environments and formats
|
|
8
|
+
|
|
9
|
+
- ACS 2.x characters and ACS 1.5 compound-file characters (read-only); sounds in PCM, ADPCM, G.711, IEEE float and MPEG Layer-3.
|
|
10
|
+
- `@lubomirklimes/character-runtime`: the portable runtime. It uses no browser API and runs in Node.js 22 or later.
|
|
11
|
+
- `@lubomirklimes/character-runtime/browser`: Canvas 2D rendering, Web Audio, Web Speech and page hosting for current browsers.
|
|
12
|
+
- Browsers keep audio suspended until the first user gesture on the page; the Web Audio adapter resumes it then.
|
|
13
|
+
- Web Speech speaks without audio data or visemes, so lip-sync is approximate, and a page has one speech queue: speech of several
|
|
14
|
+
characters is spoken one utterance at a time.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm install @lubomirklimes/character-runtime@preview
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Preview versions are published with the `preview` tag, stable versions with `latest`. The package is ES modules with TypeScript
|
|
23
|
+
declarations and has no dependencies.
|
|
24
|
+
|
|
25
|
+
## Example
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { BrowserCharacterHost } from '@lubomirklimes/character-runtime/browser';
|
|
29
|
+
|
|
30
|
+
const host = new BrowserCharacterHost(document.getElementById('stage'));
|
|
31
|
+
const agent = await host.load('characters/merlin.acs', { position: { x: 100, y: 80 }, speechEnabled: true });
|
|
32
|
+
agent.on((event) => console.log(event.type));
|
|
33
|
+
agent.show();
|
|
34
|
+
agent.play('Greet');
|
|
35
|
+
const speech = agent.speak('Hello!');
|
|
36
|
+
agent.moveTo(300, 80);
|
|
37
|
+
console.log((await speech.completion).status);
|
|
38
|
+
agent.hide();
|
|
39
|
+
host.remove(agent); // host.dispose() removes every character
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Characters load from a URL, a `File`, an `ArrayBuffer` or a `Uint8Array`. In Node.js, `AgentHost`, `ManualClock` and
|
|
43
|
+
`CharacterSource.fromBytes` run the same runtime deterministically.
|
|
44
|
+
|
|
45
|
+
## Build and test
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
npm ci
|
|
49
|
+
npm run typecheck
|
|
50
|
+
npm test # unit tests and the conformance suite
|
|
51
|
+
npm run build # ES modules and declarations in dist/
|
|
52
|
+
npm run test:browser # Playwright tests in an installed Google Chrome (PLAYWRIGHT_CHANNEL=msedge for Edge)
|
|
53
|
+
npm run pack:check # packs the package and checks its contents
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`CHARACTER_RUNTIME_CORPUS=<directories>` adds an optional test over local character files.
|
|
57
|
+
|
|
58
|
+
## CharacterPlayer.Web
|
|
59
|
+
|
|
60
|
+
[`samples/CharacterPlayer.Web/`](https://github.com/lubomirklimes/CharacterRuntime.JS/tree/main/samples/CharacterPlayer.Web) is a
|
|
61
|
+
reference player built on the public browser API: load ACS characters from files, drag and drop or
|
|
62
|
+
a URL, play animations, states, speech and movement on a shared stage, inspect requests and events, and run, save and load request
|
|
63
|
+
sequences. It is not part of the npm package and needs no server code.
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
npm run player # development server (Vite) with the runtime sources
|
|
67
|
+
npm run player:build # static site in samples/CharacterPlayer.Web/dist (relative paths, deployable to any subdirectory)
|
|
68
|
+
npm run player:preview # serves that build at http://127.0.0.1:4174/apps/character-player/
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`npm test` includes the player's unit tests; `npm run test:browser` builds the player and runs its browser tests against the static
|
|
72
|
+
build. Characters are not bundled: open local `.acs` files, or load a URL from a server that allows cross-origin requests.
|
|
73
|
+
|
|
74
|
+
## Conformance
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
git submodule update --init
|
|
78
|
+
npm run conformance
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Releases
|
|
82
|
+
|
|
83
|
+
Pushing a version tag such as `v0.1.0-preview.1` or `v1.0.0` runs the release workflow: the tag is the version, applied to the build
|
|
84
|
+
workspace only, and after the type check, tests, conformance suite and browser tests a GitHub Release receives the npm package,
|
|
85
|
+
`CharacterPlayer.Web-<version>-web.zip` (the static player) and `SHA256SUMS.txt`. The workflow **Publish npm**, started manually with
|
|
86
|
+
the tag of a published release, verifies that release's package and publishes exactly that archive to npm through Trusted Publishing.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT, see [LICENSE](https://github.com/lubomirklimes/CharacterRuntime.JS/blob/main/LICENSE). The `conformance/` submodule is a separate repository with its own MIT license.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Default budget of decoded ACS 1.5 animation streams kept for image and sound reads. */
|
|
2
|
+
export declare const ACS15_STREAM_CACHE_BYTES: number;
|
|
3
|
+
/** An animation whose stream cannot be loaded, with a diagnostic category (`missing-stream`, `envelope`, `compression`, `structure`). */
|
|
4
|
+
export interface UnavailableAnimation {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly streamName: string;
|
|
7
|
+
readonly category: string;
|
|
8
|
+
readonly message: string;
|
|
9
|
+
}
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { foldCase, guidText, i16, i32, u16, u32, utf16 } from './binary.js';
|
|
2
|
+
import { CompoundFile, hasCompoundFileSignature } from './compound-file.js';
|
|
3
|
+
import { AcsFormatError } from './errors.js';
|
|
4
|
+
import { MAX_FILE_BYTES, ordinal } from './acs2-reader.js';
|
|
5
|
+
import { BlockWriter } from './metadata.js';
|
|
6
|
+
import { decodeLz, MAX_DECODED_BYTES } from './lz.js';
|
|
7
|
+
import { MAX_IMAGES } from './image.js';
|
|
8
|
+
const HEADER_SIGNATURE = 0xabcdabc1;
|
|
9
|
+
const HEADER_STREAM = 'char.acf';
|
|
10
|
+
const MAX_STRING = 65536;
|
|
11
|
+
const NO_IMAGE = 0xffff;
|
|
12
|
+
/** Default budget of decoded ACS 1.5 animation streams kept for image and sound reads. */
|
|
13
|
+
export const ACS15_STREAM_CACHE_BYTES = 8 * 1024 * 1024;
|
|
14
|
+
class ParseError extends Error {
|
|
15
|
+
code;
|
|
16
|
+
constructor(code, message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** A strict cursor over a decoded payload: every failure is `legacy-structure` (or `unsupported-string`). */
|
|
22
|
+
class Cursor {
|
|
23
|
+
bytes;
|
|
24
|
+
pos = 0;
|
|
25
|
+
constructor(bytes) {
|
|
26
|
+
this.bytes = bytes;
|
|
27
|
+
}
|
|
28
|
+
get remaining() {
|
|
29
|
+
return this.bytes.length - this.pos;
|
|
30
|
+
}
|
|
31
|
+
take(count, what) {
|
|
32
|
+
if (count < 0 || count > this.remaining)
|
|
33
|
+
throw new ParseError('legacy-structure', `Truncated ${what} at 0x${this.pos.toString(16)}.`);
|
|
34
|
+
const at = this.pos;
|
|
35
|
+
this.pos += count;
|
|
36
|
+
return at;
|
|
37
|
+
}
|
|
38
|
+
u8(what) {
|
|
39
|
+
return this.bytes[this.take(1, what)];
|
|
40
|
+
}
|
|
41
|
+
u16(what) {
|
|
42
|
+
return u16(this.bytes, this.take(2, what));
|
|
43
|
+
}
|
|
44
|
+
i16(what) {
|
|
45
|
+
return i16(this.bytes, this.take(2, what));
|
|
46
|
+
}
|
|
47
|
+
u32(what) {
|
|
48
|
+
return u32(this.bytes, this.take(4, what));
|
|
49
|
+
}
|
|
50
|
+
i32(what) {
|
|
51
|
+
return i32(this.bytes, this.take(4, what));
|
|
52
|
+
}
|
|
53
|
+
length(what) {
|
|
54
|
+
const n = this.u32(what);
|
|
55
|
+
if (n > this.remaining)
|
|
56
|
+
throw new ParseError('legacy-structure', `${what} ${n} exceeds the ${this.remaining} remaining bytes.`);
|
|
57
|
+
return n;
|
|
58
|
+
}
|
|
59
|
+
string(what) {
|
|
60
|
+
const n = this.u32(`${what} length`);
|
|
61
|
+
if (n === 0)
|
|
62
|
+
return '';
|
|
63
|
+
if (n > MAX_STRING)
|
|
64
|
+
throw new ParseError('legacy-structure', `${what} declares ${n} UTF-16 units (policy ${MAX_STRING}).`);
|
|
65
|
+
const at = this.take(n * 2, what);
|
|
66
|
+
const value = utf16(this.bytes, at, n);
|
|
67
|
+
if (value === null)
|
|
68
|
+
throw new ParseError('unsupported-string', `${what} contains unpaired UTF-16 code units.`);
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function parseHeader(d) {
|
|
73
|
+
const r = new Cursor(d);
|
|
74
|
+
const versionWord = r.u32('version');
|
|
75
|
+
const major = versionWord >>> 16, minor = versionWord & 0xffff;
|
|
76
|
+
if (major !== 1 || (minor !== 30 && minor !== 31))
|
|
77
|
+
throw new ParseError('legacy-version', `Decoded version ${major}.${minor} is not 1.30 or 1.31.`);
|
|
78
|
+
const count = r.u16('animation count');
|
|
79
|
+
const animations = [];
|
|
80
|
+
const names = new Set();
|
|
81
|
+
for (let i = 0; i < count; i++) {
|
|
82
|
+
const entry = { name: r.string('animation name'), streamName: r.string('stream name'), returnName: r.string('return name'), checksum: r.u32('checksum') };
|
|
83
|
+
if (entry.name.length === 0)
|
|
84
|
+
throw new ParseError('legacy-structure', `Animation ${i} has an empty name.`);
|
|
85
|
+
const key = foldCase(entry.name);
|
|
86
|
+
if (names.has(key))
|
|
87
|
+
throw new ParseError('legacy-structure', `Duplicate animation name '${entry.name}'.`);
|
|
88
|
+
names.add(key);
|
|
89
|
+
animations.push(entry);
|
|
90
|
+
}
|
|
91
|
+
const guid = guidText(d, r.take(16, 'GUID'));
|
|
92
|
+
const name = r.string('name'), description = r.string('description'), extra = r.string('extra description');
|
|
93
|
+
const width = r.u16('width'), height = r.u16('height');
|
|
94
|
+
if (width === 0 || width > 2048 || height === 0 || height > 2048)
|
|
95
|
+
throw new ParseError('legacy-structure', `Character frame ${width}x${height} is outside 1..2048.`);
|
|
96
|
+
const transparentIndex = r.u8('transparent index');
|
|
97
|
+
const style = r.u32('style');
|
|
98
|
+
let tts = null, balloon = null;
|
|
99
|
+
if ((style & 0x20) !== 0) {
|
|
100
|
+
// Re-serialized in the ACS 2.1 layout without the optional voice part (ACS15.md §6).
|
|
101
|
+
const at = r.take(38, 'TTS block');
|
|
102
|
+
const w = new BlockWriter();
|
|
103
|
+
w.raw(d.subarray(at, at + 38));
|
|
104
|
+
w.u8(0);
|
|
105
|
+
tts = w.toArray();
|
|
106
|
+
}
|
|
107
|
+
if ((style & 0x200) !== 0) {
|
|
108
|
+
const head = r.take(14, 'balloon block');
|
|
109
|
+
const fontLength = r.u32('balloon font length');
|
|
110
|
+
if (fontLength > MAX_STRING)
|
|
111
|
+
throw new ParseError('legacy-structure', 'Balloon font name exceeds the string policy.');
|
|
112
|
+
const fontAt = r.take(fontLength * 2, 'balloon font');
|
|
113
|
+
if (fontLength > 0 && utf16(d, fontAt, fontLength) === null)
|
|
114
|
+
throw new ParseError('unsupported-string', 'Balloon font name contains unpaired UTF-16 code units.');
|
|
115
|
+
const metrics = r.take(6, 'balloon font metrics');
|
|
116
|
+
const tail = r.take(minor >= 31 ? 4 : 3, 'balloon tail');
|
|
117
|
+
const w = new BlockWriter();
|
|
118
|
+
w.raw(d.subarray(head, head + 14));
|
|
119
|
+
w.string(d.subarray(fontAt, fontAt + fontLength * 2), fontLength);
|
|
120
|
+
w.raw(d.subarray(metrics, metrics + 6));
|
|
121
|
+
const words = new Uint8Array(4);
|
|
122
|
+
words.set(d.subarray(tail, tail + (minor >= 31 ? 4 : 3)));
|
|
123
|
+
w.raw(words);
|
|
124
|
+
balloon = w.toArray();
|
|
125
|
+
}
|
|
126
|
+
const paletteCount = r.u32('palette count');
|
|
127
|
+
if (paletteCount === 0 || paletteCount > 256)
|
|
128
|
+
throw new ParseError('legacy-structure', `Palette count ${paletteCount} is outside 1..256.`);
|
|
129
|
+
const palette = new Uint32Array(paletteCount);
|
|
130
|
+
for (let i = 0; i < paletteCount; i++)
|
|
131
|
+
palette[i] = r.u32('palette entry');
|
|
132
|
+
const stateCount = r.u16('state count');
|
|
133
|
+
const states = new Map();
|
|
134
|
+
for (let i = 0; i < stateCount; i++) {
|
|
135
|
+
const state = r.string('state name');
|
|
136
|
+
const members = r.u16('state member count');
|
|
137
|
+
const list = [];
|
|
138
|
+
for (let j = 0; j < members; j++)
|
|
139
|
+
list.push(r.string('state member'));
|
|
140
|
+
if (states.has(state))
|
|
141
|
+
throw new ParseError('legacy-structure', `Duplicate state name '${state}'.`);
|
|
142
|
+
states.set(state, list);
|
|
143
|
+
}
|
|
144
|
+
if (r.remaining !== 0)
|
|
145
|
+
throw new ParseError('legacy-structure', `${r.remaining} trailing bytes after the states.`);
|
|
146
|
+
return { versionWord, minor, animations, guid, name, description, extra, width, height, transparentIndex, style, tts, balloon, palette, states };
|
|
147
|
+
}
|
|
148
|
+
function parseAnimation(d, width, height) {
|
|
149
|
+
const r = new Cursor(d);
|
|
150
|
+
const framePixels = ((width + 3) & ~3) * height;
|
|
151
|
+
const soundCount = r.u16('sound count');
|
|
152
|
+
const sounds = [];
|
|
153
|
+
for (let i = 0; i < soundCount; i++) {
|
|
154
|
+
const length = r.length('sound size');
|
|
155
|
+
sounds.push({ dataOffset: r.pos, length });
|
|
156
|
+
r.take(length, 'sound data');
|
|
157
|
+
}
|
|
158
|
+
const imageCount = r.u16('image count');
|
|
159
|
+
const images = [];
|
|
160
|
+
for (let i = 0; i < imageCount; i++) {
|
|
161
|
+
const length = r.length('image size');
|
|
162
|
+
if (length !== framePixels)
|
|
163
|
+
throw new ParseError('legacy-structure', `Image ${i} declares ${length} pixel bytes; the frame needs ${framePixels}.`);
|
|
164
|
+
const flag = r.u8('image flag');
|
|
165
|
+
if (flag !== 0)
|
|
166
|
+
throw new ParseError('legacy-structure', `Image ${i} has flag ${flag}; only 0 is supported.`);
|
|
167
|
+
const pixelOffset = r.take(length, 'image pixels');
|
|
168
|
+
const regionLength = r.length('region size');
|
|
169
|
+
r.take(regionLength, 'region');
|
|
170
|
+
images.push({ pixelOffset, pixelLength: length, regionLength });
|
|
171
|
+
}
|
|
172
|
+
const frameCount = r.u16('frame count');
|
|
173
|
+
const frames = [];
|
|
174
|
+
for (let i = 0; i < frameCount; i++) {
|
|
175
|
+
const image = r.u16('frame image'), sound = r.i16('frame sound'), duration = r.u16('frame duration');
|
|
176
|
+
r.u16('frame word +6');
|
|
177
|
+
r.u16('frame word +8');
|
|
178
|
+
if (image !== NO_IMAGE && image >= imageCount)
|
|
179
|
+
throw new ParseError('legacy-structure', `Frame ${i} references image ${image} of ${imageCount}.`);
|
|
180
|
+
if (sound < -1 || sound >= soundCount)
|
|
181
|
+
throw new ParseError('legacy-structure', `Frame ${i} references sound ${sound} of ${soundCount}.`);
|
|
182
|
+
const branchCount = r.u8('branch count');
|
|
183
|
+
const branches = [];
|
|
184
|
+
for (let b = 0; b < branchCount; b++)
|
|
185
|
+
branches.push(r.u32('branch'));
|
|
186
|
+
const overlayCount = r.u8('mouth overlay count');
|
|
187
|
+
const mouths = [];
|
|
188
|
+
for (let m = 0; m < overlayCount; m++) {
|
|
189
|
+
const shape = r.u8('mouth shape'), size = r.u32('mouth size');
|
|
190
|
+
if (size === 0) {
|
|
191
|
+
mouths.push({ shape, size: 0, x: 0, y: 0, width: 0, height: 0, pixelOffset: 0 });
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
r.u8('mouth flag'); // not interpreted: converted records use Replace 0
|
|
195
|
+
const x = r.i16('mouth x'), y = r.i16('mouth y'), w = r.i16('mouth width'), h = r.i16('mouth height');
|
|
196
|
+
if (w < 1 || h < 1 || size !== ((w + 3) & ~3) * h)
|
|
197
|
+
throw new ParseError('legacy-structure', `Frame ${i} mouth ${m}: ${size} bytes do not match a ${w}x${h} bitmap.`);
|
|
198
|
+
const pixelOffset = r.take(size, 'mouth pixels');
|
|
199
|
+
mouths.push({ shape, size, x, y, width: w, height: h, pixelOffset });
|
|
200
|
+
}
|
|
201
|
+
frames.push({ image, sound, duration, branches, mouths });
|
|
202
|
+
}
|
|
203
|
+
if (r.remaining !== 0)
|
|
204
|
+
throw new ParseError('legacy-structure', `${r.remaining} trailing bytes after the frames.`);
|
|
205
|
+
return { sounds, images, frames };
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* @internal An ACS 1.5 character opened for direct playback (ACS15.md): the compound file, the header and an ACS 2.x-shaped character
|
|
209
|
+
* model, with images and sounds decoded on demand from a byte-bounded cache of expanded animation streams. Damaged animation streams
|
|
210
|
+
* make only their own animation unavailable.
|
|
211
|
+
*/
|
|
212
|
+
export class Acs15File {
|
|
213
|
+
streamBudget;
|
|
214
|
+
character;
|
|
215
|
+
unavailable;
|
|
216
|
+
images;
|
|
217
|
+
sounds;
|
|
218
|
+
container;
|
|
219
|
+
entries;
|
|
220
|
+
versionWord;
|
|
221
|
+
width;
|
|
222
|
+
height;
|
|
223
|
+
cache = new Map(); // insertion order = least recently used first
|
|
224
|
+
cachedBytes = 0;
|
|
225
|
+
constructor(bytes, streamBudget = ACS15_STREAM_CACHE_BYTES) {
|
|
226
|
+
this.streamBudget = streamBudget;
|
|
227
|
+
if (bytes.length > MAX_FILE_BYTES)
|
|
228
|
+
throw new AcsFormatError('limit', 'File exceeds 256 MiB policy.');
|
|
229
|
+
if (!hasCompoundFileSignature(bytes))
|
|
230
|
+
throw new AcsFormatError('signature', 'Not a compound file.');
|
|
231
|
+
this.container = new CompoundFile(bytes);
|
|
232
|
+
const headerStream = this.container.find(HEADER_STREAM);
|
|
233
|
+
if (!headerStream)
|
|
234
|
+
throw new AcsFormatError('unsupported-format', 'Compound file has no char.acf stream; it is not an ACS 1.5 character.');
|
|
235
|
+
const raw = this.container.read(headerStream);
|
|
236
|
+
if (raw.length < 12 || u32(raw, 0) !== HEADER_SIGNATURE)
|
|
237
|
+
throw new AcsFormatError('unsupported-format', 'char.acf does not start with the ACS 1.5 signature.');
|
|
238
|
+
const decodedSize = u32(raw, 4), encodedSize = u32(raw, 8);
|
|
239
|
+
if (encodedSize + 12 !== raw.length)
|
|
240
|
+
throw new AcsFormatError('legacy-envelope', 'char.acf encoded size does not match its stream.');
|
|
241
|
+
if (decodedSize > MAX_DECODED_BYTES)
|
|
242
|
+
throw new AcsFormatError('limit', 'char.acf decoded size exceeds the decompressor policy.');
|
|
243
|
+
let data;
|
|
244
|
+
try {
|
|
245
|
+
data = decodeLz(raw.subarray(12), decodedSize);
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
throw new AcsFormatError('legacy-compression', `char.acf: ${e.message}`);
|
|
249
|
+
}
|
|
250
|
+
let h;
|
|
251
|
+
try {
|
|
252
|
+
h = parseHeader(data);
|
|
253
|
+
}
|
|
254
|
+
catch (e) {
|
|
255
|
+
if (e instanceof ParseError)
|
|
256
|
+
throw new AcsFormatError(e.code, `char.acf: ${e.message}`);
|
|
257
|
+
throw e;
|
|
258
|
+
}
|
|
259
|
+
this.entries = h.animations;
|
|
260
|
+
this.versionWord = h.versionWord;
|
|
261
|
+
this.width = h.width;
|
|
262
|
+
this.height = h.height;
|
|
263
|
+
const unavailable = [];
|
|
264
|
+
const images = [], sounds = [];
|
|
265
|
+
const animations = [];
|
|
266
|
+
for (let a = 0; a < h.animations.length; a++) {
|
|
267
|
+
const entry = h.animations[a];
|
|
268
|
+
const loaded = this.load(a);
|
|
269
|
+
if ('category' in loaded) {
|
|
270
|
+
unavailable.push(loaded);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const layout = loaded.layout;
|
|
274
|
+
const imageBase = images.length, soundBase = sounds.length;
|
|
275
|
+
for (const im of layout.images)
|
|
276
|
+
images.push({ animation: a, pixelOffset: im.pixelOffset, pixelLength: im.pixelLength, width: h.width, height: h.height });
|
|
277
|
+
for (const so of layout.sounds)
|
|
278
|
+
sounds.push({ animation: a, dataOffset: so.dataOffset, length: so.length });
|
|
279
|
+
const frames = layout.frames.map((fr) => {
|
|
280
|
+
const mouths = [];
|
|
281
|
+
for (const mo of fr.mouths) {
|
|
282
|
+
if (mo.size === 0)
|
|
283
|
+
continue; // empty records are dropped
|
|
284
|
+
const global = images.length;
|
|
285
|
+
if (global > 0xffff)
|
|
286
|
+
throw new AcsFormatError('limit', 'More than 65,536 images cannot be referenced by mouth records.');
|
|
287
|
+
images.push({ animation: a, pixelOffset: mo.pixelOffset, pixelLength: mo.size, width: mo.width, height: mo.height });
|
|
288
|
+
mouths.push({ shape: mo.shape, replace: 0, image: global, x: mo.x, y: mo.y, anchorX: 0, anchorY: 0 });
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
layers: fr.image === NO_IMAGE ? [] : [{ image: imageBase + fr.image, x: 0, y: 0 }],
|
|
292
|
+
sound: fr.sound < 0 ? fr.sound : soundBase + fr.sound,
|
|
293
|
+
durationTicks: fr.duration,
|
|
294
|
+
exitFrame: -1,
|
|
295
|
+
branches: fr.branches.map((w) => ({ target: w & 0xffff, percent: w >>> 16 })),
|
|
296
|
+
mouths,
|
|
297
|
+
};
|
|
298
|
+
});
|
|
299
|
+
animations.push({ name: entry.name, storedName: entry.name, returnType: entry.returnName.length > 0 ? 0 : 2, returnName: entry.returnName, frames, trailingBytes: new Uint8Array(0) });
|
|
300
|
+
}
|
|
301
|
+
if (images.length > MAX_IMAGES)
|
|
302
|
+
throw new AcsFormatError('image-limit', 'Image count exceeds decode policy.');
|
|
303
|
+
const retained = [];
|
|
304
|
+
if (h.tts)
|
|
305
|
+
retained.push({ kind: 'tts', offset: 0, bytes: h.tts });
|
|
306
|
+
if (h.balloon)
|
|
307
|
+
retained.push({ kind: 'balloon', offset: 0, bytes: h.balloon });
|
|
308
|
+
this.images = images;
|
|
309
|
+
this.sounds = sounds;
|
|
310
|
+
this.unavailable = unavailable;
|
|
311
|
+
this.character = {
|
|
312
|
+
format: 'acs15', major: 1, minor: h.minor, guid: h.guid, width: h.width, height: h.height, transparentIndex: h.transparentIndex,
|
|
313
|
+
style: h.style, headerWord: 0, names: [{ language: 0x0409, name: h.name, description: h.description, extra: h.extra }],
|
|
314
|
+
palette: h.palette, states: h.states, animations, imageCount: images.length, soundCount: sounds.length, retained, warnings: [],
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
/** Names of the unavailable animations in ordinal order. */
|
|
318
|
+
get unavailableNames() {
|
|
319
|
+
return this.unavailable.map((u) => u.name).sort(ordinal);
|
|
320
|
+
}
|
|
321
|
+
/** The decoded payload of a loaded legacy animation stream, from the bounded cache. */
|
|
322
|
+
decodedStream(animation) {
|
|
323
|
+
const hit = this.cache.get(animation);
|
|
324
|
+
if (hit) {
|
|
325
|
+
this.cache.delete(animation);
|
|
326
|
+
this.cache.set(animation, hit);
|
|
327
|
+
return hit;
|
|
328
|
+
}
|
|
329
|
+
const loaded = this.load(animation);
|
|
330
|
+
if ('category' in loaded)
|
|
331
|
+
throw new AcsFormatError('animation-index', 'Missing or unavailable legacy animation.');
|
|
332
|
+
this.cache.set(animation, loaded.data);
|
|
333
|
+
this.cachedBytes += loaded.data.length;
|
|
334
|
+
// The stream just used stays, even when it alone exceeds the budget.
|
|
335
|
+
for (const [key, value] of this.cache) {
|
|
336
|
+
if (this.cachedBytes <= this.streamBudget || key === animation)
|
|
337
|
+
break;
|
|
338
|
+
this.cache.delete(key);
|
|
339
|
+
this.cachedBytes -= value.length;
|
|
340
|
+
}
|
|
341
|
+
return loaded.data;
|
|
342
|
+
}
|
|
343
|
+
/** Drops every cached decoded stream. */
|
|
344
|
+
clearCache() {
|
|
345
|
+
this.cache.clear();
|
|
346
|
+
this.cachedBytes = 0;
|
|
347
|
+
}
|
|
348
|
+
load(index) {
|
|
349
|
+
const entry = this.entries[index];
|
|
350
|
+
const unavailable = (category, message) => ({ name: entry.name, streamName: entry.streamName, category, message });
|
|
351
|
+
const stream = this.container.find(entry.streamName);
|
|
352
|
+
if (!stream)
|
|
353
|
+
return unavailable('missing-stream', `Stream '${entry.streamName}' does not exist.`);
|
|
354
|
+
const raw = this.container.read(stream);
|
|
355
|
+
if (raw.length < 17)
|
|
356
|
+
return unavailable('envelope', 'Animation stream is shorter than its 17-byte envelope.');
|
|
357
|
+
if (u32(raw, 0) !== this.versionWord)
|
|
358
|
+
return unavailable('envelope', 'Stream version word differs from char.acf.');
|
|
359
|
+
if (raw[8] !== 1)
|
|
360
|
+
return unavailable('envelope', `Stream flag byte ${raw[8]} is not 1.`);
|
|
361
|
+
const decodedSize = u32(raw, 9), encodedSize = u32(raw, 13);
|
|
362
|
+
if (encodedSize + 17 !== raw.length)
|
|
363
|
+
return unavailable('envelope', 'Encoded size does not match the stream.');
|
|
364
|
+
if (decodedSize > MAX_DECODED_BYTES)
|
|
365
|
+
return unavailable('envelope', 'Decoded size exceeds the decompressor policy.');
|
|
366
|
+
let data;
|
|
367
|
+
try {
|
|
368
|
+
data = decodeLz(raw.subarray(17), decodedSize);
|
|
369
|
+
}
|
|
370
|
+
catch (e) {
|
|
371
|
+
return unavailable('compression', e.message);
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
return { layout: parseAnimation(data, this.width, this.height), data };
|
|
375
|
+
}
|
|
376
|
+
catch (e) {
|
|
377
|
+
if (e instanceof ParseError)
|
|
378
|
+
return unavailable('structure', e.message);
|
|
379
|
+
throw e;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|