@threenative/playtest 0.1.0
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/README.md +51 -0
- package/dist/diagnostics-Ckm_5lrw.d.ts +549 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +3057 -0
- package/dist/protocol-CeC1lz_G.d.ts +130 -0
- package/dist/runner/cli.d.ts +20 -0
- package/dist/runner/cli.js +5219 -0
- package/dist/runner/index.d.ts +390 -0
- package/dist/runner/index.js +5081 -0
- package/dist/three/index.d.ts +65 -0
- package/dist/three/index.js +394 -0
- package/package.json +63 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { u as PlaytestClockMode, r as JsonValue, l as IPlaytestObservationSnapshot, m as IPlaytestSampleRequest, e as IPlaytestBridgeV1, k as IPlaytestGameplayObservation } from '../protocol-CeC1lz_G.js';
|
|
2
|
+
import { Object3D, Camera, Vector2, Scene } from 'three';
|
|
3
|
+
|
|
4
|
+
interface IThreePlaytestEntity {
|
|
5
|
+
id: string;
|
|
6
|
+
object: Object3D;
|
|
7
|
+
path?: string;
|
|
8
|
+
}
|
|
9
|
+
declare class ThreePlaytestEntityRegistry {
|
|
10
|
+
#private;
|
|
11
|
+
register(entry: IThreePlaytestEntity): void;
|
|
12
|
+
get(id: string): Required<IThreePlaytestEntity> | undefined;
|
|
13
|
+
replace(entries: readonly IThreePlaytestEntity[]): void;
|
|
14
|
+
select(ids?: readonly string[]): Required<IThreePlaytestEntity>[];
|
|
15
|
+
}
|
|
16
|
+
declare function objectPath(object: Object3D): string;
|
|
17
|
+
|
|
18
|
+
interface IThreeObservationInput {
|
|
19
|
+
camera: Camera;
|
|
20
|
+
clockMode: PlaytestClockMode;
|
|
21
|
+
diagnostics?: () => JsonValue[];
|
|
22
|
+
registry: ThreePlaytestEntityRegistry;
|
|
23
|
+
renderer: ThreePlaytestRenderer;
|
|
24
|
+
resources?: () => Record<string, JsonValue>;
|
|
25
|
+
scene: Scene;
|
|
26
|
+
gameplay?: () => IPlaytestObservationSnapshot["gameplay"];
|
|
27
|
+
tick?: number;
|
|
28
|
+
}
|
|
29
|
+
interface ThreePlaytestRenderer {
|
|
30
|
+
getDrawingBufferSize(target: Vector2): Vector2;
|
|
31
|
+
}
|
|
32
|
+
declare function sampleThreeObservations(input: IThreeObservationInput, request: IPlaytestSampleRequest): IPlaytestObservationSnapshot;
|
|
33
|
+
|
|
34
|
+
interface IThreePlaytestResources {
|
|
35
|
+
read(): Record<string, JsonValue>;
|
|
36
|
+
write?(id: string, path: string | undefined, value: JsonValue): boolean;
|
|
37
|
+
}
|
|
38
|
+
interface IThreePlaytestBridgeOptions {
|
|
39
|
+
camera: Camera;
|
|
40
|
+
components?: () => Record<string, Record<string, JsonValue>>;
|
|
41
|
+
diagnostics?: () => JsonValue[];
|
|
42
|
+
entities?: readonly IThreePlaytestEntity[] | (() => readonly IThreePlaytestEntity[]);
|
|
43
|
+
fixedStep?: (ticks: number) => Promise<void> | void;
|
|
44
|
+
gameplay?: () => IPlaytestGameplayObservation;
|
|
45
|
+
gameplayChannels?: () => readonly ("runtime.contacts" | "runtime.tags")[];
|
|
46
|
+
events?: () => JsonValue[];
|
|
47
|
+
renderer: ThreePlaytestRenderer;
|
|
48
|
+
resources?: IThreePlaytestResources;
|
|
49
|
+
scene: Scene;
|
|
50
|
+
}
|
|
51
|
+
interface IThreePlaytestBridgeInstallation {
|
|
52
|
+
bridge: IPlaytestBridgeV1;
|
|
53
|
+
dispose(): void;
|
|
54
|
+
registerEntity(entry: IThreePlaytestEntity): void;
|
|
55
|
+
syncEntities(): void;
|
|
56
|
+
}
|
|
57
|
+
declare function installThreePlaytestBridge(options: IThreePlaytestBridgeOptions): IThreePlaytestBridgeInstallation;
|
|
58
|
+
|
|
59
|
+
interface IDeviceBridgeInstallation {
|
|
60
|
+
close(): void;
|
|
61
|
+
}
|
|
62
|
+
declare function connectDevicePlaytestBridge(bridge: IPlaytestBridgeV1, endpoint: string): IDeviceBridgeInstallation;
|
|
63
|
+
declare function readPlaytestEndpoint(): string | undefined;
|
|
64
|
+
|
|
65
|
+
export { type IDeviceBridgeInstallation, type IThreeObservationInput, type IThreePlaytestBridgeInstallation, type IThreePlaytestBridgeOptions, type IThreePlaytestEntity, type IThreePlaytestResources, ThreePlaytestEntityRegistry, type ThreePlaytestRenderer, connectDevicePlaytestBridge, installThreePlaytestBridge, objectPath, readPlaytestEndpoint, sampleThreeObservations };
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { Vector2, Vector3, Box3, Matrix4, Frustum } from 'three';
|
|
2
|
+
|
|
3
|
+
// src/protocol.ts
|
|
4
|
+
var PLAYTEST_BRIDGE_GLOBAL = "__THREENATIVE_PLAYTEST_BRIDGE__";
|
|
5
|
+
var PLAYTEST_PROTOCOL_VERSION = 1;
|
|
6
|
+
var PLAYTEST_PROTOCOL_LIMITS = {
|
|
7
|
+
maxEntitiesPerSample: 100,
|
|
8
|
+
maxEventsPerDrain: 1e3,
|
|
9
|
+
maxPayloadBytes: 1e6,
|
|
10
|
+
operationTimeoutMs: 5e3
|
|
11
|
+
};
|
|
12
|
+
function jsonByteLength(value) {
|
|
13
|
+
return new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
14
|
+
}
|
|
15
|
+
function assertJsonSafe(value, path = "$") {
|
|
16
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (typeof value === "number") {
|
|
20
|
+
if (Number.isFinite(value)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
throw new TypeError(`${path} must contain only finite JSON numbers.`);
|
|
24
|
+
}
|
|
25
|
+
if (Array.isArray(value)) {
|
|
26
|
+
value.forEach((item, index) => assertJsonSafe(item, `${path}[${index}]`));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (typeof value === "object") {
|
|
30
|
+
for (const [key, item] of Object.entries(value)) {
|
|
31
|
+
assertJsonSafe(item, `${path}.${key}`);
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
throw new TypeError(`${path} must be JSON-safe.`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/three/entities.ts
|
|
39
|
+
var ThreePlaytestEntityRegistry = class {
|
|
40
|
+
#entries = /* @__PURE__ */ new Map();
|
|
41
|
+
register(entry) {
|
|
42
|
+
const path = entry.path ?? objectPath(entry.object);
|
|
43
|
+
const existing = this.#entries.get(entry.id);
|
|
44
|
+
if (existing !== void 0) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Duplicate playtest entity id '${entry.id}' conflicts between '${existing.path}' and '${path}'. Register a unique stable id for each observed object.`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
this.#entries.set(entry.id, { ...entry, path });
|
|
50
|
+
}
|
|
51
|
+
get(id) {
|
|
52
|
+
return this.#entries.get(id);
|
|
53
|
+
}
|
|
54
|
+
replace(entries) {
|
|
55
|
+
this.#entries.clear();
|
|
56
|
+
entries.forEach((entry) => this.register(entry));
|
|
57
|
+
}
|
|
58
|
+
select(ids) {
|
|
59
|
+
if (ids === void 0) return [...this.#entries.values()];
|
|
60
|
+
return ids.flatMap((id) => {
|
|
61
|
+
const entry = this.#entries.get(id);
|
|
62
|
+
return entry === void 0 ? [] : [entry];
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
function objectPath(object) {
|
|
67
|
+
const parts = [];
|
|
68
|
+
let current = object;
|
|
69
|
+
while (current !== null) {
|
|
70
|
+
parts.unshift(current.name || `${current.type}[${current.uuid.slice(0, 8)}]`);
|
|
71
|
+
current = current.parent;
|
|
72
|
+
}
|
|
73
|
+
return parts.join("/");
|
|
74
|
+
}
|
|
75
|
+
function sampleThreeObservations(input, request) {
|
|
76
|
+
input.scene.updateMatrixWorld(true);
|
|
77
|
+
input.camera.updateMatrixWorld(true);
|
|
78
|
+
const rendererSize = input.renderer.getDrawingBufferSize(new Vector2());
|
|
79
|
+
const entities = input.registry.select(request.entities).map(({ id, object }) => observeEntity(id, object, input.camera, rendererSize.x, rendererSize.y));
|
|
80
|
+
return {
|
|
81
|
+
clock: {
|
|
82
|
+
mode: input.clockMode,
|
|
83
|
+
...input.tick === void 0 ? { timeMs: performance.now() } : { tick: input.tick }
|
|
84
|
+
},
|
|
85
|
+
...input.diagnostics === void 0 ? {} : { diagnostics: input.diagnostics() },
|
|
86
|
+
entities,
|
|
87
|
+
...input.gameplay === void 0 ? {} : { gameplay: input.gameplay() },
|
|
88
|
+
...input.resources === void 0 ? {} : { resources: input.resources() }
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function observeEntity(id, object, camera, viewportWidth, viewportHeight) {
|
|
92
|
+
const position = object.getWorldPosition(new Vector3());
|
|
93
|
+
const bounds = projectedBounds(object, camera, viewportWidth, viewportHeight);
|
|
94
|
+
return {
|
|
95
|
+
...bounds === void 0 ? {} : { bounds },
|
|
96
|
+
id,
|
|
97
|
+
transform: {
|
|
98
|
+
position: [position.x, position.y, position.z],
|
|
99
|
+
rotation: [object.quaternion.x, object.quaternion.y, object.quaternion.z, object.quaternion.w],
|
|
100
|
+
scale: [object.scale.x, object.scale.y, object.scale.z]
|
|
101
|
+
},
|
|
102
|
+
visible: object.visible && bounds !== void 0 && bounds.width > 0 && bounds.height > 0
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function projectedBounds(object, camera, viewportWidth, viewportHeight) {
|
|
106
|
+
const worldBounds = new Box3().setFromObject(object);
|
|
107
|
+
if (worldBounds.isEmpty()) return void 0;
|
|
108
|
+
const projection = new Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
109
|
+
if (!new Frustum().setFromProjectionMatrix(projection).intersectsBox(worldBounds)) return void 0;
|
|
110
|
+
const min = worldBounds.min;
|
|
111
|
+
const max = worldBounds.max;
|
|
112
|
+
const points = [
|
|
113
|
+
new Vector3(min.x, min.y, min.z),
|
|
114
|
+
new Vector3(min.x, min.y, max.z),
|
|
115
|
+
new Vector3(min.x, max.y, min.z),
|
|
116
|
+
new Vector3(min.x, max.y, max.z),
|
|
117
|
+
new Vector3(max.x, min.y, min.z),
|
|
118
|
+
new Vector3(max.x, min.y, max.z),
|
|
119
|
+
new Vector3(max.x, max.y, min.z),
|
|
120
|
+
new Vector3(max.x, max.y, max.z)
|
|
121
|
+
].map((point) => point.project(camera));
|
|
122
|
+
const minX = Math.min(...points.map(({ x }) => x));
|
|
123
|
+
const maxX = Math.max(...points.map(({ x }) => x));
|
|
124
|
+
const minY = Math.min(...points.map(({ y }) => y));
|
|
125
|
+
const maxY = Math.max(...points.map(({ y }) => y));
|
|
126
|
+
return {
|
|
127
|
+
height: Math.max(0, (maxY - minY) * 0.5 * viewportHeight),
|
|
128
|
+
width: Math.max(0, (maxX - minX) * 0.5 * viewportWidth),
|
|
129
|
+
x: (minX + 1) * 0.5 * viewportWidth,
|
|
130
|
+
y: (1 - maxY) * 0.5 * viewportHeight
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/three/device.ts
|
|
135
|
+
var BRIDGE_METHODS = /* @__PURE__ */ new Set([
|
|
136
|
+
"advance",
|
|
137
|
+
"applySetup",
|
|
138
|
+
"describe",
|
|
139
|
+
"drainEvents",
|
|
140
|
+
"focus",
|
|
141
|
+
"ready",
|
|
142
|
+
"sample"
|
|
143
|
+
]);
|
|
144
|
+
function connectDevicePlaytestBridge(bridge, endpoint) {
|
|
145
|
+
const nativeInstallation = connectNativeMailbox(bridge);
|
|
146
|
+
if (nativeInstallation !== void 0) return nativeInstallation;
|
|
147
|
+
let closed = false;
|
|
148
|
+
let reportedError = false;
|
|
149
|
+
let scheduled;
|
|
150
|
+
const schedule = () => {
|
|
151
|
+
if (closed) return;
|
|
152
|
+
if (typeof globalThis.requestAnimationFrame === "function") {
|
|
153
|
+
scheduled = globalThis.requestAnimationFrame(() => void poll());
|
|
154
|
+
} else {
|
|
155
|
+
scheduled = setTimeout(() => void poll(), 50);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const poll = async () => {
|
|
159
|
+
if (closed) return;
|
|
160
|
+
try {
|
|
161
|
+
const response = await fetch(endpoint);
|
|
162
|
+
if (response.status !== 204) {
|
|
163
|
+
if (!response.ok) throw new Error(`Device bridge poll failed with HTTP ${response.status}.`);
|
|
164
|
+
const request = parseRequest(await response.json());
|
|
165
|
+
await postResponse(endpoint, await dispatch(bridge, request));
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (!reportedError) {
|
|
169
|
+
reportedError = true;
|
|
170
|
+
console.error(`TN_PLAYTEST_DEVICE_TRANSPORT: ${error instanceof Error ? error.message : String(error)}`);
|
|
171
|
+
}
|
|
172
|
+
} finally {
|
|
173
|
+
schedule();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
schedule();
|
|
177
|
+
return {
|
|
178
|
+
close: () => {
|
|
179
|
+
closed = true;
|
|
180
|
+
if (scheduled === void 0) return;
|
|
181
|
+
if (typeof globalThis.cancelAnimationFrame === "function" && typeof scheduled === "number") {
|
|
182
|
+
globalThis.cancelAnimationFrame(scheduled);
|
|
183
|
+
} else clearTimeout(scheduled);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function connectNativeMailbox(bridge) {
|
|
188
|
+
const globals = globalThis;
|
|
189
|
+
const mailbox = globals.TN_PLAYTEST_MAILBOX;
|
|
190
|
+
const host = globals.__THREENATIVE_NATIVE__?.playtest;
|
|
191
|
+
if (typeof mailbox?.request !== "string" || typeof mailbox.response !== "string" || typeof host?.receive !== "function" || typeof host.respond !== "function") return void 0;
|
|
192
|
+
let closed = false;
|
|
193
|
+
let frame;
|
|
194
|
+
const respond = (response) => {
|
|
195
|
+
if (!host.respond(mailbox.response, JSON.stringify(response))) {
|
|
196
|
+
console.error("TN_PLAYTEST_DEVICE_TRANSPORT: native mailbox response failed");
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const poll = () => {
|
|
200
|
+
if (closed) return;
|
|
201
|
+
const raw = host.receive(mailbox.request);
|
|
202
|
+
if (raw !== void 0) {
|
|
203
|
+
try {
|
|
204
|
+
const request = parseRequest(JSON.parse(raw));
|
|
205
|
+
void dispatch(bridge, request).then(respond);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
respond({
|
|
208
|
+
error: { message: error instanceof Error ? error.message : String(error) },
|
|
209
|
+
id: "invalid"
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
frame = globalThis.requestAnimationFrame(poll);
|
|
214
|
+
};
|
|
215
|
+
respond({ id: "ready", result: null });
|
|
216
|
+
frame = globalThis.requestAnimationFrame(poll);
|
|
217
|
+
return {
|
|
218
|
+
close: () => {
|
|
219
|
+
closed = true;
|
|
220
|
+
if (frame !== void 0) globalThis.cancelAnimationFrame(frame);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function readPlaytestEndpoint() {
|
|
225
|
+
const value = globalThis.TN_PLAYTEST_ENDPOINT;
|
|
226
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
227
|
+
}
|
|
228
|
+
async function dispatch(bridge, request) {
|
|
229
|
+
try {
|
|
230
|
+
if (request.method.startsWith("input.")) {
|
|
231
|
+
return { id: request.id, result: dispatchInput(request.method, request.argument) };
|
|
232
|
+
}
|
|
233
|
+
if (!BRIDGE_METHODS.has(request.method)) {
|
|
234
|
+
throw new Error(`Bridge operation '${request.method}' is unavailable.`);
|
|
235
|
+
}
|
|
236
|
+
const operation = bridge[request.method];
|
|
237
|
+
if (typeof operation !== "function") throw new Error(`Bridge operation '${request.method}' is unavailable.`);
|
|
238
|
+
const result = await operation.call(bridge, request.argument);
|
|
239
|
+
if (result !== void 0) assertBounded(result);
|
|
240
|
+
return { id: request.id, ...result === void 0 ? {} : { result } };
|
|
241
|
+
} catch (error) {
|
|
242
|
+
return { error: { message: error instanceof Error ? error.message : String(error) }, id: request.id };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function dispatchInput(method, argument) {
|
|
246
|
+
const host = globalThis.__THREENATIVE_NATIVE__?.playtestInput;
|
|
247
|
+
if (!isRecord(argument)) throw new Error(`Device ${method} requires an object argument.`);
|
|
248
|
+
if (method === "input.keyDown" || method === "input.keyUp") {
|
|
249
|
+
if (typeof argument.key !== "string") throw new Error(`Device ${method} requires a key.`);
|
|
250
|
+
const code = argument.key;
|
|
251
|
+
const key = /^Key[A-Z]$/u.test(code) ? code.slice(3).toLowerCase() : code;
|
|
252
|
+
if (typeof host?.keyboard !== "function") throw new Error("Native playtest keyboard input is unavailable.");
|
|
253
|
+
host.keyboard(method === "input.keyDown" ? "keydown" : "keyup", key, code);
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
if (method === "input.pointer") {
|
|
257
|
+
if (typeof argument.x !== "number" || typeof argument.y !== "number" || typeof argument.buttons !== "number") {
|
|
258
|
+
throw new Error("Device input.pointer requires numeric x, y, and buttons.");
|
|
259
|
+
}
|
|
260
|
+
if (typeof host?.pointer !== "function") throw new Error("Native playtest pointer input is unavailable.");
|
|
261
|
+
const type = argument.type === "down" ? "pointerdown" : argument.type === "up" ? "pointerup" : "pointermove";
|
|
262
|
+
host.pointer(type, argument.x, argument.y, argument.buttons);
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
throw new Error(`Device input operation '${method}' is unavailable.`);
|
|
266
|
+
}
|
|
267
|
+
function parseRequest(value) {
|
|
268
|
+
assertBounded(value);
|
|
269
|
+
if (!isRecord(value) || typeof value.id !== "string" || typeof value.method !== "string") {
|
|
270
|
+
throw new Error("Device request must contain string id and method fields.");
|
|
271
|
+
}
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
async function postResponse(endpoint, response) {
|
|
275
|
+
const result = await fetch(endpoint, {
|
|
276
|
+
body: JSON.stringify(response),
|
|
277
|
+
headers: { "content-type": "application/json" },
|
|
278
|
+
method: "POST"
|
|
279
|
+
});
|
|
280
|
+
if (!result.ok) throw new Error(`Device bridge response failed with HTTP ${result.status}.`);
|
|
281
|
+
}
|
|
282
|
+
function assertBounded(value) {
|
|
283
|
+
assertJsonSafe(value);
|
|
284
|
+
if (jsonByteLength(value) > PLAYTEST_PROTOCOL_LIMITS.maxPayloadBytes) {
|
|
285
|
+
throw new Error(`Device bridge payload exceeds ${PLAYTEST_PROTOCOL_LIMITS.maxPayloadBytes} bytes.`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function isRecord(value) {
|
|
289
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/three/bridge.ts
|
|
293
|
+
function installThreePlaytestBridge(options) {
|
|
294
|
+
const host = globalThis;
|
|
295
|
+
const previous = host[PLAYTEST_BRIDGE_GLOBAL];
|
|
296
|
+
const registry = new ThreePlaytestEntityRegistry();
|
|
297
|
+
syncEntities(registry, options.entities);
|
|
298
|
+
let tick = 0;
|
|
299
|
+
const capabilities = () => [
|
|
300
|
+
"camera.observe",
|
|
301
|
+
"entity.bounds",
|
|
302
|
+
"entity.observe",
|
|
303
|
+
"entity.setup",
|
|
304
|
+
...options.fixedStep === void 0 ? [] : ["runtime.fixedStep"],
|
|
305
|
+
...options.resources === void 0 ? [] : ["runtime.resources"],
|
|
306
|
+
...options.diagnostics === void 0 ? [] : ["runtime.diagnostics"],
|
|
307
|
+
...Object.keys(options.components?.() ?? {}).length === 0 ? [] : ["runtime.components"],
|
|
308
|
+
...options.events === void 0 ? [] : ["runtime.events"],
|
|
309
|
+
...options.gameplay === void 0 ? [] : ["runtime.animation", "runtime.state"],
|
|
310
|
+
...options.gameplayChannels?.().includes("runtime.contacts") === true ? ["runtime.contacts"] : [],
|
|
311
|
+
...options.gameplayChannels?.().includes("runtime.tags") === true ? ["runtime.tags"] : []
|
|
312
|
+
];
|
|
313
|
+
const bridge = {
|
|
314
|
+
...options.fixedStep === void 0 ? {} : {
|
|
315
|
+
advance: async (ticks) => {
|
|
316
|
+
if (!Number.isInteger(ticks) || ticks <= 0) throw new Error("advance ticks must be a positive integer.");
|
|
317
|
+
await options.fixedStep(ticks);
|
|
318
|
+
const startTick = tick;
|
|
319
|
+
tick += ticks;
|
|
320
|
+
return { clock: { mode: "fixed-step", tick }, ticks: tick - startTick };
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
applySetup: async (request) => applySetup(registry, options.resources, request),
|
|
324
|
+
...options.events === void 0 ? {} : {
|
|
325
|
+
drainEvents: async (limit = PLAYTEST_PROTOCOL_LIMITS.maxEventsPerDrain) => {
|
|
326
|
+
if (!Number.isInteger(limit) || limit < 0) throw new Error("drainEvents limit must be a non-negative integer.");
|
|
327
|
+
const events = options.events();
|
|
328
|
+
assertJsonSafe(events);
|
|
329
|
+
return events.slice(0, Math.min(limit, PLAYTEST_PROTOCOL_LIMITS.maxEventsPerDrain));
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
describe: () => ({
|
|
333
|
+
capabilities: capabilities(),
|
|
334
|
+
limits: PLAYTEST_PROTOCOL_LIMITS,
|
|
335
|
+
name: "@threenative/playtest/three",
|
|
336
|
+
protocolVersion: PLAYTEST_PROTOCOL_VERSION
|
|
337
|
+
}),
|
|
338
|
+
ready: () => ({ ready: true }),
|
|
339
|
+
sample: (request) => {
|
|
340
|
+
syncEntities(registry, options.entities);
|
|
341
|
+
const snapshot = sampleThreeObservations({
|
|
342
|
+
camera: options.camera,
|
|
343
|
+
clockMode: options.fixedStep === void 0 ? "render-frame" : "fixed-step",
|
|
344
|
+
diagnostics: options.diagnostics,
|
|
345
|
+
gameplay: options.gameplay,
|
|
346
|
+
registry,
|
|
347
|
+
renderer: options.renderer,
|
|
348
|
+
resources: options.resources === void 0 ? void 0 : () => options.resources.read(),
|
|
349
|
+
scene: options.scene,
|
|
350
|
+
tick: options.fixedStep === void 0 ? void 0 : tick
|
|
351
|
+
}, request);
|
|
352
|
+
const components = options.components?.();
|
|
353
|
+
const result = components === void 0 || Object.keys(components).length === 0 ? snapshot : { ...snapshot, components };
|
|
354
|
+
assertJsonSafe(result);
|
|
355
|
+
return result;
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
host[PLAYTEST_BRIDGE_GLOBAL] = bridge;
|
|
359
|
+
const endpoint = readPlaytestEndpoint();
|
|
360
|
+
let device;
|
|
361
|
+
if (endpoint !== void 0) device = connectDevicePlaytestBridge(bridge, endpoint);
|
|
362
|
+
return {
|
|
363
|
+
bridge,
|
|
364
|
+
dispose: () => {
|
|
365
|
+
device?.close();
|
|
366
|
+
if (host[PLAYTEST_BRIDGE_GLOBAL] !== bridge) return;
|
|
367
|
+
if (previous === void 0) delete host[PLAYTEST_BRIDGE_GLOBAL];
|
|
368
|
+
else host[PLAYTEST_BRIDGE_GLOBAL] = previous;
|
|
369
|
+
},
|
|
370
|
+
registerEntity: (entry) => registry.register(entry),
|
|
371
|
+
syncEntities: () => syncEntities(registry, options.entities)
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function syncEntities(registry, entities) {
|
|
375
|
+
if (entities === void 0) return;
|
|
376
|
+
registry.replace(typeof entities === "function" ? entities() : entities);
|
|
377
|
+
}
|
|
378
|
+
function applySetup(registry, resources, request) {
|
|
379
|
+
request.entities?.forEach(({ entity, transform }) => {
|
|
380
|
+
const object = registry.get(entity)?.object;
|
|
381
|
+
if (object === void 0) throw new Error(`Setup entity '${entity}' is not registered.`);
|
|
382
|
+
if (transform.position !== void 0) object.position.fromArray(transform.position);
|
|
383
|
+
if (transform.rotation !== void 0) object.quaternion.fromArray(transform.rotation);
|
|
384
|
+
if (transform.scale !== void 0) object.scale.fromArray(transform.scale);
|
|
385
|
+
object.updateMatrix();
|
|
386
|
+
});
|
|
387
|
+
request.resources?.forEach(({ id, path, value }) => {
|
|
388
|
+
assertJsonSafe(value);
|
|
389
|
+
if (resources?.write === void 0) throw new Error(`Resource setup '${id}' requires a writable resources provider.`);
|
|
390
|
+
if (!resources.write(id, path, value)) throw new Error(`Resource setup '${id}' was rejected by the application provider.`);
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export { ThreePlaytestEntityRegistry, connectDevicePlaytestBridge, installThreePlaytestBridge, objectPath, readPlaytestEndpoint, sampleThreeObservations };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@threenative/playtest",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Scenario-driven playtest harness for Three.js games. Runs on plain Three.js with zero ThreeNative dependencies.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./three": {
|
|
18
|
+
"types": "./dist/three/index.d.ts",
|
|
19
|
+
"import": "./dist/three/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./runner": {
|
|
22
|
+
"types": "./dist/runner/index.d.ts",
|
|
23
|
+
"import": "./dist/runner/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"bin": {
|
|
28
|
+
"threenative-playtest": "./dist/runner/cli.js"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"pngjs": "7.0.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"playwright": ">=1.60.0 <2",
|
|
39
|
+
"three": ">=0.185.0 <0.186.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"playwright": {
|
|
43
|
+
"optional": true
|
|
44
|
+
},
|
|
45
|
+
"three": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/pngjs": "6.0.5",
|
|
51
|
+
"@types/three": "0.185.3",
|
|
52
|
+
"playwright": "1.62.1",
|
|
53
|
+
"publint": "0.3.22",
|
|
54
|
+
"three": "0.185.1",
|
|
55
|
+
"tsup": "8.5.1",
|
|
56
|
+
"typescript": "5.9.3"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsup --config tsup.config.ts && publint",
|
|
60
|
+
"typecheck": "tsc --noEmit",
|
|
61
|
+
"test": "pnpm run build && pnpm --dir ../.. exec vitest run --config vitest.config.ts && bash __tests__/orphan-cleanup.sh"
|
|
62
|
+
}
|
|
63
|
+
}
|