@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
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The handle of one submitted request. Every accepted request reaches exactly one terminal state; {@link started} and
|
|
3
|
+
* {@link completion} always resolve, never reject.
|
|
4
|
+
*/
|
|
5
|
+
export class AgentRequest {
|
|
6
|
+
agent;
|
|
7
|
+
kind;
|
|
8
|
+
name;
|
|
9
|
+
waitTarget;
|
|
10
|
+
/** The request id within its agent (1, 2, 3 … in submission order). */
|
|
11
|
+
id = 0;
|
|
12
|
+
statusValue = 'queued';
|
|
13
|
+
reasonValue = 'none';
|
|
14
|
+
detailValue = '';
|
|
15
|
+
failureCodeValue = '';
|
|
16
|
+
startedAt = null;
|
|
17
|
+
endedAt = null;
|
|
18
|
+
speechValue = 'none';
|
|
19
|
+
speechAudio = false;
|
|
20
|
+
speechMs = null;
|
|
21
|
+
speechNoteValue = '';
|
|
22
|
+
resolveStarted;
|
|
23
|
+
resolveCompletion;
|
|
24
|
+
startedSettled = false;
|
|
25
|
+
/** Resolves with true when the request started, false when it ended without starting. */
|
|
26
|
+
started;
|
|
27
|
+
/** Resolves with the outcome once the request is terminal. */
|
|
28
|
+
completion;
|
|
29
|
+
/** @internal */
|
|
30
|
+
constructor(agent, kind, name, waitTarget = null) {
|
|
31
|
+
this.agent = agent;
|
|
32
|
+
this.kind = kind;
|
|
33
|
+
this.name = name;
|
|
34
|
+
this.waitTarget = waitTarget;
|
|
35
|
+
this.started = new Promise((resolve) => (this.resolveStarted = resolve));
|
|
36
|
+
this.completion = new Promise((resolve) => (this.resolveCompletion = resolve));
|
|
37
|
+
}
|
|
38
|
+
get status() {
|
|
39
|
+
return this.statusValue;
|
|
40
|
+
}
|
|
41
|
+
get reason() {
|
|
42
|
+
return this.reasonValue;
|
|
43
|
+
}
|
|
44
|
+
get detail() {
|
|
45
|
+
return this.detailValue;
|
|
46
|
+
}
|
|
47
|
+
get failureCode() {
|
|
48
|
+
return this.failureCodeValue;
|
|
49
|
+
}
|
|
50
|
+
get startedAtMs() {
|
|
51
|
+
return this.startedAt;
|
|
52
|
+
}
|
|
53
|
+
/** The logical time of the terminal state, or null while queued or running. */
|
|
54
|
+
get endedAtMs() {
|
|
55
|
+
return this.endedAt;
|
|
56
|
+
}
|
|
57
|
+
get isTerminal() {
|
|
58
|
+
return this.endedAt !== null;
|
|
59
|
+
}
|
|
60
|
+
/** Speak/Think: where the speech is. */
|
|
61
|
+
get speechProgress() {
|
|
62
|
+
return this.speechValue;
|
|
63
|
+
}
|
|
64
|
+
/** Speak/Think: the speech has audio (synthesized or recorded). */
|
|
65
|
+
get speechHasAudio() {
|
|
66
|
+
return this.speechAudio;
|
|
67
|
+
}
|
|
68
|
+
/** Speak/Think: the speech duration once known. */
|
|
69
|
+
get speechDurationMs() {
|
|
70
|
+
return this.speechMs;
|
|
71
|
+
}
|
|
72
|
+
/** Speak/Think: how the speech is produced and why it has no audio, when it has none. */
|
|
73
|
+
get speechNote() {
|
|
74
|
+
return this.speechNoteValue;
|
|
75
|
+
}
|
|
76
|
+
/** Cancels the request (queued: removed; running: stops gracefully). False when it is already terminal. */
|
|
77
|
+
cancel() {
|
|
78
|
+
return this.agent.cancel(this);
|
|
79
|
+
}
|
|
80
|
+
/** Cancels the request gracefully when `signal` aborts. The outcome is still reported by {@link completion}. */
|
|
81
|
+
cancelOn(signal) {
|
|
82
|
+
if (signal.aborted) {
|
|
83
|
+
this.cancel();
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
const abort = () => this.cancel();
|
|
87
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
88
|
+
void this.completion.then(() => signal.removeEventListener('abort', abort));
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/** @internal */
|
|
92
|
+
setSpeech(progress, audio, durationMs, note) {
|
|
93
|
+
this.speechValue = progress;
|
|
94
|
+
if (audio !== null)
|
|
95
|
+
this.speechAudio = audio;
|
|
96
|
+
if (durationMs !== null)
|
|
97
|
+
this.speechMs = durationMs;
|
|
98
|
+
if (note !== null)
|
|
99
|
+
this.speechNoteValue = note;
|
|
100
|
+
}
|
|
101
|
+
/** @internal */
|
|
102
|
+
update(record) {
|
|
103
|
+
if (this.endedAt !== null)
|
|
104
|
+
return;
|
|
105
|
+
this.id = record.id;
|
|
106
|
+
this.statusValue = record.status;
|
|
107
|
+
this.reasonValue = record.reason;
|
|
108
|
+
this.detailValue = record.detail;
|
|
109
|
+
this.failureCodeValue = record.failureCode;
|
|
110
|
+
this.startedAt = record.startedAtMs;
|
|
111
|
+
let start = record.status === 'running' ? true : null;
|
|
112
|
+
if (record.status === 'completed' || record.status === 'cancelled' || record.status === 'failed') {
|
|
113
|
+
this.endedAt = record.endedAtMs ?? record.queuedAtMs;
|
|
114
|
+
start ??= this.startedAt !== null;
|
|
115
|
+
const result = {
|
|
116
|
+
requestId: this.id, kind: this.kind, name: this.name, status: this.statusValue, reason: this.reasonValue, detail: this.detailValue,
|
|
117
|
+
failureCode: this.failureCodeValue, startedAtMs: this.startedAt, endedAtMs: this.endedAt,
|
|
118
|
+
};
|
|
119
|
+
this.settleStarted(start);
|
|
120
|
+
this.resolveCompletion(result);
|
|
121
|
+
}
|
|
122
|
+
else if (start !== null)
|
|
123
|
+
this.settleStarted(start);
|
|
124
|
+
}
|
|
125
|
+
settleStarted(value) {
|
|
126
|
+
if (this.startedSettled)
|
|
127
|
+
return;
|
|
128
|
+
this.startedSettled = true;
|
|
129
|
+
this.resolveStarted(value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type RgbaImage } from '../acs/image.js';
|
|
2
|
+
import type { CharacterSource } from './source.js';
|
|
3
|
+
/** Counters of one cache (a snapshot). */
|
|
4
|
+
export interface CacheStatistics {
|
|
5
|
+
readonly bytes: number;
|
|
6
|
+
readonly peakBytes: number;
|
|
7
|
+
readonly budgetBytes: number;
|
|
8
|
+
readonly entries: number;
|
|
9
|
+
readonly hits: number;
|
|
10
|
+
readonly misses: number;
|
|
11
|
+
readonly evictions: number;
|
|
12
|
+
readonly bypassed: number;
|
|
13
|
+
}
|
|
14
|
+
/** Byte budgets of one {@link CharacterResources}. */
|
|
15
|
+
export interface ResourceBudget {
|
|
16
|
+
/** Decoded indexed images kept (one byte per pixel). Default 64 MiB. */
|
|
17
|
+
readonly imageCacheBytes?: number;
|
|
18
|
+
/** Composed RGBA frames kept. Default 16 MiB. */
|
|
19
|
+
readonly frameCacheBytes?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Resource counters (a snapshot). */
|
|
22
|
+
export interface ResourceStatistics {
|
|
23
|
+
readonly images: CacheStatistics;
|
|
24
|
+
readonly frames: CacheStatistics;
|
|
25
|
+
readonly imageDecodes: number;
|
|
26
|
+
readonly compositions: number;
|
|
27
|
+
readonly mouthCompositions: number;
|
|
28
|
+
readonly animationChecks: number;
|
|
29
|
+
readonly generation: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Bounded resources of one character source: decoded indexed images and composed RGBA frames are produced on demand and kept in
|
|
33
|
+
* byte-bounded LRU caches. Nothing is decoded or composed when it is created. Identical layer sets (in any animation) share one composed
|
|
34
|
+
* frame. {@link invalidate} drops everything deterministically; {@link dispose} releases the caches for good.
|
|
35
|
+
*/
|
|
36
|
+
export declare class CharacterResources {
|
|
37
|
+
readonly source: CharacterSource;
|
|
38
|
+
private readonly images;
|
|
39
|
+
private readonly frames;
|
|
40
|
+
private readonly undecodable;
|
|
41
|
+
private readonly checks;
|
|
42
|
+
private generation;
|
|
43
|
+
private decodes;
|
|
44
|
+
private compositions;
|
|
45
|
+
private mouthCompositions;
|
|
46
|
+
private animationChecks;
|
|
47
|
+
private disposed;
|
|
48
|
+
constructor(source: CharacterSource, budget?: ResourceBudget);
|
|
49
|
+
get statistics(): ResourceStatistics;
|
|
50
|
+
/** Drops every cached image, frame and animation check and starts a new generation. */
|
|
51
|
+
invalidate(): void;
|
|
52
|
+
/** Releases every cached resource. Later calls still work, but nothing is cached any more. */
|
|
53
|
+
dispose(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Checks that every image named by a frame layer of `animation` can be prepared (Runtime.md §4.2 scope of `resource`): an undecodable
|
|
56
|
+
* image is fine; any other image error, or more than 1,024 layers on a frame, throws. Memoized; decoded images warm the cache.
|
|
57
|
+
*/
|
|
58
|
+
checkAnimation(animation: number): void;
|
|
59
|
+
/** The frame's own layers composed (a frame without layers is a transparent canvas). Throws for layer images that cannot be read. */
|
|
60
|
+
composeFrame(animation: number, frame: number): RgbaImage;
|
|
61
|
+
/** The frame with one mouth overlay (the mouth rule's refusals throw). */
|
|
62
|
+
composeMouthFrame(animation: number, frame: number, shape: number): RgbaImage;
|
|
63
|
+
/** The content key of a frame (layer images and offsets, plus the overlay); equal keys compose to equal pixels. */
|
|
64
|
+
frameKey(animation: number, frame: number, shape?: number | null): string;
|
|
65
|
+
private frameOf;
|
|
66
|
+
private key;
|
|
67
|
+
private compose;
|
|
68
|
+
/** The decoded image, or null when it is undecodable; throws for every other image error. */
|
|
69
|
+
private image;
|
|
70
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { AcsFormatError, InvalidArgumentError, NotSupportedError } from '../acs/errors.js';
|
|
2
|
+
import { MAX_LAYERS_PER_FRAME } from '../acs/image.js';
|
|
3
|
+
import { checkMouth } from '../acs/mouth.js';
|
|
4
|
+
import { composeFrame, composeMouthFrame } from './compositor.js';
|
|
5
|
+
/** @internal A least-recently-used cache bounded by accounted bytes. An entry larger than the budget is not stored. */
|
|
6
|
+
export class ByteBudgetCache {
|
|
7
|
+
budgetBytes;
|
|
8
|
+
map = new Map();
|
|
9
|
+
bytes = 0;
|
|
10
|
+
peak = 0;
|
|
11
|
+
hits = 0;
|
|
12
|
+
misses = 0;
|
|
13
|
+
evictions = 0;
|
|
14
|
+
bypassed = 0;
|
|
15
|
+
constructor(budgetBytes) {
|
|
16
|
+
this.budgetBytes = budgetBytes;
|
|
17
|
+
if (!(budgetBytes >= 0))
|
|
18
|
+
throw new RangeError('A cache budget is non-negative.');
|
|
19
|
+
}
|
|
20
|
+
get size() {
|
|
21
|
+
return this.map.size;
|
|
22
|
+
}
|
|
23
|
+
get(key) {
|
|
24
|
+
const entry = this.map.get(key);
|
|
25
|
+
if (!entry) {
|
|
26
|
+
this.misses++;
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
this.hits++;
|
|
30
|
+
this.map.delete(key);
|
|
31
|
+
this.map.set(key, entry);
|
|
32
|
+
return entry.value;
|
|
33
|
+
}
|
|
34
|
+
add(key, value, cost) {
|
|
35
|
+
const existing = this.map.get(key);
|
|
36
|
+
if (existing) {
|
|
37
|
+
this.map.delete(key);
|
|
38
|
+
this.bytes -= existing.cost;
|
|
39
|
+
}
|
|
40
|
+
if (cost > this.budgetBytes) {
|
|
41
|
+
this.bypassed++;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
for (const [k, v] of this.map) {
|
|
45
|
+
if (this.bytes + cost <= this.budgetBytes)
|
|
46
|
+
break;
|
|
47
|
+
this.map.delete(k);
|
|
48
|
+
this.bytes -= v.cost;
|
|
49
|
+
this.evictions++;
|
|
50
|
+
}
|
|
51
|
+
this.map.set(key, { value, cost });
|
|
52
|
+
this.bytes += cost;
|
|
53
|
+
if (this.bytes > this.peak)
|
|
54
|
+
this.peak = this.bytes;
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
clear() {
|
|
58
|
+
this.map.clear();
|
|
59
|
+
this.bytes = 0;
|
|
60
|
+
}
|
|
61
|
+
get statistics() {
|
|
62
|
+
return { bytes: this.bytes, peakBytes: this.peak, budgetBytes: this.budgetBytes, entries: this.map.size, hits: this.hits, misses: this.misses, evictions: this.evictions, bypassed: this.bypassed };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Bounded resources of one character source: decoded indexed images and composed RGBA frames are produced on demand and kept in
|
|
67
|
+
* byte-bounded LRU caches. Nothing is decoded or composed when it is created. Identical layer sets (in any animation) share one composed
|
|
68
|
+
* frame. {@link invalidate} drops everything deterministically; {@link dispose} releases the caches for good.
|
|
69
|
+
*/
|
|
70
|
+
export class CharacterResources {
|
|
71
|
+
source;
|
|
72
|
+
images;
|
|
73
|
+
frames;
|
|
74
|
+
undecodable = new Set();
|
|
75
|
+
checks = new Map();
|
|
76
|
+
generation = 1;
|
|
77
|
+
decodes = 0;
|
|
78
|
+
compositions = 0;
|
|
79
|
+
mouthCompositions = 0;
|
|
80
|
+
animationChecks = 0;
|
|
81
|
+
disposed = false;
|
|
82
|
+
constructor(source, budget = {}) {
|
|
83
|
+
this.source = source;
|
|
84
|
+
this.images = new ByteBudgetCache(budget.imageCacheBytes ?? 64 * 1024 * 1024);
|
|
85
|
+
this.frames = new ByteBudgetCache(budget.frameCacheBytes ?? 16 * 1024 * 1024);
|
|
86
|
+
}
|
|
87
|
+
get statistics() {
|
|
88
|
+
return {
|
|
89
|
+
images: this.images.statistics, frames: this.frames.statistics, imageDecodes: this.decodes, compositions: this.compositions,
|
|
90
|
+
mouthCompositions: this.mouthCompositions, animationChecks: this.animationChecks, generation: this.generation,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** Drops every cached image, frame and animation check and starts a new generation. */
|
|
94
|
+
invalidate() {
|
|
95
|
+
this.generation++;
|
|
96
|
+
this.images.clear();
|
|
97
|
+
this.frames.clear();
|
|
98
|
+
this.undecodable.clear();
|
|
99
|
+
this.checks.clear();
|
|
100
|
+
}
|
|
101
|
+
/** Releases every cached resource. Later calls still work, but nothing is cached any more. */
|
|
102
|
+
dispose() {
|
|
103
|
+
this.disposed = true;
|
|
104
|
+
this.invalidate();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks that every image named by a frame layer of `animation` can be prepared (Runtime.md §4.2 scope of `resource`): an undecodable
|
|
108
|
+
* image is fine; any other image error, or more than 1,024 layers on a frame, throws. Memoized; decoded images warm the cache.
|
|
109
|
+
*/
|
|
110
|
+
checkAnimation(animation) {
|
|
111
|
+
const known = this.checks.get(animation);
|
|
112
|
+
if (known !== undefined) {
|
|
113
|
+
if (known)
|
|
114
|
+
throw known;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const generation = this.generation;
|
|
118
|
+
let error = null;
|
|
119
|
+
try {
|
|
120
|
+
const a = this.source.character.animations[animation];
|
|
121
|
+
if (!a)
|
|
122
|
+
throw new AcsFormatError('animation-index', 'Missing animation.');
|
|
123
|
+
const seen = new Set();
|
|
124
|
+
let composedAny = false;
|
|
125
|
+
for (const frame of a.frames) {
|
|
126
|
+
if (frame.layers.length > MAX_LAYERS_PER_FRAME)
|
|
127
|
+
throw new AcsFormatError('layer-limit', 'Too many frame layers.');
|
|
128
|
+
if (frame.layers.length === 0 && composedAny)
|
|
129
|
+
continue;
|
|
130
|
+
for (const layer of frame.layers) {
|
|
131
|
+
if (seen.has(layer.image))
|
|
132
|
+
continue;
|
|
133
|
+
seen.add(layer.image);
|
|
134
|
+
this.image(layer.image);
|
|
135
|
+
}
|
|
136
|
+
composedAny = true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
if (!(e instanceof AcsFormatError))
|
|
141
|
+
throw e;
|
|
142
|
+
error = e;
|
|
143
|
+
}
|
|
144
|
+
this.animationChecks++;
|
|
145
|
+
if (generation === this.generation && !this.disposed)
|
|
146
|
+
this.checks.set(animation, error);
|
|
147
|
+
if (error)
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
/** The frame's own layers composed (a frame without layers is a transparent canvas). Throws for layer images that cannot be read. */
|
|
151
|
+
composeFrame(animation, frame) {
|
|
152
|
+
const f = this.frameOf(animation, frame);
|
|
153
|
+
return this.compose(this.key(f.layers, null), f.layers, null);
|
|
154
|
+
}
|
|
155
|
+
/** The frame with one mouth overlay (the mouth rule's refusals throw). */
|
|
156
|
+
composeMouthFrame(animation, frame, shape) {
|
|
157
|
+
const f = this.frameOf(animation, frame);
|
|
158
|
+
const mouth = selectMouth(f.layers.length, f.mouths, shape);
|
|
159
|
+
return this.compose(this.key(f.layers, mouth), f.layers, mouth);
|
|
160
|
+
}
|
|
161
|
+
/** The content key of a frame (layer images and offsets, plus the overlay); equal keys compose to equal pixels. */
|
|
162
|
+
frameKey(animation, frame, shape = null) {
|
|
163
|
+
const f = this.frameOf(animation, frame);
|
|
164
|
+
return this.key(f.layers, shape === null ? null : selectMouth(f.layers.length, f.mouths, shape));
|
|
165
|
+
}
|
|
166
|
+
frameOf(animation, frame) {
|
|
167
|
+
const a = this.source.character.animations[animation];
|
|
168
|
+
const f = a?.frames[frame];
|
|
169
|
+
if (!f)
|
|
170
|
+
throw new AcsFormatError('frame-index', 'Missing frame.');
|
|
171
|
+
return f;
|
|
172
|
+
}
|
|
173
|
+
key(layers, mouth) {
|
|
174
|
+
let key = '';
|
|
175
|
+
for (const l of layers)
|
|
176
|
+
key += `${l.image},${l.x},${l.y};`;
|
|
177
|
+
if (mouth)
|
|
178
|
+
key += `|${mouth.image & 0xffff},${mouth.x},${mouth.y},${mouth.replace === 1 ? 1 : 0}`;
|
|
179
|
+
return key;
|
|
180
|
+
}
|
|
181
|
+
compose(key, layers, mouth) {
|
|
182
|
+
const hit = this.frames.get(key);
|
|
183
|
+
if (hit)
|
|
184
|
+
return hit;
|
|
185
|
+
const generation = this.generation;
|
|
186
|
+
const c = this.source.character;
|
|
187
|
+
const indexed = layers.map((l) => {
|
|
188
|
+
const image = this.image(l.image);
|
|
189
|
+
return image ? { image, x: l.x, y: l.y } : null;
|
|
190
|
+
});
|
|
191
|
+
let bitmap;
|
|
192
|
+
if (!mouth) {
|
|
193
|
+
bitmap = composeFrame(c.width, c.height, indexed, c.palette, c.transparentIndex);
|
|
194
|
+
this.compositions++;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const image = this.image(mouth.image & 0xffff);
|
|
198
|
+
bitmap = composeMouthFrame(c.width, c.height, indexed, image ? { image, x: mouth.x, y: mouth.y } : null, mouth.replace === 1, c.palette, c.transparentIndex);
|
|
199
|
+
this.mouthCompositions++;
|
|
200
|
+
}
|
|
201
|
+
if (generation === this.generation && !this.disposed)
|
|
202
|
+
this.frames.add(key, bitmap, bitmap.pixels.length);
|
|
203
|
+
return bitmap;
|
|
204
|
+
}
|
|
205
|
+
/** The decoded image, or null when it is undecodable; throws for every other image error. */
|
|
206
|
+
image(index) {
|
|
207
|
+
if (this.undecodable.has(index))
|
|
208
|
+
return null;
|
|
209
|
+
const cached = this.images.get(index);
|
|
210
|
+
if (cached)
|
|
211
|
+
return cached;
|
|
212
|
+
let image;
|
|
213
|
+
try {
|
|
214
|
+
image = this.source.document.readImage(index);
|
|
215
|
+
this.decodes++;
|
|
216
|
+
}
|
|
217
|
+
catch (e) {
|
|
218
|
+
this.decodes++;
|
|
219
|
+
if (e instanceof AcsFormatError && e.isUndecodableImage) {
|
|
220
|
+
if (!this.disposed)
|
|
221
|
+
this.undecodable.add(index);
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
throw e;
|
|
225
|
+
}
|
|
226
|
+
if (!this.disposed)
|
|
227
|
+
this.images.add(index, image, image.byteLength);
|
|
228
|
+
return image;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** The mouth record a frame draws for `shape`, with the composition path's refusals. @internal */
|
|
232
|
+
export function selectMouth(layerCount, mouths, shape) {
|
|
233
|
+
if (!Number.isInteger(shape) || shape < 0 || shape > 6)
|
|
234
|
+
throw new InvalidArgumentError('Mouth shape is outside 0..6.');
|
|
235
|
+
if (layerCount === 0)
|
|
236
|
+
throw new NotSupportedError('Mouth preview needs a frame with at least one image layer.');
|
|
237
|
+
const check = checkMouth(mouths, shape, layerCount);
|
|
238
|
+
if (check.tooManyRecords || check.matches !== 1)
|
|
239
|
+
throw new NotSupportedError('Missing, duplicate or excessive mouth shape records.');
|
|
240
|
+
if (!check.canDraw)
|
|
241
|
+
throw new NotSupportedError('Mouth record has unsupported flags.');
|
|
242
|
+
return check.record;
|
|
243
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** The state of a request. */
|
|
2
|
+
export type RequestStatus = 'queued' | 'running' | 'completed' | 'cancelled' | 'failed';
|
|
3
|
+
/** Why a request reached its terminal state (`none` while it is not terminal). */
|
|
4
|
+
export type RequestEndReason = 'none' | 'completed' | 'cancelled-by-caller' | 'interrupted' | 'stopped' | 'disposed' | 'failed';
|
|
5
|
+
/** The kinds of runtime requests. */
|
|
6
|
+
export type RequestKind = 'play' | 'state' | 'show' | 'hide' | 'move' | 'gesture' | 'speak' | 'think' | 'wait' | 'idle';
|
|
7
|
+
/** Every request kind. */
|
|
8
|
+
export declare const ALL_REQUEST_KINDS: readonly RequestKind[];
|
|
9
|
+
/** The kinds `stopAll()` stops by default: everything except visibility transitions. */
|
|
10
|
+
export declare const STOP_ALL_DEFAULT_KINDS: readonly RequestKind[];
|
|
11
|
+
/** Portable failure codes of requests (Runtime.md §4.2). */
|
|
12
|
+
export declare const RequestFailureCode: {
|
|
13
|
+
readonly unknownAnimation: "unknown-animation";
|
|
14
|
+
readonly unavailableAnimation: "unavailable-animation";
|
|
15
|
+
readonly invalidAnimation: "invalid-animation";
|
|
16
|
+
readonly unknownState: "unknown-state";
|
|
17
|
+
readonly stateUnusable: "state-unusable";
|
|
18
|
+
readonly resource: "resource";
|
|
19
|
+
readonly queueLimit: "queue-limit";
|
|
20
|
+
readonly updateLimit: "update-limit";
|
|
21
|
+
readonly transitionLimit: "transition-limit";
|
|
22
|
+
readonly effectDuration: "effect-duration";
|
|
23
|
+
readonly randomSample: "random-sample";
|
|
24
|
+
readonly refused: "refused";
|
|
25
|
+
};
|