-
Notifications
You must be signed in to change notification settings - Fork 207
/
app-manager.js
710 lines (643 loc) · 20.9 KB
/
app-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
app manager binds z.js data to live running metaversefile apps.
you can have as many app managers as you want.
*/
import * as THREE from 'three';
import * as Z from 'zjs';
import {makePromise, getRandomString} from './util.js';
import physicsManager from './physics-manager.js';
import metaversefile from 'metaversefile';
import * as metaverseModules from './metaverse-modules.js';
import {jsonParse} from './util.js';
import {worldMapName} from './constants.js';
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localMatrix = new THREE.Matrix4();
const localData = {
timestamp: 0,
frame: null,
timeDiff: 0,
};
const localFrameOpts = {
data: localData,
};
const frameEvent = new MessageEvent('frame', localFrameOpts);
const physicsScene = physicsManager.getScene();
const appManagers = [];
class AppManager extends EventTarget {
constructor({
appsArray = new Z.Doc().getArray(worldMapName),
} = {}) {
super();
this.appsArray = null;
this.apps = [];
this.transform = new Float32Array(10);
this.pendingAddPromises = new Map();
// this.pushingLocalUpdates = false;
this.unbindStateFn = null;
this.trackedAppUnobserveMap = new Map();
this.bindState(appsArray);
this.bindEvents();
appManagers.push(this);
}
tick(timestamp, timeDiff, frame) {
localData.timestamp = timestamp;
localData.frame = frame;
localData.timeDiff = timeDiff;
this.dispatchEvent(frameEvent);
}
/* setPushingLocalUpdates(pushingLocalUpdates) {
this.pushingLocalUpdates = pushingLocalUpdates;
} */
getPeerOwnerAppManager(instanceId) {
for (const appManager of appManagers) {
if (appManager !== this && appManager.appsArray.doc === this.appsArray.doc && appManager.hasTrackedApp(instanceId)) {
return appManager;
}
}
return null;
}
isBound() {
return !!this.appsArray;
}
unbindState() {
if (this.isBound()) {
this.unbindStateFn();
this.appsArray = null;
this.unbindStateFn = null;
}
}
bindState(nextAppsArray) {
this.unbindState();
if (nextAppsArray) {
const observe = e => {
const {added, deleted} = e.changes;
for (const item of added.values()) {
let appMap = item.content.type;
if (appMap.constructor === Object) {
for (let i = 0; i < this.appsArray.length; i++) {
const localAppMap = this.appsArray.get(i, Z.Map); // force to be a map
if (localAppMap.binding === item.content.type) {
appMap = localAppMap;
break;
}
}
}
const instanceId = appMap.get('instanceId');
const oldApp = this.apps.find(app => app.instanceId === instanceId);
if (oldApp) {
// console.log('accept migration add', instanceId);
this.dispatchEvent(new MessageEvent('trackedappimport', {
data: {
instanceId,
app: oldApp,
// sourceAppManager: this,
// destinationAppManager: peerOwnerAppManager,
},
}));
} else {
const trackedApp = this.getOrCreateTrackedApp(instanceId);
// console.log('detected add app', instanceId, trackedApp.toJSON(), new Error().stack);
this.dispatchEvent(new MessageEvent('trackedappadd', {
data: {
trackedApp,
},
}));
}
}
for (const item of deleted.values()) {
const appMap = item.content.type;
const instanceId = item.content.type._map.get('instanceId').content.arr[0]; // needed to get the old data
const app = this.getAppByInstanceId(instanceId);
let migrated = false;
const peerOwnerAppManager = this.getPeerOwnerAppManager(instanceId);
if (peerOwnerAppManager) {
// console.log('detected migrate app 1', instanceId, appManagers.length);
const e = new MessageEvent('trackedappexport', {
data: {
instanceId,
app,
sourceAppManager: this,
destinationAppManager: peerOwnerAppManager,
},
});
this.dispatchEvent(e);
peerOwnerAppManager.dispatchEvent(e);
migrated = true;
break;
}
// console.log('detected remove app 2', instanceId, appManagers.length);
if (!migrated) {
// console.log('detected remove app 3', instanceId, appManagers.length);
this.dispatchEvent(new MessageEvent('trackedappremove', {
data: {
instanceId,
app,
},
}));
}
}
};
nextAppsArray.observe(observe);
this.unbindStateFn = () => {
nextAppsArray.unobserve(observe);
};
}
this.appsArray = nextAppsArray;
}
async loadApps() {
for (let i = 0; i < this.appsArray.length; i++) {
const trackedApp = this.appsArray.get(i, Z.Map);
if(this.hasTrackedApp(trackedApp.get('instanceId'))) {
const app = this.apps.find(app => app.instanceId === trackedApp.get('instanceId'));
if(!app){
await this.importTrackedApp(trackedApp);
}
}
}
}
trackedAppBound (instanceId) {
return !!this.trackedAppUnobserveMap.get(instanceId)
}
async importTrackedApp(trackedApp) {
const trackedAppBinding = trackedApp.toJSON();
const {instanceId, contentId, transform, components} = trackedAppBinding;
const p = makePromise();
p.instanceId = instanceId;
this.pendingAddPromises.set(instanceId, p);
let live = true;
const clear = e => {
live = false;
cleanup();
};
const cleanup = () => {
this.removeEventListener('clear', clear);
this.pendingAddPromises.delete(instanceId);
};
this.addEventListener('clear', clear);
const _bailout = app => {
// Add Error placeholder
const errorPH = this.getErrorPlaceholder();
if (app) {
errorPH.position.fromArray(app.position);
errorPH.quaternion.fromArray(app.quaternion);
errorPH.scale.fromArray(app.scale);
}
this.addApp(errorPH);
// Remove app
if (app) {
this.removeApp(app);
app.destroy();
}
p.reject(new Error('app cleared during load: ' + contentId));
};
// attempt to load app
try {
const m = await metaversefile.import(contentId);
if (!live) return _bailout(null);
// create app
// as an optimization, the app may be reused by calling addApp() before tracking it
const app = metaversefile.createApp();
// setup
{
// set pose
app.position.fromArray(transform);
app.quaternion.fromArray(transform, 3);
app.scale.fromArray(transform, 7);
app.updateMatrixWorld();
app.lastMatrix.copy(app.matrixWorld);
// set components
app.instanceId = instanceId;
app.setComponent('physics', true);
for (const {key, value} of components) {
app.setComponent(key, value);
}
}
// initialize app
{
// console.log('add module', m);
const mesh = await app.addModule(m);
if (!live) return _bailout(app);
if (!mesh) {
console.warn('failed to load object', {contentId});
}
this.addApp(app);
}
this.bindTrackedApp(trackedApp, app);
p.accept(app);
} catch (err) {
p.reject(err);
} finally {
cleanup();
}
}
bindTrackedApp(trackedApp, app) {
// console.log('bind tracked app', trackedApp.get('instanceId'));
const _observe = (e, origin) => {
// ! bellow code is bugged
// if (origin !== 'push') {
// if (e.changes.keys.has('transform')) {
// app.position.fromArray(trackedApp.get('transform'));
// app.quaternion.fromArray(trackedApp.get('transform'), 3);
// app.scale.fromArray(trackedApp.get('transform'), 7);
// app.updateMatrixWorld();
// }
// }
};
trackedApp.observe(_observe);
const instanceId = trackedApp.get('instanceId');
this.trackedAppUnobserveMap.set(instanceId, trackedApp.unobserve.bind(trackedApp, _observe));
}
unbindTrackedApp(instanceId) {
const fn = this.trackedAppUnobserveMap.get(instanceId);
if (fn) {
this.trackedAppUnobserveMap.delete(instanceId);
fn();
} else {
console.warn('tracked app was not bound:', instanceId);
}
}
bindEvents() {
this.addEventListener('trackedappadd', async e => {
const {trackedApp} = e.data;
this.importTrackedApp(trackedApp);
});
this.addEventListener('trackedappremove', async e => {
const {instanceId, app} = e.data;
this.unbindTrackedApp(instanceId);
this.removeApp(app);
app.destroy();
});
this.addEventListener('trackedappimport', async e => {
const {
instanceId,
app,
} = e.data;
/* if (!this.apps.includes(app)) {
this.apps.push(app);
} */
});
this.addEventListener('trackedappexport', async e => {
const {
instanceId,
app,
sourceAppManager,
destinationAppManager,
} = e.data;
// console.log('handle migrate', sourceAppManager === this, destinationAppManager === this);
if (sourceAppManager === this) {
const index = this.apps.indexOf(app);
if (index !== -1) {
this.apps.splice(index, 1);
}
} else if (destinationAppManager === this) {
if (!this.apps.includes(app)) {
this.apps.push(app);
}
}
});
const resize = e => {
this.resize(e);
};
window.addEventListener('resize', resize);
this.cleanup = () => {
window.removeEventListener('resize', resize);
};
}
getApps() {
return this.apps;
}
getAppByInstanceId(instanceId) {
return this.apps.find(app => app.instanceId === instanceId);
}
getAppByPhysicsId(physicsId) {
for (const app of this.apps) {
if (app.getPhysicsObjects && app.getPhysicsObjects().some(o => o.physicsId === physicsId)) {
return app;
}
}
return null;
}
getPhysicsObjectByPhysicsId(physicsId) {
for (const app of this.apps) {
const physicsObjects = app.getPhysicsObjects();
for (const physicsObject of physicsObjects) {
if (physicsObject.physicsId === physicsId) {
return physicsObject;
}
}
}
return null;
}
getPairByPhysicsId(physicsId) {
for (const app of this.apps) {
const physicsObjects = app.getPhysicsObjects();
for (const physicsObject of physicsObjects) {
if (physicsObject.physicsId === physicsId) {
return [app, physicsObject];
}
}
}
return null;
}
getOrCreateTrackedApp(instanceId) {
for (let i = 0; this.appsArray.length > i; i++) {
const app = this.appsArray.get(i, Z.Map);
if (app.get('instanceId') === instanceId) {
return app;
}
}
const appMap = new Z.Map();
this.appsArray.push([appMap]);
return appMap;
}
getTrackedApp(instanceId) {
for (const app of this.appsArray) {
if (app.get('instanceId') === instanceId) {
return app;
}
}
return null;
}
hasTrackedApp(instanceId) {
for (const app of this.appsArray) {
if (app.get('instanceId') === instanceId) {
return true;
}
}
return false;
}
clear() {
if (!this.isBound()) {
const apps = this.apps.slice();
for (const app of apps) {
this.removeApp(app);
app.destroy();
}
this.dispatchEvent(new MessageEvent('clear'));
} else {
throw new Error('cannot clear world while it is bound');
}
}
addTrackedAppInternal(
instanceId,
contentId,
transform,
components,
) {
// console.log('add tracked app internal', instanceId, contentId);
const trackedApp = this.getOrCreateTrackedApp(instanceId);
trackedApp.set('instanceId', instanceId);
trackedApp.set('contentId', contentId);
trackedApp.set('transform', transform);
trackedApp.set('components', components);
return trackedApp;
}
addTrackedApp(
contentId,
position = new THREE.Vector3(),
quaternion = new THREE.Quaternion(),
scale = new THREE.Vector3(1, 1, 1),
components = [],
instanceId = getRandomString(),
) {
const self = this;
this.appsArray.doc.transact(function tx() {
const transform = new Float32Array(10);
position.toArray(transform);
quaternion.toArray(transform, 3);
scale.toArray(transform, 7);
self.addTrackedAppInternal(
instanceId,
contentId,
transform,
components,
);
});
const p = this.pendingAddPromises.get(instanceId);
if (p) {
return p;
} else {
const app = this.getAppByInstanceId(instanceId);
if (app) {
return Promise.resolve(app);
} else {
throw new Error('no pending world add object promise');
}
}
}
getTrackedAppIndex(instanceId) {
for (let i = 0; i < this.appsArray.length; i++) {
const app = this.appsArray.get(i);
if (app.get('instanceId') === instanceId) {
return i;
}
}
return -1;
}
removeTrackedAppInternal(instanceId) {
// console.log('remove tracked app internal', removeInstanceId);
const removeIndex = this.getTrackedAppIndex(instanceId);
if (removeIndex !== -1) {
this.appsArray.delete(removeIndex, 1);
} else {
console.warn('invalid remove instance id', instanceId);
}
}
removeTrackedApp(removeInstanceId) {
const self = this;
this.appsArray.doc.transact(function tx() {
self.removeTrackedAppInternal(removeInstanceId);
});
}
addApp(app) {
this.apps.push(app);
this.dispatchEvent(new MessageEvent('appadd', {
data: app,
}));
}
removeApp(app) {
const index = this.apps.indexOf(app);
// console.log('remove app', app.instanceId, app.contentId, index, this.apps.map(a => a.instanceId), new Error().stack);
if (index !== -1) {
this.apps.splice(index, 1);
this.dispatchEvent(new MessageEvent('appremove', {
data: app,
}));
}
}
resize(e) {
const apps = this.apps.slice();
for (const app of apps) {
app.resize && app.resize(e);
}
}
getErrorPlaceholder() {
const app = metaversefile.createApp({
name: 'error-placeholder',
});
app.contentId = 'error-placeholder';
(async () => {
await metaverseModules.waitForLoad();
const {modules} = metaversefile.useDefaultModules();
const m = modules['errorPlaceholder'];
await app.addModule(m);
})();
return app;
}
/* setBlindStateMode(stateBlindMode) {
this.stateBlindMode = stateBlindMode;
} */
transplantApp(app, dstAppManager) {
const {instanceId} = app;
const srcAppManager = this;
// srcAppManager.setBlindStateMode(true);
// dstAppManager.setBlindStateMode(true);
this.unbindTrackedApp(instanceId);
let dstTrackedApp = null;
const wrapTxFn = (srcAppManager.appsArray.doc === dstAppManager.appsArray.doc) ?
innerFn => srcAppManager.appsArray.doc.transact(innerFn)
:
innerFn => dstAppManager.appsArray.doc.transact(() => {
srcAppManager.appsArray.doc.transact(innerFn);
});
wrapTxFn(() => {
const srcTrackedApp = srcAppManager.getTrackedApp(instanceId);
/* if (!srcTrackedApp) {
console.log('transplant app', {app, srcTrackedApp});
debugger;
} */
const contentId = srcTrackedApp.get('contentId');
const transform = srcTrackedApp.get('transform');
const components = srcTrackedApp.get('components');
srcAppManager.removeTrackedAppInternal(instanceId);
dstTrackedApp = dstAppManager.addTrackedAppInternal(
instanceId,
contentId,
transform,
components,
);
});
dstAppManager.bindTrackedApp(dstTrackedApp, app);
/* } else {
throw new Error('cannot transplant apps between app manager with different state binding');
} */
// srcAppManager.setBlindStateMode(false);
// dstAppManager.setBlindStateMode(false);
}
importApp(app) {
const self = this;
this.appsArray.doc.transact(() => {
const contentId = app.contentId;
const instanceId = app.instanceId;
const components = app.components.slice();
const transform = new Float32Array(10);
app.position.toArray(transform);
app.quaternion.toArray(transform, 3);
app.scale.toArray(transform, 7);
const dstTrackedApp = self.addTrackedAppInternal(
instanceId,
contentId,
transform,
components,
);
self.addApp(app);
self.bindTrackedApp(dstTrackedApp, app);
});
}
hasApp(app) {
return this.apps.includes(app);
}
pushAppUpdates() {
if (this.appsArray) {
this.appsArray.doc.transact(() => {
this.updatePhysics();
}, 'push');
}
}
updatePhysics() {
for (const app of this.apps) {
if (!app.matrix.equals(app.lastMatrix)) {
const _updateTrackedApp = () => {
// note: not all apps are tracked in multiplayer. for those that are, we push the transform update here.
const trackedApp = this.getTrackedApp(app.instanceId);
if (trackedApp) {
app.matrixWorld.decompose(localVector, localQuaternion, localVector2);
localVector.toArray(this.transform);
localQuaternion.toArray(this.transform, 3);
localVector2.toArray(this.transform, 7);
trackedApp.set('transform', this.transform);
app.updateMatrixWorld();
}
};
_updateTrackedApp();
const _updatePhysicsObjects = () => {
// update attached physics objects with a relative transform
const physicsObjects = app.getPhysicsObjects();
if (physicsObjects.length > 0) {
const lastMatrixInverse = localMatrix.copy(app.lastMatrix).invert();
for (const physicsObject of physicsObjects) {
if (!physicsObject.detached) {
physicsObject.matrix
.premultiply(lastMatrixInverse)
.premultiply(app.matrix)
.decompose(physicsObject.position, physicsObject.quaternion, physicsObject.scale);
physicsObject.matrixWorld.copy(physicsObject.matrix);
for (const child of physicsObject.children) {
child.updateMatrixWorld();
}
physicsScene.setTransform(physicsObject);
physicsScene.getBoundingBoxForPhysicsId(physicsObject.physicsId, physicsObject.physicsMesh.geometry.boundingBox);
}
}
}
};
_updatePhysicsObjects();
app.lastMatrix.copy(app.matrix);
}
}
}
exportJSON() {
const objects = [];
// iterate over appsArray
for (const trackedApp of this.appsArray) {
const transform = trackedApp.get('transform');
const components = trackedApp.get('components') ?? [];
const object = {
transform,
components,
};
// console.log('got app object', object);
let contentId = trackedApp.get('contentId');
const match = contentId.match(/^\/@proxy\/data:([^;,]+),([\s\S]*)$/);
if (match) {
const type = match[1];
const content = decodeURIComponent(match[2]);
object.type = type;
object.content = jsonParse(content) ?? {};
} else {
object.contentId = contentId;
}
objects.push(object);
}
return objects;
}
destroy() {
if (!this.isBound()) {
this.clear();
const index = appManagers.indexOf(this);
if (index !== -1) {
this.clear();
appManagers.splice(index, 1);
} else {
throw new Error('double destroy of app manager');
}
} else {
throw new Error('destroy of bound app manager');
}
}
}
export {
AppManager,
};