@usions/sdk 2.24.2 → 3.0.0-next.7
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 +45 -277
- package/dist/browser.global.js +1972 -0
- package/dist/browser.global.js.map +1 -0
- package/dist/index.d.ts +337 -0
- package/dist/index.js +1955 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.d.ts +23 -0
- package/dist/testing.js +63 -0
- package/dist/testing.js.map +1 -0
- package/dist/types-G075L8Nl.d.ts +83 -0
- package/package.json +25 -46
- package/src/browser.js +0 -6725
- package/src/index.js +0 -10
- package/src/modules/back-button.js +0 -26
- package/src/modules/backend-channel.js +0 -109
- package/src/modules/bot.js +0 -52
- package/src/modules/chat.js +0 -35
- package/src/modules/cloud.js +0 -67
- package/src/modules/core.js +0 -424
- package/src/modules/errors.js +0 -120
- package/src/modules/file-storage.js +0 -46
- package/src/modules/game-ack.js +0 -32
- package/src/modules/game-core.js +0 -278
- package/src/modules/game-direct.js +0 -267
- package/src/modules/game-methods.js +0 -782
- package/src/modules/game-netcode.js +0 -349
- package/src/modules/game-proxy.js +0 -184
- package/src/modules/game-reliability.js +0 -278
- package/src/modules/game-resilience.js +0 -245
- package/src/modules/game-socket.js +0 -214
- package/src/modules/index.js +0 -78
- package/src/modules/leaderboard.js +0 -46
- package/src/modules/lobby.js +0 -103
- package/src/modules/matchmaking.js +0 -140
- package/src/modules/misc.js +0 -152
- package/src/modules/netcode/binary.js +0 -113
- package/src/modules/netcode/delta.js +0 -236
- package/src/modules/netcode/index.js +0 -43
- package/src/modules/netcode/interest-grid.js +0 -227
- package/src/modules/netcode/interpolation.js +0 -235
- package/src/modules/netcode/lagcomp.js +0 -104
- package/src/modules/netcode/lockstep.js +0 -103
- package/src/modules/netcode/mesh-network.js +0 -103
- package/src/modules/netcode/mesh.js +0 -188
- package/src/modules/netcode/netsim.js +0 -77
- package/src/modules/netcode/ping.js +0 -69
- package/src/modules/netcode/prediction.js +0 -113
- package/src/modules/netcode/sender.js +0 -102
- package/src/modules/netcode/webtransport.js +0 -195
- package/src/modules/notify.js +0 -70
- package/src/modules/permissions.js +0 -157
- package/src/modules/results.js +0 -34
- package/src/modules/session.js +0 -62
- package/src/modules/storage.js +0 -64
- package/src/modules/ui.js +0 -111
- package/src/modules/user.js +0 -61
- package/src/modules/wallet.js +0 -253
- package/types/browser.d.ts +0 -5
- package/types/index.d.ts +0 -1308
package/src/modules/misc.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Misc — submit, error, exit, share, log, on, requestPayment (legacy)
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { isTrustedMessageSource } from './core.js';
|
|
6
|
-
|
|
7
|
-
export const miscMethods = {
|
|
8
|
-
/**
|
|
9
|
-
* Request payment from user (legacy method)
|
|
10
|
-
* @deprecated Use Usion.wallet.requestPayment instead
|
|
11
|
-
*/
|
|
12
|
-
requestPayment: function(amount, reason, data) {
|
|
13
|
-
return this.wallet.requestPayment(amount, reason, data);
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Live SDK diagnostics snapshot: version, transport, connection and
|
|
18
|
-
* sequence state. Surfaced automatically in the platform debug overlay
|
|
19
|
-
* (game.debug attaches it as _diag) and useful in bug reports.
|
|
20
|
-
*/
|
|
21
|
-
diagnostics: function() {
|
|
22
|
-
const game = this.game || {};
|
|
23
|
-
return {
|
|
24
|
-
version: this.version,
|
|
25
|
-
transport: game.directMode ? 'direct' : (game._useProxy ? 'proxy' : (game.socket ? 'socket' : 'none')),
|
|
26
|
-
connected: !!game.connected,
|
|
27
|
-
joined: !!game._joined,
|
|
28
|
-
roomId: game.roomId || null,
|
|
29
|
-
playerId: game.playerId || null,
|
|
30
|
-
lastSequence: game._lastSequence || 0,
|
|
31
|
-
lastActionApplied: game._lastActionApplied || 0,
|
|
32
|
-
rejoining: !!game._rejoinPromise,
|
|
33
|
-
};
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Submit result and signal completion
|
|
38
|
-
* @param {object} data - Result data to send to parent
|
|
39
|
-
*/
|
|
40
|
-
submit: function(data) {
|
|
41
|
-
this._post({
|
|
42
|
-
type: 'SUBMIT',
|
|
43
|
-
data: data
|
|
44
|
-
});
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Report an error to parent app
|
|
49
|
-
* @param {string} message - Error message
|
|
50
|
-
*/
|
|
51
|
-
error: function(message) {
|
|
52
|
-
this._post({
|
|
53
|
-
type: 'ERROR',
|
|
54
|
-
message: message
|
|
55
|
-
});
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Request to close the mini app
|
|
60
|
-
* @param {object} [options] - Optional exit options
|
|
61
|
-
* @param {number} [options.backCount] - Number of screens to go back (default 1)
|
|
62
|
-
*/
|
|
63
|
-
exit: function(options) {
|
|
64
|
-
var msg = { type: 'EXIT' };
|
|
65
|
-
if (options && typeof options.backCount === 'number' && options.backCount > 1) {
|
|
66
|
-
msg.backCount = options.backCount;
|
|
67
|
-
}
|
|
68
|
-
this._post(msg);
|
|
69
|
-
},
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Share content through the app's native share and optionally post to Usions feed
|
|
73
|
-
*
|
|
74
|
-
* @param {string} contentType - Type of content: 'audio' | 'image' | 'video' | 'text' | 'mixed'
|
|
75
|
-
* @param {object} data - Content data to share:
|
|
76
|
-
* - text: Optional text/caption for the post
|
|
77
|
-
* - audioUrl: URL for audio content (when contentType is 'audio')
|
|
78
|
-
* - imageUrl: URL for image content (when contentType is 'image')
|
|
79
|
-
* - videoUrl: URL for video content (when contentType is 'video')
|
|
80
|
-
* - thumbnailUrl: Optional thumbnail URL for video/audio
|
|
81
|
-
* - width: Optional width for image/video
|
|
82
|
-
* - height: Optional height for image/video
|
|
83
|
-
* - duration: Optional duration in seconds for audio/video
|
|
84
|
-
* - media: Array of media items for 'mixed' content type
|
|
85
|
-
* - Each item: { type: 'image'|'video'|'audio', url: string, thumbnailUrl?, width?, height?, duration? }
|
|
86
|
-
*/
|
|
87
|
-
share: function(contentType, data) {
|
|
88
|
-
var shareData = Object.assign({}, data, {
|
|
89
|
-
contentType: contentType,
|
|
90
|
-
serviceId: this.config.serviceId,
|
|
91
|
-
serviceName: this.config.serviceName
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
this._post({
|
|
95
|
-
type: 'SHARE',
|
|
96
|
-
contentType: contentType,
|
|
97
|
-
data: shareData
|
|
98
|
-
});
|
|
99
|
-
},
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Download a file to the device's storage / gallery.
|
|
103
|
-
* @param {string} url - URL of the file to download
|
|
104
|
-
* @param {string} [filename] - Optional filename (default: 'download.mp4')
|
|
105
|
-
* @returns {Promise<{success: boolean, error?: string}>}
|
|
106
|
-
*/
|
|
107
|
-
download: function(url, filename) {
|
|
108
|
-
return this._request('DOWNLOAD_FILE', {
|
|
109
|
-
url: url,
|
|
110
|
-
filename: filename || 'download.mp4'
|
|
111
|
-
}, 120000);
|
|
112
|
-
},
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Log message to native console (for debugging)
|
|
116
|
-
* @param {string} msg - Message to log
|
|
117
|
-
*/
|
|
118
|
-
log: function(msg) {
|
|
119
|
-
this._post({
|
|
120
|
-
type: 'LOG',
|
|
121
|
-
msg: msg
|
|
122
|
-
});
|
|
123
|
-
console.log('[Usion]', msg);
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Listen for messages from parent app
|
|
128
|
-
* @param {string} type - Message type to listen for
|
|
129
|
-
* @param {function} callback - Handler function
|
|
130
|
-
* @returns {function} Unsubscribe function
|
|
131
|
-
*/
|
|
132
|
-
on: function(type, callback) {
|
|
133
|
-
function listener(event) {
|
|
134
|
-
if (!isTrustedMessageSource(event)) return;
|
|
135
|
-
|
|
136
|
-
let data;
|
|
137
|
-
try {
|
|
138
|
-
data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
139
|
-
} catch (e) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (data.type === type) {
|
|
144
|
-
callback(data);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
window.addEventListener('message', listener);
|
|
148
|
-
return function() {
|
|
149
|
-
window.removeEventListener('message', listener);
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Netcode — compact binary codec (zero-dependency).
|
|
3
|
-
*
|
|
4
|
-
* A tiny MessagePack-style serializer for JSON-like snapshot payloads. Pair it
|
|
5
|
-
* with the snapshot sender/receiver (`encode`/`decode` options) to send game
|
|
6
|
-
* state as binary over the WebRTC data channel or a binary-capable WebSocket —
|
|
7
|
-
* meaningfully smaller than JSON, especially after quantization (small ints
|
|
8
|
-
* pack into 1–2 bytes). WebRTC/Socket.IO transmit ArrayBuffers natively.
|
|
9
|
-
*
|
|
10
|
-
* NOT for the iframe/WebView proxy path (that bridge relays JSON) — use binary
|
|
11
|
-
* on direct/mesh transports.
|
|
12
|
-
*
|
|
13
|
-
* Wire tags: 0xc0 null · 0xc2 false · 0xc3 true · 0xc4 int(zigzag varint) ·
|
|
14
|
-
* 0xc5 float64 · 0xc6 string · 0xc7 array · 0xc8 object
|
|
15
|
-
*/
|
|
16
|
-
const NULL = 0xc0, FALSE = 0xc2, TRUE = 0xc3, INT = 0xc4, F64 = 0xc5, STR = 0xc6, ARR = 0xc7, OBJ = 0xc8;
|
|
17
|
-
|
|
18
|
-
const _enc = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
|
|
19
|
-
const _dec = typeof TextDecoder !== 'undefined' ? new TextDecoder() : null;
|
|
20
|
-
|
|
21
|
-
function pushVarint(out, n) {
|
|
22
|
-
// unsigned LEB128
|
|
23
|
-
n = n >>> 0;
|
|
24
|
-
while (n > 0x7f) { out.push((n & 0x7f) | 0x80); n >>>= 7; }
|
|
25
|
-
out.push(n);
|
|
26
|
-
}
|
|
27
|
-
function zigzag(n) { return ((n << 1) ^ (n >> 31)) >>> 0; }
|
|
28
|
-
function unzigzag(u) { return (u >>> 1) ^ -(u & 1); }
|
|
29
|
-
|
|
30
|
-
function encodeValue(out, v) {
|
|
31
|
-
if (v === null || v === undefined) { out.push(NULL); return; }
|
|
32
|
-
const t = typeof v;
|
|
33
|
-
if (t === 'boolean') { out.push(v ? TRUE : FALSE); return; }
|
|
34
|
-
if (t === 'number') {
|
|
35
|
-
if (Number.isInteger(v) && v >= -2147483648 && v <= 2147483647) {
|
|
36
|
-
out.push(INT); pushVarint(out, zigzag(v));
|
|
37
|
-
} else {
|
|
38
|
-
out.push(F64);
|
|
39
|
-
const b = new Uint8Array(8); new DataView(b.buffer).setFloat64(0, v, true);
|
|
40
|
-
for (let i = 0; i < 8; i++) out.push(b[i]);
|
|
41
|
-
}
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
if (t === 'string') {
|
|
45
|
-
out.push(STR);
|
|
46
|
-
const bytes = _enc ? _enc.encode(v) : Buffer.from(v, 'utf8');
|
|
47
|
-
pushVarint(out, bytes.length);
|
|
48
|
-
for (let i = 0; i < bytes.length; i++) out.push(bytes[i]);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (Array.isArray(v)) {
|
|
52
|
-
out.push(ARR); pushVarint(out, v.length);
|
|
53
|
-
for (let i = 0; i < v.length; i++) encodeValue(out, v[i]);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
if (t === 'object') {
|
|
57
|
-
const keys = Object.keys(v);
|
|
58
|
-
out.push(OBJ); pushVarint(out, keys.length);
|
|
59
|
-
for (let i = 0; i < keys.length; i++) {
|
|
60
|
-
const k = keys[i];
|
|
61
|
-
const kb = _enc ? _enc.encode(k) : Buffer.from(k, 'utf8');
|
|
62
|
-
pushVarint(out, kb.length);
|
|
63
|
-
for (let j = 0; j < kb.length; j++) out.push(kb[j]);
|
|
64
|
-
encodeValue(out, v[k]);
|
|
65
|
-
}
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
out.push(NULL);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Encode a JSON-like value to a Uint8Array. */
|
|
72
|
-
export function encode(value) {
|
|
73
|
-
const out = [];
|
|
74
|
-
encodeValue(out, value);
|
|
75
|
-
return Uint8Array.from(out);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function reader(bytes) {
|
|
79
|
-
let off = 0;
|
|
80
|
-
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
81
|
-
function varint() {
|
|
82
|
-
let shift = 0, result = 0, b;
|
|
83
|
-
do { b = bytes[off++]; result |= (b & 0x7f) << shift; shift += 7; } while (b & 0x80);
|
|
84
|
-
return result >>> 0;
|
|
85
|
-
}
|
|
86
|
-
function str() {
|
|
87
|
-
const len = varint();
|
|
88
|
-
const slice = bytes.subarray(off, off + len);
|
|
89
|
-
off += len;
|
|
90
|
-
return _dec ? _dec.decode(slice) : Buffer.from(slice).toString('utf8');
|
|
91
|
-
}
|
|
92
|
-
function value() {
|
|
93
|
-
const tag = bytes[off++];
|
|
94
|
-
switch (tag) {
|
|
95
|
-
case NULL: return null;
|
|
96
|
-
case TRUE: return true;
|
|
97
|
-
case FALSE: return false;
|
|
98
|
-
case INT: return unzigzag(varint());
|
|
99
|
-
case F64: { const v = dv.getFloat64(off, true); off += 8; return v; }
|
|
100
|
-
case STR: return str();
|
|
101
|
-
case ARR: { const n = varint(); const a = new Array(n); for (let i = 0; i < n; i++) a[i] = value(); return a; }
|
|
102
|
-
case OBJ: { const n = varint(); const o = {}; for (let i = 0; i < n; i++) { const k = str(); o[k] = value(); } return o; }
|
|
103
|
-
default: return null;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return value;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** Decode a Uint8Array / ArrayBuffer produced by `encode`. */
|
|
110
|
-
export function decode(buf) {
|
|
111
|
-
const bytes = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
|
|
112
|
-
return reader(bytes)();
|
|
113
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Netcode — Delta compression for JSON game state.
|
|
3
|
-
*
|
|
4
|
-
* Real-time games typically re-send their entire world every tick. Most of
|
|
5
|
-
* that payload is unchanged. `diff` produces a minimal patch describing only
|
|
6
|
-
* what changed since the previous state; `patch` reconstructs the new state
|
|
7
|
-
* from a base + patch. Both are plain JSON, so the patch rides on the existing
|
|
8
|
-
* realtime/action channels unchanged.
|
|
9
|
-
*
|
|
10
|
-
* Wire format:
|
|
11
|
-
* - Objects → `{ k: <patch> }` for changed keys, `_d: [keys]` for deletions.
|
|
12
|
-
* - **Entity arrays** (every element a plain object with a stable `id`) →
|
|
13
|
-
* keyed diff `{ _ka:{id:patch}, _add:[entities], _del:[ids], _ord:[ids] }`.
|
|
14
|
-
* This is O(changes), not O(index shift): inserting/removing an element no
|
|
15
|
-
* longer rewrites every following index. (Gaffer-on-Games "state sync".)
|
|
16
|
-
* - Other arrays → index diff `{ _a:{i:patch}, _n:length }`.
|
|
17
|
-
* - Primitives / type changes → `{ _s: value }`.
|
|
18
|
-
* - No change → `undefined`.
|
|
19
|
-
*
|
|
20
|
-
* `quantize` rounds numeric fields to a fixed resolution before diffing, so
|
|
21
|
-
* sub-resolution jitter doesn't produce spurious deltas and payloads shrink
|
|
22
|
-
* (the single biggest, simplest snapshot-compression win — Gaffer on Games).
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
const SET = '_s'; // wholesale set
|
|
26
|
-
const DEL = '_d'; // deleted object keys
|
|
27
|
-
const ARR = '_a'; // index array patches
|
|
28
|
-
const LEN = '_n'; // index array length
|
|
29
|
-
const KA = '_ka'; // keyed-array: per-id patches
|
|
30
|
-
const ADD = '_add'; // keyed-array: added entities (full)
|
|
31
|
-
const KDEL = '_del';// keyed-array: removed ids
|
|
32
|
-
const ORD = '_ord'; // keyed-array: explicit id order (only when it changes)
|
|
33
|
-
|
|
34
|
-
function isPlainObject(v) {
|
|
35
|
-
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function isEntityArray(v) {
|
|
39
|
-
if (!Array.isArray(v) || v.length === 0) return false;
|
|
40
|
-
for (let i = 0; i < v.length; i++) {
|
|
41
|
-
const e = v[i];
|
|
42
|
-
if (!isPlainObject(e) || (typeof e.id !== 'string' && typeof e.id !== 'number')) return false;
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function sameOrder(a, b) {
|
|
48
|
-
if (a.length !== b.length) return false;
|
|
49
|
-
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Compute a minimal patch turning `prev` into `next`.
|
|
55
|
-
* @returns the patch, or `undefined` when nothing changed.
|
|
56
|
-
*/
|
|
57
|
-
export function diff(prev, next) {
|
|
58
|
-
if (prev === next) return undefined;
|
|
59
|
-
|
|
60
|
-
const prevArr = Array.isArray(prev);
|
|
61
|
-
const nextArr = Array.isArray(next);
|
|
62
|
-
|
|
63
|
-
// Type changed (or one side primitive) — send the whole value.
|
|
64
|
-
if (prevArr !== nextArr || isPlainObject(prev) !== isPlainObject(next)) {
|
|
65
|
-
return { [SET]: next };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (nextArr) {
|
|
69
|
-
// Keyed (entity) arrays: diff by id so index shifts are free.
|
|
70
|
-
if (isEntityArray(prev) && isEntityArray(next)) return diffKeyedArray(prev, next);
|
|
71
|
-
return diffIndexArray(prev, next);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (isPlainObject(next)) return diffObject(prev, next);
|
|
75
|
-
|
|
76
|
-
// Two differing primitives.
|
|
77
|
-
return { [SET]: next };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function diffObject(prev, next) {
|
|
81
|
-
const out = {};
|
|
82
|
-
let changed = false;
|
|
83
|
-
for (const k in next) {
|
|
84
|
-
if (!Object.prototype.hasOwnProperty.call(next, k)) continue;
|
|
85
|
-
const d = diff(prev[k], next[k]);
|
|
86
|
-
if (d !== undefined) { out[k] = d; changed = true; }
|
|
87
|
-
}
|
|
88
|
-
const deleted = [];
|
|
89
|
-
for (const k in prev) {
|
|
90
|
-
if (!Object.prototype.hasOwnProperty.call(prev, k)) continue;
|
|
91
|
-
if (!Object.prototype.hasOwnProperty.call(next, k)) deleted.push(k);
|
|
92
|
-
}
|
|
93
|
-
if (deleted.length) { out[DEL] = deleted; changed = true; }
|
|
94
|
-
return changed ? out : undefined;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function diffIndexArray(prev, next) {
|
|
98
|
-
const out = { [ARR]: {} };
|
|
99
|
-
let changed = false;
|
|
100
|
-
for (let i = 0; i < next.length; i++) {
|
|
101
|
-
const d = i < prev.length ? diff(prev[i], next[i]) : { [SET]: next[i] };
|
|
102
|
-
if (d !== undefined) { out[ARR][i] = d; changed = true; }
|
|
103
|
-
}
|
|
104
|
-
if (next.length !== prev.length) { out[LEN] = next.length; changed = true; }
|
|
105
|
-
return changed ? out : undefined;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function diffKeyedArray(prev, next) {
|
|
109
|
-
const prevById = {};
|
|
110
|
-
const prevIds = [];
|
|
111
|
-
for (let i = 0; i < prev.length; i++) { prevById[prev[i].id] = prev[i]; prevIds.push(prev[i].id); }
|
|
112
|
-
const nextIds = [];
|
|
113
|
-
const out = {};
|
|
114
|
-
let changed = false;
|
|
115
|
-
|
|
116
|
-
for (let i = 0; i < next.length; i++) {
|
|
117
|
-
const e = next[i];
|
|
118
|
-
nextIds.push(e.id);
|
|
119
|
-
if (Object.prototype.hasOwnProperty.call(prevById, e.id)) {
|
|
120
|
-
const d = diff(prevById[e.id], e);
|
|
121
|
-
if (d !== undefined) {
|
|
122
|
-
if (!out[KA]) out[KA] = {};
|
|
123
|
-
out[KA][e.id] = d;
|
|
124
|
-
changed = true;
|
|
125
|
-
}
|
|
126
|
-
} else {
|
|
127
|
-
if (!out[ADD]) out[ADD] = [];
|
|
128
|
-
out[ADD].push(e);
|
|
129
|
-
changed = true;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const nextIdSet = {};
|
|
134
|
-
for (let i = 0; i < nextIds.length; i++) nextIdSet[nextIds[i]] = true;
|
|
135
|
-
for (let i = 0; i < prevIds.length; i++) {
|
|
136
|
-
if (!nextIdSet[prevIds[i]]) {
|
|
137
|
-
if (!out[KDEL]) out[KDEL] = [];
|
|
138
|
-
out[KDEL].push(prevIds[i]);
|
|
139
|
-
changed = true;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (!sameOrder(prevIds, nextIds)) { out[ORD] = nextIds; changed = true; }
|
|
144
|
-
return changed ? out : undefined;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Apply a patch produced by `diff` to `base`, returning the new value.
|
|
149
|
-
* Does not mutate `base`. A nullish patch returns `base` unchanged.
|
|
150
|
-
*/
|
|
151
|
-
export function patch(base, p) {
|
|
152
|
-
if (p === undefined || p === null) return base;
|
|
153
|
-
if (Object.prototype.hasOwnProperty.call(p, SET)) return p[SET];
|
|
154
|
-
|
|
155
|
-
// Keyed (entity) array.
|
|
156
|
-
if (p[KA] || p[ADD] || p[KDEL] || p[ORD]) return patchKeyedArray(base, p);
|
|
157
|
-
|
|
158
|
-
// Index array.
|
|
159
|
-
if (Object.prototype.hasOwnProperty.call(p, ARR) || Object.prototype.hasOwnProperty.call(p, LEN)) {
|
|
160
|
-
const src = Array.isArray(base) ? base : [];
|
|
161
|
-
const len = Object.prototype.hasOwnProperty.call(p, LEN) ? p[LEN] : src.length;
|
|
162
|
-
const out = src.slice(0, len);
|
|
163
|
-
const patches = p[ARR] || {};
|
|
164
|
-
for (const i in patches) {
|
|
165
|
-
if (!Object.prototype.hasOwnProperty.call(patches, i)) continue;
|
|
166
|
-
out[i] = patch(out[i], patches[i]);
|
|
167
|
-
}
|
|
168
|
-
return out;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Object patch.
|
|
172
|
-
const out = isPlainObject(base) ? Object.assign({}, base) : {};
|
|
173
|
-
const deleted = p[DEL];
|
|
174
|
-
for (const k in p) {
|
|
175
|
-
if (k === DEL) continue;
|
|
176
|
-
if (!Object.prototype.hasOwnProperty.call(p, k)) continue;
|
|
177
|
-
out[k] = patch(out[k], p[k]);
|
|
178
|
-
}
|
|
179
|
-
if (Array.isArray(deleted)) for (let i = 0; i < deleted.length; i++) delete out[deleted[i]];
|
|
180
|
-
return out;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function patchKeyedArray(base, p) {
|
|
184
|
-
const baseArr = Array.isArray(base) ? base : [];
|
|
185
|
-
const byId = {};
|
|
186
|
-
const baseOrder = [];
|
|
187
|
-
for (let i = 0; i < baseArr.length; i++) {
|
|
188
|
-
const e = baseArr[i];
|
|
189
|
-
if (e && (typeof e.id === 'string' || typeof e.id === 'number')) { byId[e.id] = e; baseOrder.push(e.id); }
|
|
190
|
-
}
|
|
191
|
-
// Apply per-id field patches.
|
|
192
|
-
if (p[KA]) for (const id in p[KA]) {
|
|
193
|
-
if (Object.prototype.hasOwnProperty.call(p[KA], id) && byId[id] !== undefined) byId[id] = patch(byId[id], p[KA][id]);
|
|
194
|
-
}
|
|
195
|
-
// Additions.
|
|
196
|
-
if (p[ADD]) for (let i = 0; i < p[ADD].length; i++) { const e = p[ADD][i]; byId[e.id] = e; }
|
|
197
|
-
// Removals.
|
|
198
|
-
const removed = {};
|
|
199
|
-
if (p[KDEL]) for (let i = 0; i < p[KDEL].length; i++) removed[p[KDEL][i]] = true;
|
|
200
|
-
|
|
201
|
-
if (p[ORD]) {
|
|
202
|
-
const out = [];
|
|
203
|
-
for (let i = 0; i < p[ORD].length; i++) {
|
|
204
|
-
const id = p[ORD][i];
|
|
205
|
-
if (byId[id] !== undefined) out.push(byId[id]);
|
|
206
|
-
}
|
|
207
|
-
return out;
|
|
208
|
-
}
|
|
209
|
-
// No order change: keep base order minus removals, patched in place.
|
|
210
|
-
const out = [];
|
|
211
|
-
for (let i = 0; i < baseOrder.length; i++) {
|
|
212
|
-
const id = baseOrder[i];
|
|
213
|
-
if (!removed[id]) out.push(byId[id]);
|
|
214
|
-
}
|
|
215
|
-
return out;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Round every numeric field to `precision` decimal places (default 2),
|
|
220
|
-
* returning a new value. Apply before diffing so sub-resolution jitter and
|
|
221
|
-
* float noise don't generate spurious deltas, and payloads stay small.
|
|
222
|
-
*/
|
|
223
|
-
export function quantize(value, precision = 2) {
|
|
224
|
-
const f = Math.pow(10, precision);
|
|
225
|
-
function q(v) {
|
|
226
|
-
if (typeof v === 'number') return Number.isFinite(v) ? Math.round(v * f) / f : v;
|
|
227
|
-
if (Array.isArray(v)) return v.map(q);
|
|
228
|
-
if (isPlainObject(v)) {
|
|
229
|
-
const o = {};
|
|
230
|
-
for (const k in v) if (Object.prototype.hasOwnProperty.call(v, k)) o[k] = q(v[k]);
|
|
231
|
-
return o;
|
|
232
|
-
}
|
|
233
|
-
return v;
|
|
234
|
-
}
|
|
235
|
-
return q(value);
|
|
236
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Netcode — public namespace.
|
|
3
|
-
*
|
|
4
|
-
* A small, zero-dependency, transport-agnostic toolkit for smooth low-latency
|
|
5
|
-
* multiplayer. Works across all Usion connection modes (platform / direct /
|
|
6
|
-
* proxy) and both platforms, computing on top of the existing realtime/action
|
|
7
|
-
* plumbing — no host or backend changes required to use it.
|
|
8
|
-
*
|
|
9
|
-
* SnapshotInterpolation — smooth rendering; adaptive buffer + capped extrapolation
|
|
10
|
-
* Predictor — client prediction + reconciliation + error smoothing
|
|
11
|
-
* Coalescer — fixed-Hz outbound send batching
|
|
12
|
-
* PingMeter — RTT / jitter telemetry
|
|
13
|
-
* MeshConnection — WebRTC P2P (sequenced, TURN-ready, auto-reconnect)
|
|
14
|
-
* MeshNetwork — N-peer full mesh
|
|
15
|
-
* diff / patch / quantize — JSON delta compression (id-keyed arrays)
|
|
16
|
-
* encode / decode — compact binary codec
|
|
17
|
-
* createInterestGrid — spatial hash for area-of-interest (AOI) filtering
|
|
18
|
-
*/
|
|
19
|
-
import { SnapshotInterpolation, Vault } from './interpolation.js';
|
|
20
|
-
import { Predictor } from './prediction.js';
|
|
21
|
-
import { Coalescer } from './sender.js';
|
|
22
|
-
import { PingMeter } from './ping.js';
|
|
23
|
-
import { MeshConnection } from './mesh.js';
|
|
24
|
-
import { MeshNetwork } from './mesh-network.js';
|
|
25
|
-
import { WebTransportConnection } from './webtransport.js';
|
|
26
|
-
import { NetworkSim } from './netsim.js';
|
|
27
|
-
import { Lockstep } from './lockstep.js';
|
|
28
|
-
import { LagCompensator } from './lagcomp.js';
|
|
29
|
-
import { diff, patch, quantize } from './delta.js';
|
|
30
|
-
import { encode, decode } from './binary.js';
|
|
31
|
-
import { createInterestGrid } from './interest-grid.js';
|
|
32
|
-
|
|
33
|
-
export const netcode = {
|
|
34
|
-
SnapshotInterpolation, Vault, Predictor, Coalescer, PingMeter,
|
|
35
|
-
MeshConnection, MeshNetwork, WebTransportConnection, NetworkSim, Lockstep, LagCompensator,
|
|
36
|
-
diff, patch, quantize, encode, decode, createInterestGrid,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export {
|
|
40
|
-
SnapshotInterpolation, Vault, Predictor, Coalescer, PingMeter,
|
|
41
|
-
MeshConnection, MeshNetwork, WebTransportConnection, NetworkSim, Lockstep, LagCompensator,
|
|
42
|
-
diff, patch, quantize, encode, decode, createInterestGrid,
|
|
43
|
-
};
|