This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
parcelSceneManager.ts
197 lines (166 loc) · 5.87 KB
/
parcelSceneManager.ts
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
import { scenesChanged } from '../loading/actions'
import { LoadableScene } from '../types'
import { SceneWorker } from './SceneWorker'
import { store } from 'shared/store/isolatedStore'
import { Observable } from 'mz-observable'
import { ParcelSceneLoadingState } from './types'
import { getFeatureFlagVariantValue } from 'shared/meta/selectors'
import { Transport } from '@dcl/rpc'
import { defaultParcelPermissions } from 'shared/apis/host/Permissions'
declare const globalThis: any
const PARCEL_DENY_LISTED_FEATURE_FLAG = 'parcel-denylist'
export function isParcelDenyListed(coordinates: string[]) {
const denylist = getFeatureFlagVariantValue(store.getState(), PARCEL_DENY_LISTED_FEATURE_FLAG) as string
const setOfCoordinates = new Set(coordinates)
if (denylist) {
return denylist.split(/[\s\r\n]+/gm).some(($) => setOfCoordinates.has($.trim()))
}
return false
}
export function generateBannedLoadableScene(entity: LoadableScene): LoadableScene {
return {
...entity,
entity: {
...entity.entity,
content: []
}
}
}
export const onLoadParcelScenesObservable = new Observable<LoadableScene[]>()
export const loadedSceneWorkers = new Map<string, SceneWorker>()
globalThis['sceneWorkers'] = loadedSceneWorkers
/**
* Retrieve the Scene based on it's ID, usually RootCID
*/
export function getSceneWorkerBySceneID(sceneId: string) {
return loadedSceneWorkers.get(sceneId)
}
/**
* Retrieve the Scene based on it's Scene Number
*/
export function getSceneWorkerBySceneNumber(sceneNumber: number) {
// TODO: Optimize this fetch
for (const sceneWorker of loadedSceneWorkers.values()) {
if (sceneWorker.rpcContext.sceneData.sceneNumber === sceneNumber) {
return sceneWorker
}
}
}
export function forceStopScene(sceneId: string) {
const worker = loadedSceneWorkers.get(sceneId)
if (worker) {
worker.dispose()
loadedSceneWorkers.delete(sceneId)
store.dispatch(scenesChanged())
}
}
// finds a parcel scene by parcel position (that is not a portable experience)
export function getLoadedParcelSceneByParcel(parcelPosition: string) {
for (const [, w] of loadedSceneWorkers) {
if (!w.rpcContext.sceneData.isPortableExperience && w.metadata.scene?.parcels?.includes(parcelPosition)) {
return w
}
}
}
/**
* Creates a worker for the ParcelSceneAPI
*/
export function loadParcelSceneWorker(loadableScene: LoadableScene, transport?: Transport) {
const sceneId = loadableScene.id
let parcelSceneWorker = loadedSceneWorkers.get(sceneId)
if (!parcelSceneWorker) {
parcelSceneWorker = new SceneWorker(loadableScene, transport)
setNewParcelScene(parcelSceneWorker)
queueMicrotask(() => store.dispatch(scenesChanged()))
}
return parcelSceneWorker
}
/**
* idempotent.
*
* accepts a new worker, stops the previous one if there was any collision with
* the same ID
*/
function setNewParcelScene(worker: SceneWorker) {
const sceneId = worker.loadableScene.id
// NOTE: getSceneWorkerBySceneID is not used because when the change to
// sceneNumber happens we still need to look for ID collissions
// unload all the conflicting workers
for (const [id, w] of loadedSceneWorkers) {
if (worker !== w && w.loadableScene.id === sceneId) {
forceStopScene(id)
}
}
loadedSceneWorkers.set(sceneId, worker)
}
// @internal
export const parcelSceneLoadingState: ParcelSceneLoadingState = {
isWorldLoadingEnabled: true,
desiredParcelScenes: new Map()
}
/**
* @internal
* Returns a set of Set<SceneId>
*/
export function getDesiredParcelScenes(): Map<string, LoadableScene> {
return new Map(parcelSceneLoadingState.desiredParcelScenes)
}
/**
* Receives a set of Set<SceneId>
*/
export async function setDesiredParcelScenes(desiredParcelScenes: Map<string, LoadableScene>) {
const previousSet = new Set(parcelSceneLoadingState.desiredParcelScenes)
const newSet = (parcelSceneLoadingState.desiredParcelScenes = desiredParcelScenes)
// react to changes
for (const [oldSceneId] of previousSet) {
if (!newSet.has(oldSceneId) && loadedSceneWorkers.has(oldSceneId)) {
// destroy old scene
unloadParcelSceneById(oldSceneId)
}
}
for (const [newSceneId, entity] of newSet) {
if (!loadedSceneWorkers.has(newSceneId)) {
// create new scene
await loadParcelSceneByIdIfMissing(newSceneId, entity)
}
}
}
export async function reloadScene(sceneId: string) {
unloadParcelSceneById(sceneId)
await setDesiredParcelScenes(getDesiredParcelScenes())
}
export function unloadParcelSceneById(sceneId: string) {
const worker = loadedSceneWorkers.get(sceneId)
if (!worker) {
return
}
forceStopScene(sceneId)
}
/**
* @internal
**/
async function loadParcelSceneByIdIfMissing(sceneId: string, entity: LoadableScene) {
// create the worker if don't exis
if (!getSceneWorkerBySceneID(sceneId)) {
// If we are running in isolated mode and it is builder mode, we create a stateless worker instead of a normal worker
const denyListed = isParcelDenyListed(entity.entity.metadata.scene.parcels)
const usedEntity = denyListed ? generateBannedLoadableScene(entity) : entity
const worker = loadParcelSceneWorker(usedEntity)
// add default permissions for Parcel based scenes
defaultParcelPermissions.forEach(($) => worker.rpcContext.permissionGranted.add($))
// and enablle FPS throttling, it will lower the frame-rate based on the distance
worker.rpcContext.sceneData.useFPSThrottling = true
setNewParcelScene(worker)
}
}
export type AllScenesEvents<T extends IEventNames> = {
eventType: T
payload: IEvents[T]
}
export function allScenesEvent<T extends IEventNames>(data: AllScenesEvents<T>) {
for (const [, scene] of loadedSceneWorkers) {
scene.rpcContext.sendSceneEvent(data.eventType, data.payload)
}
TEST_OBJECT_ObservableAllScenesEvent.notifyObservers(data)
}
export const TEST_OBJECT_ObservableAllScenesEvent = new Observable<AllScenesEvents<any>>()