Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Refactor Engine World & Scene #7692

Merged
merged 17 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/client-core/src/admin/adminRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const ProtectedRoutes = () => {
const scopes = admin?.scopes?.value || []

useEffect(() => {
initSystems(Engine.instance.currentWorld, [AdminSystemInjection]).then(async () => {
initSystems([AdminSystemInjection]).then(async () => {
dispatchAction(EngineActions.initializeEngine({ initialised: true }))
})
}, [])
Expand Down
7 changes: 2 additions & 5 deletions packages/client-core/src/admin/components/Avatars/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ const Avatar = () => {
...AvatarCommonModule(),
...AvatarClientModule()
]
initSystems(Engine.instance.currentWorld, systems)
initSystems(systems)
return () => {
unloadSystems(
Engine.instance.currentWorld,
systems.map((s) => s.uuid)
)
unloadSystems(systems.map((s) => s.uuid))
}
}, [])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Projects = () => {
} as const

useEffect(() => {
initSystems(Engine.instance.currentWorld, [ProjectUpdateSystemInjection])
initSystems([ProjectUpdateSystemInjection])
ProjectService.checkReloadStatus()
}, [])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const LocationInstanceState = defineState({
export function useWorldInstance() {
const [state, setState] = React.useState(null as null | State<InstanceState>)
const worldInstanceState = useState(getState(LocationInstanceState).instances)
const worldHostId = useState(Engine.instance.currentWorld.hostIds.world)
const worldHostId = useState(Engine.instance.hostIds.world)
useEffect(() => {
setState(worldHostId.value ? worldInstanceState[worldHostId.value] : null)
}, [worldInstanceState, worldHostId])
Expand All @@ -50,8 +50,8 @@ export const LocationInstanceConnectionServiceReceptor = (action) => {
const s = getState(LocationInstanceState)
matches(action)
.when(LocationInstanceConnectionAction.serverProvisioned.matches, (action) => {
Engine.instance.currentWorld.hostIds.world.set(action.instanceId)
Engine.instance.currentWorld.networks.set(
Engine.instance.hostIds.world.set(action.instanceId)
Engine.instance.networks.set(
action.instanceId,
new SocketWebRTCClientNetwork(action.instanceId, NetworkTopics.world)
)
Expand Down Expand Up @@ -84,10 +84,10 @@ export const LocationInstanceConnectionServiceReceptor = (action) => {
})
.when(LocationInstanceConnectionAction.changeActiveConnectionHostId.matches, (action) => {
const currentNetwork = s.instances[action.currentInstanceId].get({ noproxy: true })
Engine.instance.currentWorld.worldNetwork.hostId = action.newInstanceId as UserId
Engine.instance.currentWorld.networks.set(action.newInstanceId, Engine.instance.currentWorld.worldNetwork)
Engine.instance.currentWorld.networks.delete(action.currentInstanceId)
Engine.instance.currentWorld.hostIds.world.set(action.newInstanceId as UserId)
Engine.instance.worldNetwork.hostId = action.newInstanceId as UserId
Engine.instance.networks.set(action.newInstanceId, Engine.instance.worldNetwork)
Engine.instance.networks.delete(action.currentInstanceId)
Engine.instance.hostIds.world.set(action.newInstanceId as UserId)
s.instances.merge({ [action.newInstanceId]: currentNetwork })
s.instances[action.currentInstanceId].set(none)
})
Expand Down Expand Up @@ -231,7 +231,7 @@ export const LocationInstanceConnectionService = {
},
connectToServer: async (instanceId: string) => {
dispatchAction(LocationInstanceConnectionAction.connecting({ instanceId }))
const network = Engine.instance.currentWorld.worldNetwork as SocketWebRTCClientNetwork
const network = Engine.instance.worldNetwork as SocketWebRTCClientNetwork
logger.info({ primus: !!network.primus, transport: network }, 'Connect To World Server')
if (network.primus) {
leaveNetwork(network, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const MediaInstanceState = defineState({
export function useMediaInstance() {
const [state, setState] = React.useState(null as null | State<InstanceState>)
const mediaInstanceState = useState(getState(MediaInstanceState).instances)
const mediaHostId = useState(Engine.instance.currentWorld.hostIds.media)
const mediaHostId = useState(Engine.instance.hostIds.media)
useEffect(() => {
setState(mediaHostId.value ? mediaInstanceState[mediaHostId.value] : null)
}, [mediaInstanceState, mediaHostId])
Expand All @@ -55,8 +55,8 @@ export const MediaInstanceConnectionServiceReceptor = (action) => {
const s = getState(MediaInstanceState)
matches(action)
.when(MediaInstanceConnectionAction.serverProvisioned.matches, (action) => {
Engine.instance.currentWorld.hostIds.media.set(action.instanceId)
Engine.instance.currentWorld.networks.set(
Engine.instance.hostIds.media.set(action.instanceId)
Engine.instance.networks.set(
action.instanceId,
new SocketWebRTCClientNetwork(action.instanceId, NetworkTopics.media)
)
Expand Down Expand Up @@ -97,10 +97,10 @@ export const MediaInstanceConnectionServiceReceptor = (action) => {
})
.when(MediaInstanceConnectionAction.changeActiveConnectionHostId.matches, (action) => {
const currentNetwork = s.instances[action.currentInstanceId].get({ noproxy: true })
Engine.instance.currentWorld.mediaNetwork.hostId = action.newInstanceId as UserId
Engine.instance.currentWorld.networks.set(action.newInstanceId, Engine.instance.currentWorld.mediaNetwork)
Engine.instance.currentWorld.networks.delete(action.currentInstanceId)
Engine.instance.currentWorld.hostIds.media.set(action.newInstanceId as UserId)
Engine.instance.mediaNetwork.hostId = action.newInstanceId as UserId
Engine.instance.networks.set(action.newInstanceId, Engine.instance.mediaNetwork)
Engine.instance.networks.delete(action.currentInstanceId)
Engine.instance.hostIds.media.set(action.newInstanceId as UserId)
s.instances.merge({ [action.newInstanceId]: currentNetwork })
s.instances[action.currentInstanceId].set(none)
})
Expand Down Expand Up @@ -145,7 +145,7 @@ export const MediaInstanceConnectionService = {
const user = authState.user.value
const { ipAddress, port } = accessMediaInstanceConnectionState().instances.value[instanceId]

const network = Engine.instance.currentWorld.mediaNetwork as SocketWebRTCClientNetwork
const network = Engine.instance.mediaNetwork as SocketWebRTCClientNetwork
logger.info({ primus: !!network.primus, network }, 'Connect To Media Server.')
if (network.primus) {
await endVideoChat(network, { endConsumers: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ConferenceMode = (): JSX.Element => {
const selfUserId = useState(accessAuthState().user.id)
const userState = useNetworkUserState()
const channelConnectionState = useMediaInstanceConnectionState()
const network = Engine.instance.currentWorld.mediaNetwork
const network = Engine.instance.mediaNetwork
const currentChannelInstanceConnection = network && channelConnectionState.instances[network.hostId].ornull
const displayedUsers =
network?.hostId && currentChannelInstanceConnection
Expand Down
28 changes: 12 additions & 16 deletions packages/client-core/src/components/Debug/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ export const Debug = ({ showingStateRef }) => {
const engineState = useHookstate(getState(EngineState))
const { t } = useTranslation()
const hasActiveControlledAvatar =
engineState.joinedWorld.value &&
hasComponent(Engine.instance.currentWorld.localClientEntity, AvatarControllerComponent)
engineState.joinedWorld.value && hasComponent(Engine.instance.localClientEntity, AvatarControllerComponent)

const networks = mapToObject(Engine.instance.currentWorld.networks)
const networks = mapToObject(Engine.instance.networks)

const onClickRespawn = (): void => {
respawnAvatar(Engine.instance.currentWorld.localClientEntity)
respawnAvatar(Engine.instance.localClientEntity)
}

const toggleDebug = () => {
Expand Down Expand Up @@ -82,24 +81,21 @@ export const Debug = ({ showingStateRef }) => {
const renderEntityComponents = (entity: Entity) => {
return Object.fromEntries(
entityExists(entity)
? getEntityComponents(Engine.instance.currentWorld, entity).reduce<[string, any][]>(
(components, C: Component<any, any>) => {
if (C !== NameComponent) {
const component = getComponent(entity, C)
components.push([C.name, { ...component }])
}
return components
},
[]
)
? getEntityComponents(Engine.instance, entity).reduce<[string, any][]>((components, C: Component<any, any>) => {
if (C !== NameComponent) {
const component = getComponent(entity, C)
components.push([C.name, { ...component }])
}
return components
}, [])
: []
)
}

const renderAllEntities = () => {
return {
...Object.fromEntries(
[...Engine.instance.currentWorld.entityQuery().entries()]
[...Engine.instance.entityQuery().entries()]
.map(([key, eid]) => {
try {
return [
Expand Down Expand Up @@ -128,7 +124,7 @@ export const Debug = ({ showingStateRef }) => {

const namedEntities = useHookstate({})
const entityTree = useHookstate({} as any)
const pipelines = Engine.instance.currentWorld.pipelines
const pipelines = Engine.instance.pipelines

namedEntities.set(renderAllEntities())
entityTree.set(renderEntityTreeRoots())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const useChatHooks = ({ chatWindowOpen, setUnreadMessages, messageRefInpu
const currentInstanceConnection = useWorldInstance()

useEffect(() => {
if (Engine.instance.currentWorld.worldNetwork?.hostId && currentInstanceConnection?.connected?.value) {
if (Engine.instance.worldNetwork?.hostId && currentInstanceConnection?.connected?.value) {
ChatService.getInstanceChannel()
}
}, [currentInstanceConnection?.connected])
Expand Down
6 changes: 3 additions & 3 deletions packages/client-core/src/components/MediaIconsBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const MediaIconsBox = () => {

const currentLocation = useLocationState().currentLocation.location
const channelConnectionState = useMediaInstanceConnectionState()
const mediaHostId = Engine.instance.currentWorld.mediaNetwork?.hostId
const mediaHostId = Engine.instance.mediaNetwork?.hostId
const currentChannelInstanceConnection = mediaHostId && channelConnectionState.instances[mediaHostId].ornull
const mediastream = useMediaStreamState()
const videoEnabled = currentLocation?.locationSetting?.value
Expand Down Expand Up @@ -68,7 +68,7 @@ export const MediaIconsBox = () => {
<section className={`${styles.drawerBox} ${topShelfStyle}`}>
{audioEnabled &&
hasAudioDevice &&
Engine.instance.currentWorld.mediaNetwork &&
Engine.instance.mediaNetwork &&
currentChannelInstanceConnection?.connected.value ? (
<button
type="button"
Expand All @@ -83,7 +83,7 @@ export const MediaIconsBox = () => {
) : null}
{videoEnabled &&
hasVideoDevice &&
Engine.instance.currentWorld.mediaNetwork &&
Engine.instance.mediaNetwork &&
currentChannelInstanceConnection?.connected.value ? (
<>
<button
Expand Down
10 changes: 5 additions & 5 deletions packages/client-core/src/components/UserMediaWindow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const useUserMediaWindowHook = ({ peerID, type }: Props) => {
currentLocation?.locationSetting?.locationType?.value === 'showroom' &&
selfUser?.locationAdmins?.find((locationAdmin) => currentLocation?.id?.value === locationAdmin.locationId) != null

const mediaNetwork = Engine.instance.currentWorld.mediaNetwork
const mediaNetwork = Engine.instance.mediaNetwork
const isSelf = !mediaNetwork || peerID === mediaNetwork?.peerID
const volume = isSelf ? audioState.microphoneGain.value : _volume
const isScreen = type === 'screen'
Expand All @@ -106,7 +106,7 @@ export const useUserMediaWindowHook = ({ peerID, type }: Props) => {

const mediaStreamState = useHookstate(getState(MediaStreamState))
const mediaSettingState = useHookstate(getState(MediaSettingsState))
const mediaState = getMediaSceneMetadataState(Engine.instance.currentWorld)
const mediaState = getMediaSceneMetadataState(Engine.instance.currentScene)
const rendered =
mediaSettingState.immersiveMediaMode.value === 'off' ||
(mediaSettingState.immersiveMediaMode.value === 'auto' && !mediaState.immersiveMedia.value)
Expand Down Expand Up @@ -237,7 +237,7 @@ export const useUserMediaWindowHook = ({ peerID, type }: Props) => {

const toggleVideo = async (e) => {
e.stopPropagation()
const mediaNetwork = Engine.instance.currentWorld.mediaNetwork as SocketWebRTCClientNetwork
const mediaNetwork = Engine.instance.mediaNetwork as SocketWebRTCClientNetwork
if (isSelf && !isScreen) {
toggleWebcamPaused()
} else if (isSelf && isScreen) {
Expand All @@ -255,7 +255,7 @@ export const useUserMediaWindowHook = ({ peerID, type }: Props) => {

const toggleAudio = async (e) => {
e.stopPropagation()
const mediaNetwork = Engine.instance.currentWorld.mediaNetwork as SocketWebRTCClientNetwork
const mediaNetwork = Engine.instance.mediaNetwork as SocketWebRTCClientNetwork
if (isSelf && !isScreen) {
toggleMicrophonePaused()
} else if (isSelf && isScreen) {
Expand All @@ -273,7 +273,7 @@ export const useUserMediaWindowHook = ({ peerID, type }: Props) => {

const toggleGlobalMute = async (e) => {
e.stopPropagation()
const mediaNetwork = Engine.instance.currentWorld.mediaNetwork as SocketWebRTCClientNetwork
const mediaNetwork = Engine.instance.mediaNetwork as SocketWebRTCClientNetwork
const audioStreamProducer = audioStream as ConsumerExtension
if (!audioProducerGlobalMute) {
await globalMuteProducer(mediaNetwork, { id: audioStreamProducer.producerId })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ const InstanceServerWarnings = () => {

case WarningModalTypes.INSTANCE_DISCONNECTED: {
if (!Engine.instance.userId) return
const transport = Engine.instance.currentWorld.networks.get(
Engine.instance.currentWorld.worldNetwork?.hostId
const transport = Engine.instance.networks.get(
Engine.instance.worldNetwork?.hostId
) as SocketWebRTCClientNetwork
if (engineState.isTeleporting.value || transport.reconnecting) return

Expand All @@ -208,12 +208,12 @@ const InstanceServerWarnings = () => {

case WarningModalTypes.CHANNEL_DISCONNECTED: {
if (!Engine.instance.userId) return
const transport = Engine.instance.currentWorld.mediaNetwork as SocketWebRTCClientNetwork
const transport = Engine.instance.mediaNetwork as SocketWebRTCClientNetwork
if (transport.reconnecting) return

const channels = chatState.channels.channels.value
const instanceChannel = Object.values(channels).find(
(channel) => channel.instanceId === Engine.instance.currentWorld.mediaNetwork?.hostId
(channel) => channel.instanceId === Engine.instance.mediaNetwork?.hostId
)
setModalValues({
open: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const useLocationSpawnAvatar = (spectate = false) => {
}

if (
Engine.instance.currentWorld.localClientEntity ||
Engine.instance.localClientEntity ||
!engineState.sceneLoaded.value ||
!authState.user.value ||
!authState.user.avatar.value ||
Expand Down Expand Up @@ -112,8 +112,7 @@ export const usePortalTeleport = () => {
useEffect(() => {
if (engineState.isTeleporting.value) {
logger.info('Resetting connection for portal teleport.')
const world = Engine.instance.currentWorld
const activePortal = world.activePortal
const activePortal = Engine.instance.activePortal

if (!activePortal) return

Expand All @@ -123,11 +122,11 @@ export const usePortalTeleport = () => {
UUIDComponent.entitiesByUUID[activePortal.linkedPortalId]?.value
) {
teleportAvatar(
world.localClientEntity,
Engine.instance.localClientEntity,
activePortal.remoteSpawnPosition
// activePortal.remoteSpawnRotation
)
world.activePortal = null
Engine.instance.activePortal = null
dispatchAction(EngineActions.setTeleporting({ isTeleporting: false, $time: Date.now() + 500 }))
return
}
Expand All @@ -137,16 +136,16 @@ export const usePortalTeleport = () => {
return
}

route('/location/' + world.activePortal!.location)
LocationService.getLocationByName(world.activePortal!.location, authState.user.id.value)
route('/location/' + Engine.instance.activePortal!.location)
LocationService.getLocationByName(Engine.instance.activePortal!.location, authState.user.id.value)

// shut down connection with existing world instance server
// leaving a world instance server will check if we are in a location media instance and shut that down too
leaveNetwork(world.worldNetwork as SocketWebRTCClientNetwork)
leaveNetwork(Engine.instance.worldNetwork as SocketWebRTCClientNetwork)

setAvatarToLocationTeleportingState(world)
setAvatarToLocationTeleportingState()
if (activePortal.effectType !== 'None') {
addComponent(world.localClientEntity, PortalEffects.get(activePortal.effectType), true)
addComponent(Engine.instance.localClientEntity, PortalEffects.get(activePortal.effectType), true)
} else {
dispatchAction(AppLoadingAction.setLoadingState({ state: AppLoadingStates.START_STATE }))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ export const retrieveLocationByName = (locationName: string, userId: string) =>
export const initClient = async (injectedSystems: SystemModuleType<any>[] = []) => {
if (getState(EngineState).isEngineInitialized.value) return

const world = Engine.instance.currentWorld
const projects = API.instance.client.service('projects').find()

await ClientModules()
await initSystems(world, injectedSystems)
await loadEngineInjection(world, await projects)
await initSystems(injectedSystems)
await loadEngineInjection(await projects)

dispatchAction(EngineActions.initializeEngine({ initialised: true }))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export const NetworkInstanceProvisioning = () => {
const engineState = useEngineState()
const partyState = usePartyState()

const worldNetworkHostId = Engine.instance.currentWorld.worldNetwork?.hostId
const worldNetworkHostId = Engine.instance.worldNetwork?.hostId
const currentLocationInstanceConnection = useWorldInstance()

const mediaNetworkHostId = Engine.instance.currentWorld.mediaNetwork?.hostId
const mediaNetworkHostId = Engine.instance.mediaNetwork?.hostId
const currentChannelInstanceConnection = useMediaInstance()

MediaInstanceConnectionService.useAPIListeners()
Expand Down
Loading