@driftengine/tools 4.0.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Drift Technologies
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,29 @@
1
+ DriftEngine
2
+ Copyright 2026 Drift Technologies
3
+
4
+ This product includes software developed at Drift Technologies
5
+ (https://github.com/drftrun/driftengine).
6
+
7
+ Licensed under the Apache License, Version 2.0. Section 4(d) of that licence
8
+ requires this NOTICE to be reproduced in any derivative work you distribute,
9
+ in the source, the documentation, or a display generated by the work.
10
+
11
+ Models run by DriftCapture
12
+ --------------------------
13
+ DriftCapture runs the models below. Their weights are not in this repository
14
+ and are not distributed with it: tools/capture-weights/ fetches each at the
15
+ revision its manifest pins, verifies it, and converts it, and a game that
16
+ ships one ships it under that model's own licence. Each model's definition in
17
+ this engine is written from its project's published code.
18
+
19
+ - Depth Anything 3 Small and Depth Anything 3 Base. Apache License 2.0.
20
+ Copyright (c) 2025 ByteDance Ltd. and/or its affiliates.
21
+ - Depth Anything V2 Small. Apache License 2.0. The University of Hong Kong
22
+ and TikTok; converted to the Transformers layout by The HuggingFace Team.
23
+ - MobileSAM. Apache License 2.0. Its image encoder is TinyViT, Copyright (c)
24
+ 2022 Microsoft; its prompt encoder and mask decoder are Segment Anything's,
25
+ Copyright (c) Meta Platforms, Inc. and affiliates.
26
+ - SAM 2.1 tiny. Apache License 2.0. Copyright (c) Meta Platforms, Inc. and
27
+ affiliates.
28
+ - OWLv2 base patch16. Apache License 2.0. Copyright 2023 Google AI and The
29
+ HuggingFace Team.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # `@driftengine/tools`
2
+
3
+ The editor's panels, as something a shipped game can carry: an inspector, a console, a profiler and
4
+ a network panel, with the command stack that makes their edits undoable.
5
+
6
+ **3,395 bytes gzipped**, measured by `scripts/size-gate.test.mjs` against
7
+ `scripts/fixtures/size/tools-only.ts`. Optional — nothing in `@driftengine/core` imports it, so a
8
+ game that never asks for these pays nothing.
9
+
10
+ ## Why four panels and not seven
11
+
12
+ The editor has seven. Three of them stayed behind, and the line between them is what a panel needs
13
+ rather than what it shows.
14
+
15
+ A **scene tree** and an **asset browser** need a project: a hierarchy somebody is authoring and a
16
+ directory of source files. A shipped game has neither — its scene is whatever was baked and its
17
+ assets are inside a `.drft` container. A **graph editor** is the same, one level further out.
18
+
19
+ An **inspector**, a **console**, a **profiler** and a **network panel** need only the running world.
20
+ That is exactly the situation a tester is in on somebody else's machine, or a developer is in on a
21
+ console devkit where no debugger will attach, or anyone is in when the bug happens in a build. This
22
+ package is the four that work there.
23
+
24
+ ## The panels mutate through commands, so an in-game edit is undoable
25
+
26
+ `Panel.route` returns a `Command` or nothing. It is a signature rather than a convention: a panel is
27
+ handed the world `Readonly` and given no other way to change it, so "everything goes through the
28
+ undo stack" has no worst offender. A game carrying these panels carries that rule, and gets undo for
29
+ nothing extra.
30
+
31
+ ```ts
32
+ import { createUndoStack, inspectorPanel, createInspectorView } from '@driftengine/tools';
33
+
34
+ const undo = createUndoStack(200);
35
+ const view = createInspectorView({});
36
+ const command = inspectorPanel.route(world, view, event);
37
+ if (command !== null) undo.push(command);
38
+ ```
39
+
40
+ ## Building it in or leaving it out
41
+
42
+ Import it and it is in the bundle; do not and it is not. There is no flag, because a flag would be a
43
+ second mechanism doing what a module graph already does — and the size gate is what says so: the
44
+ floors for every other package are unchanged by this one existing.
45
+
46
+ A game that wants the tools in a development build and not in a shipping one imports them behind the
47
+ bundler's own dead-code elimination, the same way it would any other optional dependency.
48
+
49
+ ## What is not here
50
+
51
+ The timeline scrubber. It belongs to the editor's play-in-editor session, which needs snapshots of
52
+ the authored world to put back — a shipped game has no authored world to return to. The pieces
53
+ underneath it (`@driftengine/network`'s `Snapshotter`, `RewindLoop` and input log) are available to
54
+ a game directly.
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Every change to the world, as an object that can be undone.
3
+ *
4
+ * **Built before any panel exists, and that order is the point.** An undo stack added after the
5
+ * panels is an undo stack with holes in it, and the holes are found by users rather than by tests:
6
+ * one panel mutated directly, once, and now a particular sequence of actions cannot be taken back.
7
+ * Built first, every panel is written against it.
8
+ *
9
+ * **A panel is never handed the world.** `Panel.route` returns a `Command` or nothing, so a panel
10
+ * physically cannot change anything — the rule is enforced by the signature rather than by review.
11
+ *
12
+ * **Merging is what makes a drag usable.** A gizmo emits a command per frame; sixty of them in the
13
+ * undo stack means sixty presses of undo to take back one movement. `merge` lets a command absorb
14
+ * the next of its kind, so a drag is one entry.
15
+ */
16
+ export interface Command {
17
+ /** Shown in the undo menu and in the palette. */
18
+ readonly label: string;
19
+ apply(): void;
20
+ revert(): void;
21
+ /**
22
+ * Absorb a following command of the same kind. Return true if it was absorbed.
23
+ *
24
+ * **Called after the stack has already applied `next`, and an implementation must not apply it
25
+ * again.** Apply happens exactly once, by the stack, for every command — the alternative splits
26
+ * that responsibility between the stack and each mergeable command, and the first implementation
27
+ * of this got it wrong in exactly that way: the change ran twice and a drag moved twice as far
28
+ * as the pointer.
29
+ *
30
+ * What absorbing means is that the *earlier* command's `revert` now takes back both.
31
+ */
32
+ merge?(next: Command): boolean;
33
+ }
34
+ export interface UndoStack {
35
+ push(command: Command): void;
36
+ undo(): boolean;
37
+ redo(): boolean;
38
+ canUndo(): boolean;
39
+ canRedo(): boolean;
40
+ undoLabel(): string | null;
41
+ redoLabel(): string | null;
42
+ clear(): void;
43
+ }
44
+ export declare function createUndoStack(limit: number): UndoStack;
@@ -0,0 +1,53 @@
1
+ export function createUndoStack(limit) {
2
+ /* Entries below `at` are done; entries from `at` up are the redo branch. */
3
+ let entries = [];
4
+ let at = 0;
5
+ return {
6
+ push(command) {
7
+ command.apply();
8
+ const previous = at > 0 ? entries[at - 1] : undefined;
9
+ if (previous?.merge !== undefined && previous.merge(command)) {
10
+ /*
11
+ * Absorbed. `command` has already been applied above and must not be applied again; what
12
+ * changed is that `previous.revert` now takes back both. The redo branch still dies.
13
+ */
14
+ entries.length = at;
15
+ return;
16
+ }
17
+ /* A new command after an undo discards the redo branch, which is what every editor does. */
18
+ entries.length = at;
19
+ entries.push(command);
20
+ at += 1;
21
+ if (entries.length > limit) {
22
+ /*
23
+ * The oldest is forgotten, never reverted. Reverting it on the way out would undo a change
24
+ * the user made and kept, which is the ring-buffer bug this comment exists to prevent.
25
+ */
26
+ entries = entries.slice(entries.length - limit);
27
+ at = entries.length;
28
+ }
29
+ },
30
+ undo() {
31
+ if (at === 0)
32
+ return false;
33
+ at -= 1;
34
+ entries[at].revert();
35
+ return true;
36
+ },
37
+ redo() {
38
+ if (at >= entries.length)
39
+ return false;
40
+ entries[at].apply();
41
+ at += 1;
42
+ return true;
43
+ },
44
+ canUndo: () => at > 0,
45
+ canRedo: () => at < entries.length,
46
+ undoLabel: () => (at > 0 ? entries[at - 1].label : null),
47
+ redoLabel: () => (at < entries.length ? entries[at].label : null),
48
+ clear() {
49
+ entries = [];
50
+ at = 0;
51
+ },
52
+ };
53
+ }
@@ -0,0 +1,75 @@
1
+ import type { UiNode } from '@driftengine/ui2d';
2
+ import type { Command } from './command.ts';
3
+ import { type Panel } from './panel.ts';
4
+ export type Severity = 'debug' | 'info' | 'warn' | 'error';
5
+ export interface LogEntry {
6
+ severity: Severity;
7
+ text: string;
8
+ /** How many identical entries this one stands for. One for a single occurrence. */
9
+ count: number;
10
+ /** Where it came from, or empty. */
11
+ file: string;
12
+ line: number;
13
+ }
14
+ /** A fixed window of entries. Pooled, because a log is appended to forever. */
15
+ export interface LogRing {
16
+ readonly pool: LogEntry[];
17
+ readonly capacity: number;
18
+ head: number;
19
+ filled: number;
20
+ }
21
+ export declare function createLogRing(capacity: number): LogRing;
22
+ /**
23
+ * Append, collapsing an exact repeat of the newest entry into its count.
24
+ *
25
+ * **Against the newest only**, and not against everything in the window. Collapsing against any
26
+ * earlier entry would reorder the log — the repeat would appear where the first one was, out of
27
+ * sequence with whatever happened in between, and the sequence is what a log is for.
28
+ */
29
+ export declare function appendLog(log: LogRing, severity: Severity, text: string, file?: string, line?: number): void;
30
+ /** The window, oldest first. */
31
+ export declare function logEntries(log: LogRing): LogEntry[];
32
+ export interface LogFilter {
33
+ minSeverity: Severity;
34
+ /** Matched case-insensitively against the entry's text. Empty matches everything. */
35
+ text: string;
36
+ }
37
+ export declare function createLogFilter(): LogFilter;
38
+ export declare function filteredEntries(log: LogRing, filter: LogFilter): LogEntry[];
39
+ /** How much of one entry a row shows. Past this the rest is still on the entry. */
40
+ export declare const DISPLAY_LIMIT = 200;
41
+ export declare function displayText(entry: LogEntry): string;
42
+ /** Where a caller is looking. The console asks to move it; the caller owns it. */
43
+ export interface SourceCursor {
44
+ file: string;
45
+ line: number;
46
+ }
47
+ /**
48
+ * Ask to look at a place in a file.
49
+ *
50
+ * **A command, so going back is the same gesture as any other undo.** Whether a caller puts it on
51
+ * the undo stack or merely applies it is the caller's decision — the console knows how to describe
52
+ * the jump and not what navigation should cost.
53
+ */
54
+ export declare function jumpToSourceCommand(cursor: SourceCursor, file: string, line: number): Command | null;
55
+ export interface ConsoleWorld {
56
+ readonly log: LogRing;
57
+ }
58
+ export interface ConsoleView {
59
+ readonly root: UiNode;
60
+ readonly filter: LogFilter;
61
+ /** Where the caller is looking. Read by `route` to refuse a jump that goes nowhere. */
62
+ readonly cursor: SourceCursor;
63
+ rowHeight: number;
64
+ viewHeight: number;
65
+ scrollY: number;
66
+ /** Whether the view follows the newest entry. True while it is at the bottom. */
67
+ pinned: boolean;
68
+ /** What the last build showed, so a click finds the entry rather than the row. */
69
+ shown: LogEntry[];
70
+ }
71
+ export declare function createConsoleView(options: {
72
+ rowHeight?: number;
73
+ viewHeight?: number;
74
+ }): ConsoleView;
75
+ export declare const consolePanel: Panel<ConsoleWorld, ConsoleView>;
@@ -0,0 +1,175 @@
1
+ /**
2
+ * The log, filtered, with a way back to the line that produced it.
3
+ *
4
+ * **Scrolling up to read something must not be yanked away by the next log line.** That is the
5
+ * whole point of this panel: a console that always jumps to the bottom is unreadable during the
6
+ * thing being debugged, because that is exactly when lines arrive fastest. So the view follows the
7
+ * newest entry *only while it was already at the bottom*, and leaving the bottom stops it.
8
+ *
9
+ * **A repeat collapses into a count.** A warning inside a frame loop arrives sixty times a second,
10
+ * and without this the whole ring becomes one message and everything before it is gone — which is
11
+ * precisely what somebody is looking for when a warning starts repeating.
12
+ *
13
+ * **A long entry is truncated for display and kept whole underneath.** A single line of a hundred
14
+ * kilobytes — a serialised object, a shader source — would otherwise be the panel.
15
+ */
16
+ import { addUiChild, createUiNode } from '@driftengine/ui2d';
17
+ import { createPanelRoot, emptyPanel } from './panel.js';
18
+ /** Ascending, so a minimum keeps everything at least as severe. */
19
+ const RANK = { debug: 0, info: 1, warn: 2, error: 3 };
20
+ export function createLogRing(capacity) {
21
+ const size = Math.max(1, capacity);
22
+ return {
23
+ pool: Array.from({ length: size }, () => ({
24
+ severity: 'info',
25
+ text: '',
26
+ count: 0,
27
+ file: '',
28
+ line: 0,
29
+ })),
30
+ capacity: size,
31
+ head: 0,
32
+ filled: 0,
33
+ };
34
+ }
35
+ /** The newest entry, or null. What a repeat is compared against. */
36
+ function newest(log) {
37
+ if (log.filled === 0)
38
+ return null;
39
+ return log.pool[(log.head + log.capacity - 1) % log.capacity];
40
+ }
41
+ /**
42
+ * Append, collapsing an exact repeat of the newest entry into its count.
43
+ *
44
+ * **Against the newest only**, and not against everything in the window. Collapsing against any
45
+ * earlier entry would reorder the log — the repeat would appear where the first one was, out of
46
+ * sequence with whatever happened in between, and the sequence is what a log is for.
47
+ */
48
+ export function appendLog(log, severity, text, file = '', line = 0) {
49
+ const last = newest(log);
50
+ if (last !== null &&
51
+ last.severity === severity &&
52
+ last.text === text &&
53
+ last.file === file &&
54
+ last.line === line) {
55
+ last.count += 1;
56
+ return;
57
+ }
58
+ const entry = log.pool[log.head];
59
+ entry.severity = severity;
60
+ entry.text = text;
61
+ entry.count = 1;
62
+ entry.file = file;
63
+ entry.line = line;
64
+ log.head = (log.head + 1) % log.capacity;
65
+ if (log.filled < log.capacity)
66
+ log.filled += 1;
67
+ }
68
+ /** The window, oldest first. */
69
+ export function logEntries(log) {
70
+ const first = log.filled < log.capacity ? 0 : log.head;
71
+ const out = [];
72
+ for (let at = 0; at < log.filled; at += 1) {
73
+ out.push(log.pool[(first + at) % log.capacity]);
74
+ }
75
+ return out;
76
+ }
77
+ export function createLogFilter() {
78
+ return { minSeverity: 'debug', text: '' };
79
+ }
80
+ export function filteredEntries(log, filter) {
81
+ const floor = RANK[filter.minSeverity];
82
+ const needle = filter.text.toLowerCase();
83
+ return logEntries(log).filter((entry) => RANK[entry.severity] >= floor && (needle === '' || entry.text.toLowerCase().includes(needle)));
84
+ }
85
+ /** How much of one entry a row shows. Past this the rest is still on the entry. */
86
+ export const DISPLAY_LIMIT = 200;
87
+ export function displayText(entry) {
88
+ const body = entry.text.length > DISPLAY_LIMIT ? `${entry.text.slice(0, DISPLAY_LIMIT)}…` : entry.text;
89
+ return entry.count > 1 ? `${body} (${entry.count})` : body;
90
+ }
91
+ /**
92
+ * Ask to look at a place in a file.
93
+ *
94
+ * **A command, so going back is the same gesture as any other undo.** Whether a caller puts it on
95
+ * the undo stack or merely applies it is the caller's decision — the console knows how to describe
96
+ * the jump and not what navigation should cost.
97
+ */
98
+ export function jumpToSourceCommand(cursor, file, line) {
99
+ if (file === '')
100
+ return null;
101
+ if (cursor.file === file && cursor.line === line)
102
+ return null;
103
+ const was = { file: cursor.file, line: cursor.line };
104
+ return {
105
+ label: `Go to ${file}:${line}`,
106
+ apply: () => {
107
+ cursor.file = file;
108
+ cursor.line = line;
109
+ },
110
+ revert: () => {
111
+ cursor.file = was.file;
112
+ cursor.line = was.line;
113
+ },
114
+ };
115
+ }
116
+ export function createConsoleView(options) {
117
+ return {
118
+ root: createPanelRoot(consolePanel),
119
+ filter: createLogFilter(),
120
+ cursor: { file: '', line: 0 },
121
+ rowHeight: options.rowHeight ?? 16,
122
+ viewHeight: options.viewHeight ?? 200,
123
+ scrollY: 0,
124
+ pinned: true,
125
+ shown: [],
126
+ };
127
+ }
128
+ /** The furthest down the view can go. Zero where the content is shorter than the view. */
129
+ function maxScroll(view, rows) {
130
+ return Math.max(0, rows * view.rowHeight - view.viewHeight);
131
+ }
132
+ export const consolePanel = {
133
+ id: 'console',
134
+ title: 'Console',
135
+ build(world, view, root) {
136
+ view.shown = filteredEntries(world.log, view.filter);
137
+ if (view.shown.length === 0) {
138
+ emptyPanel(root, 'Nothing logged');
139
+ view.scrollY = 0;
140
+ return;
141
+ }
142
+ root.children.length = 0;
143
+ for (let at = 0; at < view.shown.length; at += 1) {
144
+ const entry = view.shown[at];
145
+ addUiChild(root, createUiNode({
146
+ width: 'grow',
147
+ height: view.rowHeight,
148
+ text: displayText(entry),
149
+ interactive: true,
150
+ name: `log:${at}`,
151
+ }));
152
+ }
153
+ /* The follow happens here and not on append, because only a build knows how tall the list is. */
154
+ if (view.pinned)
155
+ view.scrollY = maxScroll(view, view.shown.length);
156
+ },
157
+ route(_world, view, event) {
158
+ if (event.kind === 'wheel') {
159
+ /* `+= dy`, which is `scrollBy`'s convention in `@driftengine/ui2d`: positive goes down the
160
+ content. Inverting it here would make this the one scrollable thing in the editor that
161
+ went the other way. */
162
+ const limit = maxScroll(view, view.shown.length);
163
+ view.scrollY = Math.min(limit, Math.max(0, view.scrollY + event.dy));
164
+ view.pinned = view.scrollY >= limit;
165
+ return null;
166
+ }
167
+ if (event.kind !== 'pointer' || event.phase !== 'up' || event.button !== 0)
168
+ return null;
169
+ const at = Math.floor((event.y + view.scrollY) / view.rowHeight);
170
+ const entry = view.shown[at];
171
+ if (entry === undefined)
172
+ return null;
173
+ return jumpToSourceCommand(view.cursor, entry.file, entry.line);
174
+ },
175
+ };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Which of two sets of component hashes disagree.
3
+ *
4
+ * **Here rather than beside the divergence search, because a shipped game gets this half.** The
5
+ * search walks a timeline, which only the editor has; comparing what two peers hashed is what an
6
+ * in-game network panel does the moment a rollback session desynchronises, and that is a panel a
7
+ * game carries. `editor/src/pie/divergence.ts` holds the search and imports these.
8
+ */
9
+ /**
10
+ * Which components two sides hash differently, into `out`. Returns how many.
11
+ *
12
+ * A component only one side has is a divergence too, and the loudest kind: it means one peer
13
+ * created or destroyed something the other did not.
14
+ */
15
+ export declare function divergentComponents(ours: ReadonlyMap<string, string>, theirs: ReadonlyMap<string, string>, out: string[]): number;
16
+ /**
17
+ * Whether the difference is in something nobody hashed.
18
+ *
19
+ * The honest answer when the whole-world fingerprints disagree and every recorded component
20
+ * agrees: the state that differs was not in the breakdown. Reporting agreement there would be a
21
+ * panel saying everything is fine on a frame that is provably not.
22
+ */
23
+ export declare function divergenceIsUnaccounted(differing: number, worldsDiffer: boolean): boolean;